尝试用Java简单写一个航空客运订票系统
时间: 2023-10-07 19:13:23 浏览: 87
好的,我可以为您提供一些基本的思路和代码示例。首先,我们需要定义一些基本的实体类,如航班、乘客、座位等。以下是一个简单的代码示例:
1. 定义航班类
```
public class Flight {
private String flightNumber;
private String departureCity;
private String arrivalCity;
private Date departureTime;
private Date arrivalTime;
private int availableSeats;
// 省略构造函数和getter/setter方法
}
```
2. 定义乘客类
```
public class Passenger {
private String name;
private String idNumber;
private String phoneNumber;
// 省略构造函数和getter/setter方法
}
```
3. 定义座位类
```
public class Seat {
private String seatNumber;
private boolean isAvailable;
// 省略构造函数和getter/setter方法
}
```
4. 定义订票类
```
public class Booking {
private Flight flight;
private Passenger passenger;
private Seat seat;
// 省略构造函数和getter/setter方法
}
```
然后,我们需要实现一些基本的功能,如查询航班、预订座位等。以下是一个简单的代码示例:
1. 查询航班
```
public class FlightService {
private List<Flight> flights;
public FlightService() {
// 初始化航班数据
flights = new ArrayList<>();
flights.add(new Flight("CA001", "北京", "上海", new Date(), new Date(), 100));
flights.add(new Flight("CA002", "上海", "北京", new Date(), new Date(), 100));
}
public List<Flight> searchFlights(String departureCity, String arrivalCity) {
List<Flight> result = new ArrayList<>();
for (Flight flight : flights) {
if (flight.getDepartureCity().equals(departureCity) && flight.getArrivalCity().equals(arrivalCity)) {
result.add(flight);
}
}
return result;
}
}
```
2. 预订座位
```
public class SeatService {
private Map<Flight, List<Seat>> seats;
public SeatService() {
// 初始化座位数据
seats = new HashMap<>();
for (Flight flight : flights) {
List<Seat> seatList = new ArrayList<>();
for (int i = 1; i <= flight.getAvailableSeats(); i++) {
seatList.add(new Seat(String.valueOf(i), true));
}
seats.put(flight, seatList);
}
}
public Seat bookSeat(Flight flight, Passenger passenger) throws Exception {
List<Seat> seatList = seats.get(flight);
for (Seat seat : seatList) {
if (seat.isAvailable()) {
seat.setAvailable(false);
return seat;
}
}
throw new Exception("No available seat for flight " + flight.getFlightNumber());
}
}
```
这只是一个简单的示例,实际的系统可能需要更复杂的功能和更多的实体类。希望这个示例能为您提供一些帮助。
阅读全文