-
Notifications
You must be signed in to change notification settings - Fork 13
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
Open
StanimalTheMan
wants to merge
15
commits into
amigoscode:main
Choose a base branch
from
StanimalTheMan:initial-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9d80957
Refactor Main.java via putting it in com.stan package
StanimalTheMan 899a354
Add get car logic
StanimalTheMan 8f084f2
Add naive display menu and main logic
StanimalTheMan e176d9e
Add get electric cars logic
StanimalTheMan 8b1fe0f
Add get user logic
StanimalTheMan 9a58553
Try to create booking
StanimalTheMan 2c079a9
Get all user booked cars
StanimalTheMan 9b5a2f1
Naively implement get available cars, available electric cars, and fi…
StanimalTheMan 699fa91
Create initial readme
StanimalTheMan 3366c66
Expand capacity when full
StanimalTheMan 6ee81d4
Make car reg number a string
StanimalTheMan 57f38d1
Refactor via DRY getAvailableCars logic
StanimalTheMan 90d7885
Try to address PR feedback based on memory
StanimalTheMan d4c54fe
toString minor changes
StanimalTheMan dc90f51
Remove booking is null check as there can be empty slots in bookings …
StanimalTheMan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.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(); | ||
| 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(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"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 + | ||
| '}'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| 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 + 1 == capacity) { | ||
| // expand capacity when full | ||
| // copy over existing bookings | ||
| Booking[] newBookings = new Booking[capacity * 2]; | ||
| for (int i = 0; i < newBookings.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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| 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.getUser().getUserId().equals(userId)) { | ||
| userBookingsCount++; | ||
| } | ||
| } | ||
| // 2. create user bookings of length of found count | ||
| Booking[] userBookings = new Booking[userBookingsCount]; | ||
| int curUserBookingIdx = 0; | ||
| // 3. iterate second time to populate userBookings | ||
| for (Booking booking : bookings) { | ||
| if (booking.getUser().getUserId().equals(userId)) { | ||
| userBookings[curUserBookingIdx] = booking; | ||
| curUserBookingIdx++; | ||
| } | ||
| } | ||
|
|
||
| return userBookings; | ||
| } | ||
|
|
||
| public int getCurrentBookingNumber() { | ||
| return this.bookingDao.getCurBookingIdx(); | ||
| } | ||
|
|
||
|
|
||
| public Car[] getCarsByUserId(String userId) { | ||
| if (bookingDao.getCurBookingIdx() == 0) { | ||
| return new Car[0]; | ||
| } | ||
|
|
||
| Booking[] userBookings = getBookingsByUserId(UUID.fromString(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"); | ||
| 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); | ||
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.stan.car; | ||
|
|
||
| public enum Brand { | ||
| MERCEDES, | ||
| TESLA, | ||
| AUDI | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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