-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarCompany.java
424 lines (376 loc) · 16.1 KB
/
CarCompany.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
/**
* The CarCompany class is used to buy and rent cars from a car company. This class has twenty attributes.
* @author 16033148, Keyur Khadka
* @version 9th April 2017
*/
public class CarCompany implements ActionListener
{
private JFrame frame;
private JTextField txtPrice;
private JTextField txtYear;
private JTextField txtMileage;
private JTextField txtDescription;
private JTextField txtAdminFee;
private JTextField txtDailyRate;
private JTextField txtCustomerName;
private JTextField txtRentalDate;
private JTextField txtReturnDate;
private JTextField txtNumberOfDays;
private JTextField txtCarNumber;
private JButton btnCarToBuy;
private JButton btnCarToRent;
private JButton btnBuyCar;
private JButton btnRentCar;
private JButton btnReturnCar;
private JButton btnDisplayAll;
private JButton btnClear;
private ArrayList <Car> list;
/**
* Constructor with no parameter. All the components for the GUI is created here.
*/
public CarCompany()
{
frame = new JFrame("Car Company");
list = new ArrayList<>();
JLabel lblPrice = new JLabel("Price: ");
lblPrice.setBounds(0,0,150,30);
frame.add(lblPrice);
txtPrice = new JTextField();
txtPrice.setBounds(0,30,150,30);
frame.add(txtPrice);
JLabel lblYear = new JLabel("Year: ");
lblYear.setBounds(150,0,150,30);
frame.add(lblYear);
txtYear = new JTextField();
txtYear.setBounds(150,30,150,30);
frame.add(txtYear);
JLabel lblMileage = new JLabel("Mileage: ");
lblMileage.setBounds(300,0,150,30);
frame.add(lblMileage);
txtMileage = new JTextField();
txtMileage.setBounds(300,30,150,30);
frame.add(txtMileage);
btnCarToBuy = new JButton("Add Car To Buy");
btnCarToBuy.addActionListener(this);
btnCarToBuy.setBounds(450,30,150,30);
frame.add(btnCarToBuy);
JLabel lblDescription = new JLabel("Description: ");
lblDescription.setBounds(0,60,150,30);
frame.add(lblDescription);
txtDescription = new JTextField();
txtDescription.setBounds(0,90,150,30);
frame.add(txtDescription);
JLabel lblAdminFee = new JLabel("Admin Fee: ");
lblAdminFee.setBounds(150,60,150,30);
frame.add(lblAdminFee);
txtAdminFee = new JTextField();
txtAdminFee.setBounds(150,90,150,30);
frame.add(txtAdminFee);
JLabel lblDailyRate = new JLabel("Daily Rate: ");
lblDailyRate.setBounds(300,60,150,30);
frame.add(lblDailyRate);
txtDailyRate = new JTextField();
txtDailyRate.setBounds(300,90,150,30);
frame.add(txtDailyRate);
btnCarToRent = new JButton("Add Car To Rent");
btnCarToRent.addActionListener(this);
btnCarToRent.setBounds(450,90,150,30);
frame.add(btnCarToRent);
JLabel lblCustomerName = new JLabel("Customer Name: ");
lblCustomerName.setBounds(0,120,150,30);
frame.add(lblCustomerName);
txtCustomerName = new JTextField();
txtCustomerName.setBounds(0,150,150,30);
frame.add(txtCustomerName);
JLabel lblRentalDate = new JLabel("Rental Date: ");
lblRentalDate.setBounds(150,120,150,30);
frame.add(lblRentalDate);
txtRentalDate = new JTextField();
txtRentalDate.setBounds(150,150,150,30);
frame.add(txtRentalDate);
JLabel lblReturnDate = new JLabel("Return Date: ");
lblReturnDate.setBounds(300,120,150,30);
frame.add(lblReturnDate);
txtReturnDate = new JTextField();
txtReturnDate.setBounds(300,150,150,30);
frame.add(txtReturnDate);
JLabel lblNumberOfDays = new JLabel("Number Of Days: ");
lblNumberOfDays.setBounds(450,120,150,30);
frame.add(lblNumberOfDays);
txtNumberOfDays = new JTextField();
txtNumberOfDays.setBounds(450,150,150,30);
frame.add(txtNumberOfDays);
JLabel lblCarNumber = new JLabel("Car Number: ");
lblCarNumber.setBounds(0,180,150,30);
frame.add(lblCarNumber);
txtCarNumber = new JTextField();
txtCarNumber.setBounds(0,220,150,30);
frame.add(txtCarNumber);
btnBuyCar = new JButton("Buy Car");
btnBuyCar.addActionListener(this);
btnBuyCar.setBounds(150,220,150,30);
frame.add(btnBuyCar);
btnRentCar = new JButton("Rent Car");
btnRentCar.addActionListener(this);
btnRentCar.setBounds(300,220,150,30);
frame.add(btnRentCar);
btnReturnCar = new JButton("Return Car");
btnReturnCar.addActionListener(this);
btnReturnCar.setBounds(450,220,150,30);
frame.add(btnReturnCar);
btnDisplayAll = new JButton("Display All");
btnDisplayAll.addActionListener(this);
btnDisplayAll.setBounds(0,280,300,30);
frame.add(btnDisplayAll);
btnClear = new JButton("Clear");
btnClear.addActionListener(this);
btnClear.setBounds(300,280,300,30);
frame.add(btnClear);
frame.setLayout(null);
frame.setSize(650,400);
frame.setVisible(true);
}
/**
* Accessor Method to get the car number. Exception handling is done by try/catch statement.
* The methods return type is void and access modifier is public.
* This method checks if user input number is inside array index. If yes, carNumber is returned else error message pops up.
* -1 is returned at last.
*/
public int getCarNumber()
{
int carNumber = -1;
try {
carNumber = Integer.parseInt(txtCarNumber.getText());
if(carNumber > -1 && carNumber < list.size()) {
return carNumber;
}
else {
JOptionPane.showMessageDialog(frame, "Invalid car number", "Error", JOptionPane.ERROR_MESSAGE);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Please enter a valid car number", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
return -1;
}
/**
* Method to add car to buy. Exception handling is done by try/catch statement.
* The methods return type is void and access modifier is public.
* This method checks if description, price, year and mileage are empty or 0.
* If not, object of CarToBuy is added to arraylist else error message pops up.
*/
public void addCarToBuy()
{
try {
String description = txtDescription.getText();
int price = Integer.parseInt(txtPrice.getText());
int year = Integer.parseInt(txtYear.getText());
int mileage = Integer.parseInt(txtMileage.getText());
if(price > 0 && year > 0 && mileage > 0 && !description.isEmpty()) {
list.add(new CarToBuy(description, price, year, mileage));
JOptionPane.showMessageDialog(frame, "Car successfully added for buying", "Car Added", JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(frame, "Invalid input because Integer is 0 or less and/or string is empty", "Error", JOptionPane.ERROR_MESSAGE);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Please enter appropriate required values", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Method to add car to rent. Exception handling is done by try/catch statement.
* The methods return type is void and access modifier is public.
* This method checks if description, adminFee and dailyRate are empty or 0.
* If not, object of CarToRent is added to arraylist else error message pops up.
*/
public void addCarToRent()
{
try {
String description = txtDescription.getText();
int adminFee = Integer.parseInt(txtAdminFee.getText());
int dailyRate = Integer.parseInt(txtDailyRate.getText());
if(!description.isEmpty() && adminFee > 0 && dailyRate > 0) {
list.add(new CarToRent(description, adminFee, dailyRate));
JOptionPane.showMessageDialog(frame, "Car successfully added for renting", "Car Added", JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(frame, "Invalid input because Integer is 0 or less and/or string is empty", "Error", JOptionPane.ERROR_MESSAGE);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Please enter appropriate required values", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Method to buy the car. Exception handling is done by try/catch statement.
* The methods return type is void and access modifier is public.
* This method checks if carNumber is within array range or not.
* If yes, it again checks if carNumber is an instance of CarToBuy. If yes, type casting is done and buy method from CarToBuy is called.
* Else error message pops up.
*/
public void buyCar()
{
try {
if(getCarNumber() > -1) {
String customerName = txtCustomerName.getText();
if(!customerName.isEmpty()) {
if(list.get(getCarNumber()) instanceof CarToBuy) {
CarToBuy carToBuyObj = (CarToBuy) list.get(getCarNumber());
carToBuyObj.buy(customerName);
}
else {
JOptionPane.showMessageDialog(frame, "There is no car for buying", "Error", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(frame, "Cannot buy car because input is not as required", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Please enter appropriate required values", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Method to rent the car. Exception handling is done by try/catch statement.
* The methods return type is void and access modifier is public.
* This method checks if carNumber is within array range or not.
* If yes, it checks if customerName, rentalDate, returnDate and numberOfDays are empty or 0.
* If yes, it again checks if carNumber is an instance of CarToRent. If yes, type casting is done and rentCar method from CarToRent is called.
* Else error message pops up.
*/
public void rentCar()
{
try {
if(getCarNumber() > -1) {
String customerName = txtCustomerName.getText();
String rentalDate = txtRentalDate.getText();
String returnDate = txtReturnDate.getText();
int numberOfDays = Integer.parseInt(txtNumberOfDays.getText());
if(!customerName.isEmpty() && !rentalDate.isEmpty() && !returnDate.isEmpty() && numberOfDays > 0) {
if(list.get(getCarNumber()) instanceof CarToRent) {
CarToRent carToRentObj = (CarToRent) list.get(getCarNumber());
carToRentObj.rentCar(customerName, rentalDate, returnDate, numberOfDays);
}
else {
JOptionPane.showMessageDialog(frame, "There is no car for renting", "Error", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(frame, "Unable to rent car because input is not as required", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Please enter appropriate required values", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Method to return the car. Exception handling is done by try/catch statement.
* The methods return type is void and access modifier is public.
* This method initializes carNumber equal to array range.
* If carNumber is not equal to -1, it checks if carNumber is an instance of CarToRent.
* If yes, type casting is done and returnCar method from CarToRent is called.
* Else error message pops up.
*/
public void returnCar()
{
try {
int carNumber = getCarNumber();
if(carNumber > -1) {
if(list.get(carNumber) instanceof CarToRent) {
CarToRent carToRentOb = (CarToRent) list.get(carNumber);
carToRentOb.returnCar();
}
else {
JOptionPane.showMessageDialog(frame, "Cannot return car because the car has not been rented", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Please enter appropriate required values", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Method to display all the details of the car.
* The methods return type is void and access modifier is public.
*/
public void displayAll()
{
for (Car c: list) {
System.out.println("Car Number: " +list.indexOf(c));
if(c instanceof CarToBuy) {
CarToBuy carToBuy = (CarToBuy) c;
carToBuy.display();
}
else if(c instanceof CarToRent) {
CarToRent carToRent = (CarToRent) c;
carToRent.display();
}
}
}
/**
* Method to clear all the texts from the textfield.
* The methods return type is void and access modifier is public.
*/
public void clear()
{
txtPrice.setText("");
txtYear.setText("");
txtMileage.setText("");
txtDescription.setText("");
txtAdminFee.setText("");
txtDailyRate.setText("");
txtCustomerName.setText("");
txtRentalDate.setText("");
txtReturnDate.setText("");
txtNumberOfDays.setText("");
txtCarNumber.setText("");
}
/**
* Method to override the method from Interface ActionListener class.
* The methods return type is void and access modifier is public.
* This method checks which button triggered the event. When a button triggers an event, the method corresponding to the button is called.
*/
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnCarToBuy) {
addCarToBuy();
}
else if (e.getSource() == btnCarToRent) {
addCarToRent();
}
else if (e.getSource() == btnBuyCar) {
buyCar();
}
else if (e.getSource() == btnRentCar) {
rentCar();
}
else if (e.getSource() == btnReturnCar) {
returnCar();
}
else if (e.getSource() == btnDisplayAll) {
displayAll();
}
else if (e.getSource() == btnClear) {
clear();
}
}
/**
* This is the main method. Constructor is called in this main method.
*/
public static void main(String[] args)
{
new CarCompany();
}
}