-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flight.java
311 lines (253 loc) · 6.32 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package DSL_MiniProject;
import java.util.Stack;
class Flight{
private int seats;
private int economy;
private int business;
private int firstClass;
private double economy_price;
private double business_price;
private double first_price;
private String src;
private String dst;
private String d_time;
private String a_time;
private String flightNo;
private FlightPassenger root;
FlightPassenger parent;
Flight(){
this.seats=0;
this.economy=0;
this.business=0;
this.firstClass=0;
this.economy_price=0;
this.business_price=0;
this.first_price=0;
this.src="";
this.dst="";
this.flightNo="";
this.root=null;
}
Flight(int seats, int economy,int business, int firstClass, int economy_price, int business_price, int first_price, String src, String dst,String d_time, String a_time, String flightNo){
this.seats=seats;
this.economy=economy;
this.business=business;
this.firstClass=firstClass;
this.economy_price=economy_price;
this.business_price=business_price;
this.first_price=first_price;
this.src="";
this.dst="";
this.d_time=d_time;
this.a_time=a_time;
this.flightNo=flightNo;
this.root=null;
}
double get_economyPrice() {
return economy_price;
}
double get_businessPrice() {
return business_price;
}
double get_firstPrice() {
return first_price;
}
double get_price(char seatType) {
if(seatType=='e') {
return economy_price;
}
else if(seatType=='b') {
return business_price;
}
else if(seatType=='f') {
return first_price;
}
return 0;
}
String get_flightNo() {
return flightNo;
}
String get_atime() {
return a_time;
}
String get_dtime() {
return d_time;
}
boolean addPassenger(Passenger P,char seatType, int seatNos) {
int flag=1;
switch(seatType) {
case 'e':
if(economy>=seatNos) {
economy-=seatNos;
}
else {
flag=0;
}
break;
case 'b':
if(business>=seatNos) {
business-=seatNos;
}
else {
flag=0;
}
break;
case 'f':
if(firstClass>=seatNos) {
firstClass-=seatNos;
}
else {
flag=0;
}
break;
default: seats-=seatNos;
}
if(flag==0) {
return false;
}
FlightPassenger temp=new FlightPassenger(P,seatType,seatNos);
if(root==null) {
root=temp;
}
else {
FlightPassenger ptr=root;
if(ptr!=null) {
while(true) {
if(ptr.get_flightPassenger().getEmail().compareTo(temp.get_flightPassenger().getEmail())==0){
break;
}
else {
if(ptr.get_flightPassenger().getEmail().compareTo(temp.get_flightPassenger().getEmail())>0) {
if(ptr.get_lc()==null) {
ptr.set_lc(temp);
break;
}
else {
ptr=ptr.get_lc();
}
}
if(ptr.get_flightPassenger().getEmail().compareTo(temp.get_flightPassenger().getEmail())<0) {
if(ptr.get_rc()==null) {
ptr.set_rc(temp);
break;
}
else {
ptr=ptr.get_rc();
}
}
}
}
}
}
return true;
}
void displayFlightPassengers(){
if(root==null) {
System.out.println("No flight bookings yet!");
return;
}
Stack<FlightPassenger> stack=new Stack<>();
FlightPassenger ptr=root;
while(ptr!=null || !stack.isEmpty()) {
while(ptr!=null) {
stack.push(ptr);
ptr=ptr.get_lc();
}
if(!stack.isEmpty()) {
ptr=stack.pop();
//System.out.print(ptr.get_flightPassenger().display_Tickets()+"\n"+ptr.get_seatType()+" "+ptr.getSeatNos());
}
}
}
FlightPassenger searchPassenger(Passenger P) {
FlightPassenger ptr=root;
parent=root;
while(ptr!=null) {
if(P.getEmail().compareTo(ptr.get_flightPassenger().getEmail())>0) {
parent=ptr;
ptr=ptr.get_rc();
}
else if(P.getEmail().compareTo(ptr.get_flightPassenger().getEmail())<0) {
parent=ptr;
ptr=ptr.get_lc();
}
else if(P.getEmail().compareTo(ptr.get_flightPassenger().getEmail())==0) {
return ptr;
}
}
return null;
}
void deletePassenger(Passenger P,char seatType, int seatNos) {
FlightPassenger ptr=searchPassenger(P);
if(ptr==null) {
System.out.println("User has no booked seats in this flight!");
return;
}
if(ptr.get_lc()!=null && ptr.get_rc()!=null) { //node deletion with both children present
parent=ptr;
FlightPassenger p=ptr.get_lc(); //root node as well as non-root node
while(p.get_rc()!=null) {
parent=p;
p=p.get_rc();
}
ptr.set_flightPassenger(p.get_flightPassenger());
ptr.set_seatType(p.get_seatType());
ptr.setSeatNos(p.getSeatNos());
if(parent!=ptr) {
parent.set_rc(p.get_lc());
}
else {
parent.set_lc(p.get_lc());
}
}
if(parent!=null) { //non root node deletion
if(ptr.get_lc()==null && ptr.get_rc()==null) {
if(ptr==parent.get_lc()) {
parent.set_lc(null);
}
if(ptr==parent.get_rc()) {
parent.set_rc(null);
}
}
else if(ptr.get_lc()!=null && ptr.get_rc()==null) {
if(ptr==parent.get_lc()) {
parent.set_lc(ptr.get_lc());
}
if(ptr==parent.get_rc()) {
parent.set_rc(ptr.get_lc());
}
}
else if(ptr.get_lc()==null && ptr.get_rc()!=null) {
if(ptr==parent.get_lc()) {
if(ptr==parent.get_lc()) {
parent.set_lc(ptr.get_rc());
}
if(ptr==parent.get_rc()) {
parent.set_rc(ptr.get_rc());
}
}
}
}
if(parent==null) { //root node deletion
if(ptr.get_lc()==null && ptr.get_rc()==null) {
root=null;
}
else if(ptr.get_lc()!=null && ptr.get_rc()==null) {
root=root.get_lc();
}
else if(ptr.get_lc()==null && ptr.get_rc()!=null){
root=root.get_rc();
}
}
switch(seatType) {
case 'e': economy+=seatNos;
break;
case 'b': business+=seatNos;
break;
case 'f':firstClass+=seatNos;
break;
default: seats+=seatNos;
break;
}
}
}