-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tark1.java
664 lines (629 loc) · 33.6 KB
/
Tark1.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.*;
import javax.xml.transform.Source;
import java.time.format.*;
public class Tark1{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String trainnum1="17726";
String trainnum2="37392";
String trainnum3="29772";
String[] stop1={"Rajkot","Ahmedabad","Vadodra","Surat","Mumbai"};
String[] stop2={"Ahmedabad", "Anand", "Vadodara","Bharuch","Surat"};
String[] stop3={"Vadodra","Dahod","Indore"};
int[] dist1={0,200,300,500,750};
int[] dist2={0,50,100,200,300};
int[] dist3={0,150,350};
String[] coach1={"S1","S2","B1","A1","H1"};
String[] coach2={"S1","S2","S3","B1","B2"};
String[] coach3={"S1","S2","B1","A1"};
int[] numofseats1={72,72,72,48,24};
int[] numofseats2={15,20,50,36,48};
int[] numofseats3={72,72,72,48};
int ticknum=10000001;
Train[] trains = new Train[3];
trains[0]=new Train(trainnum1,stop1,dist1,coach1,numofseats1);
trains[1]=new Train(trainnum2,stop2,dist2,coach2,numofseats2);
trains[2]=new Train(trainnum3,stop3,dist3,coach3,numofseats3);
Map<Integer,String>pnr=new HashMap<>();
Map<Integer,String>report=new HashMap<>();
while (true)
{
System.out.println("Write BOOK for booking");
System.out.println("Write PNR for booked ticket details");
System.out.println("Write REPORT for report of booked tickets");
System.out.println("Write EXIT to exit");
String in=sc.nextLine();
if(in.equals("EXIT"))
{
System.out.println("Thank You for choosing Indian Railways");
System.out.println("We hope to serve you again!");
break;
}
else if(in.equals("REPORT"))
{
if(ticknum!=10000001)
{
for (Integer key : report.keySet()) {
String value = report.get(key);
System.out.println(key + " " + value);
}
}
else
{
System.out.println("No booked tickets found! Book a ticket to get data!");
}
System.out.println();
}
else if(in.equals("PNR"))
{
System.out.println("Enter the pnr number of the ticket");
Integer pnrid=sc.nextInt();
if(ticknum==10000001)
{
System.out.println("No booked tickets found.Book a ticket now!");
}
else
{
if(pnr.containsKey(pnrid))
{
System.out.println(pnrid+" "+pnr.get(pnrid));
}
else
{
System.out.println("There is no such PNR for booked ticket");
}
}
}
else if(in.equals("BOOK"))
{
List<Seat>booked=new ArrayList<>();
System.out.println("Enter the input");
String input=sc.nextLine();
String[] inputs=input.split(" ");
String source=inputs[0];
String dest=inputs[1];
LocalDate inputdate=LocalDate.parse(inputs[2]);
String coa=inputs[3];
int num=Integer.parseInt(inputs[4]);
int num1=num;
boolean flag=false,flag1=false;
City sourceCity=new City();
City destCity=new City();
for(Train train:trains)
{
for (City city : train.cities)
{
if(source.equals(city.name))
{
flag=true;
sourceCity=city;
}
else if(dest.equals(city.name) && flag==true && train.check(num,coa,inputdate,sourceCity,destCity)==true)
{
flag1=true;
System.out.println(train.trainno);
destCity=city;
break;
}
}
}
if(flag1==true)
{
System.out.println("Enter the train you want to book");
String trainnum=sc.nextLine();
for(Train train:trains)
{
if(train.trainno.equals(trainnum))
{
String st="";
outerloop:
for(Coach coach:train.coaches)
{
if(coach.coachType.equals(CoachType.Sleeper) && coa.startsWith("S"))
{
for(Seat seat:coach.seats)
{
if(num==0)
{
break outerloop;
}
else
{
boolean dtfound=false;
for(ReservedDate reservedate:seat.ReservedDates)
{
if(reservedate.date.equals(inputdate))
{
dtfound=true;
boolean locfound=false;
for(ReservedLoc reservedloc:reservedate.reservedLocs)
{
if((reservedloc.source.equals(sourceCity)||reservedloc.destination.equals(destCity))||((reservedloc.source.dist<sourceCity.dist && reservedloc.destination.dist>sourceCity.dist) || (reservedloc.source.dist<destCity.dist && reservedloc.destination.dist>destCity.dist))||((reservedloc.source.dist>sourceCity.dist && reservedloc.source.dist<destCity.dist)||(reservedloc.destination.dist>sourceCity.dist && reservedloc.destination.dist<destCity.dist)))
{
locfound=true;
break;
}
}
if(!locfound)
{
st+=seat.seatNo+" ";
booked.add(seat);
num--;
reservedate.reservedLocs.add(new ReservedLoc(sourceCity,destCity));
}
break;
}
}
if(!dtfound)
{
ReservedDate reserv=new ReservedDate();
reserv.date=inputdate;
reserv.reservedLocs=new ArrayList<>();
reserv.reservedLocs.add(new ReservedLoc(sourceCity, destCity));
seat.ReservedDates.add(reserv);
st+=seat.seatNo+" ";
booked.add(seat);
num--;
}
}
}
}
else if(coach.coachType.equals(CoachType.TIER_3) && coa.startsWith("3"))
{
for(Seat seat:coach.seats)
{
if(num==0)
{
break outerloop;
}
else
{
boolean dtfound=false;
for(ReservedDate reservedate:seat.ReservedDates)
{
if(reservedate.date.equals(inputdate))
{
dtfound=true;
boolean locfound=false;
for(ReservedLoc reservedloc:reservedate.reservedLocs)
{
if((reservedloc.source.equals(sourceCity)||reservedloc.destination.equals(destCity))||((reservedloc.source.dist<sourceCity.dist && reservedloc.destination.dist>sourceCity.dist) || (reservedloc.source.dist<destCity.dist && reservedloc.destination.dist>destCity.dist))||((reservedloc.source.dist>sourceCity.dist && reservedloc.source.dist<destCity.dist)||(reservedloc.destination.dist>sourceCity.dist && reservedloc.destination.dist<destCity.dist)))
{
locfound=true;
break;
}
}
if(!locfound)
{
booked.add(seat);
st+=seat.seatNo+" ";
num--;
reservedate.reservedLocs.add(new ReservedLoc(sourceCity,destCity));
}
break;
}
}
if(!dtfound)
{
ReservedDate reserv=new ReservedDate();
reserv.date=inputdate;
reserv.reservedLocs=new ArrayList<>();
reserv.reservedLocs.add(new ReservedLoc(sourceCity, destCity));
seat.ReservedDates.add(reserv);
booked.add(seat);
st+=seat.seatNo+" ";
num--;
}
}
}
}
else if(coach.coachType.equals(CoachType.TIER_2) && coa.startsWith("2"))
{
for(Seat seat:coach.seats)
{
if(num==0)
{
break outerloop;
}
else
{
boolean dtfound=false;
for(ReservedDate reservedate:seat.ReservedDates)
{
if(reservedate.date.equals(inputdate))
{
dtfound=true;
boolean locfound=false;
for(ReservedLoc reservedloc:reservedate.reservedLocs)
{
if((reservedloc.source.equals(sourceCity)||reservedloc.destination.equals(destCity))||((reservedloc.source.dist<sourceCity.dist && reservedloc.destination.dist>sourceCity.dist) || (reservedloc.source.dist<destCity.dist && reservedloc.destination.dist>destCity.dist))||((reservedloc.source.dist>sourceCity.dist && reservedloc.source.dist<destCity.dist)||(reservedloc.destination.dist>sourceCity.dist && reservedloc.destination.dist<destCity.dist)))
{
locfound=true;
break;
}
}
if(!locfound)
{
booked.add(seat);
st+=seat.seatNo+" ";
num--;
reservedate.reservedLocs.add(new ReservedLoc(sourceCity,destCity));
}
break;
}
}
if(!dtfound)
{
ReservedDate reserv=new ReservedDate();
reserv.date=inputdate;
reserv.reservedLocs=new ArrayList<>();
reserv.reservedLocs.add(new ReservedLoc(sourceCity, destCity));
seat.ReservedDates.add(reserv);
booked.add(seat);
st+=seat.seatNo+" ";
num--;
}
}
}
}
else if(coach.coachType.equals(CoachType.TIER_1) && coa.startsWith("1"))
{
for(Seat seat:coach.seats)
{
if(num==0)
{
break outerloop;
}
else
{
boolean dtfound=false;
for(ReservedDate reservedate:seat.ReservedDates)
{
if(reservedate.date.equals(inputdate))
{
dtfound=true;
boolean locfound=false;
for(ReservedLoc reservedloc:reservedate.reservedLocs)
{
if((reservedloc.source.equals(sourceCity)||reservedloc.destination.equals(destCity))||((reservedloc.source.dist<sourceCity.dist && reservedloc.destination.dist>sourceCity.dist) || (reservedloc.source.dist<destCity.dist && reservedloc.destination.dist>destCity.dist))||((reservedloc.source.dist>sourceCity.dist && reservedloc.source.dist<destCity.dist)||(reservedloc.destination.dist>sourceCity.dist && reservedloc.destination.dist<destCity.dist)))
{
locfound=true;
break;
}
}
if(!locfound)
{
booked.add(seat);
st+=seat.seatNo+" ";
num--;
reservedate.reservedLocs.add(new ReservedLoc(sourceCity,destCity));
}
break;
}
}
if(!dtfound)
{
ReservedDate reserv=new ReservedDate();
reserv.date=inputdate;
reserv.reservedLocs=new ArrayList<>();
reserv.reservedLocs.add(new ReservedLoc(sourceCity, destCity));
seat.ReservedDates.add(reserv);
booked.add(seat);
st+=seat.seatNo+" ";
num--;
}
}
}
}
}
System.out.println(ticknum);
String s1="",s2="";
s1+=String.valueOf(trainnum)+" "+sourceCity.name+" "+destCity.name+" "+String.valueOf(inputdate)+" ";
s2+=String.valueOf(inputdate)+" "+String.valueOf(trainnum)+" "+sourceCity.name+" "+destCity.name+" ";
if(coa.startsWith("S"))
{
System.out.println(CoachType.Sleeper.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1));
s1+=CoachType.Sleeper.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1);
s2+=CoachType.Sleeper.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1);
}
else if(coa.startsWith("3"))
{
System.out.println(CoachType.TIER_3.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1));
s1+=CoachType.TIER_3.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1);
s2+=CoachType.TIER_3.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1);
}
else if(coa.startsWith("2"))
{
System.out.println(CoachType.TIER_2.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1));
s1+=CoachType.TIER_2.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1);
s2+=CoachType.TIER_2.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1);
}
else if(coa.startsWith("1"))
{
System.out.println(CoachType.TIER_1.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1));
s1+=CoachType.TIER_1.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1);
s2+=CoachType.TIER_1.calculateFare(Math.abs(sourceCity.dist-destCity.dist), num1);
}
s1=s1+" "+st;
s2=s2+" "+st;
pnr.put(ticknum,s1);
report.put(ticknum,s2);
System.out.println("Ticket Booked Successfully");
ticknum++;
}
}
}
else
{
System.out.println("Try booking seats with a lesser number");
}
}
else
{
System.out.println("Please enter a valid keyword from given instructions");
}
}
}
}
class City {
String name;
int dist;
}
enum CoachType{
Sleeper(1),TIER_3(2),TIER_2(3),TIER_1(4);
int fareperkm;
CoachType(int fare)
{
this.fareperkm=fare;
}
int calculateFare(int distance,int passenger)
{
return this.fareperkm*distance*passenger;
}
}
class Coach{
String coachName;
CoachType coachType;
List<Seat>seats;
void getCoachType(String coachName)
{
if (coachName.charAt(0)== 'S')
{
this.coachType=CoachType.Sleeper;
}
else if (coachName.charAt(0)== 'B')
{
this.coachType=CoachType.TIER_3;
}
else if (coachName.charAt(0)== 'A')
{
this.coachType=CoachType.TIER_2;
}
else
{
this.coachType=CoachType.TIER_1;
}
}
}
class Train{
String trainno;
List<City> cities = new ArrayList<>();
List<Coach>coaches=new ArrayList<>();
Train(String trainno,String[] stop,int[] dist,String[] coach,int[] numofseats)
{
this.trainno=trainno;
for(int i=0;i<stop.length;i++)
{
City city=new City();
city.name=stop[i];
city.dist=dist[i];
cities.add(city);
}
for(int i=0;i<coach.length;i++)
{
Coach coache=new Coach();
String coachName=coach[i];
coache.coachName=coachName;
coache.getCoachType(coachName);
coache.seats=new ArrayList<>();
for(int k=1;k<=numofseats[i];k++)
{
Seat seat=new Seat();
seat.seatNo=coache.coachName+"-"+k;
coache.seats.add(seat);
seat.ReservedDates=new ArrayList<>();
}
coaches.add(coache);
}
}
String[] booking(int num,String source,String dest,LocalDate date,int fare)
{
String[] str=new String[5];
str[0]=String.valueOf(num);
str[1]=source;
str[2]=dest;
str[3]=String.valueOf(date);
str[4]=String.valueOf(fare);
return str;
}
boolean check(int num,String coa,LocalDate inputdate,City sourceCity,City destCity)
{
int c=0,num1=num;
outerloop:
for(Coach coach:coaches)
{
if(coach.coachType.equals(CoachType.Sleeper) && coa.startsWith("S"))
{
for(Seat seat:coach.seats)
{
if(num<=0)
{
break outerloop;
}
boolean dtfound=false;
for(ReservedDate reservedate:seat.ReservedDates)
{
if(reservedate.date.equals(inputdate))
{
dtfound=true;
boolean locfound=false;
for(ReservedLoc reservedloc:reservedate.reservedLocs)
{
if((reservedloc.source.equals(sourceCity)||reservedloc.destination.equals(destCity))||((reservedloc.source.dist<sourceCity.dist && reservedloc.destination.dist>sourceCity.dist) || (reservedloc.source.dist<destCity.dist && reservedloc.destination.dist>destCity.dist))||((reservedloc.source.dist>sourceCity.dist && reservedloc.source.dist<destCity.dist)||(reservedloc.destination.dist>sourceCity.dist && reservedloc.destination.dist<destCity.dist)))
{
locfound=true;
break;
}
}
if(!locfound)
{
c++;
num--;
}
break;
}
}
if(!dtfound)
{
c++;
num--;
}
}
}
else if(coach.coachType.equals(CoachType.TIER_3) && coa.startsWith("3"))
{
for(Seat seat:coach.seats)
{
if(num<=0)
{
break outerloop;
}
boolean dtfound=false;
for(ReservedDate reservedate:seat.ReservedDates)
{
if(reservedate.date.equals(inputdate))
{
dtfound=true;
boolean locfound=false;
for(ReservedLoc reservedloc:reservedate.reservedLocs)
{
if((reservedloc.source.equals(sourceCity)||reservedloc.destination.equals(destCity))||((reservedloc.source.dist<sourceCity.dist && reservedloc.destination.dist>sourceCity.dist) || (reservedloc.source.dist<destCity.dist && reservedloc.destination.dist>destCity.dist))||((reservedloc.source.dist>sourceCity.dist && reservedloc.source.dist<destCity.dist)||(reservedloc.destination.dist>sourceCity.dist && reservedloc.destination.dist<destCity.dist)))
{
locfound=true;
break;
}
}
if(!locfound)
{
c++;
num--;
}
break;
}
}
if(!dtfound)
{
c++;
num--;
}
}
}
else if(coach.coachType.equals(CoachType.TIER_2) && coa.startsWith("2"))
{
for(Seat seat:coach.seats)
{
if(num<=0)
{
break outerloop;
}
boolean dtfound=false;
for(ReservedDate reservedate:seat.ReservedDates)
{
if(reservedate.date.equals(inputdate))
{
dtfound=true;
boolean locfound=false;
for(ReservedLoc reservedloc:reservedate.reservedLocs)
{
if((reservedloc.source.equals(sourceCity)||reservedloc.destination.equals(destCity))||((reservedloc.source.dist<sourceCity.dist && reservedloc.destination.dist>sourceCity.dist) || (reservedloc.source.dist<destCity.dist && reservedloc.destination.dist>destCity.dist))||((reservedloc.source.dist>sourceCity.dist && reservedloc.source.dist<destCity.dist)||(reservedloc.destination.dist>sourceCity.dist && reservedloc.destination.dist<destCity.dist)))
{
locfound=true;
break;
}
}
if(!locfound)
{
c++;
num--;
}
break;
}
}
if(!dtfound)
{
c++;
num--;
}
}
}
else if(coach.coachType.equals(CoachType.TIER_1) && coa.startsWith("1"))
{
for(Seat seat:coach.seats)
{
if(num<=0)
{
break outerloop;
}
boolean dtfound=false;
for(ReservedDate reservedate:seat.ReservedDates)
{
if(reservedate.date.equals(inputdate))
{
dtfound=true;
boolean locfound=false;
for(ReservedLoc reservedloc:reservedate.reservedLocs)
{
if((reservedloc.source.equals(sourceCity)||reservedloc.destination.equals(destCity))||((reservedloc.source.dist<sourceCity.dist && reservedloc.destination.dist>sourceCity.dist) || (reservedloc.source.dist<destCity.dist && reservedloc.destination.dist>destCity.dist))||((reservedloc.source.dist>sourceCity.dist && reservedloc.source.dist<destCity.dist)||(reservedloc.destination.dist>sourceCity.dist && reservedloc.destination.dist<destCity.dist)))
{
locfound=true;
break;
}
}
if(!locfound)
{
c++;
num--;
}
break;
}
}
if(!dtfound)
{
c++;
num--;
}
}
}
}
return c==num1;
}
}
class Seat{
String seatNo;
List<ReservedDate>ReservedDates;
}
class ReservedDate{
LocalDate date;
List<ReservedLoc>reservedLocs;
}
class ReservedLoc{
City source;
City destination;
ReservedLoc(City source,City Dest)
{
this.source=source;
this.destination=Dest;
}
}