火车购票系统概述
火车购票系统是一个用于处理乘客购买火车票的应用程序,该系统需要实现以下功能:
1、显示可用的火车线路和座位信息;
2、允许乘客选择要购买的车票类型(如硬座、软座、硬卧、软卧等);
3、计算票价,包括票务费、手续费等;
4、处理乘客支付和退票操作;
5、记录乘客的购票信息和退票信息。
火车购票系统代码实现
以下是一个简单的火车购票系统代码实现,使用Java编写:
// 火车线路类 class TrainRoute { private String startStation; private String endStation; private double price; public TrainRoute(String startStation, String endStation, double price) { this.startStation = startStation; this.endStation = endStation; this.price = price; } public String getStartStation() { return startStation; } public String getEndStation() { return endStation; } public double getPrice() { return price; } } // 火车票类 class TrainTicket { private TrainRoute route; private String seatType; private double totalPrice; public TrainTicket(TrainRoute route, String seatType) { this.route = route; this.seatType = seatType; this.totalPrice = calculateTotalPrice(); } private double calculateTotalPrice() { double basePrice = route.getPrice(); double ticketFee = 0; double serviceFee = 0; switch (seatType) { case "硬座": ticketFee = basePrice * 0.5; break; case "软座": ticketFee = basePrice * 0.6; break; case "硬卧": ticketFee = basePrice * 0.8; break; case "软卧": ticketFee = basePrice * 0.9; break; } serviceFee = ticketFee * 0.05; return ticketFee + serviceFee; } public TrainRoute getRoute() { return route; } public String getSeatType() { return seatType; } public double getTotalPrice() { return totalPrice; } } // 购票系统类 class TrainBookingSystem { private List<TrainTicket> tickets; public TrainBookingSystem() { tickets = new ArrayList<>(); } public void addTicket(TrainTicket ticket) { tickets.add(ticket); } public void displayTickets() { System.out.println("购票信息:"); System.out.println("| 车次 | 出发站 | 到达站 | 座位类型 | 票价 |"); for (TrainTicket ticket : tickets) { System.out.printf("| %s | %s | %s | %s | %.2f |%n", ticket.getRoute().getStartStation(), ticket.getRoute().getEndStation(), ticket.getSeatType(), ticket.getTotalPrice()); } } } // 主程序入口 public class Main { public static void main(String[] args) { TrainRoute route1 = new TrainRoute("北京", "上海", 100); TrainRoute route2 = new TrainRoute("广州", "深圳", 150); TrainTicket ticket1 = new TrainTicket(route1, "硬座"); TrainTicket ticket2 = new TrainTicket(route2, "软卧"); TrainBookingSystem bookingSystem = new TrainBookingSystem(); bookingSystem.addTicket(ticket1); bookingSystem.addTicket(ticket2); bookingSystem.displayTickets(); } }
单元测试
为了确保火车购票系统的正确性,我们需要对各个模块进行单元测试,以下是一个简单的单元测试示例:
import org.junit.Test; import static org.junit.Assert.*; public class TrainBookingSystemTest { @Test public void testAddTicket() { TrainRoute route1 = new TrainRoute("北京", "上海", 100); TrainTicket ticket1 = new TrainTicket(route1, "硬座"); TrainBookingSystem bookingSystem = new TrainBookingSystem(); bookingSystem.addTicket(ticket1); assertEquals(1, bookingSystem.getTickets().size()); assertEquals(ticket1, bookingSystem.getTickets().get(0)); } }
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/641599.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复