-
Notifications
You must be signed in to change notification settings - Fork 14
Initial implementation #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9d80957
899a354
8f084f2
e176d9e
8b1fe0f
9a58553
2c079a9
9b5a2f1
699fa91
3366c66
6ee81d4
57f38d1
90d7885
d4c54fe
dc90f51
dee08cc
c9a73ea
1fcf5d0
1c8f243
c15d35f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,17 @@ | ||
| # TODO | ||
| # Car Booking CLI | ||
| ## Part 1 - Initial Implementation | ||
| This is a CLI application that enables an admin for a car company to book cars and view users. | ||
| The menu looks as follows: | ||
| ``` | ||
| 1️⃣ - Book Car | ||
| 2️⃣ - View All User Booked Cars | ||
| 3️⃣ - View All Bookings | ||
| 4️⃣ - View Available Cars | ||
| 5️⃣ - View Available Electric Cars | ||
| 6️⃣ - View all users | ||
| 7️⃣ - Exit | ||
| ``` | ||
|
|
||
| ## Further Instructions / Information | ||
| The full information can be found in the link below: | ||
| https://amigoscode.com/learn/java-cli-build/lectures/cc280bc8-cd3b-4d6c-94ab-666fc1a9349f |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| 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; | ||
| import java.util.UUID; | ||
|
|
||
| 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, carService); | ||
|
|
||
| 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.getAvailableCars(false); | ||
| 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(); | ||
| Booking booking = bookingService.createBooking(carRegNumber, UUID.fromString(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(UUID.fromString(userId)); | ||
| User user = userService.getUserById(UUID.fromString(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[] allBookings = bookingService.getBookings(); | ||
| int bookingNumber = bookingService.getCurrentBookingNumber(); | ||
| if (bookingNumber == 0) { | ||
| System.out.println("No bookings available 😕"); | ||
| } else { | ||
| for (Booking currBooking : allBookings) { | ||
| if (currBooking != null) { | ||
| System.out.println(currBooking); | ||
| } | ||
| } | ||
| } | ||
| break; | ||
| case "4": | ||
| cars = carService.getAvailableCars(false); | ||
| 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.getAvailableCars(true); | ||
| 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"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package com.stan.booking; | ||
|
|
||
| import com.stan.car.Car; | ||
| import com.stan.user.User; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.UUID; | ||
|
|
||
| public class Booking { | ||
| private UUID bookingId; | ||
| private Car car; | ||
| private User user; | ||
| private LocalDateTime bookingTime; | ||
| private boolean isCanceled; | ||
|
|
||
| public Booking(UUID bookingId, Car car, User user, LocalDateTime bookingTime, boolean isCanceled) { | ||
| this.bookingId = bookingId; | ||
| this.car = car; | ||
| this.user = user; | ||
| this.bookingTime = bookingTime; | ||
| this.isCanceled = isCanceled; | ||
| } | ||
|
|
||
| public UUID getBookingId() { | ||
| return bookingId; | ||
| } | ||
|
|
||
| public void setBookingId(UUID bookingId) { | ||
| this.bookingId = bookingId; | ||
| } | ||
|
|
||
| public Car getCar() { | ||
| return car; | ||
| } | ||
|
|
||
| public void setCar(Car car) { | ||
| this.car = car; | ||
| } | ||
|
|
||
| public User getUser() { | ||
| return user; | ||
| } | ||
|
|
||
| public void setUser(User user) { | ||
| this.user = user; | ||
| } | ||
|
|
||
| public LocalDateTime getBookingTime() { | ||
| return bookingTime; | ||
| } | ||
|
|
||
| public void setBookingTime(LocalDateTime bookingTime) { | ||
| this.bookingTime = bookingTime; | ||
| } | ||
|
|
||
| public boolean isCanceled() { | ||
| return isCanceled; | ||
| } | ||
|
|
||
| public void setCanceled(boolean canceled) { | ||
| isCanceled = canceled; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Booking{" + | ||
| "bookingId=" + bookingId + | ||
| ", car=" + car + | ||
| ", user=" + user + | ||
| ", bookingTime=" + bookingTime + | ||
| ", isCanceled=" + isCanceled + | ||
| '}'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.stan.booking; | ||
|
|
||
| import com.stan.car.Car; | ||
| import com.stan.user.User; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.UUID; | ||
|
|
||
| public class BookingDao { | ||
| private static int capacity = 100; | ||
|
|
||
| private static Booking[] bookings = new Booking[capacity] ; | ||
| private static int curBookingIdx = 0; | ||
|
|
||
| public Booking[] getBookings() { | ||
| return bookings; | ||
| } | ||
|
|
||
| public int getCurBookingIdx() { | ||
| return curBookingIdx; | ||
| } | ||
|
|
||
| public Booking createBooking(Car car, User user) { | ||
| if (curBookingIdx >= capacity) { | ||
| // expand capacity when full | ||
| // copy over existing bookings | ||
| capacity *= 2; | ||
| Booking[] newBookings = new Booking[capacity]; | ||
| for (int i = 0; i < bookings.length; i++) { | ||
| newBookings[i] = bookings[i]; | ||
| } | ||
| bookings = newBookings; | ||
| } | ||
| Booking booking = new Booking(UUID.randomUUID(), car, user, LocalDateTime.now(), false); | ||
| bookings[curBookingIdx] = booking; | ||
| curBookingIdx++; | ||
| return booking; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,106 @@ | ||||||
| package com.stan.booking; | ||||||
|
|
||||||
| import com.stan.car.Car; | ||||||
| import com.stan.car.CarService; | ||||||
| import com.stan.user.User; | ||||||
| import com.stan.user.UserDao; | ||||||
|
|
||||||
| import java.util.UUID; | ||||||
|
|
||||||
| public class BookingService { | ||||||
| private BookingDao bookingDao; | ||||||
| private UserDao userDao; | ||||||
| private CarService carService; | ||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it bad practice to mix and match dao and services in a service class? |
||||||
|
|
||||||
| public BookingService(BookingDao bookingDao, UserDao userDao, CarService carService) { | ||||||
| this.bookingDao = bookingDao; | ||||||
| this.userDao = userDao; | ||||||
| this.carService = carService; | ||||||
| } | ||||||
|
|
||||||
| public Booking[] getBookings() { | ||||||
| return this.bookingDao.getBookings(); | ||||||
| } | ||||||
|
|
||||||
| public Booking[] getBookingsByUserId(UUID userId) { | ||||||
| // TODO: use lists but will iterate twice | ||||||
| // 1. in first iteration, get count of bookings that are for user | ||||||
| int userBookingsCount = 0; | ||||||
| Booking[] bookings = getBookings(); | ||||||
|
|
||||||
| for (Booking booking : bookings) { | ||||||
| if (booking == null) { | ||||||
| continue; | ||||||
| } | ||||||
| if (booking.getUser().getUserId().equals(userId)) { | ||||||
| userBookingsCount++; | ||||||
| } | ||||||
| } | ||||||
| // 2. create user bookings of length of found count | ||||||
| Booking[] userBookings = new Booking[userBookingsCount]; | ||||||
| int curUserBookingIdx = 0; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could also use fori |
||||||
| // 3. iterate second time to populate userBookings | ||||||
| for (Booking booking : bookings) { | ||||||
| if (booking == null) { | ||||||
| continue; | ||||||
| } | ||||||
| if (booking.getUser().getUserId().equals(userId)) { | ||||||
| userBookings[curUserBookingIdx] = booking; | ||||||
| curUserBookingIdx++; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return userBookings; | ||||||
| } | ||||||
|
|
||||||
| public int getCurrentBookingNumber() { | ||||||
| return this.bookingDao.getCurBookingIdx(); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| public Car[] getCarsByUserId(UUID userId) { | ||||||
| if (bookingDao.getCurBookingIdx() == 0) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove initial if. so change logic to get all bookings if size is 0 then return empty array otherwise perform your current logic. |
||||||
| return new Car[0]; | ||||||
| } | ||||||
|
|
||||||
| Booking[] userBookings = getBookingsByUserId(userId); | ||||||
| Car[] userCars = new Car[userBookings.length]; | ||||||
| int curUserCarsIdx = 0; | ||||||
| for (Booking booking : userBookings) { | ||||||
| userCars[curUserCarsIdx] = booking.getCar(); | ||||||
| } | ||||||
| return userCars; | ||||||
| } | ||||||
|
|
||||||
| public Booking createBooking(String carRegNumber, UUID userId) { | ||||||
| // Do I need to robustly handle invalid car reg number and/or user ids for now? | ||||||
| Car[] cars = carService.getAvailableCars(false); | ||||||
| Car foundCar = null; | ||||||
| for (Car car : cars) { | ||||||
| if (car.getRegNumber().equals(carRegNumber)) { | ||||||
| foundCar = car; | ||||||
| } | ||||||
| } | ||||||
| if (foundCar == null) { | ||||||
| System.out.println("❌ Unable to book car that doesn't exist or is unavailable"); | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| User[] users = userDao.getUsers(); | ||||||
| User foundUser = null; | ||||||
| for (User user : users) { | ||||||
| if (user.getUserId().equals(userId)) { | ||||||
| foundUser = user; | ||||||
| } | ||||||
| } | ||||||
| if (foundUser == null) { | ||||||
| System.out.println("❌ Unable to book car for user that doesn't exist"); | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| Booking booking = bookingDao.createBooking(foundCar, foundUser); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| System.out.println("🎉 Successfully booked car with reg number " + carRegNumber + " for user " + foundUser); | ||||||
| System.out.println(String.format("Booking ref: %s", booking.getBookingId().toString())); | ||||||
| return booking; | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expand capacity when full