Design and code cab hailing service like Uber

We will use Java as programming language. This project will try to cover the following requirements. We will try to first define entities and then services. We will also build class diagram as we proceed as I don’t want to bombard with a picture fill of random boxes and arrows at the end.

If you are new to java, this post will be a good start.

Requirement

  1. Register Rider
  2. Register driver
  3. Book a ride within the given radius
  4. Show ride history of the ride
  5. Update location of the cab
  6. Use In memory database

Design Motivation

Rider Entity

Rider Entity will have following fields:

  • Name
  • Phone Number
  • Country Code
  • List of booking Ids that this consumer has completed

Driver Entity

Driver entity should have following fields:

  • Name
  • Phone Number
  • Country Code

Possibility of abstract class Personal Info

If we notice following fields are common in both the entities:

  • Name
  • Phone Number
  • Country Code

That mean we can safely abstract out a PersonalInfo class which will be extended by both Driver and Rider class. Rider class can have “List of booking Ids that this consumer has completed

At this point, our class diagram should look like one below:

Rider and Driver Entities

Vehicle Entity

Vehicle entity should have following fields:

  • Car Number
  • Latitude
  • Longitude
  • Type – To store type of car, for example: SUV, Compact, Sedan etcetera
  • IsAvailable – To store if the car is available to hire or not.
  • DriverId – We will not tightly couple driver and the vehicle. So, whenever a driver will login, he will pick up one of the vehicle. This field will store the id of driver who is currently driving this vehicle
Adding Vehicle Entity

Booking Entity

Booking entity should have following fields:

  • Booking Id
  • Rider User Id
  • Car Number
  • Start Time
  • End Time
  • Status

Storage Service

The requirement says that we need to use in-memory database. But what if tomorrow, someone asks us to implement the storage in database. Or in cache, something like Redis or memcache.

We will make an IStorageService. This will be an interface which will currently have just one implementation that stores data in-memory using Maps and Lists.

This interface will be like the one shown in gist below:

public interface IStorageService {
Boolean saveRider(Rider rider);
Boolean saveDriver(Driver driver);
Boolean saveVehicle(Vehicle vehicle);
Boolean updateLocation(Vehicle vehicle);
Boolean book(Booking booking);
Vehicle find(Double lat, Double lon, Double maxDistance);
List<Booking> rideHistory(String riderUserId);
Boolean endTrip(Long timeStamp, String bookingId);
}
view raw IStorageService hosted with ❤ by GitHub

Our new class diagram should look like the one below:

Storage Included

Rider Service, Driver Service, Vehicle Service and Booking Service

The first three services will be again have interfaces to save the data of new rider, driver and vehicle. In addition to that, vehicle service should also have contract to find the vehicle near the given position within the given distance as well as a contract to update the position of vehicle.

These three different services will be injected with Storage Service at initialisation so as to store the data.

Booking Service will have Vehicle Service injected in addition to the Storage Service as during booking we need to find vehicles within the given radius.

If you don’t understand this statement right now, its okay. Please wait for the main class at the end. Hope it will be clear then.

The class diagrams looks like this now:

Complete Class Diagram

Download the project

Download the project using the following link.

https://github.com/rohitsingh20122992/cab

Main Class

import booking.models.Booking;
import booking.service.BookingServiceImpl;
import booking.service.IBookingService;
import driver.models.Driver;
import driver.services.DriverServiceImpl;
import driver.services.IDriverService;
import rider.models.Rider;
import rider.services.IRiderService;
import rider.services.RiderServiceImpl;
import storage.IStorageService;
import storage.StorageServiceImpl;
import vehicle.models.Vehicle;
import vehicle.services.IVehicleService;
import vehicle.services.VehicleServiceImpl;
import java.util.List;
public class CabMain {
private static IStorageService storageService = new StorageServiceImpl();
private static IRiderService riderService = new RiderServiceImpl(storageService);
private static IDriverService driverService = new DriverServiceImpl(storageService);
private static IVehicleService vehicleService = new VehicleServiceImpl(storageService);
private static IBookingService bookingService = new BookingServiceImpl(vehicleService, storageService);
public static void main(String args[]){
Rider rider = new Rider();
rider.setName("harsh");
rider.setCountryCode("+91");
rider.setPhoneNumber("910");
riderService.register(rider);
Driver driver = new Driver();
driver.setName("harsh Driver");
driver.setCountryCode("+91");
driver.setPhoneNumber("9431");
driverService.register(driver);
Vehicle vehicle = new Vehicle();
vehicle.setCarNumber("KA01HK");
vehicle.setLat(1D);
vehicle.setLon(1D);
vehicleService.registerVehicle(vehicle);
vehicle.setLat(2D);
vehicle.setLon(2D);
vehicleService.updateLocation(vehicle);
bookingService.book("+91910", 1D, 2D);
List<Booking> bookingHistory = bookingService.history("+91910");
System.out.println("bookingHistory"+bookingHistory);
}
}
view raw CabMain.java hosted with ❤ by GitHub

Drop comment to discuss about the post.

3 thoughts on “Design and code cab hailing service like Uber

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: