写出以下java程序某航空公司对航班进行管理,管理控制台用如下几条命令进行管理:命令1: create命令 格式: create <航班名><多少排座位><每排座位数>功能:创建一个航班。 命令2: reserve命令 格式: reserve <旅客名1><旅客名2>功能:旅客预订座位,返回预订号。要求: 同一批的旅客必须安排在同一排且座位相邻,每一位旅客返人预订号。预订号自已定义,只要不重号即可。若安排不下(条件不能满足),则显示反馈信息。命令3: Cancel命令 格式: Cancel<预订号1><预订号2> 功能:取消预订 命令: list命令 格式: list 功能:显示座位预订情况 命令: exit命令 功能:退出程序。
时间: 2024-03-09 11:45:04 浏览: 70
好的,以下是一个简单的Java程序,实现了航班管理的功能:
```java
import java.util.*;
public class FlightManagement {
private int rows;
private int cols;
private String flightName;
private boolean[][] seats;
private Map<String, List<Integer>> reservations;
public FlightManagement(String flightName, int rows, int cols) {
this.flightName = flightName;
this.rows = rows;
this.cols = cols;
this.seats = new boolean[rows][cols];
this.reservations = new HashMap<>();
}
public void create(String[] args) {
if (args.length != 3) {
System.out.println("Invalid command format");
return;
}
this.flightName = args[0];
this.rows = Integer.parseInt(args[1]);
this.cols = Integer.parseInt(args[2]);
this.seats = new boolean[rows][cols];
this.reservations.clear();
System.out.println("Flight " + flightName + " created with " + rows + " rows and " + cols + " seats in each row");
}
public void reserve(String[] args) {
if (args.length < 1) {
System.out.println("Invalid command format");
return;
}
List<Integer> seatsToReserve = new ArrayList<>();
String reservationId = UUID.randomUUID().toString();
for (String passengerName : args) {
int row = -1;
int col = -1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (!seats[i][j]) {
if (col == -1) {
row = i;
col = j;
} else if (j == col + 1) {
col = j;
} else {
row = -1;
col = -1;
}
}
}
if (row != -1) {
break;
}
}
if (row != -1) {
seats[row][col] = true;
seatsToReserve.add(row * cols + col);
} else {
System.out.println("Cannot reserve seats for " + passengerName);
for (int seat : seatsToReserve) {
seats[seat / cols][seat % cols] = false;
}
return;
}
}
reservations.put(reservationId, seatsToReserve);
System.out.println("Reserved seats " + seatsToReserve + " for passengers " + Arrays.toString(args) + " with reservation ID " + reservationId);
}
public void cancel(String[] args) {
if (args.length < 1) {
System.out.println("Invalid command format");
return;
}
for (String reservationId : args) {
List<Integer> seatsToCancel = reservations.get(reservationId);
if (seatsToCancel != null) {
for (int seat : seatsToCancel) {
seats[seat / cols][seat % cols] = false;
}
reservations.remove(reservationId);
System.out.println("Cancelled reservation " + reservationId);
} else {
System.out.println("Reservation " + reservationId + " not found");
}
}
}
public void list() {
System.out.println("Flight " + flightName + " seat status:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(seats[i][j] ? "X " : "O ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
FlightManagement flightManagement = new FlightManagement("Flight 001", 10, 6);
boolean running = true;
while (running) {
System.out.print("Command: ");
String[] input = scanner.nextLine().split("\\s+");
switch (input[0]) {
case "create":
flightManagement.create(Arrays.copyOfRange(input, 1, input.length));
break;
case "reserve":
flightManagement.reserve(Arrays.copyOfRange(input, 1, input.length));
break;
case "cancel":
flightManagement.cancel(Arrays.copyOfRange(input, 1, input.length));
break;
case "list":
flightManagement.list();
break;
case "exit":
running = false;
break;
default:
System.out.println("Invalid command");
}
}
scanner.close();
}
}
```
这个程序实现了航班管理的各种功能,包括创建航班、预订座位、取消预订、显示预订情况和退出程序等。可以通过命令行输入不同的命令来调用不同的功能。
阅读全文