-
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
Changes from 9 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,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"); | ||
| } | ||
| } |
| 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,29 @@ | ||
| 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 final int MAX_BOOKINGS = 100; | ||
|
|
||
| private static Booking[] bookings = new Booking[MAX_BOOKINGS] ; | ||
| private int curBookingIdx = 0; | ||
|
|
||
| public Booking[] getBookings() { | ||
| return bookings; | ||
| } | ||
|
|
||
| public int getCurBookingIdx() { | ||
| return curBookingIdx; | ||
| } | ||
|
|
||
| public Booking createBooking(Car car, User user) { | ||
| 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,107 @@ | ||||||||||
| package com.stan.booking; | ||||||||||
|
|
||||||||||
| import com.stan.car.Car; | ||||||||||
| import com.stan.car.CarDao; | ||||||||||
| import com.stan.user.User; | ||||||||||
| import com.stan.user.UserDao; | ||||||||||
|
|
||||||||||
| public class BookingService { | ||||||||||
| private BookingDao bookingDao; | ||||||||||
| private UserDao userDao; | ||||||||||
| private CarDao carDao; | ||||||||||
|
|
||||||||||
| public BookingService(BookingDao bookingDao, UserDao userDao, CarDao carDao) { | ||||||||||
| this.bookingDao = bookingDao; | ||||||||||
| this.userDao = userDao; | ||||||||||
| this.carDao = carDao; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public Booking[] getBookings() { | ||||||||||
| return this.bookingDao.getBookings(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public Booking[] getBookingsByUserId(String 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) { | ||||||||||
|
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 this if check. |
||||||||||
| break; | ||||||||||
| } | ||||||||||
| if (booking.getUser().getUserId().toString().equals(userId)) { | ||||||||||
|
||||||||||
| if (booking.getUser().getUserId().toString().equals(userId)) { | |
| if (booking.getUser().getUserId().equals(userId)) { |
Outdated
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.
| if (booking.getUser().getUserId().toString().equals(userId)) { | |
| if (booking != null && booking.getUser().getUserId().toString().equals(userId)) { |
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.
you could also use fori
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.
get rid of this
Outdated
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.
remove toString
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.
remove initial if. so change logic to get all bookings if size is 0 then return empty array otherwise perform your current logic.
Outdated
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.
| public void createBooking(String carRegNumber, String userId) { | |
| public void createBooking(String carRegNumber, UUID userId) { |
Outdated
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.
you need available cars. i.e cars which are not part of a booking.
Outdated
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.
this can go. because if car is null when you finish loop it means not found
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.
| Booking booking = bookingDao.createBooking(foundCar, foundUser); | |
| return bookingDao.createBooking(foundCar, foundUser); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.stan.car; | ||
|
|
||
| public enum Brand { | ||
| MERCEDES, | ||
| TESLA, | ||
| AUDI | ||
| } |
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