火车订票系统JAVA代码
时间: 2023-12-23 16:04:00 浏览: 72
Java火车订票系统源码.zip
5星 · 资源好评率100%
根据提供的引用内容,以下是一个简单的火车订票系统的JAVA代码示例:
```java
import java.util.ArrayList;
import java.util.List;
class TrainTicket {
private String orderNumber;
private String trainNumber;
private String startStation;
private String endStation;
private String departureTime;
private int ticketQuantity;
private String username;
private String realName;
private String phoneNumber;
private String purchaseTime;
private String carriageNumber;
private String seatNumber;
private boolean isPaid;
// 构造函数
public TrainTicket(String orderNumber, String trainNumber, String startStation, String endStation, String departureTime, int ticketQuantity, String username, String realName, String phoneNumber, String purchaseTime, String carriageNumber, String seatNumber, boolean isPaid) {
this.orderNumber = orderNumber;
this.trainNumber = trainNumber;
this.startStation = startStation;
this.endStation = endStation;
this.departureTime = departureTime;
this.ticketQuantity = ticketQuantity;
this.username = username;
this.realName = realName;
this.phoneNumber = phoneNumber;
this.purchaseTime = purchaseTime;
this.carriageNumber = carriageNumber;
this.seatNumber = seatNumber;
this.isPaid = isPaid;
}
// getter和setter方法
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getTrainNumber() {
return trainNumber;
}
public void setTrainNumber(String trainNumber) {
this.trainNumber = trainNumber;
}
public String getStartStation() {
return startStation;
}
public void setStartStation(String startStation) {
this.startStation = startStation;
}
public String getEndStation() {
return endStation;
}
public void setEndStation(String endStation) {
this.endStation = endStation;
}
public String getDepartureTime() {
return departureTime;
}
public void setDepartureTime(String departureTime) {
this.departureTime = departureTime;
}
public int getTicketQuantity() {
return ticketQuantity;
}
public void setTicketQuantity(int ticketQuantity) {
this.ticketQuantity = ticketQuantity;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPurchaseTime() {
return purchaseTime;
}
public void setPurchaseTime(String purchaseTime) {
this.purchaseTime = purchaseTime;
}
public String getCarriageNumber() {
return carriageNumber;
}
public void setCarriageNumber(String carriageNumber) {
this.carriageNumber = carriageNumber;
}
public String getSeatNumber() {
return seatNumber;
}
public void setSeatNumber(String seatNumber) {
this.seatNumber = seatNumber;
}
public boolean isPaid() {
return isPaid;
}
public void setPaid(boolean paid) {
isPaid = paid;
}
}
class TrainTicketManagementSystem {
private List<TrainTicket> trainTickets;
public TrainTicketManagementSystem() {
trainTickets = new ArrayList<>();
}
public void addTrainTicket(TrainTicket trainTicket) {
trainTickets.add(trainTicket);
}
public void deleteTrainTicket(String orderNumber) {
for (TrainTicket trainTicket : trainTickets) {
if (trainTicket.getOrderNumber().equals(orderNumber)) {
trainTickets.remove(trainTicket);
break;
}
}
}
public void modifyTrainTicket(String orderNumber, TrainTicket newTrainTicket) {
for (int i = 0; i < trainTickets.size(); i++) {
if (trainTickets.get(i).getOrderNumber().equals(orderNumber)) {
trainTickets.set(i, newTrainTicket);
break;
}
}
}
public TrainTicket getTrainTicket(String orderNumber) {
for (TrainTicket trainTicket : trainTickets) {
if (trainTicket.getOrderNumber().equals(orderNumber)) {
return trainTicket;
}
}
return null;
}
public List<TrainTicket> getAllTrainTickets() {
return trainTickets;
}
}
public class Main {
public static void main(String[] args) {
// 创建一个火车票管理系统对象
TrainTicketManagementSystem ticketManagementSystem = new TrainTicketManagementSystem();
// 创建一个火车票对象
TrainTicket trainTicket1 = new TrainTicket("123456", "G123", "Beijing", "Shanghai", "2022-01-01 08:00", 2, "user1", "张三", "123456789", "2021-12-31 10:00", "C12", "A1", true);
// 添加火车票到管理系统中
ticketManagementSystem.addTrainTicket(trainTicket1);
// 获取所有火车票
List<TrainTicket> allTrainTickets = ticketManagementSystem.getAllTrainTickets();
for (TrainTicket trainTicket : allTrainTickets) {
System.out.println("订单编号:" + trainTicket.getOrderNumber());
System.out.println("列车编号:" + trainTicket.getTrainNumber());
System.out.println("起点站:" + trainTicket.getStartStation());
System.out.println("终点站:" + trainTicket.getEndStation());
System.out.println("开车时间:" + trainTicket.getDepartureTime());
System.out.println("车票数量:" + trainTicket.getTicketQuantity());
System.out.println("用户名:" + trainTicket.getUsername());
System.out.println("真实姓名:" + trainTicket.getRealName());
System.out.println("手机号:" + trainTicket.getPhoneNumber());
System.out.println("购票时间:" + trainTicket.getPurchaseTime());
System.out.println("车厢号:" + trainTicket.getCarriageNumber());
System.out.println("座位编号:" + trainTicket.getSeatNumber());
System.out.println("是否支付:" + trainTicket.isPaid());
System.out.println("--");
}
}
}
```
阅读全文