Menu
Coddy logo textTech

Usługa wynajmu pojazdów

Część sekcji Programowanie obiektowe ścieżki Python w Coddy. Lekcja 64 z 64.

challenge icon

Wyzwanie

Średni

W tym wyzwaniu zaimplementujesz system wynajmu pojazdów. System modeluje pojazdy, wypożyczenia i agencję wynajmu, z wykorzystaniem odpowiedniego dziedziczenia i enkapsulacji.

  • vehicle.py - Zaimplementuj bazową klasę Vehicle
  • car.py - Zaimplementuj podklasę Car
  • motorcycle.py - Zaimplementuj podklasę Motorcycle
  • rental.py - Zaimplementuj klasę Rental
  • rentalagency.py - Zaimplementuj klasę RentalAgency

Każdy plik zawiera szczegółowe komentarze TODO, które ułatwią implementację. Postępuj zgodnie z tymi komentarzami, aby mieć pewność, że Twój kod spełnia wszystkie wymagania.

Spróbuj swoich sił

from vehicle import Vehicle
from car import Car
from motorcycle import Motorcycle
from rental import Rental
from rentalagency import RentalAgency

# Obsługa kompleksowych przypadków testowych
test_case = input()

if test_case == "create_vehicles":
    # Test tworzenia pojazdów
    car = Car("C123", "Toyota", "Camry", 50, 5)
    bike = Motorcycle("M456", "Honda", "CBR", 35, 600)
    
    print(car)
    print(bike)

elif test_case == "rental_workflow":
    # Test przebiegu wypożyczenia
    agency = RentalAgency("Quick Rentals")
    
    # Dodaj pojazdy
    car = Car("C123", "Toyota", "Camry", 50, 5)
    bike = Motorcycle("M456", "Honda", "CBR", 35, 600)
    agency.add_vehicle(car)
    agency.add_vehicle(bike)
    
    # Sprawdź dostępne pojazdy
    print(f"Available before rental: {len(agency.available_vehicles())}")
    
    # Wypożycz pojazd
    rental_id = agency.rent_vehicle("C123", "John Doe", 3)
    print(f"Rental created: {rental_id}")
    
    # Sprawdź dostępne pojazdy po wypożyczeniu
    print(f"Available after rental: {len(agency.available_vehicles())}")
    
    # Zwróć pojazd
    agency.return_vehicle(rental_id)
    
    # Sprawdź dostępne pojazdy po zwrocie
    print(f"Available after return: {len(agency.available_vehicles())}")

elif test_case == "rental_cost":
    # Test obliczania kosztu wypożyczenia
    car = Car("C123", "Toyota", "Camry", 50, 5)
    rental = Rental("R1", car, "John Doe", 3)
    print(f"Rental cost for 3 days: ${rental.calculate_cost()}")

elif test_case == "vehicle_methods":
    # Test metod Vehicle
    vehicle = Vehicle("V789", "Generic", "Vehicle", 25)
    print(vehicle)
    print(f"Start rental: {vehicle.start_rental()}")
    print(f"Start rental again: {vehicle.start_rental()}")
    print(f"End rental: {vehicle.end_rental()}")
    print(f"Start rental after end: {vehicle.start_rental()}")

elif test_case == "inheritance_check":
    # Test relacji dziedziczenia
    car = Car("C123", "Toyota", "Camry", 50, 5)
    bike = Motorcycle("M456", "Honda", "CBR", 35, 600)
    
    print(f"Car is a Vehicle: {isinstance(car, Vehicle)}")
    print(f"Motorcycle is a Vehicle: {isinstance(bike, Vehicle)}")
    print(f"Car is a Motorcycle: {isinstance(car, Motorcycle)}")

elif test_case == "polymorphism":
    # Przetestuj zachowanie polimorficzne
    vehicles = [
        Vehicle("V789", "Generic", "Vehicle", 25),
        Car("C123", "Toyota", "Camry", 50, 5),
        Motorcycle("M456", "Honda", "CBR", 35, 600)
    ]
    
    for i, v in enumerate(vehicles):
        print(f"Vehicle {i+1}: {v}")

