Subversion Repositories programming

Rev

Rev 100 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
81 irasnyd 1
/* Written by Ira Snyder
2
 * Started on 03-17-2005
3
 * CS435 Homework #3
4
 */
5
import java.io.*;
6
import java.sql.*;
7
 
8
class Homework03 {
9
        public static void main ( String [] args ) throws Exception {
10
 
11
                BufferedReader kb = new BufferedReader(
12
                                        new InputStreamReader( System.in ));
13
 
14
                boolean quit=false;
15
                String inStr;
16
                int input;
17
 
18
                while( quit == false ) { //the menu loop
19
                        printmenu();
20
                        inStr = kb.readLine();
21
                        input = Integer.parseInt(inStr);
22
 
23
                        switch( input ) {
24
                                case 1: userAddDriver(); break;
25
                                case 2: userEditDriver(); break;
26
                                case 3: displayAllDriver(); break;
27
                                case 4: userAddBus(); break;
28
                                case 5: userEditBus(); break;
29
                                case 6: displayAllBus(); break;
30
                                case 7: userAddTrip(); break;
31
                                case 8: userEditTrip(); break;
32
                                case 9: displayAllTrip(); break;
33
                                case 10: userAddStop(); break;
34
                                case 11: userEditStop(); break;
35
                                case 12: displayAllStop(); break;
36
                                case 13: userDisplayTrip(); break;
37
                                case 14: userAddTripOffering(); break;
38
                                case 15: userEditTripOffering(); break;
39
                                case 16: userDisplayMonthlySched(); break;
40
                                case 17: quit=true; break;
41
                                default: userBadChoice(); break;
42
                        }
43
                }
44
        }
45
 
46
        public static void userBadChoice() { 
47
                System.out.println("Bad Entry -- Try Again"); 
48
        }
49
 
50
        public static void printmenu() {
51
                System.out.println("1.  Add Driver");
52
                System.out.println("2.  Edit Driver");
53
                System.out.println("3.  Display All Drivers");
54
                System.out.println("4.  Add Bus");
55
                System.out.println("5.  Edit Bus");
56
                System.out.println("6.  Display All Busses");
57
                System.out.println("7.  Add Trip");
58
                System.out.println("8.  Edit Trip");
59
                System.out.println("9.  Display All Trips");
60
                System.out.println("10. Add Stop");
61
                System.out.println("11. Edit Stop");
62
                System.out.println("12. Display All Stops");
63
                System.out.println("13. Display All Stops on a Trip");
64
                System.out.println("14. Add to Monthly Schedule");
65
                System.out.println("15. Edit Monthly Schedule");
66
                System.out.println("16. Display Monthly schedule");
67
                System.out.println("17. QUIT");
68
                System.out.print("make a choice: ");
69
        }       
70
 
71
        public static void userAddDriver() throws Exception {
72
                BufferedReader kb = new BufferedReader(
73
                                        new InputStreamReader( System.in ));
74
 
75
                System.out.print("Enter id: ");
76
                String id=kb.readLine();
77
 
78
                System.out.print("Enter name: ");
79
                String name=kb.readLine();
80
 
81
                System.out.print("Enter address: ");
82
                String address=kb.readLine();
83
 
84
                System.out.print("Enter telnum: ");
85
                String telnum=kb.readLine();
86
 
87
                addDriver(id,name,address,telnum);
88
        }
89
 
90
        public static void addDriver( String id,
91
                                      String name,
92
                                      String address,
93
                                      String telnum ) throws Exception {
94
                String values=id+","+quote(name)+","+quote(address)+","+quote(telnum);
95
                SQLDriver.runInsert("driver","driverid,name,address,telnum",values);
96
        }
97
 
98
        public static void userEditDriver() throws Exception {
99
                BufferedReader kb = new BufferedReader(
100
                                        new InputStreamReader( System.in ));
101
 
102
                System.out.print("Enter id: ");
103
                String id=kb.readLine();
104
 
105
                System.out.print("Enter attribute to change: ");
106
                String item=kb.readLine();
107
 
108
                System.out.print("Enter the new value: ");
109
                String newval=kb.readLine();
110
 
111
                editDriver(id,item,newval);
112
        }
113
 
114
        public static void editDriver( String id,
115
                                       String item,
116
                                       String newval ) throws Exception {
117
 
118
                String cond="driverid="+id;
119
                SQLDriver.runUpdate("driver",item,quote(newval),cond);
120
        }
121
 
122
        public static void userAddBus() throws Exception {
123
 
124
                BufferedReader kb = new BufferedReader(
125
                                        new InputStreamReader( System.in ));
126
 
127
                System.out.print("Enter id: ");
128
                String id=kb.readLine();
129
 
130
                System.out.print("Enter plate: ");
131
                String plate=kb.readLine();
132
 
133
                System.out.print("Enter year: ");
134
                String year=kb.readLine();
135
 
136
                System.out.print("Enter make: ");
137
                String make=kb.readLine();
138
 
139
                addBus(id,plate,year,make);
140
        }
141
 
142
        public static void addBus( String id,
143
                                   String plate,
144
                                   String year,
145
                                   String make ) throws Exception {
146
                String items="busid,plate,year,make";
147
                String values=id+","+quote(plate)+","+quote(year)
148
                              +","+quote(make);
149
 
150
                SQLDriver.runInsert("bus",items,values);
151
        }
152
 
153
        public static void userEditBus() throws Exception {
154
 
155
                BufferedReader kb = new BufferedReader(
156
                                        new InputStreamReader( System.in ));
157
 
158
                System.out.print("Enter id: ");
159
                String id=kb.readLine();
160
 
161
                System.out.print("Enter attribute to change: ");
162
                String item=kb.readLine();
163
 
164
                System.out.print("Enter the new value: ");
165
                String newval=kb.readLine();
166
 
167
                editBus(id,item,newval);
168
        }
169
 
170
        public static void editBus( String id,
171
                                    String item,
172
                                    String newval ) throws Exception {
173
 
174
                String cond="busid="+id;
175
                SQLDriver.runUpdate("bus",item,quote(newval),cond);
176
        }
177
 
178
        public static void userAddTrip() throws Exception {
179
                BufferedReader kb=new BufferedReader(
180
                                      new InputStreamReader( System.in ));
181
 
182
                System.out.print("Enter id: ");
183
                String id=kb.readLine();
184
 
185
                System.out.print("Enter start location: ");
186
                String startloc=kb.readLine();
187
 
188
                System.out.print("Enter end location: ");
189
                String endloc=kb.readLine();
190
 
191
                addTrip(id,startloc,endloc);
192
        }
193
 
194
        public static void addTrip( String id,
195
                                    String startloc,
196
                                    String endloc ) throws Exception {
197
                String items="tripid, startloc, endloc";
198
                String values=id+","+quote(startloc)+","+quote(endloc);
199
 
200
                SQLDriver.runInsert("trip",items,values);
201
        }
202
 
203
        public static void userEditTrip() throws Exception {
204
                BufferedReader kb = new BufferedReader(
205
                                        new InputStreamReader( System.in ));
206
 
207
                System.out.print("Enter id: ");
208
                String id=kb.readLine();
209
 
210
                System.out.print("Enter attribute to change: ");
211
                String item=kb.readLine();
212
 
213
                System.out.print("Enter the new value: ");
214
                String newval=kb.readLine();
215
 
216
                editTrip(id,item,newval);
217
        }
218
 
219
        public static void editTrip( String id,
220
                                     String item,
221
                                     String newval ) throws Exception {
222
                String cond="tripid="+id;
223
                SQLDriver.runUpdate("trip",item,quote(newval),cond);
224
        }
225
 
226
        public static void userAddStop() throws Exception {
227
                BufferedReader kb=new BufferedReader(
228
                                      new InputStreamReader( System.in ));
229
 
230
                System.out.print("Enter stopid: ");
231
                String stopid=kb.readLine();
232
 
233
                System.out.print("Enter tripid: ");
234
                String tripid=kb.readLine();
235
 
236
                System.out.print("Enter sequencenum: ");
237
                String sequencenum=kb.readLine();
238
 
239
                System.out.print("Enter arrival time: ");
240
                String arrivaltime=kb.readLine();
241
 
242
                System.out.print("Enter departure time: ");
243
                String departtime=kb.readLine();
244
 
245
                addStop(stopid,tripid,sequencenum,arrivaltime,departtime);
246
        }
247
 
248
        public static void addStop( String stopid,
249
                                    String tripid,
250
                                    String sequencenum,
251
                                    String arrivaltime,
252
                                    String departtime ) throws Exception {
253
 
254
                String items="stopid,tripid,sequencenum,arrivaltime,departtime";
255
                String values=stopid+","+tripid+","+sequencenum+","
256
                              +quote(arrivaltime)+","+quote(departtime);
257
 
258
                SQLDriver.runInsert("stops",items,values);
259
        }
260
 
261
        public static void userEditStop() throws Exception {
262
                BufferedReader kb=new BufferedReader(
263
                                      new InputStreamReader( System.in ));
264
 
265
                System.out.print("Enter stopid: ");
266
                String stopid=kb.readLine();
267
 
268
                System.out.print("Enter attribute to change: ");
269
                String item=kb.readLine();
270
 
271
                System.out.print("Enter new value: ");
272
                String newval=kb.readLine();
273
 
274
                editStop(stopid,item,newval);
275
        }
276
 
277
        public static void editStop( String stopid,
278
                                     String item,
279
                                     String newval ) throws Exception {
280
 
281
                String cond="stopid="+stopid;
282
                SQLDriver.runUpdate("stops",item,quote(newval),cond);
283
        }
284
 
285
        public static void userAddTripOffering() throws Exception {
286
                BufferedReader kb = new BufferedReader(
287
                                        new InputStreamReader( System.in ));
288
 
289
                System.out.print("Enter date (day only): ");
290
                String date=kb.readLine();
291
 
292
                System.out.print("Enter start time: ");
293
                String starttime=kb.readLine();
294
 
295
                System.out.print("Enter tripid: ");
296
                String tripid=kb.readLine();
297
 
298
                System.out.print("Enter time of arrival at destination: ");
299
                String desttime=kb.readLine();
300
 
301
                System.out.print("Enter driver id: ");
302
                String driverid=kb.readLine();
303
 
304
                System.out.print("Enter bus id: ");
305
                String busid=kb.readLine();
306
 
307
                System.out.print("Enter month: ");
308
                String month=kb.readLine();
309
 
310
                System.out.print("Enter year: ");
311
                String year=kb.readLine();
312
 
313
                addTripOffering(date,starttime,tripid,desttime,driverid,
314
                                busid,month,year);
315
        }
316
 
317
        public static void addTripOffering( String date,
318
                                            String starttime,
319
                                            String tripid,
320
                                            String desttime,
321
                                            String driverid,
322
                                            String busid,
323
                                            String month,
324
                                            String year ) throws Exception {
325
                String items="date,starttime,tripid,desttime,driverid,busid,month,year";
326
                String values= quote(date)+","+quote(starttime)+","+tripid+","
327
                              +quote(desttime)+","+driverid+","+busid+","+quote(month)+","
328
                              +quote(year);
329
 
330
                SQLDriver.runInsert("tripoffering",items,values);
331
        }
332
 
333
        public static void userEditTripOffering() throws Exception {
334
                BufferedReader kb = new BufferedReader(
335
                                        new InputStreamReader( System.in ));
336
 
337
                System.out.print("Enter tripid: ");
338
                String tripid=kb.readLine();
339
 
340
                System.out.print("Enter attribute to change: ");
341
                String item=kb.readLine();
342
 
343
                System.out.print("Enter new value: ");
344
                String newval=kb.readLine();
345
 
346
                editTripOffering(tripid,item,newval);
347
        }
348
 
349
        public static void editTripOffering( String tripid,
350
                                             String item,
351
                                             String newval ) throws Exception {
352
 
353
                String cond="tripid="+tripid;
354
                SQLDriver.runUpdate("tripoffering",item,quote(newval),cond);
355
        }
356
 
357
        public static void userDisplayTrip() throws Exception {
358
                BufferedReader kb = new BufferedReader(
359
                                        new InputStreamReader( System.in ));
360
 
361
                System.out.print("Enter tripid: ");
362
                String tripid=kb.readLine();
363
                displayTrip(tripid);
364
        }
365
 
366
        public static void displayTrip( String tripid ) throws Exception {
367
 
368
                String query =  "SELECT stopid,sequencenum,arrivaltime,departtime "+
369
                                "FROM trip t,stops s "+
370
                                "WHERE t.tripid=s.tripid "+
371
                                "AND s.tripid="+tripid;
372
                ResultSet rs = SQLDriver.runSelect(query);
373
                ResultSetMetaData rsMeta = rs.getMetaData();
374
 
375
                //print header
376
                System.out.printf("%16s | %16s | %16s | %16s\n",
377
                                  rsMeta.getColumnName(1),
378
                                  rsMeta.getColumnName(2),
379
                                  rsMeta.getColumnName(3),
380
                                  rsMeta.getColumnName(4));
381
                System.out.println(seperator(80));
382
 
383
                //print data
384
                while( rs.next() ) {
385
                        System.out.printf("%16s | %16s | %16s | %16s\n",
386
                                          rs.getString(1),
387
                                          rs.getString(2),
388
                                          rs.getString(3),
389
                                          rs.getString(4));
390
                }
391
                System.out.printf("%s\n\n", seperator(80));
392
                rs.close();
393
        }
394
 
395
        public static void displayAllStop() throws Exception {
396
                String query = "SELECT * FROM stops";
397
 
398
                ResultSet rs = SQLDriver.runSelect(query);
399
                ResultSetMetaData rsMeta = rs.getMetaData();
400
 
401
                System.out.printf("%12s | %12s | %12s | %12s | %12s\n",
402
                                  rsMeta.getColumnName(1),
403
                                  rsMeta.getColumnName(2),
404
                                  rsMeta.getColumnName(3),
405
                                  rsMeta.getColumnName(4),
406
                                  rsMeta.getColumnName(5));
407
                System.out.println(seperator(80));
408
 
409
                while( rs.next() ) {
410
                        System.out.printf("%12s | %12s | %12s | %12s | %12s\n",
411
                                          rs.getString(1),
412
                                          rs.getString(2),
413
                                          rs.getString(3),
414
                                          rs.getString(4),
415
                                          rs.getString(5));
416
                }
417
                System.out.printf("%s\n\n", seperator(80));
418
                rs.close();
419
        }
420
 
421
        public static void displayAllDriver() throws Exception {
422
                String query = "SELECT * FROM driver";
423
 
424
                ResultSet rs = SQLDriver.runSelect(query);
425
                ResultSetMetaData rsMeta = rs.getMetaData();
426
 
427
                System.out.printf("%18s|%18s|%18s|%18s\n",
428
                                  rsMeta.getColumnName(1),
429
                                  rsMeta.getColumnName(2),
430
                                  rsMeta.getColumnName(3),
431
                                  rsMeta.getColumnName(4));
432
                System.out.println(seperator(80));
433
 
434
                while( rs.next() ) {
435
                        System.out.printf("%18s|%18s|%18s|%18s\n",
436
                                        rs.getString(1),
437
                                        rs.getString(2),
438
                                        rs.getString(3),
439
                                        rs.getString(4));
440
                }
441
                System.out.printf("%s\n\n", seperator(80));
442
                rs.close();
443
        }
444
 
445
        public static void displayAllBus() throws Exception {
446
                String query = "SELECT * FROM bus";
447
 
448
                ResultSet rs = SQLDriver.runSelect(query);
449
                ResultSetMetaData rsMeta = rs.getMetaData();
450
 
451
                System.out.printf("%18s|%18s|%18s|%18s\n",
452
                                  rsMeta.getColumnName(1),
453
                                  rsMeta.getColumnName(2),
454
                                  rsMeta.getColumnName(3),
455
                                  rsMeta.getColumnName(4));
456
                System.out.println(seperator(80));
457
 
458
                while( rs.next() ) {
459
                        System.out.printf("%18s|%18s|%18s|%18s\n",
460
                                        rs.getString(1),
461
                                        rs.getString(2),
462
                                        rs.getString(3),
463
                                        rs.getString(4));
464
                }
465
                System.out.printf("%s\n\n", seperator(80));
466
                rs.close();
467
        }
468
 
469
        public static void displayAllTrip() throws Exception {
470
                String query = "SELECT * FROM trip";
471
 
472
                ResultSet rs = SQLDriver.runSelect(query);
473
                ResultSetMetaData rsMeta = rs.getMetaData();
474
 
475
                System.out.printf("%20s|%20s|%20s\n",
476
                                  rsMeta.getColumnName(1),
477
                                  rsMeta.getColumnName(2),
478
                                  rsMeta.getColumnName(3));
479
                System.out.println(seperator(80));
480
 
481
                while( rs.next() ) {
482
                        System.out.printf("%20s|%20s|%20s\n",
483
                                        rs.getString(1),
484
                                        rs.getString(2),
485
                                        rs.getString(3));
486
                }
487
                System.out.printf("%s\n\n", seperator(80));
488
                rs.close();
489
        }
490
 
491
        public static void userDisplayMonthlySched() throws Exception {
492
                BufferedReader kb = new BufferedReader(
493
                                        new InputStreamReader( System.in ));
494
 
495
                System.out.print("Enter the month: ");
496
                String month=kb.readLine();
497
 
498
                System.out.print("Enter the year: ");
499
                String year=kb.readLine();
500
 
501
                displayMonthlySched(month,year);
502
        }
503
 
504
        public static void displayMonthlySched(String month, 
505
                                               String year) throws Exception {
506
                String query =  "SELECT month,date,year,tripid,driverid,"
507
                               +"busid,starttime,desttime FROM tripoffering "
508
                               +"WHERE month="+quote(month)+" "
509
                               +"AND year="+quote(year);
510
                ResultSet rs = SQLDriver.runSelect(query);
511
                ResultSetMetaData rsMeta = rs.getMetaData();
512
 
513
                System.out.printf("%9s|%9s|%9s|%9s|%9s|%9s|%9s|%9s\n",
514
                                  rsMeta.getColumnName(1),
515
                                  rsMeta.getColumnName(2),
516
                                  rsMeta.getColumnName(3),
517
                                  rsMeta.getColumnName(4),
518
                                  rsMeta.getColumnName(5),
519
                                  rsMeta.getColumnName(6),
520
                                  rsMeta.getColumnName(7),
521
                                  rsMeta.getColumnName(8));
522
                System.out.println(seperator(80));
523
 
524
                while( rs.next() ) {
525
                        System.out.printf("%9s|%9s|%9s|%9s|%9s|%9s|%9s|%9s\n",
526
                                          rs.getString(1),
527
                                          rs.getString(2),
528
                                          rs.getString(3),
529
                                          rs.getString(4),
530
                                          rs.getString(5),
531
                                          rs.getString(6),
532
                                          rs.getString(7),
533
                                          rs.getString(8));
534
                }
535
                System.out.printf("%s\n\n", seperator(80));
536
                rs.close();
537
        }
538
 
539
        public static String quote( String s ) { return "'"+s+"'"; }
540
        public static String seperator( int len ) {
541
                String s="";
542
                for( int i=0; i<len; i++ ) { s = s+"-"; }
543
                return s;
544
        }
545
}
546
 
