-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInfoSystem.java
More file actions
548 lines (485 loc) · 23.9 KB
/
InfoSystem.java
File metadata and controls
548 lines (485 loc) · 23.9 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
//import hashmap just to temporarily store date before we can create the databases
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import javax.xml.crypto.Data;
public class InfoSystem {
private static Scanner sc = new Scanner(System.in);
// just to make sure our program runs, and edit this later
private static Map<String, Map<String, String>> warehouses = new HashMap<>();
private static Map<String, Map<String, String>> drones = new HashMap<>();
private static Map<String, Map<String, String>> equipment = new HashMap<>();
private static Map<String, Map<String, String>> customers = new HashMap<>();
private static Map<String, Map<String, String>> purchaseOrders = new HashMap<>();
private static Map<String, Map<String, String>> ratings = new HashMap<>();
private static Map<String, Set<String>> entityRequirements = new HashMap<>();
private static String[] WAREHOUSE_FIELDS = {};
private static String[] DRONE_FIELDS = {};
private static String[] EQUIPMENT_FIELDS = {};
private static String[] CUSTOMER_FIELDS = {};
private static String[] PURCHASE_ORDER_FIELDS = {};
private static String[] RATING_FIELDS = {};
public static void main(String[] args) {
// trying to make an automatic database connection
String defaultDbUrl = "jdbc:sqlite:checkpoint_four.db";
String autoUrl = (args != null && args.length > 0 && args[0] != null && !args[0].isEmpty()) ? args[0]
: defaultDbUrl;
System.out.println("Attempting automatic DB connect to: " + autoUrl);
if (DatabaseControl.connect(autoUrl, null, null)) {
System.out.println("Auto-connected to database.");
/*
warehouses.clear();
warehouses.putAll(DatabaseControl.loadAllRecords("Warehouse"));
drones.clear(); drones.putAll(DatabaseControl.loadAllRecords("Drone"));
equipment.clear();
equipment.putAll(DatabaseControl.loadAllRecords("Equipment"));
customers.clear();
customers.putAll(DatabaseControl.loadAllRecords("Customer"));
purchaseOrders.clear();
purchaseOrders.putAll(DatabaseControl.loadAllRecords("Purchase Order"));
ratings.clear(); ratings.putAll(DatabaseControl.loadAllRecords("Rating"));
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WAREHOUSE_FIELDS = DatabaseControl.getColumnNames("Warehouse");
DRONE_FIELDS = DatabaseControl.getColumnNames("Drone");
EQUIPMENT_FIELDS = DatabaseControl.getColumnNames("Equipment");
CUSTOMER_FIELDS = DatabaseControl.getColumnNames("Customer");
PURCHASE_ORDER_FIELDS = DatabaseControl.getColumnNames("PurchaseOrders");
RATING_FIELDS = DatabaseControl.getColumnNames("Rating");
System.out.println("Loading column names and constraints...");
// 1. Load Names (You already have this)
// ... other fields ...
// 2. Load Requirements (ADD THIS)
entityRequirements.put("Warehouse", DatabaseControl.getRequiredColumns("Warehouse"));
entityRequirements.put("Drone", DatabaseControl.getRequiredColumns("Drone"));
entityRequirements.put("Equipment", DatabaseControl.getRequiredColumns("Equipment"));
entityRequirements.put("Customer", DatabaseControl.getRequiredColumns("Customer"));
entityRequirements.put("PurchaseOrders", DatabaseControl.getRequiredColumns("PurchaseOrders"));
entityRequirements.put("Rating", DatabaseControl.getRequiredColumns("Rating"));
} else {
System.out.println("Auto-connect failed (continuing without DB). Use menu option 12 to connect manually.");
}
while (true) {
System.out.println("\nHello, what would you like to look at today?");
System.out.println("1. Warehouse Information");
System.out.println("2. Drone Information");
System.out.println("3. Equipment Information");
System.out.println("4. Customer/Community Information");
System.out.println("5. Purchase Order Information");
System.out.println("6. Rating and Review Information");
System.out.println("7. Rent Equipment");
System.out.println("8. Return Equipment");
System.out.println("9. Schedule Equipment Delivery");
System.out.println("10. Schedule Equipment Pickup");
System.out.println("11. Useful Reports");
System.out.println("12. Connect to Database");
System.out.println("0. Exit");
System.out.print("Enter choice: ");
int choice = getInt();
switch (choice) {
case 1 -> warehouseMenu();
case 2 -> droneMenu();
case 3 -> equipmentMenu();
case 4 -> customerMenu();
case 5 -> purchaseOrderMenu();
case 6 -> ratingMenu();
case 7 -> rentEquipment();
case 8 -> returnEquipment();
case 9 -> scheduleDelivery();
case 10 -> schedulePickup();
case 11 -> usefulReportsMenu();
case 12 -> dbConnectMenu();
case 0 -> {
System.out.println("Goodbye!");
return;
}
default -> System.out.println("Invalid choice.");
}
}
}
private static int getInt() {
while (true) {
try {
return Integer.parseInt(sc.nextLine().trim());
} catch (NumberFormatException e) {
System.out.print("Please enter a valid number: ");
}
}
}
private static String getNonEmptyLine(String prompt) {
while (true) {
System.out.print(prompt);
String input = sc.nextLine().trim();
if (!input.isEmpty()) {
return input;
}
System.out.println("Input cannot be empty.");
}
}
private static void entityMenu(String entityName,
String idLabel, Map<String, Map<String, String>> records, String[] fields) {
while (true) {
System.out.println("\n" + entityName + " Menu:");
System.out.println("1. Add new record");
System.out.println("2. Edit existing record");
System.out.println("3. Delete record");
System.out.println("4. Search records");
//System.out.println("5. List all records");
System.out.println("0. Back to Main Menu");
System.out.print("Choice: ");
int choice = getInt();
switch (choice) {
case 1 -> addRecord(entityName, idLabel, records, fields, entityName);
case 2 -> editRecord(entityName, idLabel, records, fields, entityName);
case 3 -> deleteRecord(entityName, idLabel, records, entityName);
case 4 -> searchRecords(idLabel, entityName, fields);
//case 5 -> listRecords(entityName, records, fields);
case 0 -> {
return;
}
default -> System.out.println("Invalid choice.");
}
}
}
// get the primary key column name for a table in the database
private static String getPkColumnName(String entityName) {
return switch (entityName) {
case "Warehouse" -> "WarehouseID";
case "Drone" -> "DroneSerialNumber";
case "Equipment" -> "EquipmentSerialNumber";
case "Customer" -> "UserID";
case "PurchaseOrders" -> "OrderNumber";
case "Rating" -> "RatingID";
default -> "ID";
};
}
// adds a record to the database
private static void addRecord(String entityName,
String idLabel,
Map<String, Map<String, String>> records,
String[] fields, String tableName) {
String id = getNonEmptyLine("Enter " + idLabel + ": ");
/*
* Check whether the ID already exists. Prefer the in-memory map first, then
* fall back to the DB.
*/
if (records.containsKey(id) || DatabaseControl.primaryKeyValueExists(tableName, idLabel, id)) {
System.out.println("A record with that ID already exists.");
return;
}
Map<String, String> record = new HashMap<>();
Set<String> requiredFields = entityRequirements.getOrDefault(entityName, new HashSet<>());
for (String field : fields) {
// checks if this field is the ID and skips it
if (field.equals(idLabel)) {
record.put(field, id);
continue;
}
boolean isRequired = requiredFields.contains(field);
if (isRequired) {
// --- STRICT INPUT LOOP (Cannot be empty) ---
while (true) {
System.out.print("Enter " + field + " (REQUIRED): ");
String value = sc.nextLine().trim();
if (!value.isEmpty()) {
record.put(field, value);
break; // Valid input, move to next field
}
System.out.println("Error: " + field + " cannot be empty.");
}
} else {
// --- OPTIONAL INPUT (Can be skipped) ---
System.out.print("Enter " + field + " (leave blank to skip): ");
String value = sc.nextLine().trim();
if (!value.isEmpty()) {
record.put(field, value);
}
}
}
records.put(id, record);
// runs the insert function from DatabaseControl
if (DatabaseControl.insertStuff(fields, tableName, record)) {
System.out.println(entityName + " record created and saved for " + id + ".");
} else {
System.out.println(entityName + " record created in MEMORY ONLY for " + id + " (DB save failed).");
}
}
private static void editRecord(String entityName,
String idLabel,
Map<String, Map<String, String>> records,
String[] fields, String tableName) {
String id = getNonEmptyLine("Enter " + idLabel + " to edit: ");
Map<String, String> record = records.getOrDefault(id, new HashMap<>());
if (id == null || !DatabaseControl.primaryKeyValueExists(tableName, idLabel, id)) {
System.out.println("No record found with that ID: " + id);
return;
}
while (true) {
System.out.println("\nCurrent details for " + id + ":");
// above works so far
record = DatabaseControl.printRecord(tableName, id, record, fields);
System.out.println("Select a field to update (0 to stop):");
for (int i = 0; i < fields.length; i++) {
System.out.println((i + 1) + ". " + fields[i]);
}
System.out.print("Choice: ");
int choice = getInt();
if (choice == 0) {
return;
}
if (choice < 1 || choice > fields.length) {
System.out.println("Invalid choice.");
continue;
}
String field = fields[choice - 1];
System.out.print("Enter new value for " + field + " (leave blank to clear): ");
String value = sc.nextLine().trim();
DatabaseControl.updateField(tableName, idLabel, id, field, value);
if (value.isEmpty()) {
record.remove(field);
System.out.println(field + " cleared.");
} else {
record.put(field, value);
System.out.println(field + " updated.");
}
// do not persist edits — only additions are stored in DB per requirement
}
}
private static void deleteRecord(String entityName,
String idLabel,
Map<String, Map<String, String>> records,
String tableName) {
String id = getNonEmptyLine("Enter " + idLabel + " to delete: ");
// 1. Try to delete from the Database first
boolean dbSuccess = DatabaseControl.deletestuff(tableName, idLabel, id);
// 2. If successful (or if you want to force sync), remove from Local Map
if (dbSuccess) {
records.remove(id);
System.out.println(entityName + " record deleted successfully.");
} else {
// Optional: Check if it existed in the map anyway (phantom data)
if (records.containsKey(id)) {
records.remove(id);
System.out.println("Record removed from memory (was not found in DB).");
} else {
System.out.println("No record found with that ID.");
}
}
}
private static void searchRecords(String idLabel, String entityName, String[] fields) {
// 1. Ask for the ID (Just like deleteRecord)
String id = getNonEmptyLine("Enter " + idLabel + " to find: ");
// 2. Call the database helper
boolean found = DatabaseControl.searchstuff(entityName, idLabel, id, fields);
// 3. Handle the result
if (!found) {
System.out.println("No record found with that ID: " + id);
}
}
/*private static void listRecords(String entityName,
Map<String, Map<String, String>> records,
String[] fields) {
records = DatabaseControl.loadAllRecords(entityName);
if (records.isEmpty()) {
System.out.println("No " + entityName.toLowerCase() + " records available.");
return;
}
for (Map.Entry<String, Map<String, String>> entry : records.entrySet()) {
DatabaseControl.printRecord(entityName, entry.getKey(), entry.getValue(), fields);
}
}*/
// Warehouse Menu
private static void warehouseMenu() {
entityMenu("Warehouse", getPkColumnName("Warehouse"), warehouses, WAREHOUSE_FIELDS);
}
// Drone Menu
private static void droneMenu() {
entityMenu("Drone", getPkColumnName("Drone"), drones, DRONE_FIELDS);
}
// Equipment Menu
private static void equipmentMenu() {
entityMenu("Equipment", getPkColumnName("Equipment"), equipment, EQUIPMENT_FIELDS);
}
// Customer Menu
private static void customerMenu() {
entityMenu("Customer", getPkColumnName("Customer"), customers, CUSTOMER_FIELDS);
}
// Purchase Order Menu
private static void purchaseOrderMenu() {
entityMenu("Purchase Order", getPkColumnName("PurchaseOrders"), purchaseOrders, PURCHASE_ORDER_FIELDS);
}
// Rating & Review Menu
private static void ratingMenu() {
entityMenu("Rating", getPkColumnName("Rating"), ratings, RATING_FIELDS);
}
private static void rentEquipment() {
System.out.println("\n--- Rent Equipment ---");
String userId = getNonEmptyLine("Enter User ID (Required): ");
String equipSerial = getNonEmptyLine("Enter Equipment Serial Number (Required): ");
String droneSerial = getNonEmptyLine("Enter Drone Serial Number (Required): ");
String due = getNonEmptyLine("Enter Due Date (YYYY-MM-DD) (Required): ");
System.out.print("Enter Checkout Date (YYYY-MM-DD) (Press Enter to skip): ");
String checkout = sc.nextLine().trim();
System.out.print("Enter Daily Cost (e.g., 15.50) (Press Enter to skip): ");
String dailyCost = sc.nextLine().trim();
System.out.print("Enter Fees (e.g., 5.00) (Press Enter to skip): ");
String fees = sc.nextLine().trim();
System.out.print("Enter Return Date (YYYY-MM-DD) (Press Enter to skip): ");
String returnDate = sc.nextLine().trim();
System.out.print("Enter Delivery Date (YYYY-MM-DD) (Press Enter to skip): ");
String deliveryDate = sc.nextLine().trim();
System.out.print("Enter Pickup Date (YYYY-MM-DD) (Press Enter to skip): ");
String pickupDate = sc.nextLine().trim();
boolean success = DatabaseControl.insertRental(userId, due, equipSerial, droneSerial, checkout, dailyCost,
fees, returnDate, deliveryDate, pickupDate);
if (success) {
System.out.println("\n*** Rental Created Successfully ***");
System.out.println("Customer ID: " + userId);
System.out.println("Equipment: " + equipSerial);
System.out.println("Drone Deliv: " + droneSerial);
System.out.println("Due Date: " + due);
// Handle optional fields for display
String displayCheckout = checkout.isEmpty() ? "[Not set]" : checkout;
String displayCost = dailyCost.isEmpty() ? "[Not set]" : "$" + dailyCost;
System.out.println("Checkout: " + displayCheckout);
System.out.println("Daily Cost: " + displayCost);
System.out.println("-----------------------------------");
} else {
System.out.println("Rental failed. Please check the inputs and try again.");
}
}
//Changes Returned field in Rentals from 0 to 1
private static void returnEquipment() {
System.out.println("\n--- Return Equipment ---");
String rentalId = getNonEmptyLine("Enter Rental Number (Required): ");
String returnDate = getNonEmptyLine("Enter Return Date (YYYY-MM-DD) (Required): ");
DatabaseControl.updateField("Rentals", "RentalNumber", rentalId, "ReturnStatus", "1");
DatabaseControl.updateField("Rentals", "RentalNumber", rentalId, "RentalReturns", returnDate);
System.out.println("\nReturn recorded for Rental ID: " + rentalId + " on " + returnDate);
System.out.println("Equipment returned.");
}
//Allows user to change checkout date and assigned drone to order
private static void scheduleDelivery() {
System.out.println("\nSchedule Equipment Delivery");
String RentalNumber = getNonEmptyLine("Enter Rental Number: ");
String deliveryDate = getNonEmptyLine("Enter new delivery date (YYYY-MM-DD): ");
String droneId = getNonEmptyLine("Enter new drone ID: ");
System.out.println("\nDelivery scheduled and saved.");
System.out.println("Delivery date: " + deliveryDate);
System.out.println("Assigned drone: " + droneId);
DatabaseControl.updateField("Rentals", "RentalNumber", RentalNumber, "DeliveryDate", deliveryDate);
DatabaseControl.updateField("Rentals", "RentalNumber", RentalNumber, "DroneSerialNumber", droneId);
DatabaseControl.printAddress(RentalNumber, false);
}
//Allows user to change checkout date and assigned drone to order
private static void schedulePickup() {
System.out.println("\nSchedule Equipment Pickup");
String RentalNumber = getNonEmptyLine("Enter Rental Number: ");
String pickupDate = getNonEmptyLine("Enter new Pickup date (YYYY-MM-DD): ");
String droneId = getNonEmptyLine("Enter new drone ID: ");
System.out.println("\nPickup scheduled and saved.");
System.out.println("Pickup date: " + pickupDate);
System.out.println("Assigned drone: " + droneId);
DatabaseControl.updateField("Rentals", "RentalNumber", RentalNumber, "RentalPickUp", pickupDate);
DatabaseControl.updateField("Rentals", "RentalNumber", RentalNumber, "DroneSerialNumber", droneId);
DatabaseControl.printAddress(RentalNumber, true);
}
// ------------------
private static void usefulReportsMenu() {
String dburl = "jdbc:sqlite:checkpoint_four.db";
System.out.println("\nUseful Reports");
System.out.println("1. Renting Checkouts Report");
System.out.println("2. Popular Item Report");
System.out.println("3. Popular Manufacturer Report");
System.out.println("4. Popular Drone Report");
System.out.println("5. Member with Most Rentals Report");
System.out.println("6. Equipment Year By Type Report");
System.out.println("0. Back to Main Menu");
System.out.print("Choice: ");
int choice = getInt();
try {
switch (choice) {
case 1 -> {
// Placeholder for future report
System.out.println("Insert name of customer for renting checkout report:");
String customerName = sc.nextLine().trim();
ReportGenerator.rentingCheckouts(dburl, customerName);
}
case 2 -> {
ReportGenerator.popularItem(dburl);
}
case 3 -> {
ReportGenerator.popularManufacturer(dburl);
}
case 4 -> {
ReportGenerator.showPopularDroneReport(dburl);
}
case 5 -> {
ReportGenerator.showFrequentRenterReport(dburl);
}
case 6 -> {
// Choose the Type from the set
System.out.println("\nSelect Equipment Type:");
System.out.println("1. Mechanical");
System.out.println("2. Electrical");
System.out.println("3. Hydraulic");
System.out.println("4. Pneumatic");
System.out.print("Enter choice: ");
int typeChoice = getInt();
String selectedType = switch (typeChoice) {
case 1 -> "Mechanical";
case 2 -> "Electrical";
case 3 -> "Hydraulic";
case 4 -> "Pneumatic";
default -> null;
};
if (selectedType == null) {
System.out.println("Invalid selection of type.");
break;
}
System.out.print("Enter maximum year (e.g. 2020): ");
int year = getInt();
// Step C: Run Report
ReportGenerator.showEquipmentByTypeAndMaxYear(dburl, selectedType, year);
}
case 0 -> {
return;
}
default -> System.out.println("Invalid choice. Please select a number from the menu.");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
// Minimal DB connect menu
private static void dbConnectMenu() {
System.out.println("\nDatabase Connection");
String url = getNonEmptyLine("Enter JDBC URL (e.g. jdbc:sqlite:checkpoint_four.db): ");
System.out.print("Enter DB username (leave blank if not required): ");
String user = sc.nextLine().trim();
System.out.print("Enter DB password (leave blank if not required): ");
String pass = sc.nextLine().trim();
if (DatabaseControl.connect(url, user.isEmpty() ? null : user, pass.isEmpty() ? null : pass)) {
System.out.println("Connected to database.");
// load existing records into memory
warehouses.clear();
warehouses.putAll(DatabaseControl.loadAllRecords("Warehouse"));
drones.clear();
drones.putAll(DatabaseControl.loadAllRecords("Drone"));
equipment.clear();
equipment.putAll(DatabaseControl.loadAllRecords("Equipment"));
customers.clear();
customers.putAll(DatabaseControl.loadAllRecords("Customer"));
purchaseOrders.clear();
purchaseOrders.putAll(DatabaseControl.loadAllRecords("PurchaseOrders"));
ratings.clear();
ratings.putAll(DatabaseControl.loadAllRecords("Rating"));
} else {
System.out.println(
"Failed to connect to database. Make sure JDBC driver is on classpath and URL is correct.");
}
}
}