-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
640 lines (581 loc) · 26.9 KB
/
Main.java
File metadata and controls
640 lines (581 loc) · 26.9 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
import java.sql.*;
import java.util.*;
import java.io.*;
class DBConnection {
private static String url;
private static String user;
private static String password;
static {
try (InputStream input = new FileInputStream("config.properties")) {
Properties prop = new Properties();
prop.load(input);
url = prop.getProperty("db.url");
user = prop.getProperty("db.user");
password = prop.getProperty("db.password");
// Load driver
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (IOException | ClassNotFoundException e) {
System.err.println("Error loading DB configuration: " + e.getMessage());
}
}
public static Connection getConnection() throws SQLException {
if (url == null || user == null || password == null) {
throw new SQLException("Database credentials not set properly!");
}
return DriverManager.getConnection(url, user, password);
}
}
class Vehicle {
String regNo;
String model;
String ownerName;
String phone;
String address;
Vehicle(String regNo, String model, String ownerName, String phone, String address) {
this.regNo = regNo;
this.model = model;
this.ownerName = ownerName;
this.phone = phone;
this.address = address;
}
void showVehicleDetails() {
System.out.println("Reg No: " + regNo);
System.out.println("Model: " + model);
System.out.println("Owner: " + ownerName);
System.out.println("Phone: " + phone);
System.out.println("Address: " + address);
}
}
class ServiceBooking {
static int bookingCounter = 1000;
int bookingId;
Vehicle vehicle;
String serviceType;
String date;
String status;
// Modified constructor: gets current max ID from DB first time
ServiceBooking(Vehicle vehicle, String serviceType, String date) {
if (bookingCounter == 1000) { // only fetch once per JVM run
bookingCounter = getLatestBookingId() + 1;
}
this.bookingId = bookingCounter++;
this.vehicle = vehicle;
this.serviceType = serviceType;
this.date = date;
this.status = "Scheduled";
}
private int getLatestBookingId() {
int maxId = 999; // default before first entry
try (Connection con = DBConnection.getConnection()) {
String sql = "SELECT booking_id FROM users ORDER BY booking_id DESC LIMIT 1";
try (Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql)) {
if (rs.next()) {
maxId = rs.getInt("booking_id");
}
}
} catch (SQLException e) {
System.err.println("Error fetching latest booking ID: " + e.getMessage());
}
return maxId;
}
void updateStatus(String newStatus) {
this.status = newStatus;
}
void showBookingDetails() {
System.out.println("\nBooking ID: " + bookingId);
vehicle.showVehicleDetails();
System.out.println("Service Type: " + serviceType);
System.out.println("Date: " + date);
System.out.println("Status: " + status);
System.out.println("------------------------------");
}
}
class BookingService {
ArrayList<ServiceBooking> bookingList = new ArrayList<>();
void addBooking(ServiceBooking b) {
bookingList.add(b);
System.out.println("\nBooking added successfully (in memory)!");
b.showBookingDetails();
try (Connection con = DBConnection.getConnection()) {
String sql = "INSERT INTO users (booking_id, reg_no, model, owner_name, phone, address, service_type, date, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement ps = con.prepareStatement(sql)) {
ps.setInt(1, b.bookingId);
ps.setString(2, b.vehicle.regNo);
ps.setString(3, b.vehicle.model);
ps.setString(4, b.vehicle.ownerName);
ps.setString(5, b.vehicle.phone);
ps.setString(6, b.vehicle.address);
ps.setString(7, b.serviceType);
ps.setString(8, b.date);
ps.setString(9, b.status);
ps.executeUpdate();
System.out.println("Booking saved to database!");
}
} catch (SQLException e) {
System.err.println("Error saving booking to DB: " + e.getMessage());
System.err.println("Booking remains only in memory.");
}
}
void viewAllBookings() {
System.out.println("\n=== All Bookings from Database ===");
try (Connection con = DBConnection.getConnection()) {
String sql = "SELECT * FROM users ORDER BY booking_id";
try (Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql)) {
boolean found = false;
while (rs.next()) {
found = true;
System.out.println("Booking ID: " + rs.getInt("booking_id"));
System.out.println("Reg No: " + rs.getString("reg_no"));
System.out.println("Model: " + rs.getString("model"));
System.out.println("Owner: " + rs.getString("owner_name"));
System.out.println("Phone: " + rs.getString("phone"));
System.out.println("Address: " + rs.getString("address"));
System.out.println("Service Type: " + rs.getString("service_type"));
System.out.println("Date: " + rs.getString("date"));
System.out.println("Status: " + rs.getString("status"));
System.out.println("------------------------------");
}
if (!found) System.out.println("No bookings found!");
}
} catch (SQLException e) {
System.err.println("Error fetching bookings: " + e.getMessage());
System.out.println("Falling back to in-memory bookings:");
viewAllInMemory();
}
}
void viewAllInMemory() {
if (bookingList.isEmpty()) {
System.out.println("No in-memory bookings.");
return;
}
for (ServiceBooking b : bookingList) b.showBookingDetails();
}
void viewBookingById(int id) {
try (Connection con = DBConnection.getConnection()) {
String sql = "SELECT * FROM users WHERE booking_id = ?";
try (PreparedStatement ps = con.prepareStatement(sql)) {
ps.setInt(1, id);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
System.out.println("\n=== Booking Details ===");
System.out.println("Booking ID: " + rs.getInt("booking_id"));
System.out.println("Reg No: " + rs.getString("reg_no"));
System.out.println("Model: " + rs.getString("model"));
System.out.println("Owner: " + rs.getString("owner_name"));
System.out.println("Phone: " + rs.getString("phone"));
System.out.println("Address: " + rs.getString("address"));
System.out.println("Service Type: " + rs.getString("service_type"));
System.out.println("Date: " + rs.getString("date"));
System.out.println("Status: " + rs.getString("status"));
System.out.println("------------------------------");
} else {
System.out.println("No booking found with ID: " + id);
}
}
}
} catch (SQLException e) {
System.err.println("Error fetching booking by ID: " + e.getMessage());
for (ServiceBooking b : bookingList) {
if (b.bookingId == id) {
b.showBookingDetails();
return;
}
}
System.out.println("Not found in-memory either.");
}
}
void updateBookingDetails(int id, Scanner sc) {
try (Connection con = DBConnection.getConnection()) {
String checkSql = "SELECT * FROM users WHERE booking_id = ?";
try (PreparedStatement checkPs = con.prepareStatement(checkSql)) {
checkPs.setInt(1, id);
try (ResultSet rs = checkPs.executeQuery()) {
if (!rs.next()) {
System.out.println("Booking ID not found!");
return;
}
System.out.println("\n=== Enter New Details (press Enter to keep existing) ===");
sc.nextLine(); // ensure buffer clear if needed
System.out.print("Owner Name (" + rs.getString("owner_name") + "): ");
String owner = sc.nextLine();
if (owner.isEmpty()) owner = rs.getString("owner_name");
System.out.print("Phone (" + rs.getString("phone") + "): ");
String phone = sc.nextLine();
if (phone.isEmpty()) phone = rs.getString("phone");
System.out.print("Address (" + rs.getString("address") + "): ");
String address = sc.nextLine();
if (address.isEmpty()) address = rs.getString("address");
System.out.print("Model (" + rs.getString("model") + "): ");
String model = sc.nextLine();
if (model.isEmpty()) model = rs.getString("model");
System.out.print("Service Type (" + rs.getString("service_type") + "): ");
String service = sc.nextLine();
if (service.isEmpty()) service = rs.getString("service_type");
System.out.print("Date (" + rs.getString("date") + "): ");
String date = sc.nextLine();
if (date.isEmpty()) date = rs.getString("date");
System.out.print("Status (" + rs.getString("status") + "): ");
String status = sc.nextLine();
if (status.isEmpty()) status = rs.getString("status");
String updateSql = "UPDATE users SET owner_name=?, phone=?, address=?, model=?, service_type=?, date=?, status=? WHERE booking_id=?";
try (PreparedStatement ups = con.prepareStatement(updateSql)) {
ups.setString(1, owner);
ups.setString(2, phone);
ups.setString(3, address);
ups.setString(4, model);
ups.setString(5, service);
ups.setString(6, date);
ups.setString(7, status);
ups.setInt(8, id);
int rows = ups.executeUpdate();
if (rows > 0) {
System.out.println("Booking updated successfully!");
// show updated
viewBookingById(id);
} else {
System.out.println("No rows updated.");
}
}
}
}
} catch (SQLException e) {
System.err.println("Update error: " + e.getMessage());
}
}
void updateBookingStatus(int id, String status) {
try (Connection con = DBConnection.getConnection()) {
String checkSql = "SELECT * FROM users WHERE booking_id = ?";
try (PreparedStatement checkPs = con.prepareStatement(checkSql)) {
checkPs.setInt(1, id);
try (ResultSet rs = checkPs.executeQuery()) {
if (!rs.next()) {
System.out.println("Booking ID not found!");
return;
}
}
}
String sql = "UPDATE users SET status = ? WHERE booking_id = ?";
try (PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, status);
ps.setInt(2, id);
int rows = ps.executeUpdate();
if (rows > 0) {
System.out.println("Status updated successfully!");
viewBookingById(id);
} else {
System.out.println("Status update failed.");
}
}
} catch (SQLException e) {
System.err.println("Error updating status: " + e.getMessage());
}
}
void searchByOwner(String owner) {
String sql = "SELECT * FROM users WHERE owner_name LIKE ? ORDER BY booking_id";
try (Connection con = DBConnection.getConnection();
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, "%" + owner + "%");
try (ResultSet rs = ps.executeQuery()) {
boolean found = false;
while (rs.next()) {
found = true;
System.out.println("\nBooking ID: " + rs.getInt("booking_id"));
System.out.println("Reg No: " + rs.getString("reg_no"));
System.out.println("Model: " + rs.getString("model"));
System.out.println("Owner: " + rs.getString("owner_name"));
System.out.println("Phone: " + rs.getString("phone"));
System.out.println("Address: " + rs.getString("address"));
System.out.println("Service Type: " + rs.getString("service_type"));
System.out.println("Date: " + rs.getString("date"));
System.out.println("Status: " + rs.getString("status"));
System.out.println("------------------------------");
}
if (!found) System.out.println("No matching records found.");
}
} catch (SQLException e) {
System.err.println("Search error: " + e.getMessage());
}
}
void searchByPhone(String phone) {
String sql = "SELECT * FROM users WHERE phone LIKE ? ORDER BY booking_id";
try (Connection con = DBConnection.getConnection();
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, "%" + phone + "%");
try (ResultSet rs = ps.executeQuery()) {
boolean found = false;
while (rs.next()) {
found = true;
System.out.println("\nBooking ID: " + rs.getInt("booking_id"));
System.out.println("Reg No: " + rs.getString("reg_no"));
System.out.println("Model: " + rs.getString("model"));
System.out.println("Owner: " + rs.getString("owner_name"));
System.out.println("Phone: " + rs.getString("phone"));
System.out.println("Address: " + rs.getString("address"));
System.out.println("Service Type: " + rs.getString("service_type"));
System.out.println("Date: " + rs.getString("date"));
System.out.println("Status: " + rs.getString("status"));
System.out.println("------------------------------");
}
if (!found) System.out.println("No matching records found.");
}
} catch (SQLException e) {
System.err.println("Search error: " + e.getMessage());
}
}
void searchByReg(String reg) {
String sql = "SELECT * FROM users WHERE reg_no LIKE ? ORDER BY booking_id";
try (Connection con = DBConnection.getConnection();
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, "%" + reg + "%");
try (ResultSet rs = ps.executeQuery()) {
boolean found = false;
while (rs.next()) {
found = true;
System.out.println("\nBooking ID: " + rs.getInt("booking_id"));
System.out.println("Reg No: " + rs.getString("reg_no"));
System.out.println("Model: " + rs.getString("model"));
System.out.println("Owner: " + rs.getString("owner_name"));
System.out.println("Phone: " + rs.getString("phone"));
System.out.println("Address: " + rs.getString("address"));
System.out.println("Service Type: " + rs.getString("service_type"));
System.out.println("Date: " + rs.getString("date"));
System.out.println("Status: " + rs.getString("status"));
System.out.println("------------------------------");
}
if (!found) System.out.println("No matching records found.");
}
} catch (SQLException e) {
System.err.println("Search error: " + e.getMessage());
}
}
void filterByStatus(String status) {
String sql = "SELECT * FROM users WHERE status = ? ORDER BY booking_id";
try (Connection con = DBConnection.getConnection();
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, status);
try (ResultSet rs = ps.executeQuery()) {
boolean found = false;
while (rs.next()) {
found = true;
System.out.println("\nBooking ID: " + rs.getInt("booking_id"));
System.out.println("Reg No: " + rs.getString("reg_no"));
System.out.println("Model: " + rs.getString("model"));
System.out.println("Owner: " + rs.getString("owner_name"));
System.out.println("Phone: " + rs.getString("phone"));
System.out.println("Address: " + rs.getString("address"));
System.out.println("Service Type: " + rs.getString("service_type"));
System.out.println("Date: " + rs.getString("date"));
System.out.println("Status: " + rs.getString("status"));
System.out.println("------------------------------");
}
if (!found) System.out.println("No matching records found.");
}
} catch (SQLException e) {
System.err.println("Filter error: " + e.getMessage());
}
}
void deleteBooking(int id, Scanner sc) {
try (Connection con = DBConnection.getConnection()) {
String checkSql = "SELECT * FROM users WHERE booking_id = ?";
try (PreparedStatement checkPs = con.prepareStatement(checkSql)) {
checkPs.setInt(1, id);
try (ResultSet rs = checkPs.executeQuery()) {
if (!rs.next()) {
System.out.println("Booking ID not found!");
return;
} else {
System.out.println("Booking found:");
System.out.println("Booking ID: " + rs.getInt("booking_id"));
System.out.println("Owner: " + rs.getString("owner_name"));
System.out.println("Reg No: " + rs.getString("reg_no"));
System.out.print("Are you sure you want to delete this booking? (y/n): ");
String yn = sc.nextLine().trim();
if (!yn.equalsIgnoreCase("y")) {
System.out.println("Delete cancelled.");
return;
}
}
}
}
String delSql = "DELETE FROM users WHERE booking_id = ?";
try (PreparedStatement del = con.prepareStatement(delSql)) {
del.setInt(1, id);
int rows = del.executeUpdate();
if (rows > 0) System.out.println("Booking deleted successfully!");
else System.out.println("Delete failed.");
}
} catch (SQLException e) {
System.err.println("Error deleting booking: " + e.getMessage());
}
}
int getNextBookingIdFromDB() {
int next = 1000;
try (Connection con = DBConnection.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT COALESCE(MAX(booking_id), 999) AS maxid FROM users")) {
if (rs.next()) next = rs.getInt("maxid") + 1;
} catch (SQLException e) {
// fallback: next stays 1000
}
return next;
}
}
class Menu {
BookingService service;
Scanner sc;
Menu(BookingService service, Scanner sc) {
this.service = service;
this.sc = sc;
}
void showMenu() {
System.out.println("\n===== Vehicle Service Booking System =====");
System.out.println("1. Add New Booking");
System.out.println("2. View All Bookings");
System.out.println("3. View Booking by ID");
System.out.println("4. Update Booking Details");
System.out.println("5. Update Booking Status");
System.out.println("6. Search by Owner Name");
System.out.println("7. Search by Phone");
System.out.println("8. Search by Vehicle Reg No");
System.out.println("9. Filter by Status");
System.out.println("10. Delete Booking by ID");
System.out.println("11. Exit");
System.out.print("Enter your choice: ");
}
void handleChoice(int choice) {
switch (choice) {
case 1:
handleAddBooking();
break;
case 2:
service.viewAllBookings();
break;
case 3:
handleViewBookingById();
break;
case 4:
handleUpdateBookingDetails();
break;
case 5:
handleUpdateBookingStatus();
break;
case 6:
System.out.print("Enter owner name (partial allowed): ");
String owner = sc.nextLine().trim();
service.searchByOwner(owner);
break;
case 7:
System.out.print("Enter phone (partial allowed): ");
String phone = sc.nextLine().trim();
service.searchByPhone(phone);
break;
case 8:
System.out.print("Enter reg no (partial allowed): ");
String reg = sc.nextLine().trim();
service.searchByReg(reg);
break;
case 9:
System.out.print("Enter status to filter (Scheduled/In Service/Completed/Cancelled): ");
String status = sc.nextLine().trim();
service.filterByStatus(status);
break;
case 10:
System.out.print("Enter booking ID to delete: ");
String idStr = sc.nextLine().trim();
try {
int id = Integer.parseInt(idStr);
service.deleteBooking(id, sc);
} catch (NumberFormatException nfe) {
System.out.println("Invalid booking id.");
}
break;
case 11:
System.out.println("Exiting... Thank you!--Keep visiting....");
break;
default:
System.out.println("Invalid choice.");
}
}
void handleAddBooking() {
System.out.print("Enter Vehicle Reg No: ");
String reg = sc.nextLine();
System.out.print("Enter Vehicle Model: ");
String model = sc.nextLine();
System.out.print("Enter Owner Name: ");
String owner = sc.nextLine();
System.out.print("Enter Phone Number: ");
String phone = sc.nextLine();
System.out.print("Enter Address: ");
String address = sc.nextLine();
System.out.print("Enter Service Type (Regular/Maintenance/Repair): ");
String type = sc.nextLine();
System.out.print("Enter Date (dd-mm-yyyy): ");
String date = sc.nextLine();
Vehicle v = new Vehicle(reg, model, owner, phone, address);
ServiceBooking sb = new ServiceBooking(v, type, date);
service.addBooking(sb);
}
void handleViewBookingById() {
System.out.print("Enter Booking ID to view: ");
String idStr = sc.nextLine().trim();
try {
int id = Integer.parseInt(idStr);
service.viewBookingById(id);
} catch (NumberFormatException nfe) {
System.out.println("Invalid booking id.");
}
}
void handleUpdateBookingDetails() {
System.out.print("Enter Booking ID to update: ");
String idStr = sc.nextLine().trim();
try {
int id = Integer.parseInt(idStr);
service.updateBookingDetails(id, sc);
} catch (NumberFormatException nfe) {
System.out.println("Invalid booking id.");
}
}
void handleUpdateBookingStatus() {
System.out.print("Enter Booking ID to update status: ");
String idStr = sc.nextLine().trim();
System.out.print("Enter new status (In Service / Completed / Cancelled): ");
String status = sc.nextLine().trim();
try {
int id = Integer.parseInt(idStr);
service.updateBookingStatus(id, status);
} catch (NumberFormatException nfe) {
System.out.println("Invalid booking id.");
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BookingService service = new BookingService();
Menu menu = new Menu(service, sc);
while (true) {
menu.showMenu();
String line = sc.nextLine().trim();
if (line.isEmpty()) {
System.out.println("Please enter a choice.");
continue;
}
int choice;
try {
choice = Integer.parseInt(line);
} catch (NumberFormatException e) {
System.out.println("Invalid choice. Enter a number between 1 and 12.");
continue;
}
if (choice == 11) {
System.out.println("Exiting... Thank you!--Keep visiting....");
System.exit(0);
break;
}
menu.handleChoice(choice);
}
sc.close();
}
}