{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloController.java
More file actions
648 lines (435 loc) · 18 KB
/
Copy pathHelloController.java
File metadata and controls
648 lines (435 loc) · 18 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package com.hammad.jfx1;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class HelloController implements Initializable {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
public HelloController() throws SQLException {
}
//add user fxml
@FXML
private TextField email;
@FXML
private TextField fname;
@FXML
private TextField license;
@FXML
private TextField lname;
@FXML
private TextField phone;
//add car fxml
@FXML
private ChoiceBox<String> addCarMake;
private String[] carMakes = {"Toyota","KIA","Hyundai","Honda", "Nissan", "Ford", "Mitsubishi", "Mazda"};
@FXML
private ChoiceBox<String> carYear;
private String[] years = {"2015","2016","2017","2018","2019","2020","2021","2022"};
@FXML
private TextField addCarModel;
@FXML
private TextField addPlateNo;
@FXML
private RadioButton petrolRadio;
@FXML
private RadioButton dieselRadio;
@FXML
private TextField addCarCost;
//rent fxml
@FXML
private DatePicker rentDate;
//view rented cars
@FXML
private ChoiceBox<String> dropDownCarPlate;
@FXML
private ChoiceBox<String> rentedLicenseDropDown;
@FXML
private TextField daysRentedInput;
@FXML
private Label resultLabel;
@FXML
private TextField rentedFullName;
//rent out the car
@FXML
private ChoiceBox<String> rentCarPlateDropDown;
@FXML
private ChoiceBox<String> rentUserLicenseDropDown;
String carPlate = "";
double result = 0;
@FXML
void calculate(ActionEvent event) throws SQLException {
double daysRented = Double.parseDouble(daysRentedInput.getText());
double costPerDay= 0;
carPlate = dropDownCarPlate.getValue();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
ObservableList<String> list = FXCollections.observableArrayList();
PreparedStatement stmt = con.prepareStatement("select COST_PER_DAY from cars " +
"where CAR_PLATE_NO = '"+carPlate+"';");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
costPerDay = rs.getDouble("COST_PER_DAY");
}
result = daysRented * costPerDay;
resultLabel.setText(Double.toString(result));
}
@FXML
void updateValues(MouseEvent mouseEvent){
carPlate = dropDownCarPlate.getValue();
}
@FXML
void createInvoice(ActionEvent event) throws IOException, SQLException {
String fullName = rentedFullName.getText();
String license = rentedLicenseDropDown.getValue();
String plate = dropDownCarPlate.getValue();
String make="";
String model="";
String daysRented = daysRentedInput.getText();
String invoiceResult = Double.toString(result);
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
PreparedStatement stmt = con.prepareStatement("select MAKE from cars " +
"where CAR_PLATE_NO = '"+dropDownCarPlate.getValue()+"';");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
make = rs.getString("MAKE");
}
PreparedStatement stmt1 = con.prepareStatement("select MODEL from cars " +
"where CAR_PLATE_NO = '"+dropDownCarPlate.getValue()+"';");
ResultSet rs1 = stmt1.executeQuery();
while(rs1.next()){
model = rs1.getString("MODEL");
}
GenerateInvoice generateInvoice = new GenerateInvoice(fullName,license,plate, make, model,daysRented,invoiceResult);
generateInvoice.generateBill();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Success");
alert.setHeaderText("Invoice Generated");
alert.show();
File file = new File("C:\\Users\\Hammad\\Desktop\\CAT201 PROJECT\\invoice.pdf");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
}
@FXML
void addUser(ActionEvent event) {
try {
PreparedStatement stmt = con.prepareStatement("insert into users values(?,?,?,?,?);");
stmt.setString(1, fname.getText());
stmt.setString(2, lname.getText());
stmt.setString(3, email.getText());
stmt.setString(4, phone.getText());
stmt.setString(5, license.getText());
stmt.execute();
stmt.close();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Record Added");
alert.setHeaderText("New user added");
alert.show();
} catch (SQLException e) {
e.printStackTrace();
Alert error = new Alert(Alert.AlertType.ERROR);
error.setTitle("Database Error");
error.setHeaderText(e.getMessage());
error.show();
}
}
@FXML
void addCar(ActionEvent event) {
try {
String fuelType = "";
if (petrolRadio.isSelected()){
fuelType = petrolRadio.getText();
}
if (dieselRadio.isSelected()){
fuelType = dieselRadio.getText();
}
PreparedStatement stmt = con.prepareStatement("insert into cars values(?,?,?,?,?,?);");
stmt.setString(1, addCarMake.getValue());
stmt.setString(2,addCarModel.getText());
stmt.setString(3,carYear.getValue());
stmt.setString(4,addPlateNo.getText());
stmt.setString(5,fuelType);
stmt.setDouble(6,Double.parseDouble(addCarCost.getText()));
stmt.execute();
stmt.close();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Record Added");
alert.setHeaderText("New car added");
alert.show();
} catch (SQLException e) {
e.printStackTrace();
Alert error = new Alert(Alert.AlertType.ERROR);
error.setTitle("Database Error");
error.setHeaderText(e.getMessage());
error.show();
}
}
@FXML
void addRent(ActionEvent event) {
try {
LocalDate myDate = rentDate.getValue();
String formattedDate = myDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(formattedDate);
PreparedStatement rentStmt = con.prepareStatement("insert into rent_car values(?,?,?);");
rentStmt.setString(1, rentUserLicenseDropDown.getValue());
rentStmt.setString(2,rentCarPlateDropDown.getValue());
rentStmt.setString(3,formattedDate);
rentStmt.execute();
rentStmt.close();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Record Added");
alert.setHeaderText("Rent added");
alert.show();
}catch (SQLIntegrityConstraintViolationException se){
Alert error = new Alert(Alert.AlertType.ERROR);
error.setTitle("Error: Referential Integrity Violation");
error.setHeaderText("Please add user/car into database first");
error.show();
} catch (SQLException e) {
e.printStackTrace();
Alert error = new Alert(Alert.AlertType.ERROR);
error.setTitle("Database Error");
error.setHeaderText(e.getMessage());
error.show();
}
}
//Show cars
@FXML
private TableView<cars> showTableCars;
@FXML
private TableColumn<cars, String> CarMake;
@FXML
private TableColumn<cars, String> CarModel;
@FXML
private TableColumn<cars, String> CarYear;
@FXML
private TableColumn<cars, String> CarPlate;
@FXML
private TableColumn<cars, String> fuelTypeCol;
@FXML
private TableColumn<cars, Double> cost24Col;
//Show Users
@FXML
private TableView<Users> usersTable;
@FXML
private TableColumn<Users, String> fnameCol;
@FXML
private TableColumn<Users, String> lnameCol;
@FXML
private TableColumn<Users, String> emailCol;
@FXML
private TableColumn<Users, String> phoneCol;
@FXML
private TableColumn<Users, String> licenseCol;
//Rent Table
@FXML
private TableView<Rent> rentTable;
@FXML
private TableColumn<Rent, String>rentFnameCol;
@FXML
private TableColumn<Rent, String>rentLnameCol;
@FXML
private TableColumn<Rent, String>rentLicenseCol;
@FXML
private TableColumn<Rent, String>rentCarMakeCol;
@FXML
private TableColumn<Rent, String>rentCarModelCol;
@FXML
private TableColumn<Rent, String>rentCarPlateCol;
@FXML
private TableColumn<Rent, String>rentDaysRentedCol;
static ObservableList<String> plates;
public static ObservableList<String> getDataPlates() throws SQLException {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
ObservableList<String> list = FXCollections.observableArrayList();
PreparedStatement stmt = con.prepareStatement("select distinct car_plate_no from cars;");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
list.add(rs.getString("CAR_PLATE_NO"));
}
return list;
}
static ObservableList<String> licenses;
public static ObservableList<String> getDataLicenses() throws SQLException {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
ObservableList<String> list = FXCollections.observableArrayList();
PreparedStatement stmt = con.prepareStatement("select distinct LICENSE_NO from users;");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
list.add(rs.getString("LICENSE_NO"));
}
return list;
}
static ObservableList<String> rentedPlates;
public static ObservableList<String> getDataRentedPlates() throws SQLException{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
ObservableList<String> list = FXCollections.observableArrayList();
PreparedStatement stmt = con.prepareStatement("select distinct car_plate_no from rent_car;");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
list.add(rs.getString("CAR_PLATE_NO"));
}
return list;
}
static ObservableList<String> rentedLicenses;
public ObservableList<String> getDataRentedLicense() throws SQLException{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
ObservableList<String> list = FXCollections.observableArrayList();
PreparedStatement stmt = con.prepareStatement("select LICENSE_NO from rent_car " +
"where CAR_PLATE_NO = '"+carPlate+"';");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
list.add(rs.getString("LICENSE_NO"));
}
return list;
}
ObservableList<cars> listM;
public static ObservableList<cars> getDataCars() throws SQLException {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
ObservableList<cars> list = FXCollections.observableArrayList();
PreparedStatement stmt = con.prepareStatement("select * from cars");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
list.add(new cars(rs.getString("make"), rs.getString("model"), rs.getString("make_year"), rs.getString("car_plate_no"), rs.getString("fuel_type"),rs.getDouble("COST_PER_DAY")));
}
return list;
}
ObservableList<Users> listUsers;
public static ObservableList<Users> getDataUsers() throws SQLException {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
ObservableList<Users> userList = FXCollections.observableArrayList();
PreparedStatement stmt = con.prepareStatement("select * from users");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
userList.add(new Users(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)));
}
return userList;
}
ObservableList<Rent> listRent;
public static ObservableList<Rent> getDataRent() throws SQLException{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectdb","root","root");
ObservableList<Rent> rentList = FXCollections.observableArrayList();
PreparedStatement stmt = con.prepareStatement("""
SELECT FNAME, LNAME, RENT_CAR.LICENSE_NO, MAKE, MODEL,RENT_CAR.CAR_PLATE_NO,DATEDIFF(CAST( SYSDATE() AS Date ), RENT_CAR.RENT_DATE)
FROM CARS JOIN RENT_CAR JOIN USERS
WHERE USERS.LICENSE_NO = RENT_CAR.LICENSE_NO
AND\s
CARS.CAR_PLATE_NO = RENT_CAR.CAR_PLATE_NO;""");
ResultSet rs = stmt.executeQuery();
while (rs.next()){
rentList.add(new Rent(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5) , rs.getString(6) , rs.getString(7)));
}
return rentList;
}
@FXML
void showCarsAdded(ActionEvent event) throws SQLException {
listM = getDataCars();
showTableCars.setItems(listM);
}
@FXML
void showUsersAdded(ActionEvent event) throws SQLException{
listUsers = getDataUsers();
usersTable.setItems(listUsers);
}
@FXML
void showRent(MouseEvent event) throws SQLException {
listRent = getDataRent();
rentTable.setItems(listRent);
}
@FXML
void showRentCarPlates(MouseEvent event) throws SQLException{
plates = getDataPlates();
rentCarPlateDropDown.setItems(plates);
}
@FXML
void showRentUserLicense(MouseEvent event) throws SQLException{
licenses = getDataLicenses();
rentUserLicenseDropDown.setItems(licenses);
}
@FXML
void showRentedCarPlates(MouseEvent mouseEvent) throws SQLException{
rentedPlates = getDataRentedPlates();
dropDownCarPlate.setItems(rentedPlates);
}
@FXML
void showRentedLicenseDropDown(MouseEvent event) throws SQLException{
rentedLicenses = getDataRentedLicense();
rentedLicenseDropDown.setItems(rentedLicenses);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
try {
plates = getDataPlates();
rentCarPlateDropDown.setItems(plates);
} catch (SQLException e) {
e.printStackTrace();
}
try {
licenses = getDataLicenses();
rentUserLicenseDropDown.setItems(licenses);
} catch (SQLException e) {
e.printStackTrace();
}
try{
rentedLicenses = getDataRentedLicense();
rentedLicenseDropDown.setItems(rentedLicenses);
} catch (SQLException e) {
e.printStackTrace();
}
try{
rentedPlates = getDataRentedPlates();
dropDownCarPlate.setItems(rentedPlates);
} catch (SQLException e) {
e.printStackTrace();
}
addCarMake.getItems().addAll(carMakes);
carYear.getItems().addAll(years);
CarMake.setCellValueFactory(new PropertyValueFactory<cars, String>("make"));
CarModel.setCellValueFactory(new PropertyValueFactory<cars, String>("model"));
CarYear.setCellValueFactory(new PropertyValueFactory<cars, String>("year"));
CarPlate.setCellValueFactory(new PropertyValueFactory<cars, String>("plate"));
fuelTypeCol.setCellValueFactory(new PropertyValueFactory<cars, String>("fuelType"));
cost24Col.setCellValueFactory(new PropertyValueFactory<cars, Double>("cost24hrs"));
try {
listM = getDataCars();
showTableCars.setItems(listM);
} catch (SQLException e) {
e.printStackTrace();
}
fnameCol.setCellValueFactory(new PropertyValueFactory<Users, String>("fname"));
lnameCol.setCellValueFactory(new PropertyValueFactory<Users, String>("lname"));
emailCol.setCellValueFactory(new PropertyValueFactory<Users, String>("email"));
phoneCol.setCellValueFactory(new PropertyValueFactory<Users, String>("phone"));
licenseCol.setCellValueFactory(new PropertyValueFactory<Users, String>("license"));
try{
listUsers = getDataUsers();
usersTable.setItems(listUsers);
} catch (SQLException e) {
e.printStackTrace();
}
rentFnameCol.setCellValueFactory(new PropertyValueFactory<Rent,String>("rentFname"));
rentLnameCol.setCellValueFactory(new PropertyValueFactory<Rent,String>("rentLname"));
rentLicenseCol.setCellValueFactory(new PropertyValueFactory<Rent, String>("rentLicense"));
rentCarMakeCol.setCellValueFactory(new PropertyValueFactory<Rent, String>("rentCarMake"));
rentCarModelCol.setCellValueFactory(new PropertyValueFactory<Rent, String>("rentCarModel"));
rentCarPlateCol.setCellValueFactory(new PropertyValueFactory<Rent, String>("rentCarPlate"));
rentDaysRentedCol.setCellValueFactory(new PropertyValueFactory<Rent, String>("rentDaysRented"));
try{
listRent = getDataRent();
rentTable.setItems(listRent);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
You can’t perform that action at this time.
