Vehicle Rental System(Low-Level-Design)

Photo by why kei on Unsplash

Vehicle Rental System(Low-Level-Design)

·

5 min read

Problem Definition

The Vehicle Rental System serves as a bridge between customers and rental agencies, enabling a seamless rental experience for all parties involved. With its user-friendly interface and robust backend infrastructure, the system ensures that customers can easily search for available vehicles, make reservations, and complete transactions.

Requirements

  1. Rented vehicles can be Cars or Bikes and it may be extended to other vehicle types in future

  2. Users should be able to search stores on the basis of location

  3. The user should be able to get all vehicles of the required type from the selected store

  4. Users can reserve the vehicle of their choice

  5. Users can rent a vehicle on a daily or hourly basis

  6. Users must pay the bill against their reservation to confirm their booking.

Objects

  1. User

  2. Store

  3. Location

  4. Vehicle

  5. Reservation

  6. Bill

  7. Payment

  8. VehicleInventory

UML Class Diagram

Code

Full implementation on Github

package model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
public class Vehicle {
    int vehicleId;
    int vehicleNo;
    String company;
    String modelName;
    int kmsDriven;
    LocalDateTime manufacturingDate;
    int average;
    int cc;
    int dailyRentalCost;
    int hourlyRentalCost;
    int noOfSeats;
    VehicleStatus status;
    VehicleType type;

    public Vehicle(int vehicleId, VehicleType vehicleType)
    {
        this.vehicleId= vehicleId;
        this.type = vehicleType;
    }



    void updateRentalCost(int dailyRentalCost, int hourlyRentalCost)
    {
        this.dailyRentalCost = dailyRentalCost;
        this.hourlyRentalCost = hourlyRentalCost;

    }

    void setVehicleStatus(VehicleStatus status)
    {
        this.status = status;
    }

    void updateDistanceTravelled(int distance)
    {
        kmsDriven = distance;
    }

}
package model;

import java.time.LocalDateTime;

public class Bike extends Vehicle {

    public Bike(int vehicleId) {
        super(vehicleId, VehicleType.BIKE);
    }
}
package model;

import java.time.LocalDateTime;

public class Car extends Vehicle{


    public Car(int vehicleId) {
        super(vehicleId, VehicleType.CAR);

    }
}
package model;

import lombok.AllArgsConstructor;
import lombok.Getter;


@Getter
@AllArgsConstructor
public class Location {
    String address;
    String city;
    String state;
    String country;
    int pinCode;
}
package model;

import booking.Reservation;
import booking.ReservationStatus;
import booking.ReservationType;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
public class Store {
    int StoreID;
    VehicleInventory vehicleInventory;
    Location location;
    List<Reservation> reservations= new ArrayList<>();

    public Store(int storeID, Location location, List<Vehicle> vehicles)
    {
        this.StoreID= storeID;
        this.location= location;
        vehicleInventory = new VehicleInventory(vehicles);
    }


    public List<Vehicle> getAllVehicles(VehicleType vehicleType)
    {

        return vehicleInventory.getAvailableVehicles(vehicleType);
    }

    public Reservation reserveVehicle(User user, Vehicle vehicle, LocalDateTime pickupTime,
                        LocalDateTime dropTime, Location dropLocation)
    {
        Reservation myReservation = new Reservation(user, vehicle, pickupTime, location, dropTime, dropLocation, ReservationType.DAILY);
        reservations.add(myReservation);

        return myReservation;
    }

    public void completeReservation(Reservation reservation)
    {
        //take out the completed reservation from the list of reservations
        reservations.remove(reservation);
        reservation.getVehicle().setStatus(VehicleStatus.AVAILABLE);
        System.out.println("Vehicle is dropped at store");
    }
}
package model;

import lombok.Getter;

import java.util.UUID;

@Getter
public class User {
    int userId;
    String divingLicense;

    public User(int userId, String divingLicense)
    {
        this.userId= userId;
        this.divingLicense= divingLicense;
    }
}
package model;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

public class VehicleInventory {
    List<Vehicle> vehicles= new ArrayList<>();

    public VehicleInventory(List<Vehicle> vehicles)
    {
        this.vehicles= vehicles;
    }

    List<Vehicle> getAvailableVehicles(VehicleType vehicleType)
    {
        //return vehicles whose status is AVAILABLE and VehicleType is vehicletype
        return vehicles;
    }

    void addVehicle(Vehicle vehicle)
    {
        vehicles.add(vehicle);
    }

    void removeVehicle(Vehicle vehicle)
    {
        vehicles.remove(vehicle);
    }



}
package model;

import java.util.ArrayList;
import java.util.List;

public class VehicleRentalSystem {
    List<User> users= new ArrayList<>();
    List<Store> stores = new ArrayList<>();

    public void addUsers(User user)
    {
        users.add(user);
    }
    public void removeUsers(User user)
    {
        users.remove(user);
    }
    public void addStores(Store store)
    {
        stores.add(store);
    }
    public void removeStores(Store store)
    {
        stores.remove(store);
    }

    public List<Store> findStores(Location location)
    {
        List<Store> availableStores = new ArrayList<>();

        //add logic to filter out all the available stores present in the location
        availableStores.add(stores.get(0));
        availableStores.add(stores.get(1));

        return availableStores;
    }
}
package model;

