Skip to content

Instantly share code, notes, and snippets.

@theabbie
Created January 19, 2025 07:43
Show Gist options
  • Save theabbie/84f293e2e26319b89619ec7185b849d2 to your computer and use it in GitHub Desktop.
Save theabbie/84f293e2e26319b89619ec7185b849d2 to your computer and use it in GitHub Desktop.
Cab Booking Application Low-Level Design (LLD) in Python
class Status:
AVAILABLE = 0
UNAVAILABLE = 1
MAX_DISTANCE_TO_ALLOW_RIDE = 5
class Location:
def __init__(self, x, y):
self.x = x
self.y = y
def calculateDistance(source: Location, destination: Location):
return abs(source.x - destination.x) + abs(source.y - destination.y)
class User:
def __init__(self, username: str, gender: str, age: int):
self.username = username
self.gender = gender
self.age = age
self.location = None
def setLocation(self, location: Location):
self.location = location
class Driver:
def __init__(self, driver_name: str, gender: str, age: int, vehicle: str, initial_location: Location):
self.driver_name = driver_name
self.gender = gender
self.age = age
self.vehicle = vehicle
self.status = Status.AVAILABLE
self.location = initial_location
self.earning = 0
self.FARE = 1
def setStatus(self, status: Status):
self.status = status
def setLocation(self, location: Location):
self.location = location
class UserManager:
def __init__(self):
self.userMap = {}
self.driverMap = {}
def addUser(self, username: str, gender: str, age: int):
self.userMap[username] = User(username, gender, age)
def addDriver(self, driver_name: str, gender: str, age: int, vehicle: str, initial_location: Location):
self.driverMap[driver_name] = Driver(driver_name, gender, age, vehicle, initial_location)
def updateUserLocation(self, username: str, new_location: Location):
if username not in self.userMap:
raise Exception("User Doesn't Exist")
self.userMap[username].setLocation(new_location)
def updateDriverLocation(self, driver_name: str, new_location: Location):
if driver_name not in self.driverMap:
raise Exception("Driver Doesn't Exist")
self.driverMap[driver_name].setLocation(new_location)
def updateDriverStatus(self, driver_name: str, new_status: Status):
if driver_name not in self.driverMap:
raise Exception("Driver Doesn't Exist")
self.driverMap[driver_name].setStatus(new_status)
def find_total_earning(self):
for driver_name in self.driverMap:
print(f"{driver_name} earn Rs {self.driverMap[driver_name].earning}")
class Ride:
def __init__(self, source: Location, destination: Location, user: User, driver: Driver):
self.source = source
self.destination = destination
self.user = user
self.driver = driver
self.completed = False
def calculateBill(self):
distance = calculateDistance(self.source, self.destination)
return distance * self.driver.FARE
def completeRide(self):
self.completed = True
self.driver.setStatus(Status.AVAILABLE)
self.driver.earning += self.calculateBill()
self.driver.setLocation(self.destination)
self.user.setLocation(self.destination)
class RideManager:
def __init__(self, user_manager: UserManager):
self.user_manager = user_manager
self.rides = []
self.rideMap = {}
def add_ride(self, ride: Ride):
if ride.user.username in self.rideMap:
raise Exception("User already in another ride")
self.rideMap[ride.user.username] = ride
self.rides.append(ride)
def create_ride(self, source: Location, destination: Location, user: User, driver: Driver):
return Ride(source, destination, user, driver)
def find_ride(self, username: str, source: Location, destination: Location):
if username not in self.user_manager.userMap:
raise Exception("User Doesn't Exist")
driver_found = False
for driver_name in self.user_manager.driverMap:
driver = self.user_manager.driverMap[driver_name]
if driver.status == Status.AVAILABLE and calculateDistance(source, driver.location) <= MAX_DISTANCE_TO_ALLOW_RIDE:
print(f"{driver_name} [Available]")
driver_found = True
if not driver_found:
print("No ride found")
def choose_ride(self, username: str, driver_name: str, source: Location, destination: Location):
if driver_name not in self.user_manager.driverMap:
raise Exception("Driver Doesn't Exist")
if username not in self.user_manager.userMap:
raise Exception("User Doesn't Exist")
user = self.user_manager.userMap[username]
driver = self.user_manager.driverMap[driver_name]
if calculateDistance(source, driver.location) > MAX_DISTANCE_TO_ALLOW_RIDE:
print("Driver Not in Radius")
return
if driver.status != Status.AVAILABLE:
print("Driver Already in Ride, Please Refresh Available Rides")
return
if calculateDistance(driver.location, user.location) > MAX_DISTANCE_TO_ALLOW_RIDE:
raise Exception(f"Driver Not within {MAX_DISTANCE_TO_ALLOW_RIDE} distance units")
ride = self.create_ride(source, destination, user, driver)
self.add_ride(ride)
driver.setStatus(Status.UNAVAILABLE)
print("Ride started")
def calculateBill(self, username):
if username not in self.rideMap:
raise Exception("User not in ride")
self.rideMap[username].completeRide()
fare = self.rideMap[username].calculateBill()
del self.rideMap[username]
print(f"Ride ended with fare amount Rs {fare}")
userManager = UserManager()
rideManager = RideManager(userManager)
userManager.addUser("Abhay", "M", 23)
userManager.updateUserLocation("Abhay", Location(0, 0))
userManager.addUser("Vikram", "M", 29)
userManager.updateUserLocation("Vikram", Location(10, 0))
userManager.addUser("Kriti", "F", 22)
userManager.updateUserLocation("Kriti", Location(15, 6))
userManager.updateUserLocation("Abhay", Location(10, 1))
userManager.addDriver("Driver1", "M", 22, "Swift, KA-01-12345", Location(10, 1))
userManager.addDriver("Driver2", "M", 29, "Swift, KA-01-12345", Location(11, 10))
userManager.addDriver("Driver3", "M", 24, "Swift, KA-01-12345", Location(5, 3))
rideManager.find_ride("Abhay", Location(0, 0), Location(20, 1))
rideManager.find_ride("Vikram", Location(10, 0), Location(15, 3))
rideManager.choose_ride("Vikram", "Driver1", Location(10, 0), Location(15, 3))
rideManager.choose_ride("Abhay", "Driver1", Location(0, 0), Location(20, 1))
rideManager.calculateBill("Vikram")
userManager.updateDriverLocation("Driver1", Location(10, 0))
rideManager.find_ride("Abhay", Location(15, 0), Location(15, 3))
rideManager.choose_ride("Abhay", "Driver1", Location(15, 0), Location(15, 3))
rideManager.find_ride("Kriti", Location(15, 6), Location(20, 4))
rideManager.calculateBill("Abhay")
userManager.find_total_earning()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment