-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachineMaintenanceApp.java
97 lines (89 loc) · 4.36 KB
/
MachineMaintenanceApp.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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MachineMaintenanceApp {
public static void main(String[] args) {
List<MachineMaintenanceRecord> records = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Machine Maintenance Record App");
System.out.println("1. Add Machine");
System.out.println("2. Remove Machine");
System.out.println("3. Add Maintenance Record Date");
System.out.println("4. Edit Last Maintenance Date");
System.out.println("5. Retrieve Maintenance Dates");
System.out.println("6. Exit");
System.out.print("Select an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
System.out.print("Enter machine name: ");
String machineName = scanner.nextLine();
records.add(new MachineMaintenanceRecord(machineName));
break;
case 2:
System.out.print("Enter machine name to remove: ");
String machineToRemove = scanner.nextLine();
records.removeIf(record -> record.getMachineName().equals(machineToRemove));
break;
case 3:
System.out.print("Enter machine name: ");
String machineToAddDate = scanner.nextLine();
MachineMaintenanceRecord recordToAddDate = findRecord(records, machineToAddDate);
if (recordToAddDate != null) {
System.out.print("Enter maintenance date (yyyy-mm-dd): ");
String date = scanner.nextLine();
recordToAddDate.addMaintenanceDate(date);
} else {
System.out.println("Machine not found.");
}
break;
case 4:
System.out.print("Enter machine name: ");
String machineToEditDate = scanner.nextLine();
MachineMaintenanceRecord recordToEditDate = findRecord(records, machineToEditDate);
if (recordToEditDate != null) {
if (!recordToEditDate.getMaintenanceDates().isEmpty()) {
System.out.print("Enter new maintenance date (yyyy-mm-dd): ");
String newDate = scanner.nextLine();
recordToEditDate.editLastMaintenanceDate(newDate);
} else {
System.out.println("No maintenance dates available for this machine.");
}
} else {
System.out.println("Machine not found.");
}
break;
case 5:
System.out.print("Enter machine name: ");
String machineToRetrieveDates = scanner.nextLine();
MachineMaintenanceRecord recordToRetrieveDates = findRecord(records, machineToRetrieveDates);
if (recordToRetrieveDates != null) {
List<String> maintenanceDates = recordToRetrieveDates.getMaintenanceDates();
System.out.println("Maintenance dates for " + machineToRetrieveDates + ":");
for (String date : maintenanceDates) {
System.out.println(date);
}
} else {
System.out.println("Machine not found.");
}
break;
case 6:
System.out.println("Exiting the app.");
System.exit(0);
default:
System.out.println("Invalid option. Please select a valid option (1-6).");
break;
}
}
}
private static MachineMaintenanceRecord findRecord(List<MachineMaintenanceRecord> records, String machineName) {
for (MachineMaintenanceRecord record : records) {
if (record.getMachineName().equals(machineName)) {
return record;
}
}
return null;
}
}