public enum VehicleStatus {
    RENTED,
    AVAILABLE,
    MAINTENANCE
}
package model;

public enum VehicleType {
    CAR,
    BIKE
}
package booking;

import lombok.Getter;
import model.Location;
import model.User;
import model.Vehicle;

import java.time.LocalDateTime;
import java.util.UUID;

@Getter
public class Reservation {
    String rID;
    User user;
    Vehicle vehicle;
    LocalDateTime pickupTime;
    LocalDateTime dropTime;
    Location pickupLocation;
    Location dropLocation;
    ReservationStatus status;
    ReservationType reservationType;

    public Reservation(User user, Vehicle vehicle, LocalDateTime pickupTime,
                       Location pickupLocation, LocalDateTime dropTime, Location dropLocation, ReservationType reservationType){
        rID= UUID.randomUUID().toString();
        this.user = user;
        this.vehicle = vehicle;
        this.pickupTime= pickupTime;
        this.pickupLocation = pickupLocation;
        this.dropTime= dropTime;
        this.dropLocation= dropLocation;
        this.status = ReservationStatus.SCHEDULED;

    }

    void setReservationStatus(ReservationStatus status)
    {
        this.status = status;
    }



}
package booking;

public enum ReservationStatus {
    SCHEDULED,
    INPROGRESS,
    COMPLETED,
    CANCELLED
}
package booking;

public enum ReservationType {
    DAILY,
    HOURLY
}
package booking;

import lombok.Getter;

import java.util.UUID;

@Getter
public class Bill {
    String billID;
    Reservation reservation;
    int amount;
    BillStatus status;

    public Bill(Reservation reservation)
    {
        billID = UUID.randomUUID().toString();
        this.reservation = reservation;
        //calculate amount based on pickup and drop time and vehicle rental cost
        // (we can get all these from reservation object)
        amount= 100;
        status = BillStatus.PENDING;

    }
}
package booking;

public enum BillStatus {
    PAID,
    PENDING
}
package booking;

import java.util.UUID;

public class Payment {
    String paymentID;
    Bill bill;
    PaymentMode paymentMode;

    public Payment()
    {
        paymentID = UUID.randomUUID().toString();
    }

    public void makePayment(Bill bill, PaymentMode paymentMode)
    {
        this.bill= bill;
        this.paymentMode= paymentMode;
        bill.status= BillStatus.PAID;

        System.out.println("Paid Rs " + bill.amount + " successfully");
        System.out.println("Your Reservation is successful");

    }


}
package booking;

public enum PaymentMode {
    CARD,
    UPI,
    CASH
}
import booking.Reservation;
import model.*;
import booking.Bill;
import booking.Payment;
import booking.PaymentMode;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;

public class VehicleRentalSystemApplication {
    public static void main(String[] args) {

        //create users
        User user1 = new User(1, "ABCD 20212365435");
        User user2 = new User(2, "EDFE 54324567896");


        //create vehicles
        List<Vehicle> vehicleList1= new ArrayList<>();
        List<Vehicle> vehicleList2= new ArrayList<>();
        Vehicle vehicle1= new Car(1);
        Vehicle vehicle2= new Car(2);
        Vehicle vehicle3= new Bike(3);
        Vehicle vehicle4= new Bike(4);
        vehicleList1.add(vehicle1);
        vehicleList1.add(vehicle2);
        vehicleList2.add(vehicle3);
        vehicleList2. add(vehicle4);

        //create locations
        Location location1 = new Location("Sector 135", "Noida", "UP",
                "India", 201304);

        //create stores
        Store store1= new Store(1, location1, vehicleList1);
        Store store2= new Store(2, location1, vehicleList2);

        //add users and stores in vehicleRentalsystem
        VehicleRentalSystem vehicleRentalSystem = new VehicleRentalSystem();
        vehicleRentalSystem.addUsers(user1);
        vehicleRentalSystem.addUsers(user2);

        vehicleRentalSystem.addStores(store1);
        vehicleRentalSystem.addStores(store2);

        //user1 wants to rent Car in Noida location
        List<Store> allStores= vehicleRentalSystem.findStores(location1);
        //user selects store 1 in the displayed list
        Store store= allStores.get(0);
        List<Vehicle> allVehicles = store.getAllVehicles(VehicleType.CAR);
        //user wants to reserve first car in the displayed list
        Vehicle reservedVehicle = allVehicles.get(0);
        Reservation reservation= allStores.get(0).reserveVehicle(user1, reservedVehicle,
                LocalDateTime.of(2023, Month.JUNE, 29, 05, 30, 00),
                LocalDateTime.of(2023, Month.JUNE, 30, 22, 30, 00),
                location1);

        //Bill is generated
        Bill bill = new Bill(reservation);

        //User does payment
        Payment payment = new Payment();
        payment.makePayment(bill, PaymentMode.UPI);

        //Booking is confirmed
        //user comes to pickup vehicle
        reservedVehicle.setStatus(VehicleStatus.RENTED);

        //user drops the vehicle
        store.completeReservation(reservation);


    }
}

Through this low-level design document, we have explored the intricacies of the system, delving into its key components and highlighting the vital functionalities it encompasses. From the customer portal to the vehicle management system in between, every aspect of the rental process has been carefully considered and optimized.