用java写火车订票管理系统,要用到继承,接口,异常,tostring方法,增删改查
时间: 2023-10-24 16:08:40 浏览: 78
火车订票系统,JAVA语言,增删改查
4星 · 用户满意度95%
好的,我会尽力回答你的问题。
首先,我们可以定义一个基础的类来表示火车票,例如:
```java
public class TrainTicket {
private String trainNumber;
private String startStation;
private String endStation;
private String departureTime;
private String seatType;
private int remainingTickets;
// 省略构造方法、getter和setter方法
}
```
然后,我们可以定义一个继承自 TrainTicket 的子类来表示不同类型的火车票,例如:
```java
public class FirstClassTicket extends TrainTicket {
private String seatNumber;
// 省略构造方法、getter和setter方法
}
```
接着,我们可以定义一个接口来规定对火车票进行增删改查的方法,例如:
```java
public interface TrainTicketManager {
void addTicket(TrainTicket ticket) throws TicketException;
void removeTicket(String trainNumber, String seatType) throws TicketException;
void updateTicket(String trainNumber, String seatType, int remainingTickets) throws TicketException;
TrainTicket queryTicket(String trainNumber, String seatType) throws TicketException;
}
```
在这个接口中,我们定义了四个方法:addTicket、removeTicket、updateTicket和queryTicket,分别用于添加、删除、更新和查询火车票。这些方法都会抛出一个自定义的异常 TicketException,用于处理错误情况。
接下来,我们可以实现这个接口,例如:
```java
public class TrainTicketManagerImpl implements TrainTicketManager {
private List<TrainTicket> ticketList = new ArrayList<>();
@Override
public void addTicket(TrainTicket ticket) throws TicketException {
if (ticket == null) {
throw new TicketException("The ticket is null.");
}
if (queryTicket(ticket.getTrainNumber(), ticket.getSeatType()) != null) {
throw new TicketException("The ticket already exists.");
}
ticketList.add(ticket);
}
@Override
public void removeTicket(String trainNumber, String seatType) throws TicketException {
TrainTicket ticket = queryTicket(trainNumber, seatType);
if (ticket == null) {
throw new TicketException("The ticket does not exist.");
}
ticketList.remove(ticket);
}
@Override
public void updateTicket(String trainNumber, String seatType, int remainingTickets) throws TicketException {
TrainTicket ticket = queryTicket(trainNumber, seatType);
if (ticket == null) {
throw new TicketException("The ticket does not exist.");
}
ticket.setRemainingTickets(remainingTickets);
}
@Override
public TrainTicket queryTicket(String trainNumber, String seatType) throws TicketException {
for (TrainTicket ticket : ticketList) {
if (ticket.getTrainNumber().equals(trainNumber) && ticket.getSeatType().equals(seatType)) {
return ticket;
}
}
return null;
}
}
```
在这个实现中,我们使用了一个 List 来存储所有的火车票,然后分别实现了接口中定义的四个方法。在 addTicket 方法中,我们首先判断传入的火车票是否为 null,然后再判断该火车票是否已经存在;在 removeTicket 方法中,我们先查询该火车票是否存在,然后再将其从 List 中删除;在 updateTicket 方法中,我们先查询该火车票是否存在,然后再更新它的剩余票数;在 queryTicket 方法中,我们遍历 List 来查找符合条件的火车票。
最后,我们可以在 TrainTicket 类中重写 toString 方法,用于打印火车票信息,例如:
```java
@Override
public String toString() {
return "TrainTicket{" +
"trainNumber='" + trainNumber + '\'' +
", startStation='" + startStation + '\'' +
", endStation='" + endStation + '\'' +
", departureTime='" + departureTime + '\'' +
", seatType='" + seatType + '\'' +
", remainingTickets=" + remainingTickets +
'}';
}
```
这样,我们就完成了一个简单的火车订票管理系统,它使用了继承、接口、异常和 toString 方法。
阅读全文