-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarparkSystem.java
367 lines (326 loc) · 13.7 KB
/
CarparkSystem.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
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
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/**
* Project 2: CarparkSystem class. Acts as the controller of the application.
* Stores carPark, frame of GUI, carparkView, menuPanel and the form dialogs.
* Main class which starts the GUI is defined in this class.
*
* @author Abdurrahman Faqih 104675143
* @version 0.1 9 May 2024
*/
public class CarparkSystem {
// Initialize carpark model
private CarPark carPark = new CarPark();
// Initialize view components
private JFrame frame = new JFrame("Carpark");
private CarparkView carparkView = new CarparkView(this);
private MenuPanel menuPanel = new MenuPanel(this);
private TimerPanel timerPanel = new TimerPanel();
// Initialize dialogs
private FormDialog addParkingSpotDialog = new FormDialog(this, frame, "Add parking spot", "Add", "Enter spot ID: ");
private FormDialog deleteParkingSpotDialog = new FormDialog(this, frame, "Delete parking spot", "Delete",
"Enter spot ID: ");
private ParkCarDialog parkCarDialog;
private FormDialog findCarByRegNoDialog = new FormDialog(this, frame, "Find car by reg number",
"Find by registration number", "Enter registration number: ");
private FormDialog removeCarByRegNoDialog = new FormDialog(this, frame, "Remove car by reg number",
"Remove by registration number", "Enter registration number: ");
private FormDialog findCarByMakeDialog = new FormDialog(this, frame, "Find car by make", "Find by make",
"Enter make: ");
private ActionDialog resetCarParkDialog;
private ActionDialog exitProgramDialog = new ActionDialog(this, frame, "Exit program", ActionDialog.EXIT,
"Program ends!");
private ParkingSpotViewActionDialog parkingSpotViewActionDialog;
public CarparkSystem() {
frame.setSize(1000, 500); // Set dimensions of frame
frame.add(menuPanel, BorderLayout.WEST); // Place menu panel on the left side of frame
frame.add(carparkView, BorderLayout.CENTER); // Place carpark panel on the right side of the frame
frame.add(timerPanel, BorderLayout.NORTH); // Place timer panel on the top side of the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); // Show frame
}
/**
* Handles opening the add parking spot dialog
*/
public void openAddParkingSpotDialog() {
addParkingSpotDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
addParkingSpotDialog.setVisible(true); // Show dialog
}
/**
* Handles adding a parking spot.
* CarparkSystem will pass user input to CarPark model
* and the newly created parking spot to carpark view.
*
* @param spotId id input from dialog text field
*/
public void addParkingSpotHandler(String spotId) {
// Add parking spot to carpark
ParkingSpot parkingSpot = carPark.addParkingSpot(spotId);
if (parkingSpot == null) {
// If there is no parking spot returned, show the error
String message = "Each spot should have a unique spot number.";
showInfo(message, "Error");
return; // To not close the dialog
} else {
// else create a parking spot view
carparkView.addParkingSpotView(parkingSpot);
}
addParkingSpotDialog.setVisible(false); // Close add parking spot dialog
}
/**
* Handles opening the delete parking spot dialog
*/
public void openDeleteParkingSpotDialog() {
deleteParkingSpotDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
deleteParkingSpotDialog.setVisible(true); // Show dialog
}
/**
* Handles deleting a parking spot.
* CarparkSystem will pass user input to CarPark model
* to delete parking spot.
*
* @param spotId id input from dialog text field
*/
public void deleteParkingSpotHandler(String spotId) {
// Delete parking spot
String message = carPark.deleteParkingSpot(spotId);
if (!message.equals("Parking spot deleted successfully.")) {
// If deletion is not successful, show error
showInfo(message, "Error");
return; // To not close the dialog
} else {
// else delete parking spot view
// and show info dialog
carparkView.deleteParkingSpotView(spotId);
showInfo(message, "Info");
}
deleteParkingSpotDialog.setVisible(false); // Close delete parking spot dialog
}
/**
* Prints id, whether occupied, and if occupied, car's registration, make
* and duration of parking of all spots in a well-defined format.
* Also prints summary of total spots, number of occupied spots and number
* of unoccupied spots in car park
*/
public void listParkingSpots() {
String message; // Initialize message
// Get summary info
ArrayList<ParkingSpot> parkingSpots = this.carPark.getParkingSpots();
int carParkSize = parkingSpots.size(); // Total number of parking spots
int noUnoccupiedSpots = this.carPark.getNoUnoccupiedSpots();
int noOccupiedSpots = carParkSize - noUnoccupiedSpots; // Calculate number of occupied spots
// Add summary info
message = "<html>Summary<br />=====================<br />";
message = message + "Total number of parking spots: " + carParkSize + "<br />Occupied spots: "
+ noOccupiedSpots + "<br />Unoccupied spots: " + noUnoccupiedSpots;
// Add info about parking spots
if (!parkingSpots.isEmpty()) {
message += "<br /><br />List of parking spots<br />=====================";
// Iterate through parking spots
for (ParkingSpot current : parkingSpots) {
// Loop through parking spots and print out
// spot id, whether it is occupied,
// if occupied, registration and make of car
message = message + "<br />Spot ID: " + current.getId() + ", " + (current.isOccupied()
? "Parked car: " + current.getParkedCar().getRegNo() + " " + current.getParkedCar().getMake()
+ "<br />Duration of parking: " + current.getParkedCar().getDurationOfParking()
+ "<br />"
: "Empty");
}
}
// Close html tag
message += "</html>";
// Show message to user
showInfo(message, "Info");
}
/**
* Handles opening the select parking spot dialog
*/
public void openParkCarDialog(String spotId) {
parkCarDialog = new ParkCarDialog(this, frame, spotId);
parkCarDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
parkCarDialog.setVisible(true); // Show dialog
}
/**
* Handles parking a car in a parking spot.
* CarparkSystem will pass spot ID and car to CarPark model
* to park car in parking spot.
*
* @param spotId user input from dialog text field
*/
public void parkCarHandler(String spotId, Car car) {
// Park car
String message = this.carPark.parkCarInSpot(spotId, car);
if (!message.equals("Car parked successfully")) {
// If parking is not successful, show error
showInfo(message, "Error");
} else {
// else update parking spot view
// and show info dialog
carparkView.updateParkingSpotView(spotId);
showInfo(message, "Info");
}
parkCarDialog.setVisible(false); // Close dialog
}
/**
* Handles opening the find car by registration number dialog
*/
public void openFindCarByRegNoDialog() {
findCarByRegNoDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
findCarByRegNoDialog.setVisible(true); // Show dialog
}
/**
* Handles finding car by registration number.
* CarparkSystem will pass registration number to CarPark model
* to find car by registration number.
*
* @param regNo user input from dialog text field
*/
public void findCarByRegNoHandler(String regNo) {
String result = carPark.findCarByRegNo(regNo); // Find car
if (result.equals("Car not parked here.")) {
// If car is not found, show error
showInfo(result, "Error");
return; // To not close the dialog
} else {
// Show result
showInfo(result, "Info");
}
findCarByRegNoDialog.setVisible(false);
}
/**
* Handles opening the remove car by registration number dialog
*/
public void openRemoveCarByRegNoDialog() {
removeCarByRegNoDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
removeCarByRegNoDialog.setVisible(true); // Show dialog
}
/**
* Handles removing car by registration number.
* CarparkSystem will pass registration number to CarPark model
* to remove car by registration number.
*
* @param regNo user input from dialog text field
*/
public void removeCarByRegNoHandler(String regNo) {
String result = carPark.removeCarByRegNo(regNo); // Remove car
if (result.equals("Car not parked here.")) {
// If car is not found, show error
showInfo(result, "Error");
return; // To not close the dialog
} else {
// Update parking spot view
carparkView.updateParkingSpotView(result);
// Show result
showInfo("Car with registration number " + regNo + " removed from spot " + result, "Info");
}
removeCarByRegNoDialog.setVisible(false);
}
/**
* Handles opening the find car by make dialog
*/
public void openFindCarByMakeDialog() {
findCarByMakeDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
findCarByMakeDialog.setVisible(true); // Show dialog
}
/**
* Handles closing the find car by make dialog.
* CarparkSystem will pass make string to CarPark model
* to find car by make.
*
* @param make user input from dialog text field
*/
public void findCarByMakeHandler(String make) {
ArrayList<String> result = carPark.findCarByMake(make);
if (result.isEmpty()) {
// If no car is found, show error
showInfo("No cars found with make " + make, "Error");
return; // To not close the dialog
} else {
// Show result
String message = "<html>" + result.size() + " car(s) found with make " + make
+ "<br />====================="; // Initialize message
// Iterate through array list and print result
for (String current : result) {
message = message + "<br />" + current + "<br />";
}
// Close html tag
message += "</html>";
// Show message to user
showInfo(message, "Info");
}
findCarByMakeDialog.setVisible(false);
}
/**
* Handles opening reset car park dialog
*/
public void openResetCarparkDialog() {
// Get number of cars in carpark
int numberOfCarsParked = this.carPark.getParkingSpots().size() - this.carPark.getNoUnoccupiedSpots();
// Initialize message
String message = numberOfCarsParked + " car(s) is being removed to reset the carpark.";
// Open reset car park dialog
resetCarParkDialog = new ActionDialog(this, frame, "Reset carpark", "Reset", message); // Create dialog
resetCarParkDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
resetCarParkDialog.setVisible(true); // Show dialog
}
/**
* Handles resetting car park
*/
public void resetCarparkHandler() {
// Reset carpark model
this.carPark.resetCarPark();
// Update carpark view
this.carparkView.resetParkingSpotViews();
// Close dialog
resetCarParkDialog.setVisible(false);
}
/**
* Handles opening reset car park dialog
*/
public void openExitProgramDialog() {
this.exitProgramDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
this.exitProgramDialog.setVisible(true);
}
/**
* Function to exit the program
*/
public void exitProgramHandler() {
System.exit(0); // Exit the program
}
/**
* Handles opening parking spot view dialog
*/
public void openParkingSpotViewActionDialog(ParkingSpotView source) {
this.parkingSpotViewActionDialog = new ParkingSpotViewActionDialog(this, frame, source);
this.parkingSpotViewActionDialog.setLocationRelativeTo(frame); // Center dialog in the middle of the frame
this.parkingSpotViewActionDialog.setVisible(true);
}
/**
* Helper function to create a message dialog.
*
* @param message message to inform user
* @param type type of message
*/
public void showInfo(String message, String type) {
MessageDialog messageDialog;
if (type.equals(MessageDialog.ERROR)) {
messageDialog = new MessageDialog(frame,
message, MessageDialog.ERROR);
} else {
messageDialog = new MessageDialog(frame,
message, MessageDialog.INFO);
}
messageDialog.setLocationRelativeTo(frame);
messageDialog.setVisible(true);
}
/**
* Main function to run the application
*
* @param args arguments passed to application at execution
*/
public static void main(String[] args) {
new CarparkSystem(); // Start GUI
}
}