-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMain.java
More file actions
137 lines (129 loc) · 5.94 KB
/
Main.java
File metadata and controls
137 lines (129 loc) · 5.94 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
package com.stan;
// TODO 1. create a new branch called initial-implementation
// TODO 2. create a package with your name. i.e com.franco and move this file inside the new package
// TODO 3. implement https://amigoscode.com/learn/java-cli-build/lectures/3a83ecf3-e837-4ae5-85a8-f8ae3f60f7f5
import com.stan.booking.Booking;
import com.stan.booking.BookingDao;
import com.stan.booking.BookingService;
import com.stan.car.Car;
import com.stan.car.CarDao;
import com.stan.car.CarService;
import com.stan.user.User;
import com.stan.user.UserDao;
import com.stan.user.UserService;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
CarDao carDao = new CarDao();
BookingDao bookingDao = new BookingDao();
CarService carService = new CarService(carDao, bookingDao);
UserDao userDao = new UserDao();
UserService userService = new UserService(userDao);
BookingService bookingService = new BookingService(bookingDao, userDao, carDao);
try (Scanner scanner = new Scanner(System.in)) {
while (true) {
System.out.println();
displayMenu();
System.out.println();
String userInput = scanner.nextLine();
try {
int userInputNumber = Integer.parseInt(userInput);
if (userInputNumber < 1 || userInputNumber > 7) {
System.out.println(String.format("%d is not a valid option ❌", userInputNumber));
continue;
}
} catch (NumberFormatException e) {
System.out.println("Invalid user input: " + userInput);
}
switch (userInput) {
case "1":
// Initially display all cars
Car[] cars = carService.getCars();
for (Car car : cars) {
System.out.println(car);
}
System.out.println("➡️ select car reg number");
String carRegNumber = scanner.nextLine();
// Then display all users
User[] users = userService.getUsers();
for (User user : users) {
System.out.println(user);
}
System.out.println("➡️ select user id");
String userId = scanner.nextLine();
bookingService.createBooking(carRegNumber, userId);
break;
case "2":
// Initially display all users
users = userService.getUsers();
for (User user : users) {
System.out.println(user);
}
System.out.println("➡️ select user id");
userId = scanner.nextLine();
Car[] userCars = bookingService.getCarsByUserId(userId);
User user = userService.getUserById(userId);
if (userCars.length == 0) {
System.out.println("❌ user " + user + " has no cars booked");
} else {
for (Car car : userCars) {
System.out.println(car);
}
}
break;
case "3":
Booking[] bookings = bookingService.getBookings();
int bookingNumber = bookingService.getCurrentBookingNumber();
if (bookingNumber == 0) {
System.out.println("No bookings available 😕");
} else {
for (Booking booking : bookings) {
if (booking != null) {
System.out.println(booking);
}
}
}
break;
case "4":
cars = carService.getAvailableCars();
if (cars.length == 0) {
System.out.println("❌ No cars available for renting");
} else {
// probably can handle better with DTOs
for (Car car : cars) {
System.out.println(car);
}
}
break;
case "5":
Car[] electricCars = carService.getAvailableElectricCars();
if (electricCars.length == 0) {
System.out.println("❌ No electric cars available for renting");
} else {
for (Car car : electricCars) {
System.out.println(car);
}
}
break;
case "6":
users = userService.getUsers();
for (User foundUser : users) {
System.out.println(foundUser);
}
break;
case "7":
return;
}
}
}
}
public static void displayMenu() {
System.out.println("1️⃣ - Book Car");
System.out.println("2️⃣ - View All User Booked Cars");
System.out.println("3️⃣ - View All Bookings");
System.out.println("4️⃣ - View Available Cars");
System.out.println("5️⃣ - View Available Electric Cars");
System.out.println("6️⃣ - View all users");
System.out.println("7️⃣ - Exit");
}
}