547
/* SQLDriver is a nice little class to automate some sql statements for
548
 * it's user. The methods have no error checking, so be careful.
549
 */
550
class SQLDriver {
551
 
552
        /* a generic SELECT statment */
553
        public static ResultSet runSelect( String query ) throws Exception {
554
                Class.forName("org.gjt.mm.mysql.Driver").newInstance();
555
                String connString = "jdbc:mysql://localhost/cs435?user=cs435";
556
                Connection conn = DriverManager.getConnection(connString);
557
                Statement stmt = conn.createStatement();
558
                ResultSet rs = stmt.executeQuery(query);
559
                return rs;
560
        }
561
 
562
        /* run a SELECT statement. SELECT items FROM table */
563
        public static ResultSet runSelect( String items,
564
                                           String table ) throws Exception {
565
                //start the driver
566
                Class.forName("org.gjt.mm.mysql.Driver").newInstance();
567
 
568
                //connect to my database
569
                String connString = "jdbc:mysql://localhost/cs435?user=cs435";
570
                Connection conn = DriverManager.getConnection(connString);
571
 
572
                //create a new statement
573
                Statement stmt = conn.createStatement();
574
 
575
                //run the statement
576
                String sqlStatement = "SELECT " + items + " FROM " + table;
577
                ResultSet rs = stmt.executeQuery(sqlStatement);
578
 
579
                return rs;
580
        }
581
 
582
        /* run a SELECT statement. SELECT items FROM table WHERE value=cond */
583
        public static ResultSet runSelect( String items,
584
                                           String table,
585
                                           String value,
586
                                           String cond ) throws Exception {
587
                //start the driver
588
                Class.forName("org.gjt.mm.mysql.Driver").newInstance();
589
 
590
                //connect to my database
591
                String connString = "jdbc:mysql://localhost/cs435?user=cs435";
592
                Connection conn = DriverManager.getConnection(connString);
593
 
594
                //create a new statement
595
                Statement stmt = conn.createStatement();
596
 
597
                //run the statement
598
                String sqlStatement = "SELECT " + items + " FROM " + table
599
                                      + " WHERE " + value + "=" + cond;
600
                ResultSet rs = stmt.executeQuery(sqlStatement);
601
                //ResultSetMetaData rsMeta = rs.getMetaData();
602
 
603
                return rs;
604
        }
605
 
606
        /* run an INSERT statement. INSERT INTO table (items) VALUES (values) */
607
        public static int runInsert( String table, 
608
                                     String items, 
609
                                     String values ) throws Exception {
610
                //start the driver
611
                Class.forName("org.gjt.mm.mysql.Driver").newInstance();
612
 
613
                //connect to my database
614
                String connString = "jdbc:mysql://localhost/cs435?user=cs435";
615
                Connection conn = DriverManager.getConnection(connString);
616
 
617
                //create a new statement
618
                Statement stmt = conn.createStatement();
619
                String sqlStatement = "INSERT INTO " + table + " (" + items +
620
                                      ") VALUES (" + values + ")";
621
 
622
                //run the statement
623
                int result = stmt.executeUpdate(sqlStatement);
624
 
625
                return result;
626
        }
627
 
628
        /* run an UPDATE statement. UPDATE table SET item=value WHERE cond */
629
        public static int runUpdate( String table,
630
                                     String item,
631
                                     String value,
632
                                     String cond ) throws Exception {
633
                //start the driver
634
                Class.forName("org.gjt.mm.mysql.Driver").newInstance();
635
 
636
                //connect to my database
637
                String connString = "jdbc:mysql://localhost/cs435?user=cs435";
638
                Connection conn = DriverManager.getConnection(connString);
639
 
640
                //create a new statement
641
                Statement stmt = conn.createStatement();
642
                String sqlStatement = "UPDATE " + table + " SET " + item +
643
                                      "=" + value + " WHERE " + cond;
644
 
645
                //run the statement
646
                int result = stmt.executeUpdate(sqlStatement);
647
 
648
                return result;
649
        }
650
 
651
        /* run a DELETE statement. DELETE FROM table WHERE item=value */
652
        public static int runDelete( String table,
653
                                     String item,
654
                                     String value ) throws Exception {
655
 
656
                //start the driver
657
                Class.forName("org.gjt.mm.mysql.Driver").newInstance();
658
 
659
                //connect to my database
660
                String connString = "jdbc:mysql://localhost/cs435?user=cs435";
661
                Connection conn = DriverManager.getConnection(connString);
662
 
663
                //create a new statement
664
                Statement stmt = conn.createStatement();
665
                String sqlStatement = "DELETE FROM " + table + " WHERE "
666
                                      + item + "=" + value;
667
 
668
                //run the statement
669
                int result = stmt.executeUpdate(sqlStatement);
670
 
671
                return result;
672
        }
673
}
674