-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flight.java
214 lines (172 loc) · 6.28 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package flightSystem2;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import flightSystem2.Admin;
import flightSystem.Passenger;
public class Flight {
public static enum FlightStatus{TERMINATE,FULL,AVAILABLE,UNPUBLISHED}
private static FlightStatus status=FlightStatus.UNPUBLISHED;//Initial status
private String FlightID;
private String startTime;
private String arrivalTime;
private String startCity;
private String arrivalCity;
private String departureDate;
private int price;
private int seatCapacity;
private ArrayList<Passenger> passengersID;
public static ArrayList<Flight>flights=new ArrayList<Flight>();
public Flight(String flightid,String starttime,String arrivaltime,String startcity,String arrivalcity,String departuredate,int price,int seatcapacity){
FlightID=flightid;
startTime=starttime;
arrivalTime=arrivaltime;
startCity=startcity;
arrivalCity=arrivalcity;
departureDate=departuredate;
this.price=price;
seatCapacity=seatcapacity;
status=FlightStatus.UNPUBLISHED;
}
public static void initializeFlightData() throws FileNotFoundException{
ArrayList<Flight>flights=new ArrayList<Flight>();
Flight f1=new Flight("CZ1000","11:10:00","15:45:00","ShenZhen","Beijing","5/24/2017",1999,188);
Flight f2=new Flight("CZ1001","10:30:00","12:20:00","Beijing","ShenZhen","5/24/2017",880,200);
Flight f3=new Flight("CZ1002","15:20:00","17:40:00","ShenZhen","ShangHai","5/24/2017",799,132);
Flight f4=new Flight("CZ1003","15:20:00","17:40:00","ShangHai","ShenZhen","5/24/2017",799,132);
Flight f5=new Flight("CZ1004","15:20:00","17:40:00","ShangHai","Beijing","5/24/2017",799,132);
Flight f6=new Flight("CZ1005","15:20:00","17:40:00","Beijing","ShangHai","5/24/2017",799,132);
Flight f7=new Flight("CZ1006","15:20:00","17:40:00","ShenZhen","ShangHai","5/25/2017",799,132);
Flight f8=new Flight("CZ1007","15:20:00","17:40:00","ShangHai","ShenZhen","5/25/2017",799,132);
Flight f9=new Flight("CZ1008","15:20:00","17:40:00","ShenZhen","Beijing","5/25/2017",799,132);
Flight f10=new Flight("CZ1009","15:20:00","17:40:00","Beijing","ShenZhen","5/25/2017",799,132);
Flight f11=new Flight("CZ1010","15:20:00","17:40:00","ShangHai","Beijing","5/25/2017",799,132);
Flight f12=new Flight("CZ1011","15:20:00","17:40:00","Beijing","ShangHai","5/25/2017",799,132);
Flight f13=new Flight("CZ1012","15:20:00","17:40:00","ShenZhen","ShangHai","5/26/2017",799,132);
Flight f14=new Flight("CZ1013","15:20:00","17:40:00","ShangHai","ShenZhen","5/26/2017",799,132);
Flight f15=new Flight("CZ1014","15:20:00","17:40:00","ShenZhen","Beijing","5/26/2017",799,132);
Flight f16=new Flight("CZ1015","15:20:00","17:40:00","Beijing","ShenZhen","5/26/2017",799,132);
Flight f17=new Flight("CZ1016","15:20:00","17:40:00","ShangHai","Beijing","5/26/2017",799,132);
Flight f18=new Flight("CZ1017","15:20:00","17:40:00","Beijing","ShangHai","5/26/2017",799,132);
flights.add(f1);
flights.add(f2);
flights.add(f3);
flights.add(f4);
flights.add(f5);
flights.add(f6);
flights.add(f7);
flights.add(f8);
flights.add(f9);
flights.add(f10);
flights.add(f11);
flights.add(f12);
flights.add(f13);
flights.add(f14);
flights.add(f15);
flights.add(f16);
flights.add(f17);
flights.add(f18);
Admin.writeFile(flights);
}
public static FlightStatus getStatus() {
return status;
}
public void setStatus(FlightStatus status) {
this.status = status;
}
public String getFlightID() {
return FlightID;
}
public void setFlightID(String flightID) {
FlightID = flightID;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(String arrivalTime) {
this.arrivalTime = arrivalTime;
}
public String getStartCity() {
return startCity;
}
public void setStartCity(String startCity) {
this.startCity = startCity;
}
public String getArrivalCity() {
return arrivalCity;
}
public void setArrivalCity(String arrivalCity) {
this.arrivalCity = arrivalCity;
}
public String getDepartureDate() {
return departureDate;
}
public void setDepartureDate(String departureDate) {
this.departureDate = departureDate;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSeatCapacity() {
return seatCapacity;
}
public void setSeatCapacity(int seatCapacity) {
this.seatCapacity = seatCapacity;
}
public void statusUpdate(){
//transform 2 hours into milliseconds
int time;
time=120*60*1000;
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date now=new Date();
Date afterDate=new Date(now.getTime()+time);
Date dt1 = null;
try {
dt1 = df.parse(departureDate+" "+startTime);
}
catch (ParseException e)
{
e.printStackTrace();
}
//If the plane will take off in less than 2 hours,change the status to terminate
if(afterDate.getTime()>dt1.getTime())
setStatus(FlightStatus.TERMINATE);
}//end statusUpdate method
public String toString(){
return String.format("%-10s%-14s%-14s%-14s%-14s%-14s%-8d%4d\n",
getFlightID(), getStartTime(),getArrivalTime(),
getStartCity(),getArrivalCity(),getDepartureDate(),
getPrice(),getSeatCapacity());
}
public String topartString(){
return String.format("%-10s%-14s%-14s%-14s%-14s%-14s%-8d\n",
getFlightID(), getStartTime(),getArrivalTime(),
getStartCity(),getArrivalCity(),getDepartureDate(),
getPrice());
}
//模糊查找
public static void search(String flightID2, ArrayList<Flight> flights2) {
ArrayList results = new ArrayList();
Pattern pattern = Pattern.compile(flightID2);
for(int i=0; i < flights2.size(); i++){
Matcher matcher = pattern.matcher(((Flight)flights2.get(i)).getFlightID());
if(matcher.find()){
results.add(flights2.get(i));
System.out.printf("FlightID: %s Price: %s Status: %s\n",Flight.flights.get(i).getFlightID(),Flight.flights.get(i).getPrice(),Flight.flights.get(i).status); }
}
}
}