-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParkCarDialog.java
More file actions
135 lines (123 loc) · 5.73 KB
/
Copy pathParkCarDialog.java
File metadata and controls
135 lines (123 loc) · 5.73 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
import javax.swing.*;
import java.awt.*;
/**
* Project 2: ParkCarDialog class extends JDialog.
* Dialog component for parking a car command.
* Stores parent frame, action buttons, form labels and inputs.
*
* @author Abdurrahman Faqih 104675143
* @version 0.1 9 May 2024
*/
public class ParkCarDialog extends JDialog {
private CarparkSystem controller; // Store reference to controller
private JFrame parent; // Store reference to parent frame
// Initialize dialog components
private JButton parkButton = new JButton("Park car");
private JButton closeButton = new JButton("Cancel");
private JLabel spotIdLabel = new JLabel("Spot ID: ");
private JLabel carRegLabel = new JLabel("Car registration number: ");
private JLabel carMakeLabel = new JLabel("Car make: ");
private JLabel carModelLabel = new JLabel("Car model: ");
private JLabel carYearLabel = new JLabel("Car year: ");
private JTextField spotIdTextField;
private JTextField carRegTextField = new JTextField(10);
private JTextField carMakeTextField = new JTextField(10);
private JTextField carModelTextField = new JTextField(10);
private JTextField carYearTextField = new JTextField(10);
public ParkCarDialog(CarparkSystem controller, JFrame parent, String spotId) {
super(parent, "Park a car", true);
this.controller = controller;
this.parent = parent;
// Initialize spot ID text field
spotIdTextField = new JTextField(spotId, 10);
// Set layout of form
setLayout(new GridLayout(0, 2));
add(this.spotIdLabel);
add(this.spotIdTextField);
add(this.carRegLabel);
add(this.carRegTextField);
add(this.carMakeLabel);
add(this.carMakeTextField);
add(this.carModelLabel);
add(this.carModelTextField);
add(this.carYearLabel);
add(this.carYearTextField);
this.parkButton.addActionListener(e -> this.parkCarListener());
add(this.parkButton);
this.closeButton.addActionListener(e -> this.setVisible(false));
add(this.closeButton);
this.pack();
}
/**
* Handles park car button click. Gets text input into text field by user
* and validates the input. If there is an input error, the error dialog is
* shown.
*/
public void parkCarListener() {
// Extract values
String userInputId = this.spotIdTextField.getText();
String carRegNo = this.carRegTextField.getText();
String carMake = this.carMakeTextField.getText();
String carModel = this.carModelTextField.getText();
String carYear = this.carYearTextField.getText();
// Validate user inputs
try {
boolean isIdValid = Validators.validateParkingSpotId(userInputId); // Validate parking spot ID
if (!isIdValid) {
MessageDialog messageDialog = new MessageDialog(this.parent,
"Parking spot ID must be an uppercase letter followed by 3 digits.", MessageDialog.ERROR);
messageDialog.setLocationRelativeTo(this.parent);
messageDialog.setVisible(true);
return;
}
boolean isCarRegValid = Validators.validateCarRegNo(carRegNo); // Validate car reg number
if (!isCarRegValid) {
MessageDialog messageDialog = new MessageDialog(this.parent,
"Invalid car registration number. Car registration number must be an uppercase letter followed by 4 digits.",
MessageDialog.ERROR);
messageDialog.setLocationRelativeTo(this.parent);
messageDialog.setVisible(true);
return;
}
if (carMake.isEmpty()) {
MessageDialog messageDialog = new MessageDialog(this.parent,
"Car make cannot be empty.", MessageDialog.ERROR);
messageDialog.setLocationRelativeTo(this.parent);
messageDialog.setVisible(true);
return;
}
if (carModel.isEmpty()) {
MessageDialog messageDialog = new MessageDialog(this.parent,
"Car model cannot be empty.", MessageDialog.ERROR);
messageDialog.setLocationRelativeTo(this.parent);
messageDialog.setVisible(true);
return;
}
int carYearInt;
carYearInt = Integer.parseInt(carYear); // Parse car year into integer
boolean isCarYearValid = Validators.validateCarYear(carYearInt); // Validate car year
if (!isCarYearValid) {
MessageDialog messageDialog = new MessageDialog(this.parent,
"Invalid car year. Car year must be an integer between 2004-2024.", MessageDialog.ERROR);
messageDialog.setLocationRelativeTo(this.parent);
messageDialog.setVisible(true);
return;
}
// Create car
Car car = new Car(carRegNo, carMake, carModel, carYearInt);
this.controller.parkCarHandler(userInputId, car);
// Reset fields
this.spotIdTextField.setText("");
this.carRegTextField.setText("");
this.carModelTextField.setText("");
this.carMakeTextField.setText("");
this.carYearTextField.setText("");
this.spotIdTextField.grabFocus();
} catch (NumberFormatException err) {
MessageDialog messageDialog = new MessageDialog(this.parent,
"Invalid car year. Car year must be an integer between 2004-2024.", MessageDialog.ERROR);
messageDialog.setLocationRelativeTo(this.parent);
messageDialog.setVisible(true);
}
}
}