-
Notifications
You must be signed in to change notification settings - Fork 10
/
railway.java
1007 lines (909 loc) · 29.5 KB
/
railway.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.*;
import java.sql.*;
import java.io.*;
import java.text.*;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/////// User//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
class user
{
admin ad=new admin();
railway r=new railway();
String tname,bp,dp;
int tnum,seat,catnum,ch;
String[] pname=new String[1000];
int[] page=new int[1000];
String[] pgen=new String[1000];
Scanner a=new Scanner(System.in);
int check1(int tnum) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
Statement s=c.createStatement();
ResultSet r=s.executeQuery("select * from train where tnum='"+tnum+"' ");
if(r.first())
return 1;
else
return 0;
}
void inputreserve() throws Exception
{
System.out.print("Enter train number : ");
tnum=a.nextInt();
if(check1(tnum)==0)
{
System.out.println("Train number doesn't exist");
r.user_mode();
}
System.out.print("Enter boarding : ");
bp=a.next();
System.out.print("Enter destination : ");
dp=a.next();
System.out.print("Number of seats required : ");
seat=a.nextInt();
java.sql.Date dt2=null;
try
{
System.out.print("Enter date of train's journey in (yyyy-mm-dd) format : ");
String dt=a.next();
java.util.Date dt1=new SimpleDateFormat("yyyy-MM-dd").parse(dt);
dt2=new java.sql.Date(dt1.getTime());
}
catch(Exception e)
{
System.out.println(e);
}
int j=0;
int k=0;
for(int i=0;i<seat;i++)
{
System.out.print("Enter "+(i+1)+" passenger's name : ");
pname[i]=a.next();
System.out.print("Enter "+(i+1)+" passenger's age : ");
page[i]=a.nextInt();
if((page[i]>0 &&page[i]<=12)||(page[i]>=60&&page[i]<120))
k++;
if(page[i]<0||page[i]>120)
{
j=1;
System.out.println("Enter a valid age");
}
System.out.print("Enter "+(i+1)+" passenger's gender : ");
pgen[i]=a.next();
}
if(j==1)
return;
System.out.println("Enter the class : ");
System.out.println("1 - First AC");
System.out.println("2 - Second AC");
System.out.println("3 - Third AC");
System.out.println("4 - Sleeper coach");
ch=a.nextInt();
if((ch!=1)&&(ch!=2)&&(ch!=3)&&(ch!=4))
System.out.println("Choose from above options only");
else
{
String coach;
if(ch==1) coach="First AC";
else if(ch==2) coach="Second AC";
else if(ch==3) coach="Third AC";
else coach="Sleeper Coach";
System.out.println("Enter the concession category : ");
System.out.println("1. Military Personnel");
System.out.println("2. None");
catnum=a.nextInt();
if(catnum!=2&&catnum!=1)
{
System.out.println("Choose from above options only");
r.user_mode();
}
System.out.print("Confirm there is no turning back!!(y/n) ");
String conf=a.next();
if(conf=="n")
{
System.out.println("Your ticket is not booked");
r.user_mode();
}
int fare=reserve(tnum,tname,bp,dp,seat,ch);
if(fare==0)
{
System.out.println("Train number doesn't exist");
r.user_mode();
}
if(catnum==1)
{
System.out.println("Enter how many millitary personnel are traveling and make sure to carry your ID ");
int mil=a.nextInt();
System.out.println("Amount to be paid is "+(fare-(((mil*(fare/seat))*0.35)+((k*(fare/seat))*0.5))));
}
else if(catnum==2)
System.out.println("Amount to be paid is "+(fare-((k*(fare/seat))*0.5)));
chart(pname,page,coach,tnum,dt2);
}
}
int reserve(int tnum,String tname,String bp,String dp,int seat,int ch)
{
int flag=0;
int fare=0;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
Statement s=c.createStatement();
ResultSet r=s.executeQuery("select * from train");
while(r.next())
{
if(tnum==r.getInt(1))
{
flag=1;
if(ch==1)
fare=seat*r.getInt(6);
else if(ch==2)
fare=seat*r.getInt(7);
else if(ch==3)
fare=seat*r.getInt(8);
else
fare=seat*r.getInt(9);
break;
}
}
if(flag!=0)
{
PreparedStatement st=c.prepareStatement("update train set seats=seats-'"+seat+"' where tnum='"+tnum+"' ");
st.execute();
}
}
catch(Exception e)
{
System.out.println(e);
}
if(flag==0)
return 0;
else
return fare;
}
void tckt1() throws Exception
{
tckt();
System.out.print("Do you want to continue or return to main menu (y/n) respectively : ");
String ch=a.next();
if(ch.equals("y"))
{
r.user_mode();
}
else
{
r.main_menu();
}
}
void tckt()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
Statement s1=c.createStatement();
ResultSet r1=s1.executeQuery("select * from chart order by sno desc limit 1");
r1.first();
Statement s2=c.createStatement();
ResultSet r2=s2.executeQuery("select * from chart where pnr='"+r1.getLong(1)+"' ");
r2.first();
java.util.Date d=new java.util.Date();
d.setTime(r2.getTimestamp(8).getTime());
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String str7 = df.format(d);
DateFormat dF = new SimpleDateFormat("dd/MM/yyyy");
String str8 = dF.format(r2.getDate(9));
System.out.println("***************************************************************************************");
System.out.println("PNR Number : "+r2.getLong(1)+" "+"Coach : "+r2.getString(6));
System.out.println("Name : "+r2.getString(2)+" "+"Age : "+r2.getInt(3)+" "+"Gender : "+r2.getString(4));
System.out.println("Status : "+r2.getString(7)+" "+"Seat Number : "+r2.getInt(5));
while(r2.next())
{
System.out.println("Name : "+r2.getString(2)+" "+"Age : "+r2.getInt(3)+" "+"Gender : "+r2.getString(4));
System.out.println("Status : "+r2.getString(7)+" "+"Seat Number : "+r2.getInt(5));
}
System.out.println("Date of Travelling : "+str8+" "+"Booked on : "+str7);
System.out.println("***************************************************************************************");
}
catch (Exception e)
{
System.out.println(e);
}
}
void chart(String pname[],int page[],String coach,int tnum,java.sql.Date dt2)
{
try
{
java.util.Date date=new java.util.Date();
java.sql.Timestamp sqt=new java.sql.Timestamp(date.getTime());
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
Statement s2=c.createStatement();
Statement s1=c.createStatement();
Statement s3=c.createStatement();
ResultSet r3=s3.executeQuery("select * from chart order by sno desc limit 1");
r3.first();
for(int i=0;i<seat;i++)
{
ResultSet r2=s2.executeQuery("select * from chart order by sno desc limit 1");
r2.first();
ResultSet r1=s1.executeQuery("select * from train where tnum='"+tnum+"' and doj='"+dt2+"' ");
r1.first();
PreparedStatement st=c.prepareStatement("insert into chart (pnr,name,age,gender,seatno,coach,status,timestamp,dot,tnum) values(?,?,?,?,?,?,?,?,?,?)");
st.setLong(1,r3.getLong(1)+1);
st.setString(2,pname[i]);
st.setInt(3,page[i]);
st.setString(4,pgen[i]);
if(r1.getInt(3)>0) st.setInt(5,r2.getInt(5)+1);
else st.setInt(5,0);
st.setString(6,coach);
if(r1.getInt(3)>0) st.setString(7,"confirmed");
else st.setString(7,"waiting");
st.setTimestamp(8,sqt);
st.setDate(9,r1.getDate(10));
st.setInt(10,tnum);
st.executeUpdate();
}
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Congrats!!!! Your ticket is booked. Have a nice day!!");
try
{
tckt1();
}
catch(Exception e)
{
System.out.println(e);
}
}
void cancel1() throws Exception
{
cancel();
System.out.print("Do you want to continue or return to main menu (y/n) respectively : ");
String ch=a.next();
if(ch.equals("y"))
{
r.user_mode();
}
else
{
r.main_menu();
}
}
void cancel() throws Exception
{
long pnr;
String j="cancel";
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
System.out.print("Enter PNR Number ");
pnr=a.nextLong();
Statement stmt=c.createStatement();
ResultSet r=stmt.executeQuery("select * from chart where pnr='"+pnr+"' ");
if(r.first())
{
PreparedStatement st=c.prepareStatement("update chart set status='"+j+"' where pnr='"+pnr+"' ");
st.executeUpdate();
}
else
{
System.out.println("PNR number does not exist ");
}
}
void setw(int tnum, String str1, int seats,String str10,String str11, int fAc,int sAc,int tAc,int sc,java.sql.Date doj, String str7,String str9, int width)
{
String str=Integer.toString(tnum);
System.out.print(str);
for (int x = str.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str1);
for (int x = str1.length(); x < width; ++x)
System.out.print(' ');
String str8=Integer.toString(seats);
System.out.print(str8);
for (int x = str8.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str10);
for (int x = str10.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str11);
for (int x = str11.length(); x < width; ++x)
System.out.print(' ');
String str2=Integer.toString(fAc);
System.out.print(str2);
for (int x = str2.length(); x < width; ++x)
System.out.print(' ');
String str3=Integer.toString(sAc);
System.out.print(str3);
for (int x = str3.length(); x < width; ++x)
System.out.print(' ');
String str4=Integer.toString(tAc);
System.out.print(str4);
for (int x = str4.length(); x < width; ++x)
System.out.print(' ');
String str5=Integer.toString(sc);
System.out.print(str5);
for (int x = str5.length(); x < width; ++x)
System.out.print(' ');
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String str6 = df.format(doj);
System.out.print(str6);
for (int x = str6.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str7);
for (int x = str7.length(); x < width; ++x)
System.out.print(' ');
System.out.println(str9);
}
void enquiry1() throws Exception
{
enquiry();
System.out.print("Do you want to continue or return to main menu (y/n) respectively : ");
String ch=a.next();
if(ch.equals("y"))
{
r.user_mode();
}
else
{
r.main_menu();
}
}
void enquiry() throws Exception
{
System.out.print("From ");
String from=a.next();
System.out.print("To ");
String to=a.next();
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
System.out.println("***************************************************************************************************************************************************************************************");
System.out.println("Train Number Train Name Seats Boarding Destination First AC Second AC Third AC Sleeper Coach Journey date Departure Arrival");
System.out.println("***************************************************************************************************************************************************************************************");
Statement st=c.createStatement();
ResultSet r=st.executeQuery("select * from train where bp='"+from+"' and dp='"+to+"' and doj>=CURDATE()");
while(r.next())
{
//tnum*****tname****seats******bp*******dp********fAC****sAC*****tAC******sc***doj*******dtime***atime*****sno
setw(r.getInt(1),r.getString(2),r.getInt(3),r.getString(4),r.getString(5),r.getInt(6),r.getInt(7),r.getInt(8),r.getInt(9),r.getDate(10),r.getString(11),r.getString(12),15);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/////// Admin//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
class admin
{
railway r=new railway();
Scanner i=new Scanner(System.in);
void train_info()
{
int num,seats;
String name,bp,dp;
int[] f=new int[4];
System.out.print("Enter train number : ");
num=i.nextInt();
if(num<9999&&num>99999)
{
System.out.println("Enter correct train number!!!");
}
else
{
System.out.print("Enter Train name for Train number "+num+" : ");
name=i.next();
java.sql.Date date= null;
System.out.print("Enter date of train's journey in (yyyy-mm-dd) format : ");
String dt=i.next();
try
{
java.util.Date dt1=new SimpleDateFormat("yyyy-MM-dd").parse(dt);
date=new java.sql.Date(dt1.getTime());
}
catch(Exception e)
{
System.out.println(e);
}
System.out.print("Enter Boarding Point : ");
bp=i.next();
System.out.print("Destination Point : ");
dp=i.next();
System.out.print("Enter Departure time of train's journey in (hh:mm:ss) format : ");
String dtime=i.next();
System.out.print("Enter arrival time of train's journey in (hh:mm:ss) format : ");
String atime=i.next();
System.out.print("Enter the total number of seats in the train : ");
seats=i.nextInt();
System.out.println("Enter price for each ticket type : ");
System.out.print("First AC : ");
f[0]=i.nextInt();
System.out.print("Second AC : ");
f[1]=i.nextInt();
System.out.print("Third AC : ");
f[2]=i.nextInt();
System.out.print("Sleeper Class : ");
f[3]=i.nextInt();
train_db(num,name,seats,bp,dp,f[0],f[1],f[2],f[3],date,dtime,atime);
}
}
void user_signup() throws Exception
{
String uname;
String password;
String gen;
int age;
System.out.print("Enter Username : ");
uname=i.next();
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
Statement s=c.createStatement();
ResultSet rl=s.executeQuery("select uname from user where uname='"+uname+"' ");
if(!rl.first())
{
System.out.print("Please Enter Your Password : ");
password=i.next();
System.out.print("Please Enter Your Age : ");
age=i.nextInt();
System.out.print("Enter Gender(M/F) : ");
gen=i.next();
user_db(uname,password,age,gen);
}
else
System.out.println("Username alredy exist");
System.out.print("Do you want to continue or return to main menu (y/n) respectively : ");
String ch=i.next();
if(ch.equals("y"))
{
r.user_mode();
}
else
{
r.main_menu();
}
}
int user_login() throws Exception
{
String uname;
String password;
int flag=0;
System.out.print("Enter Username : ");
uname=i.next();
System.out.print("Please Enter Your Password : ");
password=i.next();
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
Statement s=c.createStatement();
ResultSet r=s.executeQuery("select uname,pass from user");
r.first();
if(uname.equals(r.getString(1))&&password.equals(r.getString(2)))
{
return 1;
}
else
{
while(r.next())
{
if(uname.equals(r.getString(1))&&password.equals(r.getString(2)))
return 1;
}
}
return 0;
}
void train_db(int tnum,String tname,int seats,String bp,String dp,int i,int j,int k,int l,java.sql.Date date,String dtime,String atime)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
PreparedStatement st=c.prepareStatement("insert into train (tnum,tname,seats,bp,dp,fAC,sAC,tAC,sc,doj,dtime,atime) values(?,?,?,?,?,?,?,?,?,?,?,?)");
st.setInt(1,tnum);
st.setString(2,tname);
st.setInt(3,seats);
st.setString(4,bp);
st.setString(5,dp);
st.setInt(6,i);
st.setInt(7,j);
st.setInt(8,k);
st.setInt(9,l);
st.setDate(10,date);
st.setString(11,dtime);
st.setString(12,atime);
st.executeUpdate();
}
catch(Exception e)
{
System.out.println(e);
}
}
void user_db(String uname,String pass,int age,String g)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
java.util.Date date=new java.util.Date();
java.sql.Timestamp sqt=new java.sql.Timestamp(date.getTime());
PreparedStatement st=c.prepareStatement("insert into user(uname,pass,age,g,timestamp) values(?,?,?,?,?)");
st.setString(1,uname);
st.setString(2,pass);
st.setInt(3,age);
st.setString(4,g);
st.setTimestamp(5,sqt);
st.executeUpdate();
}
catch(Exception e)
{
System.out.println(e);
}
}
void cr_train_info() throws Exception
{
String ch="y";
while(ch=="y")
{
train_info();
System.out.print("Do you want to continue (y/n) : ");
ch=i.next();
}
r.admin_mode();
}
void train_update_date() throws Exception
{
try
{
int tnum,d;
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
System.out.print("Enter train Number whose date you want to update : ");
tnum=i.nextInt();
System.out.print("Enter number of days to be incremented : ");
d=i.nextInt();
PreparedStatement st=c.prepareStatement("update train set doj = DATE_ADD(doj, INTERVAL '"+d+"' DAY) where tnum='"+tnum+"' ");
st.execute();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.print("Do you want to continue or return to main menu (y/n) respectively : ");
String ch=i.next();
if(ch.equals("y"))
{
r.admin_mode();
}
else
{
r.main_menu();
}
}
void setwR(int tnum, String str1, int seats,String str10,String str11, int fAc,int sAc,int tAc,int sc,java.util.Date doj, String str7,String str9, int width)
{
String str=Integer.toString(tnum);
System.out.print(str);
for (int x = str.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str1);
for (int x = str1.length(); x < width; ++x)
System.out.print(' ');
String str8=Integer.toString(seats);
System.out.print(str8);
for (int x = str8.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str10);
for (int x = str10.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str11);
for (int x = str11.length(); x < width; ++x)
System.out.print(' ');
String str2=Integer.toString(fAc);
System.out.print(str2);
for (int x = str2.length(); x < width; ++x)
System.out.print(' ');
String str3=Integer.toString(sAc);
System.out.print(str3);
for (int x = str3.length(); x < width; ++x)
System.out.print(' ');
String str4=Integer.toString(tAc);
System.out.print(str4);
for (int x = str4.length(); x < width; ++x)
System.out.print(' ');
String str5=Integer.toString(sc);
System.out.print(str5);
for (int x = str5.length(); x < width; ++x)
System.out.print(' ');
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String str6 = df.format(doj);
System.out.print(str6);
for (int x = str6.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str7);
for (int x = str7.length(); x < width; ++x)
System.out.print(' ');
System.out.println(str9);
}
void dis_train_db()
{
disp_train_db();
try
{
System.out.print("Do you want to continue or return to main menu (y/n) respectively : ");
String ch=i.next();
if(ch.equals("y"))
{
r.admin_mode();
}
else
{
r.main_menu();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
//tnum*****tname****seats******bp*******dp********fAC****sAC*****tAC******sc***doj*******dtime***atime*****sno
void disp_train_db()
{
String ch;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
System.out.println("***************************************************************************************************************************************************************************************");
System.out.println("Train Number Train Name Seats Boarding Destination First AC Second AC Third AC Sleeper Coach Journey date Departure Arrival");
System.out.println("***************************************************************************************************************************************************************************************");
Statement st=c.createStatement();
ResultSet r=st.executeQuery("select * from train");
while(r.next())
{
setwR(r.getInt(1),r.getString(2),r.getInt(3),r.getString(4),r.getString(5),r.getInt(6),r.getInt(7),r.getInt(8),r.getInt(9),r.getDate(10),r.getString(11),r.getString(12),15);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
void setwR(long pnr, String str1,int age,String str3, int seats,String str5,String str6,Timestamp tm,java.util.Date dot, int width)
{
String str=Long.toString(pnr);
System.out.print(str);
for (int x = str.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str1);
for (int x = str1.length(); x < width; ++x)
System.out.print(' ');
String str2=Integer.toString(age);
System.out.print(str2);
for (int x = str2.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str3);
for (int x = str3.length(); x < width; ++x)
System.out.print(' ');
String str4=Integer.toString(seats);
System.out.print(str4);
for (int x = str4.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str5);
for (int x = str5.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str6);
for (int x = str6.length(); x < width; ++x)
System.out.print(' ');
java.util.Date d=new java.util.Date();
d.setTime(tm.getTime());
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String str7 = df.format(d);
System.out.print(str7);
for (int x = str7.length(); x < width; ++x)
System.out.print(' ');
DateFormat dF = new SimpleDateFormat("dd/MM/yyyy");
String str8 = dF.format(dot);
System.out.println(str8);
}
void disp_chart() throws Exception
{
try
{
System.out.print("Enter the train number ");
int tn=i.nextInt();
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
System.out.println("***************************************************************************************************************************************************************************************");
System.out.println("PNR Number Name Age Gender Seat Number Coach Status Booking time Date of travelling");
System.out.println("***************************************************************************************************************************************************************************************");
Statement st=c.createStatement();
ResultSet r=st.executeQuery("select * from chart where pnr>7999999999 and tnum='"+tn+"' ");
while(r.next())
{
setwR(r.getLong(1),r.getString(2),r.getInt(3),r.getString(4),r.getInt(5),r.getString(6),r.getString(7),r.getTimestamp(8),r.getDate(9),20);
}
}
catch(Exception e)
{
System.out.println(e);
}
System.out.print("Do you want to continue or return to main menu (y/n) respectively : ");
String ch=i.next();
if(ch.equals("y"))
{
r.admin_mode();
}
else
{
r.main_menu();
}
}
void setwR(String str,int age,String str3,Timestamp tm,int width)
{
System.out.print(str);
for (int x = str.length(); x < width; ++x)
System.out.print(' ');
String str2=Integer.toString(age);
System.out.print(str2);
for (int x = str2.length(); x < width; ++x)
System.out.print(' ');
System.out.print(str3);
for (int x = str3.length(); x < width; ++x)
System.out.print(' ');
java.util.Date d=new java.util.Date();
d.setTime(tm.getTime());
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String str7 = df.format(d);
System.out.println(str7);
}
void disp_user() throws Exception
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/railway?autoReconnect=true&useSSL=false","root","1234");
System.out.println("***************************************************************************************************************************************************************************************");
System.out.println("Username Age Gender Booking time");
System.out.println("***************************************************************************************************************************************************************************************");
Statement st=c.createStatement();
ResultSet r=st.executeQuery("select * from user");
while(r.next())
{
setwR(r.getString(1),r.getInt(3),r.getString(4),r.getTimestamp(5),25);
}
}
catch(Exception e)
{
System.out.println(e);
}
System.out.print("Do you want to continue or return to main menu (y/n) respectively : ");
String ch=i.next();
if(ch.equals("y"))
{
r.admin_mode();
}
else
{
r.main_menu();
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MAIN /////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
class railway
{
Scanner r=new Scanner(System.in);
void main_menu() throws Exception
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~MAIN MENU~~~~~~~~~~~~~~~~~~~~~~~~~\n1. Admin Mode\n2. User Mode\n3. Exit");
int ch=r.nextInt();
switch(ch)
{
case 1: admin_log();
break;
case 2: u_user_login();
break;
default:System.out.println("Made By Ayush Rai & Anshuman Mishra");
System.exit(0);
break;
}
}
void admin_log() throws Exception
{
System.out.print("Enter password : ");
String ps=r.next();
if(ps.equals("toluene"))
{
admin_mode();
}
else
{
System.out.println("Wrong password contact to the creator!!!!!!");
main_menu();
}
}
void admin_mode() throws Exception
{
admin ad=new admin();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ADMINISTRATOR MENU~~~~~~~~~~~~~~~~~~~~~~~~~\n1. Create detail database of trains\n2. Add Details of trains\n3. Display all the database of trains\n4. Display Chart of a train\n5. Display all users\n6. Update train date\n7. Return to main menu \n8. Exit");
int ch=r.nextInt();
switch(ch)
{
case 1:ad.cr_train_info();
break;
case 2: ad.train_info();
break;
case 3: ad.dis_train_db();
break;
case 4: ad.disp_chart();
break;
case 5: ad.disp_user();
break;
case 6:ad.train_update_date();
break;
case 7:main_menu();
break;
default:System.out.println("Made By Ayush Rai & Anshuman Mishra");
System.exit(0);
break;
}
}
void u_user_login() throws Exception
{
admin ad=new admin();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~WELCOME TO USER MENU~~~~~~~~~~~~~~~~~~~~~~~~~\n1. Login\n2. Sign Up\n3. Return to main menu\n4. Exit");
int ch=r.nextInt();
switch(ch)
{
case 1:user_mode();
break;
case 2:ad.user_signup();
break;
case 3:main_menu();
break;
default:System.out.println("Made By Ayush Rai & Anshuman Mishra");
System.exit(0);
break;
}
}
void user_mode() throws Exception
{
user us=new user();
admin ad=new admin();
if(ad.user_login()==1)
{
System.out.println("1. Book a ticket\n2. Cancel a ticket\n3. Enquiry\n4. Return to main menu\n5. Exit");
int ch=r.nextInt();
switch(ch)
{
case 1: us.inputreserve();
break;
case 2: us.cancel1();
break;
case 3: us.enquiry1();
break;
case 4: main_menu();
break;
default:System.out.println("Made By Ayush Rai & Anshuman Mishra");
System.exit(0);
break;
}
}
else
{
System.out.println("Wrong username or password!!!!!!");
u_user_login();
}
}
public static void main(String args[]) throws Exception
{
System.out.println("**************************************************************************************************************");
System.out.println(" RAILWAY RESERVATION SYSTEM ");