-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
288 lines (248 loc) · 12.8 KB
/
Main.java
File metadata and controls
288 lines (248 loc) · 12.8 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
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
import java.time.Duration;
public class Main {
public static void main(String[] args) {
// Initializing parking spots
List<ParkingSpot> spots = Arrays.asList(
new ParkingSpot("A1", false, "Car"),
new ParkingSpot("A2", false, "Car"),
new ParkingSpot("B1", false, "Bike"),
new ParkingSpot("B2", false, "Bike"),
new ParkingSpot("C1", false, "Truck"),
new ParkingSpot("C2", false, "Truck")
);
// Initializes parking lot
ParkingLot parkingLot = new ParkingLot(spots);
Scanner scanner = new Scanner(System.in);
// Initialize sMaintenance and Safety systems
MaintenanceSystem maintenanceSystem = new MaintenanceSystem();
SafetySystem safetySystem = new SafetySystem(parkingLot);
// Main loop of the interface
while (true) {
System.out.println("\nWelcome to the Parking System!");
System.out.println("1. Customer Interface");
System.out.println("2. Staff Interface");
System.out.println("3. Exit");
int choice = getValidatedInt(scanner, "Enter your choice: ", 1, 3); // method call to handle errors
switch (choice) {
case 1:
customerInterface(parkingLot, scanner);
break;
case 2:
staffInterface(parkingLot, scanner, maintenanceSystem, safetySystem);
break;
case 3:
System.out.println("Exiting system. Thank you for using the parking system.");
return;
default:
System.out.println("Invalid option. Please choose 1, 2, or 3.");
}
}
}
// Customer interface logic
private static void customerInterface(ParkingLot parkingLot, Scanner scanner) {
while (true) {
System.out.println("\nCustomer Interface:");
System.out.println("1. Park a Vehicle");
System.out.println("2. Find My Vehicle");
System.out.println("3. Exit Customer interface");
int choice = getValidatedInt(scanner, "Enter your choice: ", 1, 3);
switch (choice) {
case 1:
System.out.println("Enter Vehicle Number:");
String vehicleNum = scanner.next();
// if(vehicleNum.equals(vehicleNum))
// {
// System.out.println("This vehicle is already present and not allowed");
// break;}
System.out.println("Enter Vehicle Type (Car/Bike/Truck/Van/Lorry):");
String vehicleType = scanner.next();
LocalTime entryTime = LocalTime.now();
Vehicle vehicle;
if (vehicleType.equalsIgnoreCase("Car")) {
int numDoors = getValidatedInt(scanner, "Enter number of doors: ", 2, 4);
vehicle = new Car(vehicleNum, entryTime, numDoors);
} else if (vehicleType.equalsIgnoreCase("Bike")) {
boolean hasSideCar = getValidatedBoolean(scanner, "Does the bike have a sidecar? (true/false): ");
vehicle = new Bike(vehicleNum, entryTime, hasSideCar);
} else if (vehicleType.equalsIgnoreCase("Truck") || vehicleType.equalsIgnoreCase("Van") || vehicleType.equalsIgnoreCase("Lorry")) {
double cargoCapacity = getValidatedDouble(scanner, "Enter cargo capacity in kg: ", 0, 10000);
vehicle = new Truck(vehicleNum, entryTime, cargoCapacity);
} else {
System.out.println("Invalid vehicle type. Please enter Car, Bike, Truck, Van, or Lorry.");
break;
}
try {
Ticket ticket = parkingLot.parkVehicle(vehicle);
System.out.println("Your vehicle is parked at Spot: " + ticket.getParkingSpot().getSpotID());
System.out.println("Ticket ID: " + ticket.getTicketID());
} catch (IllegalStateException e) {
System.out.println("Sorry, the parking lot is currently full. Please try again later.");
}
break;
case 2:
System.out.println("Enter your Vehicle Number:");
String vehicleNumber = scanner.next();
Ticket foundTicket = null;
for (Ticket ticket : parkingLot.getTickets()) {
if (ticket.getVehicle().getVehicleNum().equalsIgnoreCase(vehicleNumber)) {
foundTicket = ticket;
break;
}
}
if (foundTicket != null) {
System.out.println("Your vehicle is parked at Spot: " + foundTicket.getParkingSpot().getSpotID());
System.out.println("Ticket ID: " + foundTicket.getTicketID());
while (true) { // Keep asking until valid input is given
System.out.println("\nPress Enter to exit your vehicle from the parking lot or type 'back' to return to the Customer Interface.");
scanner.nextLine(); // Consume any leftover newlines
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("back")) {
System.out.println("Returning to Customer Interface...");
break; // Go back to the customer menu
} else if (input.isEmpty()) {
// Process vehicle exit
LocalTime exitTime = LocalTime.now();
foundTicket.setExitTime(exitTime);
String duration = foundTicket.calculateDuration();
double fee = foundTicket.getVehicle().calculateFees(
Duration.between(foundTicket.getVehicle().getEntryTime(), exitTime).toMinutes());
System.out.println("Your vehicle has been parked for: " + duration);
System.out.println("Total fee: " + fee + " PKR");
PaymentProcessing paymentProcessing = new PaymentProcessing();
paymentProcessing.processPayment(foundTicket.getVehicle(), fee);
foundTicket.getParkingSpot().setOccupancyStatus(false);
System.out.println("Your vehicle has exited the parking lot.");
break;
} else {
System.out.println("Invalid input. Please press Enter to exit or type 'back' to return.");
}
}
} else {
System.out.println("Vehicle not found in the parking lot. Please check your vehicle number and try again.");
}
break;
case 3:
System.out.println("Exiting Customer Interface.");
return;
default:
System.out.println("Invalid option. Please choose 1, 2, or 3.");
}
}
}
// Staff Interface Logic
private static void staffInterface(ParkingLot parkingLot, Scanner scanner, MaintenanceSystem maintenanceSystem, SafetySystem safetySystem) {
while (true) {
System.out.println("\nStaff Interface:");
System.out.println("1. Schedule Maintenance");
System.out.println("2. Perform Maintenance");
System.out.println("3. Log Maintenance");
System.out.println("4. Handle Fire Emergency");
System.out.println("5. Handle Theft Emergency");
System.out.println("6. Exit Staff Interface");
int choice = getValidatedInt(scanner, "Enter your choice: ", 1, 6);
switch (choice) {
case 1:
System.out.println("Enter maintenance task:");
scanner.nextLine();
String task = scanner.nextLine();
LocalDate date = getValidatedDate(scanner, "Enter maintenance date (YYYY-MM-DD): ");
maintenanceSystem.scheduleMaintenance(task, date);
break;
case 2:
System.out.println("Write the task on which you want to perform maintenance:");
scanner.nextLine();
String performTask = scanner.nextLine(); //takes string input
maintenanceSystem.performMaintenance(performTask); //maintainance system class method called
break;
case 3:
System.out.println("Enter maintenance task to log:");
scanner.nextLine(); // adds new line or skips one takes input in next line
String logTask = scanner.nextLine();
LocalDate logDate = getValidatedDate(scanner, "Enter completion date (YYYY-MM-DD): ");
maintenanceSystem.logMaintenance(logTask, logDate);
break;
case 4:
safetySystem.handleFire();
break;
case 5:
System.out.println("Enter vehicle number for theft report:");
String vehicleNum = scanner.next();
Vehicle vehicle = null; //obj value null
for (Ticket ticket : parkingLot.getTickets()) {
if (ticket.getVehicle().getVehicleNum().equals(vehicleNum)) {
vehicle = ticket.getVehicle();
break;
}
}
if (vehicle != null) { //if it is null, means no vehicle present
safetySystem.handleTheft(vehicle);
} else {
System.out.println("Vehicle not found in the parking lot.");
}
break;
case 6:
System.out.println("Exiting Staff Interface.");
return;
default:
System.out.println("Invalid option. Please choose 1, 2, 3, 4, 5, or 6.");
}
}
}
// Helper Methods for to handle errors
private static int getValidatedInt(Scanner scanner, String prompt, int min, int max) {
while (true) {
try {
System.out.print(prompt);
int input = scanner.nextInt();
if (input >= min && input <= max) {
return input;
} else {
System.out.println("Input must be between " + min + " and " + max + ".");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
scanner.next(); // Clear invalid input
}
}
}
private static double getValidatedDouble(Scanner scanner, String prompt, double min, double max) {
while (true) {
try {
System.out.print(prompt);
double input = scanner.nextDouble();
if (input >= min && input <= max) {
return input;
} else {
System.out.println("Input must be between " + min + " and " + max + ".");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid number.");
scanner.next(); // Clear invalid input
}
}
}
private static boolean getValidatedBoolean(Scanner scanner, String prompt) {
while (true) {
System.out.print(prompt);
if (scanner.hasNextBoolean()) {
return scanner.nextBoolean();
} else {
System.out.println("Invalid input. Please enter true or false.");
scanner.next(); // Clear invalid input
}
}
}
private static LocalDate getValidatedDate(Scanner scanner, String prompt) {
while (true) {
System.out.print(prompt);
try {
return LocalDate.parse(scanner.next().trim());
} catch (Exception e) {
System.out.println("Invalid date format. Please enter a valid date in the format YYYY-MM-DD.");
}
}
}
}