elif test_case == "rental_edge_cases":
    # Przetestuj przypadki brzegowe wynajmu
    agency = RentalAgency("Edge Rentals")
    
    # Spróbuj wynająć nieistniejący pojazd
    print(f"Rent non-existent: {agency.rent_vehicle('X999', 'John Doe', 3)}")
    
    # Dodaj samochód i go wynajmij
    car = Car("C123", "Toyota", "Camry", 50, 5)
    agency.add_vehicle(car)
    rental_id = agency.rent_vehicle("C123", "John Doe", 3)
    print(f"Valid rental: {rental_id}")
    
    # Spróbuj ponownie wynająć ten sam samochód
    print(f"Rent unavailable car: {agency.rent_vehicle('C123', 'Jane Smith', 2)}")
    
    # Spróbuj zakończyć nieistniejący wynajem
    print(f"Return non-existent rental: {agency.return_vehicle('R999')}")
    
    # Zakończ istniejący wynajem
    print(f"Return valid rental: {agency.return_vehicle(rental_id)}")
    
    # Spróbuj ponownie zakończyć ten sam wynajem
    print(f"Return completed rental: {agency.return_vehicle(rental_id)}")

elif test_case == "agency_operations":
    # Przetestuj wszystkie operacje wypożyczalni
    agency = RentalAgency("Full Service Rentals")
    
    # Dodaj 5 pojazdów
    vehicles = [
        Car("C1", "Toyota", "Camry", 50, 5),
        Car("C2", "Honda", "Accord", 55, 5),
        Car("C3", "Ford", "Focus", 45, 5),
        Motorcycle("M1", "Honda", "CBR", 35, 600),
        Motorcycle("M2", "Yamaha", "R1", 40, 1000)
    ]
    
    for v in vehicles:
        agency.add_vehicle(v)
    
    print(f"Available vehicles: {len(agency.available_vehicles())}")
    
    # Wynajmij 3 pojazdy
    rental_ids = [
        agency.rent_vehicle("C1", "Customer 1", 3),
        agency.rent_vehicle("C2", "Customer 2", 5),
        agency.rent_vehicle("M1", "Customer 3", 2)
    ]
    
    print(f"Available after rentals: {len(agency.available_vehicles())}")
    
    # Przyjmij zwrot 2 pojazdów
    agency.return_vehicle(rental_ids[0])
    agency.return_vehicle(rental_ids[1])
    
    print(f"Available after returns: {len(agency.available_vehicles())}")
    
    # Wyświetl dostępne pojazdy
    print("Available vehicles:")
    for v in agency.available_vehicles():
        print(f"  {v}")

elif test_case == "stress_test":
    # Test obciążeniowy z wieloma pojazdami
    agency = RentalAgency("Stress Test Rentals")
    
    # Dodaj 100 pojazdów
    for i in range(100):
        if i % 2 == 0:
            vehicle = Car(f"C{i}", "Make{i}", f"Model{i}", 50, 5)
        else:
            vehicle = Motorcycle(f"M{i}", "Make{i}", f"Model{i}", 35, 600)
        agency.add_vehicle(vehicle)
    
    # Wypożycz 50 pojazdów
    rental_ids = []
    for i in range(50):
        vin = f"C{i*2}" if i % 2 == 0 else f"M{i*2-1}"
        rental_id = agency.rent_vehicle(vin, f"Customer{i}", 3)
        rental_ids.append(rental_id)
    
    print(f"Available after 50 rentals: {len(agency.available_vehicles())}")
    
    # Zakończ wszystkie wypożyczenia
    for rental_id in rental_ids:
        agency.return_vehicle(rental_id)
    
    print(f"Available after all returns: {len(agency.available_vehicles())}")

elif test_case == "invalid_inputs":
    # Test z nieprawidłowymi danymi wejściowymi
    car = Car("C123", "Toyota", "Camry", -50, 5)
    print(f"Car with negative rate: {car}")
    
    bike = Motorcycle("M456", "Honda", "CBR", 35, 0)
    print(f"Motorcycle with zero engine: {bike}")
    
    rental_zero = Rental("R1", car, "John Doe", 0)
    print(f"Rental with zero days cost: ${rental_zero.calculate_cost()}")
    
    rental_neg = Rental("R2", car, "Jane Smith", -3)
    print(f"Rental with negative days cost: ${rental_neg.calculate_cost()}")

Wszystkie lekcje w sekcji Programowanie obiektowe

Poćwicz samodzielnie: Kompilator Python online