-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlight.java
74 lines (64 loc) · 1.93 KB
/
Flight.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package flight;
//航班类
public class Flight implements FlightInterface {
private String flightName;
private int row;
private int rowLength;
private int[] fail = {-1};
private Passenger[] passengerList;
public Flight(String FlightName,int rows,int rowLength) throws Exception{
if(FlightName == null||FlightName.trim().length() == 0||rows <= 0||rowLength <= 0)
throw new Exception("Error");
else {
this.flightName = FlightName;
this.row = rows;
this.rowLength = rowLength;
this.passengerList = new Passenger[row * rowLength];
for(int i = 0;i < row*rowLength;i++)
passengerList[i] = null;
}
}
public int[] reserve(String names[]) {//预订航班座位
if(names.length > rowLength)
return fail;
int i = 0,j = 0,k = 0;
boolean flag = false;//T--能安排 F--不能安排
labelA:for(i = 0;i <= row - 1;i++) {//在同一排找相邻且没有被预订的座位,座位的个数是 names.length
for(j = 0;j <= rowLength - names.length;j++) {
for(k = j;k <= j + names.length - 1;k++) {
if(passengerList[i * rowLength + k] != null)
break;
}
if(k > j +names.length - 1) {
flag = true;
break labelA;
}
}
}
if(!flag)
return fail;
int[] bn = new int[names.length ];//每一个旅客返回一个预订号
for(k = j;k <= j + names.length - 1;k++) {
bn[k - j] = i * rowLength + k + 1;
passengerList[i * rowLength + k] = new Passenger(names[k - j],i * rowLength + k + 1,i,k);
}
return bn;
}
public boolean cancel(int bookingNumber) {//取消预订座位
boolean Status = false;
for(int i = 0;i < row * rowLength;i++) {
if(passengerList[i] != null && bookingNumber == passengerList[i].getBookingNumber()) {
Status = true;
passengerList[i] = null;
break;
}
}
return Status;
}
public Passenger[] getPassengerList() {
return passengerList;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
}
}