Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9d80957
Refactor Main.java via putting it in com.stan package
StanimalTheMan Jan 10, 2026
899a354
Add get car logic
StanimalTheMan Jan 10, 2026
8f084f2
Add naive display menu and main logic
StanimalTheMan Jan 10, 2026
e176d9e
Add get electric cars logic
StanimalTheMan Jan 10, 2026
8b1fe0f
Add get user logic
StanimalTheMan Jan 10, 2026
9a58553
Try to create booking
StanimalTheMan Jan 11, 2026
2c079a9
Get all user booked cars
StanimalTheMan Jan 11, 2026
9b5a2f1
Naively implement get available cars, available electric cars, and fi…
StanimalTheMan Jan 11, 2026
699fa91
Create initial readme
StanimalTheMan Jan 11, 2026
3366c66
Expand capacity when full
StanimalTheMan Jan 15, 2026
6ee81d4
Make car reg number a string
StanimalTheMan Jan 15, 2026
57f38d1
Refactor via DRY getAvailableCars logic
StanimalTheMan Jan 15, 2026
90d7885
Try to address PR feedback based on memory
StanimalTheMan Jan 15, 2026
d4c54fe
toString minor changes
StanimalTheMan Jan 15, 2026
dc90f51
Remove booking is null check as there can be empty slots in bookings …
StanimalTheMan Jan 15, 2026
dee08cc
Make consistent pattern of passing uuid after converting from scanner…
StanimalTheMan Jan 17, 2026
c9a73ea
Update display message if car is not found among available cars when …
StanimalTheMan Jan 17, 2026
1fcf5d0
Fix error bc i didn't provide false isElectric value
StanimalTheMan Jan 17, 2026
1c8f243
continue to avoid null pointer exception when iterating through bookings
StanimalTheMan Jan 17, 2026
c15d35f
Address capacity check and resizing when capacity is full code review
StanimalTheMan Jan 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
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
11 changes: 0 additions & 11 deletions src/main/java/Main.java

This file was deleted.

137 changes: 137 additions & 0 deletions src/main/java/com/stan/Main.java
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");
}
}
74 changes: 74 additions & 0 deletions src/main/java/com/stan/booking/Booking.java
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 +
'}';
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/stan/booking/BookingDao.java
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expand capacity when full

bookings[curBookingIdx] = booking;
curBookingIdx++;
return booking;
}
}
107 changes: 107 additions & 0 deletions src/main/java/com/stan/booking/BookingService.java
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UUID instead of String

// 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this if check.

break;
}
if (booking.getUser().getUserId().toString().equals(userId)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (booking.getUser().getUserId().toString().equals(userId)) {
if (booking.getUser().getUserId().equals(userId)) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (booking.getUser().getUserId().toString().equals(userId)) {
if (booking != null && booking.getUser().getUserId().toString().equals(userId)) {

userBookingsCount++;
}
}
// 2. create user bookings of length of found count
Booking[] userBookings = new Booking[userBookingsCount];
int curUserBookingIdx = 0;
Copy link
Contributor

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

// 3. iterate second time to populate userBookings
for (Booking booking : bookings) {
if (booking == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get rid of this

break;
}
if (booking.getUser().getUserId().toString().equals(userId)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove toString

userBookings[curUserBookingIdx] = booking;
curUserBookingIdx++;
}
}

return userBookings;
}

public int getCurrentBookingNumber() {
return this.bookingDao.getCurBookingIdx();
}


public Car[] getCarsByUserId(String userId) {
if (bookingDao.getCurBookingIdx() == 0) {
Copy link
Contributor

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.

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 void createBooking(String carRegNumber, String userId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void createBooking(String carRegNumber, String userId) {
public void createBooking(String carRegNumber, UUID userId) {

// Do I need to robustly handle invalid car reg number and/or user ids for now?
Car[] cars = carDao.getCars();
Copy link
Contributor

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.

boolean isCarFound = false;
Copy link
Contributor

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

Car foundCar = null;
for (Car car : cars) {
if (car.getRegNumber().toString().equals(carRegNumber)) {
isCarFound = true;
foundCar = car;
}
}
if (!isCarFound) {
System.out.println("❌ Unable to book car that doesn't exist");
return;
}

User[] users = userDao.getUsers();
boolean isUserFound = false;
User foundUser = null;
for (User user : users) {
if (user.getUserId().toString().equals(userId)) {
isUserFound = true;
foundUser = user;
}
}
if (!isUserFound) {
System.out.println("❌ Unable to book car for user that doesn't exist");
return;
}

Booking booking = bookingDao.createBooking(foundCar, foundUser);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Booking booking = bookingDao.createBooking(foundCar, foundUser);
return 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()));
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/stan/car/Brand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.stan.car;

public enum Brand {
MERCEDES,
TESLA,
AUDI
}
Loading