165k views
5 votes
Build an online car rental platform using object-oriented programming in python

User Enea Dume
by
7.4k points

1 Answer

5 votes

Answer:

class Car:

def __init__(self, make, model, year, daily_rate):

self.make = make

self.model = model

self.year = year

self.daily_rate = daily_rate

def __str__(self):

return f"{self.year} {self.make} {self.model}, Daily Rate: ${self.daily_rate}"

class RentalPlatform:

def __init__(self):

self.cars = []

def add_car(self, car):

self.cars.append(car)

def list_cars(self):

for i, car in enumerate(self.cars):

print(f"{i+1}. {car}")

def rent_car(self, car_index, num_days):

car = self.cars[car_index-1]

cost = car.daily_rate * num_days

print(f"Renting {car} for {num_days} days: ${cost}")

platform = RentalPlatform()

car1 = Car("Toyota", "Camry", 2020, 50)

car2 = Car("Honda", "Accord", 2019, 55)

car3 = Car("Tesla", "Model S", 2021, 100)

platform.add_car(car1)

platform.add_car(car2)

platform.add_car(car3)

platform.list_cars()

# Rent the second car for 3 days

platform.rent_car(2, 3)

Step-by-step explanation:

This code defines two classes: Car and RentalPlatform. The Car class represents a car available for rent and contains information such as the make, model, year, and daily rental rate. The RentalPlatform class represents the car rental platform and contains a list of Car objects. The RentalPlatform class has methods for adding cars to the platform, listing the available cars, and renting a car for a specified number of days.

User Jhonatan
by
7.2k points