-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
380 lines (315 loc) · 13.2 KB
/
Main.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
368
369
370
371
372
373
374
375
376
377
378
379
380
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
// Base Vehicle class
class Vehicle {
private String licensePlate;
private String model;
private int year;
public Vehicle(String licensePlate, String model, int year) {
this.licensePlate = licensePlate;
this.model = model;
this.year = year;
}
public String getLicensePlate() {
return licensePlate;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public String displayInfo() {
return "License Plate: " + licensePlate + ", Model: " + model + ", Year: " + year;
}
public String getType() {
return "Vehicle";
}
}
// Subclass for Car
class Car extends Vehicle {
private int doors;
public Car(String licensePlate, String model, int year, int doors) {
super(licensePlate, model, year);
this.doors = doors;
}
@Override
public String displayInfo() {
return super.displayInfo() + ", Doors: " + doors;
}
@Override
public String getType() {
return "Car";
}
}
// Subclass for Motorcycle
class Motorcycle extends Vehicle {
private boolean hasSidecar;
public Motorcycle(String licensePlate, String model, int year, boolean hasSidecar) {
super(licensePlate, model, year);
this.hasSidecar = hasSidecar;
}
@Override
public String displayInfo() {
return super.displayInfo() + ", Has Sidecar: " + (hasSidecar ? "Yes" : "No");
}
@Override
public String getType() {
return "Motorcycle";
}
}
public class Main {
private JFrame frame;
private JPanel vehicleCardsPanel;
private ArrayList<Vehicle> vehicles;
public Main() {
vehicles = new ArrayList<>();
preloadVehicles(); // Add initial vehicles
setupMainGUI();
}
private void preloadVehicles() {
vehicles.add(new Car("ABC123", "Toyota Corolla", 2020, 4));
vehicles.add(new Motorcycle("XYZ789", "Harley Davidson", 2019, false));
vehicles.add(new Car("DEF456", "Honda Civic", 2021, 2));
}
private void setupMainGUI() {
frame = new JFrame("Car Dealership - Main");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLayout(new BorderLayout());
// Top Menu Bar
JMenuBar menuBar = new JMenuBar();
JMenu actionsMenu = new JMenu("Actions");
JMenuItem addVehicleItem = new JMenuItem("Add Vehicle");
JMenuItem searchVehicleItem = new JMenuItem("Search Vehicle");
JMenuItem filterVehicleItem = new JMenuItem("Filter Vehicles");
JMenuItem viewAllVehiclesItem = new JMenuItem("View All Vehicles");
JMenuItem exitItem = new JMenuItem("Exit");
actionsMenu.add(addVehicleItem);
actionsMenu.add(searchVehicleItem);
actionsMenu.add(filterVehicleItem);
actionsMenu.add(viewAllVehiclesItem);
actionsMenu.add(exitItem);
menuBar.add(actionsMenu);
frame.setJMenuBar(menuBar);
// Center Panel: Vehicle Cards
vehicleCardsPanel = new JPanel();
vehicleCardsPanel.setLayout(new BoxLayout(vehicleCardsPanel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(vehicleCardsPanel);
scrollPane.setBorder(BorderFactory.createTitledBorder("Saved Vehicles"));
frame.add(scrollPane, BorderLayout.CENTER);
// Action Listeners for Menu Items
addVehicleItem.addActionListener(e -> openAddVehicleGUI());
searchVehicleItem.addActionListener(e -> openSearchVehicleGUI());
filterVehicleItem.addActionListener(e -> openFilterVehicleGUI());
viewAllVehiclesItem.addActionListener(e -> displayAllVehicles());
exitItem.addActionListener(e -> System.exit(0));
// Display Preloaded Vehicles
displayAllVehicles();
frame.setVisible(true);
}
private void openAddVehicleGUI() {
JFrame addFrame = new JFrame("Add Vehicle");
addFrame.setSize(400, 300);
addFrame.setLayout(new GridLayout(6, 2));
JTextField licensePlateField = new JTextField();
JTextField modelField = new JTextField();
JTextField yearField = new JTextField();
JTextField doorsField = new JTextField();
JTextField sidecarField = new JTextField();
addFrame.add(new JLabel("License Plate:"));
addFrame.add(licensePlateField);
addFrame.add(new JLabel("Model:"));
addFrame.add(modelField);
addFrame.add(new JLabel("Year:"));
addFrame.add(yearField);
addFrame.add(new JLabel("Doors (Car Only):"));
addFrame.add(doorsField);
addFrame.add(new JLabel("Has Sidecar (Motorcycle Only, true/false):"));
addFrame.add(sidecarField);
JButton addCarButton = new JButton("Add Car");
JButton addMotorcycleButton = new JButton("Add Motorcycle");
addFrame.add(addCarButton);
addFrame.add(addMotorcycleButton);
addCarButton.addActionListener(e -> {
try {
String licensePlate = licensePlateField.getText();
String model = modelField.getText();
int year = Integer.parseInt(yearField.getText());
int doors = Integer.parseInt(doorsField.getText());
vehicles.add(new Car(licensePlate, model, year, doors));
displayAllVehicles();
addFrame.dispose();
} catch (Exception ex) {
JOptionPane.showMessageDialog(addFrame, "Invalid input. Please check the values.");
}
});
addMotorcycleButton.addActionListener(e -> {
try {
String licensePlate = licensePlateField.getText();
String model = modelField.getText();
int year = Integer.parseInt(yearField.getText());
boolean hasSidecar = Boolean.parseBoolean(sidecarField.getText());
vehicles.add(new Motorcycle(licensePlate, model, year, hasSidecar));
displayAllVehicles();
addFrame.dispose();
} catch (Exception ex) {
JOptionPane.showMessageDialog(addFrame, "Invalid input. Please check the values.");
}
});
addFrame.setVisible(true);
}
private void openSearchVehicleGUI() {
JFrame searchFrame = new JFrame("Search Vehicle");
searchFrame.setSize(440, 200);
searchFrame.setLocationRelativeTo(null); // Center the frame
searchFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Use GridBagLayout for better alignment
searchFrame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10); // Add padding around components
gbc.anchor = GridBagConstraints.WEST;
// Components
JLabel instructionLabel = new JLabel("Enter License Plate:");
JTextField searchField = new JTextField(20); // Default width
searchField.setPreferredSize(new Dimension(250, 25)); // Set preferred size for input field
JButton searchButton = new JButton("Search");
JLabel resultLabel = new JLabel("Result: ");
// Style components
instructionLabel.setFont(new Font("Arial", Font.BOLD, 14));
searchButton.setFont(new Font("Arial", Font.PLAIN, 12));
searchButton.setBackground(new Color(60, 120, 180)); // Stylish blue button
searchButton.setForeground(Color.WHITE);
searchButton.setFocusPainted(false);
resultLabel.setFont(new Font("Arial", Font.PLAIN, 12));
// Layout constraints for components
// Add instruction label in row 0
gbc.gridx = 0;
gbc.gridy = 0;
searchFrame.add(instructionLabel, gbc);
// Add search field in row 0, column 1 (same row as the label)
gbc.gridx = 1;
searchFrame.add(searchField, gbc);
// Add search button in row 1, column 0 (below the label)
gbc.gridx = 0;
gbc.gridy = 1;
searchFrame.add(searchButton, gbc);
// Add result label in row 2, column 0 (below the search button)
gbc.gridx = 0;
gbc.gridy = 2;
searchFrame.add(resultLabel, gbc);
// Search button action listener
searchButton.addActionListener(e -> {
String licensePlate = searchField.getText().trim();
for (Vehicle vehicle : vehicles) {
if (vehicle.getLicensePlate().equalsIgnoreCase(licensePlate)) {
resultLabel.setText("Result: " + vehicle.displayInfo());
return;
}
}
resultLabel.setText("Result: Not Found");
});
searchFrame.setVisible(true);
}
private void openFilterVehicleGUI() {
JFrame filterFrame = new JFrame("Filter Vehicles");
filterFrame.setSize(400, 200);
filterFrame.setLocationRelativeTo(null); // Center the frame
filterFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Use GridBagLayout for better alignment
filterFrame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10); // Add some padding around components
gbc.anchor = GridBagConstraints.WEST;
// Components
JLabel filterLabel = new JLabel("Select Type:");
JComboBox<String> filterComboBox = new JComboBox<>(new String[]{"All", "Car", "Motorcycle"});
JButton filterButton = new JButton("Filter");
// Style components
filterLabel.setFont(new Font("Arial", Font.BOLD, 14));
filterButton.setFont(new Font("Arial", Font.PLAIN, 12));
filterButton.setBackground(new Color(60, 120, 180)); // Stylish blue button
filterButton.setForeground(Color.WHITE);
filterButton.setFocusPainted(false);
// Layout constraints for components
gbc.gridx = 0;
gbc.gridy = 0;
filterFrame.add(filterLabel, gbc);
gbc.gridx = 1;
filterFrame.add(filterComboBox, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
filterFrame.add(filterButton, gbc);
// Filter button action listener
filterButton.addActionListener(e -> {
String filterType = (String) filterComboBox.getSelectedItem();
vehicleCardsPanel.removeAll();
for (Vehicle vehicle : vehicles) {
if (filterType.equals("All") || vehicle.getType().equals(filterType)) {
addVehicleCard(vehicle); // Assuming this method handles adding a vehicle card to the panel
}
}
vehicleCardsPanel.revalidate();
vehicleCardsPanel.repaint();
filterFrame.dispose();
});
filterFrame.setVisible(true);
}
private void displayAllVehicles() {
vehicleCardsPanel.removeAll();
for (Vehicle vehicle : vehicles) {
addVehicleCard(vehicle);
}
vehicleCardsPanel.revalidate();
vehicleCardsPanel.repaint();
}
private void addVehicleCard(Vehicle vehicle) {
// Create a card panel with a BorderLayout for better control of positioning
JPanel card = new JPanel(new GridBagLayout());
card.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
card.setBackground(new Color(245, 245, 245)); // Light grey background for the card
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10); // Padding between components
gbc.anchor = GridBagConstraints.WEST;
// Title label for the vehicle (display license plate and model)
JLabel titleLabel = new JLabel(vehicle.getModel() + " (" + vehicle.getLicensePlate() + ")");
titleLabel.setFont(new Font("Arial", Font.BOLD, 16));
titleLabel.setForeground(new Color(60, 60, 60)); // Dark text color
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
card.add(titleLabel, gbc);
// Vehicle year label
JLabel yearLabel = new JLabel("Year: " + vehicle.getYear());
yearLabel.setFont(new Font("Arial", Font.PLAIN, 14));
yearLabel.setForeground(new Color(80, 80, 80));
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
card.add(yearLabel, gbc);
// Vehicle type label (Car, Motorcycle, etc.)
JLabel typeLabel = new JLabel("Type: " + vehicle.getType());
typeLabel.setFont(new Font("Arial", Font.PLAIN, 14));
typeLabel.setForeground(new Color(80, 80, 80));
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
card.add(typeLabel, gbc);
// Vehicle specific details (Car doors or Motorcycle sidecar)
JLabel detailsLabel = new JLabel(vehicle.displayInfo());
detailsLabel.setFont(new Font("Arial", Font.PLAIN, 12));
detailsLabel.setForeground(new Color(100, 100, 100));
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
card.add(detailsLabel, gbc);
// Add the card to the panel
vehicleCardsPanel.add(card);
}
public static void main(String[] args) {
new Main();
}
}