Indrive

mail@pastecode.io avatar
unknown
python
a year ago
2.6 kB
1
Indexable
Never
class Car:
    def __init__(self, make, model, year, available=True):
        self.make = make
        self.model = model
        self.year = year
        self.available = available

    def rent(self):
        if self.available:
            self.available = False
            return f"Rented: {self.year} {self.make} {self.model}"
        else:
            return f"{self.year} {self.make} {self.model} is not available for rent."

    def return_car(self):
        if not self.available:
            self.available = True
            return f"Returned: {self.year} {self.make} {self.model}"
        else:
            return f"{self.year} {self.make} {self.model} is already available."

class RentalSystem:
    def __init__(self):
        self.cars = []

    def add_car(self, car):
        self.cars.append(car)

    def list_available_cars(self):
        available_cars = [car for car in self.cars if car.available]
        if available_cars:
            print("Available Cars:")
            for car in available_cars:
                print(f"{car.year} {car.make} {car.model}")
        else:
            print("No cars available for rent.")

if __name__ == "__main__":
    rental_system = RentalSystem()

    car1 = Car("Toyota", "Camry", 2022)
    car2 = Car("Honda", "Civic", 2021)
    car3 = Car("Ford", "Mustang", 2020)

    rental_system.add_car(car1)
    rental_system.add_car(car2)
    rental_system.add_car(car3)

    while True:
        print("\nOptions:")
        print("1. List available cars")
        print("2. Rent a car")
        print("3. Return a car")
        print("4. Quit")

        choice = input("Enter your choice: ")

        if choice == "1":
            rental_system.list_available_cars()
        elif choice == "2":
            car_to_rent = input("Enter the car you want to rent (e.g., '2022 Toyota Camry'): ")
            for car in rental_system.cars:
                if f"{car.year} {car.make} {car.model}" == car_to_rent:
                    print(car.rent())
                    break
            else:
                print("Car not found or already rented.")
        elif choice == "3":
            car_to_return = input("Enter the car you want to return (e.g., '2022 Toyota Camry'): ")
            for car in rental_system.cars:
                if f"{car.year} {car.make} {car.model}" == car_to_return:
                    print(car.return_car())
                    break
            else:
                print("Car not found or already available.")
        elif choice == "4":
            break
        else:
            print("Invalid input. Please choose a valid option.")