-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_Program.java
More file actions
1951 lines (1644 loc) · 67.4 KB
/
Main_Program.java
File metadata and controls
1951 lines (1644 loc) · 67.4 KB
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.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.util.Scanner;
public class Main_Program {
private static int ERROR_FLAG = 0;
private static final String[] query_Options = { "(1) Enter a new customer", "(2) Enter a new department",
"(3) Enter a new assembly with its customer-name, assembly-details, assembly-id, and date-ordered",
"(4) Enter a new process-id and its department together with its type and information relevant to the type",
"(5) Create a new account and associate it with the process, assembly, or department to which it is applicable",
"(6) Enter a new job, given its job-no, assembly-id, process-id, and date the job commenced",
"(7) At the completion of a job, enter the date it completed and the information relevant to the type of job",
"(8) Enter a transaction-no and its sup-cost and update all the costs (details) of the affected accounts by adding sup-cost to their current values of details",
"(9) Retrieve the cost incurred on an assembly-id",
"(10) Retrieve the total labor time within a department for jobs completed in the department during a given date",
"(11) Retrieve the processes through which a given assembly-id has passed so far (in date-commenced order) and the department responsible for each process",
"(12) Retrieve the jobs (together with their type information and assembly-id) completed during a given date in a given department",
"(13) Retrieve the customers (in name order) whose category is in a given range",
"(14) Delete all cut-jobs whose job-no is in a given range", "(15) Change the color of a given paint job",
"(16) Import new customers from file", "(17) Export customers to file based on category range",
"(18) Quit" };
// Print the query options
public static void print_queries() {
for (int i = 0; i < query_Options.length; ++i) {
System.out.println(query_Options[i]);
}
System.out.println();
System.out.print("Please select the number corresponding to the query: ");
}
// Print all customer information
public static void print_table(Connection connection, int table) throws SQLException {
String query = "";
switch (table) {
case 1: // Print Customer table
query = "SELECT * FROM Customer";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Customer table:");
System.out.println("--Customer Name--|--Customer Address--|--Customer Category--");
// Printing out table
while (resultSet.next()) {
System.out.println(String.format("----%s | %s | %s", resultSet.getString(1), resultSet.getString(2),
resultSet.getString(3)));
}
}
break;
case 2: // Print Department table
query = "SELECT * FROM Department";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Department table:");
System.out.println("--Department ID--|--Department Data--|--Account ID--");
// Printing out table
while (resultSet.next()) {
System.out.println(String.format("----%s | %s | %s", resultSet.getString(1), resultSet.getString(2),
resultSet.getString(3)));
}
}
break;
case 3: // Print Process table
query = "SELECT * FROM Process";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Process table:");
System.out.println("--Process ID--|--Process Data--|--Department ID--|--Account ID--");
// Printing out table
while (resultSet.next()) {
System.out.println(String.format("----%s | %s | %s | %s", resultSet.getString(1),
resultSet.getString(2), resultSet.getString(3), resultSet.getString(4)));
}
}
break;
case 4: // Print Assembly table
query = "SELECT * FROM Assembly";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Assembly table:");
System.out.println(
"--Assembly ID--|--Date Ordered--|--Assembly Details--|--Customer Name--|--Account ID--");
// Printing out table
while (resultSet.next()) {
System.out.println(
String.format("----%s | %s | %s | %s | %s", resultSet.getString(1), resultSet.getString(2),
resultSet.getString(3), resultSet.getString(4), resultSet.getString(5)));
}
}
break;
case 5: // Print the Job table
query = "SELECT * FROM Job";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Job table:");
System.out.println("--Job ID--|--Date Commenced--|--Date Completed--|--Assembly ID--|--Process ID--|");
// Printing out table
while (resultSet.next()) {
System.out.println(
String.format("----%s | %s | %s | %s | %s", resultSet.getString(1), resultSet.getString(2),
resultSet.getString(3), resultSet.getString(4), resultSet.getString(5)));
}
}
break;
case 6: // Print the Account table
query = "SELECT * FROM Account";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Account table:");
System.out.println("--Account ID--|--Date Established--");
// Printing out table
while (resultSet.next()) {
System.out.println(String.format("----%s | %s ", resultSet.getString(1), resultSet.getString(2)));
}
}
break;
case 7: // Print the Department_Account table
query = "SELECT * FROM Department_Account";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Department_Account table:");
System.out.println("--Account ID--|--Details 2--");
// Printing out table
while (resultSet.next()) {
System.out.println(String.format("----%s | %s ", resultSet.getString(1), resultSet.getString(2)));
}
}
break;
case 8: // Print the Assembly_Account table
query = "SELECT * FROM Assembly_Account";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Assembly_Account table:");
System.out.println("--Account ID--|--Details 1--");
// Printing out table
while (resultSet.next()) {
System.out.println(String.format("----%s | %s ", resultSet.getString(1), resultSet.getString(2)));
}
}
break;
case 9: // Print the Process_Account table
query = "SELECT * FROM Process_Account";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Process_Account table:");
System.out.println("--Account ID--|--Details 3--");
// Printing out table
while (resultSet.next()) {
System.out.println(String.format("----%s | %s ", resultSet.getString(1), resultSet.getString(2)));
}
}
break;
case 10: // Print jobs where date_completed in not null
query = "SELECT * FROM Job WHERE date_completed IS NOT NULL";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Job table where date_completed is not null:");
System.out.println("--Job ID--|--Date Completed--");
// Printing out table
while (resultSet.next()) {
System.out.println(
String.format("----%s | %s ", resultSet.getString(1), resultSet.getString(3)));
}
}
break;
case 11: // Print Cut Jobs
query = "SELECT * FROM Cut_Job";
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
System.out.println();
System.out.println("Contents of Cut Job table:");
System.out.println("--Job IDs of Cut Jobs--");
// Printing out table
while (resultSet.next()) {
System.out.println(String.format("----%s ", resultSet.getString(1)));
}
}
break;
default:
System.out.println("Print Table");
break;
}
}
// Check if primary key exists
public static boolean check_key(Connection connection, String ID, String table, String where_condition,
int column_number) throws SQLException {
boolean check = false;
String query = "SELECT * FROM " + table + " " + where_condition;
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
if (ID.equalsIgnoreCase(resultSet.getString(column_number))) {
check = true;
}
}
}
return check;
}
// Selectively print a column of a table based on a condition
public static void selective_print(Connection connection, String table, String where_condition, int column_number,
String label) throws SQLException {
String query = "SELECT * FROM " + table + " " + where_condition;
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
// Printing label
System.out.println("Existing " + table + " entries");
System.out.println(label);
while (resultSet.next()) {
System.out.println(String.format("----%s ", resultSet.getString(column_number)));
}
}
}
/*------------------------------------------QUERY FUNCTIONS------------------------------------------*/
// COMPLETELY DONE: Function enters new customer: Query 1 and Query 3
public static String enter_new_customer(Connection connection, Scanner scanner, int query_flag)
throws SQLException {
// Print existing customer names
System.out.println();
selective_print(connection, "Customer", "", 1, "--Customer Name");
// Asking for customer name
System.out.println();
System.out.print("New Customer Name: ");
String customer_name = scanner.nextLine();
// If customer name already exists
if (check_key(connection, customer_name, "Customer", "", 1)) {
System.out.println();
System.out.println("Customer Name " + customer_name + " already exists, please enter a new name");
System.out.println();
print_queries();
ERROR_FLAG = 1;
} else {
// Asking for customer address
System.out.println();
System.out.print("Customer's address: ");
String customer_address = scanner.nextLine();
// Asking for customer category
System.out.println();
System.out.print("Customer's category (1-10): ");
int customer_category = scanner.nextInt();
scanner.nextLine();
// Adding information from user into sql statement
try (final Statement statement = connection.createStatement();) {
statement.executeUpdate(
"INSERT INTO Customer(customer_name, customer_address, customer_category)\n" + "VALUES ('"
+ customer_name + "', '" + customer_address + "', '" + customer_category + "')\n");
System.out.println();
System.out.println("Successfully added new customer\n");
// Print queries if flag is set
if (query_flag == 1) {
print_queries();
}
} catch (SQLException e) {
System.out.println("\nERROR: " + e.getMessage() + "\n");
System.out.print("Please select a number corresponding to the query: ");
ERROR_FLAG = 1; // cycles to query 3 if customer input is incorrect
}
}
return customer_name;
}
// COMPLETELY DONE: Function adds a new department: also part of Query 4 and 5
public static int enter_new_department(Connection connection, Scanner scanner, int query_flag) throws SQLException {
// Print existing department ID
System.out.println();
selective_print(connection, "Department", "", 1, "--Department ID");
// Enter a department ID
System.out.println();
System.out.print("New Department ID: ");
int department_ID = scanner.nextInt();
scanner.nextLine();
// If department name already exists
if (check_key(connection, Integer.toString(department_ID), "Department", "", 1)) {
System.out.println();
System.out.println("Department ID " + department_ID + " already exists, please enter a new ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
} else {
// Enter department data
System.out.println();
System.out.print("Department Data: ");
String department_data = scanner.nextLine();
// Adding new department into database
try (final Statement statement = connection.createStatement();) {
statement.executeUpdate("INSERT INTO Department(dept_ID, dept_data)\n" + "VALUES ('" + department_ID
+ "', '" + department_data + "')\n");
System.out.println();
System.out.println("Successfully added new department\n");
// Print queries if flag is set
if (query_flag == 1) {
print_queries();
}
} catch (SQLException e) {
System.out.println("\nERROR: " + e.getMessage() + "\n");
System.out.print("Please select a number corresponding to the query: ");
ERROR_FLAG = 1; // cycles to query 4
}
}
return department_ID;
}
// COMPLETELY DONE: Function adds a new assembly: Part of Query 3
public static int enter_new_assembly(Connection connection, Scanner scanner, String customer_name, int query_flag)
throws SQLException {
// Print existing Assembly ID
System.out.println();
selective_print(connection, "Assembly", "", 1, "--Assembly ID");
// Asking for assembly_ID
System.out.println();
System.out.print("New Assembly ID: ");
int assembly_ID = scanner.nextInt();
scanner.nextLine();
// If department name already exists
if (check_key(connection, Integer.toString(assembly_ID), "Assembly", "", 1)) {
System.out.println();
System.out.println("Department ID " + assembly_ID + " already exists, please enter a new ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
} else {
// Asking for date_ordered
System.out.println();
System.out.print("Assembly Ordered (YYYY-MM-DD): ");
String assembly_date_ordered = scanner.nextLine();
// Asking for assembly_details
System.out.println();
System.out.print("Assembly Details: ");
String assembly_details = scanner.nextLine();
// Adding new assembly to database
try (final Statement statement = connection.createStatement();) {
statement.executeUpdate(
"INSERT INTO Assembly(assembly_ID, date_ordered, assembly_details, customer_name)\n"
+ "VALUES ('" + assembly_ID + "', '" + assembly_date_ordered + "', '" + assembly_details
+ "', '" + customer_name + "')");
System.out.println();
System.out.println("Successfully added new assembly\n");
// Print queries if flag is set
if (query_flag == 1) {
print_queries();
}
} catch (SQLException e) {
System.out.println("\nERROR: " + e.getMessage() + "\n");
System.out.print("Please select a number corresponding to the query: ");
ERROR_FLAG = 1;
}
}
return assembly_ID;
}
// COMPLETELY DONE: Function enters a new assembly: Part of Query 5
public static int query_3(Connection connection, Scanner scanner) throws SQLException {
int assembly_ID = 0;
String customer_name = "";
// Query for new or current customer for new assembly
System.out.println();
System.out.print("Is customer for this assembly new or currently in database (new/current): ");
String customer_query = scanner.nextLine();
// If customer is new, enter new customer, if current, ask for name
if (customer_query.equalsIgnoreCase("new") || customer_query.equalsIgnoreCase("new ")) {
// Entering new customer and getting customer_name
customer_name = enter_new_customer(connection, scanner, 0);
} else if (customer_query.equalsIgnoreCase("current") || customer_query.equalsIgnoreCase("customer ")) {
// Print existing customer names
System.out.println();
selective_print(connection, "Customer", "", 1, "--Customer Name");
// Asking for customer name
System.out.println();
System.out.print("Existing Customer's Name: ");
customer_name = scanner.nextLine();
// If customer name already exists
if (check_key(connection, customer_name, "Customer", "", 1)) {
} else {
System.out.println();
System.out.println("ERROR: Customer name does not exists, please pick an existing customer");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
} else {
System.out.println();
System.out.println("Please enter 'new' for new customer or 'current' for current customer");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
if (ERROR_FLAG == 0) {
// Enter new assembly with customer name
assembly_ID = enter_new_assembly(connection, scanner, customer_name, 1);
}
return assembly_ID;
}
// COMPLETELY DONE: Function adds new process: Part of Query 4
public static int enter_new_process(Connection connection, Scanner scanner, int department_ID, int query_flag)
throws SQLException {
// Print existing Assembly ID
System.out.println();
selective_print(connection, "Process", "", 1, "--Process ID");
// Asking for process ID
System.out.println();
System.out.print("Process ID: ");
int process_ID = scanner.nextInt();
scanner.nextLine();
// If department name already exists
if (check_key(connection, Integer.toString(process_ID), "Process", "", 1)) {
System.out.println();
System.out.println("Process ID " + process_ID + " already exists, please enter a new ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
} else {
// Asking for process data
System.out.println();
System.out.print("Process Data: ");
String process_data = scanner.nextLine();
// Adding new assembly to database
try (final Statement statement = connection.createStatement();) {
statement.executeUpdate("INSERT INTO Process(proc_ID, process_data, dept_ID)\n" + "VALUES ('"
+ process_ID + "', '" + process_data + "', '" + department_ID + "')");
System.out.println();
System.out.println("Successfully added new process\n");
// Print queries if flag is set
if (query_flag == 1) {
print_queries();
}
} catch (SQLException e) {
System.out.println("\nERROR: " + e.getMessage() + "\n");
System.out.print("Please select a number corresponding to the query: ");
ERROR_FLAG = 1;
}
}
return process_ID;
}
// COMPLETELY DONE: Function enters a new process: Part of Query 5
public static int query_4(Connection connection, Scanner scanner) throws SQLException {
int process_ID = 0;
int department_ID = 0;
String department_query = "";
// Query for new or current department
System.out.println();
System.out.print("Is department for this process new or currently in database (new/current): ");
department_query = scanner.nextLine();
// If department is new, enter new department, if current, ask for dept_ID
if (department_query.equalsIgnoreCase("new") || department_query.equalsIgnoreCase("new ")) {
department_ID = enter_new_department(connection, scanner, 0);
} else if (department_query.equalsIgnoreCase("current") || department_query.equalsIgnoreCase("current ")) {
// Print existing department ID
System.out.println();
selective_print(connection, "Department", "", 1, "--Department ID");
// Asking for department
System.out.println();
System.out.print("Existing Department ID: ");
department_ID = scanner.nextInt();
scanner.nextLine();
// If department ID already exists
if (check_key(connection, Integer.toString(department_ID), "Department", "", 1)) {
// Do nothing
} else {
System.out.println();
System.out.println("ERROR: Department ID does not exists, please pick an existing department ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
} else {
System.out.println();
System.out.println("Please enter 'new' for new department or 'current' for current department");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
if (ERROR_FLAG == 0) {
// Entering new process with department ID
process_ID = enter_new_process(connection, scanner, department_ID, 1);
}
return process_ID;
}
// COMPLETELY DONE: Function adds a new account: Query 5
public static int enter_new_account(Connection connection, Scanner scanner, String type, int type_ID,
int query_flag) throws SQLException {
int account_ID = 0;
// Print existing Account ID
System.out.println();
selective_print(connection, "Account", "", 1, "--Account ID");
// Asking for account ID
System.out.println();
System.out.print("New Account ID: ");
account_ID = scanner.nextInt();
scanner.nextLine();
// If account ID already exists
if (check_key(connection, Integer.toString(account_ID), "Account", "", 1)) {
System.out.println();
System.out.println("Account ID " + account_ID + " already exists, please enter a new ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
} else {
// Asking for account date established
System.out.println();
System.out.print("Account Date Established (YYYY-MM-DD): ");
String date_established = scanner.nextLine();
if (type.equalsIgnoreCase("Department") || type.equalsIgnoreCase("Department ")) {
// Asking for details 2
System.out.println();
System.out.print("Details-2: ");
String details_2 = scanner.nextLine();
// Adding new account to database
try (final Statement statement = connection.createStatement();) {
statement.executeUpdate("INSERT INTO Account(account_ID, date_established)\n" + "VALUES ('"
+ account_ID + "', '" + date_established + "')");
System.out.println();
System.out.println("Successfully added new account\n");
statement.executeUpdate("INSERT INTO Department_Account(account_ID, details_2)\n" + "VALUES ('"
+ account_ID + "', '" + details_2 + "')");
System.out.println("Successfully added new department account\n");
statement.executeUpdate("UPDATE Department\n"
+ "SET account_ID = (SELECT account_ID FROM Department_Account WHERE account_ID = "
+ account_ID + ")\n" + "WHERE dept_ID = " + type_ID);
System.out.println("Successfully updated department with new account\n");
// Print queries if flag is set
if (query_flag == 1) {
print_queries();
}
} catch (SQLException e) {
System.out.println("\nERROR: " + e.getMessage() + "\n");
System.out.print("Please select a number corresponding to the query: ");
}
} else if (type.equalsIgnoreCase("Assembly") || type.equalsIgnoreCase("Assembly ")) {
// Asking for details 1
System.out.println();
System.out.print("Details-1: ");
String details_1 = scanner.nextLine();
// Adding new account to database
try (final Statement statement = connection.createStatement();) {
statement.executeUpdate("INSERT INTO Account(account_ID, date_established)\n" + "VALUES ('"
+ account_ID + "', '" + date_established + "')");
System.out.println();
System.out.println("Successfully added new account\n");
statement.executeUpdate("INSERT INTO Assembly_Account(account_ID, details_1)\n" + "VALUES ('"
+ account_ID + "', '" + details_1 + "')");
System.out.println("Successfully added new assembly account\n");
statement.executeUpdate("UPDATE Assembly\n"
+ "SET account_ID = (SELECT account_ID FROM Assembly_Account WHERE account_ID = "
+ account_ID + ")\n" + "WHERE assembly_ID = " + type_ID);
System.out.println("Successfully updated assembly with new account\n");
// Print queries if flag is set
if (query_flag == 1) {
print_queries();
}
} catch (SQLException e) {
System.out.println("\nERROR: " + e.getMessage() + "\n");
System.out.print("Please select a number corresponding to the query: ");
}
} else if (type.equalsIgnoreCase("Process") || type.equalsIgnoreCase("Process ")) {
// Asking for details 3
System.out.println();
System.out.print("Details-3: ");
String details_3 = scanner.nextLine();
// Adding new account to database
try (final Statement statement = connection.createStatement();) {
statement.executeUpdate("INSERT INTO Account(account_ID, date_established)\n" + "VALUES ('"
+ account_ID + "', '" + date_established + "')");
System.out.println();
System.out.println("Successfully added new account\n");
statement.executeUpdate("INSERT INTO Process_Account(account_ID, details_3)\n" + "VALUES ('"
+ account_ID + "', '" + details_3 + "')");
System.out.println("Successfully added new process account\n");
statement.executeUpdate("UPDATE Process\n"
+ "SET account_ID = (SELECT account_ID FROM Process_Account WHERE account_ID = "
+ account_ID + ")\n" + "WHERE proc_ID = " + type_ID);
System.out.println("Successfully updated process with new account\n");
// Print queries if flag is set
if (query_flag == 1) {
print_queries();
}
} catch (SQLException e) {
System.out.println("\nERROR: " + e.getMessage() + "\n");
System.out.print("Please select a number corresponding to the query: ");
}
}
}
return account_ID;
}
// COMPLETELY DONE: Function enters a new account
public static int query_5(Connection connection, Scanner scanner) throws SQLException {
int account_ID = 0;
// Query for account type
System.out.println();
System.out.print("What type of account would you like (Department, Assembly, Process): ");
String account_type = scanner.nextLine();
if (account_type.equalsIgnoreCase("Department") || account_type.equalsIgnoreCase("Department ")) {
String department_query = "";
int department_ID = 0;
// Query for new or current department
System.out.println();
System.out.print("Is department for this account new or currently in database (new/current): ");
department_query = scanner.nextLine();
// If department is new, enter new department, if current, ask for dept_ID
if (department_query.equalsIgnoreCase("new") || department_query.equalsIgnoreCase("new ")) {
department_ID = enter_new_department(connection, scanner, 0);
} else if (department_query.equalsIgnoreCase("current") || department_query.equalsIgnoreCase("current ")) {
// Print existing department ID
System.out.println();
selective_print(connection, "Department", "", 1, "--Department ID");
// Asking for department id
System.out.println();
System.out.print("Existing Department ID: ");
department_ID = scanner.nextInt();
scanner.nextLine();
// If department ID already exists
if (check_key(connection, Integer.toString(department_ID), "Department", "", 1)) {
// Do nothing
} else {
System.out.println();
System.out.println("ERROR: Department ID does not exists, please pick an existing department ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
} else {
System.out.println();
System.out.println("Please enter 'new' for new department or 'current' for current department");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
if (ERROR_FLAG == 0) {
// Entering new process with department ID
account_ID = enter_new_account(connection, scanner, account_type, department_ID, 1);
}
} else if (account_type.equalsIgnoreCase("Assembly") || account_type.equalsIgnoreCase("Assembly ")) {
String assembly_query = "";
int assembly_ID = 0;
// Query for new or current assembly
System.out.println();
System.out.print("Is assembly for this account new or currently in database (new/current): ");
assembly_query = scanner.nextLine();
// If assembly is new, enter new assembly, if current, ask for dept_ID
if (assembly_query.equalsIgnoreCase("new") || assembly_query.equalsIgnoreCase("new ")) {
// Entering in new assembly so go to query 3
assembly_ID = query_3(connection, scanner);
} else if (assembly_query.equalsIgnoreCase("current") || assembly_query.equalsIgnoreCase("current ")) {
// Print existing Assembly ID
System.out.println();
selective_print(connection, "Assembly", "", 1, "--Assembly ID");
// Asking for Assembly id
System.out.println();
System.out.print("Existing Assembly ID: ");
assembly_ID = scanner.nextInt();
scanner.nextLine();
// If assembly ID already exists
if (check_key(connection, Integer.toString(assembly_ID), "Assembly", "", 1)) {
// Do nothing
} else {
System.out.println();
System.out.println("ERROR: Assembly ID does not exists, please pick an existing assembly ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
} else {
System.out.println();
System.out.println("Please enter 'new' for new assembly or 'current' for current assembly");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
if (ERROR_FLAG == 0) {
// Entering new process with department ID
account_ID = enter_new_account(connection, scanner, account_type, assembly_ID, 1);
}
} else if (account_type.equalsIgnoreCase("Process") || account_type.equalsIgnoreCase("Process ")) {
String process_query = "";
int process_ID = 0;
// Query for new or current process
System.out.println();
System.out.print("Is process for this account new or currently in database (new/current): ");
process_query = scanner.nextLine();
// If assembly is new, enter new assembly, if current, ask for dept_ID
if (process_query.equalsIgnoreCase("new") || process_query.equalsIgnoreCase("new ")) {
// Entering in new process so go to query 4
process_ID = query_4(connection, scanner);
} else if (process_query.equalsIgnoreCase("current") || process_query.equalsIgnoreCase("current ")) {
// Print existing Process ID
System.out.println();
selective_print(connection, "Process", "", 1, "--Process ID");
// Asking for Assembly id
System.out.println();
System.out.print("Existing Process ID: ");
process_ID = scanner.nextInt();
scanner.nextLine();
// If prcoess ID already exists
if (check_key(connection, Integer.toString(process_ID), "Process", "", 1)) {
// Do nothing
} else {
System.out.println();
System.out.println("ERROR: Process ID does not exists, please pick an existing process ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
} else {
System.out.println();
System.out.println("Please enter 'new' for new process or 'current' for current process");
System.out.println();
print_queries();
ERROR_FLAG = 1;
}
if (ERROR_FLAG == 0) {
// Entering new process with department ID
account_ID = enter_new_account(connection, scanner, account_type, process_ID, 1);
}
} else {
System.out.println();
System.out.println(
"Please enter account type: 'Department' for department, 'Assembly' for Assmebly, or 'Process' for Process");
System.out.println();
print_queries();
}
return account_ID;
}
// COMPLETELY DONE: Function adds a new job: Query 6
public static int enter_new_job(Connection connection, Scanner scanner, int query_flag) throws SQLException {
int job_ID = 0;
// Print existing job ID
System.out.println();
selective_print(connection, "Job", "", 1, "--Job ID");
// Query a job_ID
System.out.println();
System.out.print("New Job ID: ");
job_ID = scanner.nextInt();
scanner.nextLine();
// If job ID already exists
if (check_key(connection, Integer.toString(job_ID), "Job", "", 1)) {
System.out.println();
System.out.println("Job ID " + job_ID + " already exists, please enter a new ID");
System.out.println();
print_queries();
ERROR_FLAG = 1;
} else {
// Query date_commenced
System.out.println();
System.out.print("Date Commenced (YYYY-MM-DD): ");
String date_commenced = scanner.nextLine();
// Printing assembly table
print_table(connection, 4);
// Query assembly_ID
System.out.println();
System.out.print("Assembly ID: ");
int assembly_ID = scanner.nextInt();
scanner.nextLine();
// Printing process table
print_table(connection, 3);
// Query process_ID
System.out.println();
System.out.print("Process ID: ");
int process_ID = scanner.nextInt();
scanner.nextLine();
// Adding new job to database
try (final Statement statement = connection.createStatement();) {
statement.executeUpdate("INSERT INTO Job(job_ID, date_commenced, assembly_ID, proc_ID)\n" + "VALUES ('"
+ job_ID + "', '" + date_commenced + "', '" + assembly_ID + "', '" + process_ID + "')");
System.out.println();
System.out.println("Successfully added new job\n");
// Print queries if flag is set
if (query_flag == 1) {
print_queries();
}
} catch (SQLException e) {
System.out.println("\nERROR: " + e.getMessage() + "\n");
System.out.print("Please select a number corresponding to the query: ");
ERROR_FLAG = 1;
}
}
return job_ID;
}
// COMPLETELY DONE: Function adds new job type and updates job: Query 7
public static int enter_new_job_type(Connection connection, Scanner scanner, int query_flag) throws SQLException {
int job_ID = 0;
int job_type_ID = 0;
// Printing job table
print_table(connection, 5);
// Query a job_ID
System.out.println();
System.out.print("Existing Job ID of completed job: ");
job_ID = scanner.nextInt();
scanner.nextLine();
// If job ID already exists
if (check_key(connection, Integer.toString(job_ID), "Job", "", 1)) {
// Query a date_completed of job_ID
System.out.println();
System.out.print("Date Completed of Job (YYYY-MM-DD): ");
String date_completed = scanner.nextLine();
String assembly_ID = "";
String process_ID = "";
// Get assembly_ID and process_ID from job_ID
String query = "SELECT * FROM Job WHERE job_ID = " + job_ID;
try (final Statement statement = connection.createStatement();
final ResultSet resultSet = statement.executeQuery(query)) {
resultSet.next();
assembly_ID = resultSet.getString(4);
process_ID = resultSet.getString(5);
}
// Query type of Job
System.out.println();
System.out.print("What is the type of job Cut, Paint, or Fit (Cut, Paint, Fit): ");
String job_type = scanner.nextLine();
// Query cut job information
if (job_type.equalsIgnoreCase("Cut") || job_type.equalsIgnoreCase("Cut ")) {
// Query machine_type
System.out.println();
System.out.print("Machine Type: ");
String machine_type = scanner.nextLine();
// Query machine_time
System.out.println();
System.out.print("Machine Time: ");
Double machine_time = scanner.nextDouble();
scanner.nextLine();
// Query material
System.out.println();
System.out.print("Material: ");
String material = scanner.nextLine();
// Query labor_time
System.out.println();
System.out.print("Labor Time: ");