Menu
CoddyTech

Dịch vụ cho thuê phương tiện

Thuộc phần Lập trình hướng đối tượng trong hành trình Python của Coddy. Bài học 64 trên 64.

challenge icon

Thử thách

Trung bình

Trong thử thách này, bạn sẽ triển khai một hệ thống cho thuê xe. Hệ thống mô hình hóa xe, hợp đồng thuê và đại lý cho thuê bằng cách sử dụng kế thừa và tính đóng gói phù hợp.

  • vehicle.py - Triển khai lớp cơ sở Vehicle
  • car.py - Triển khai lớp con Car
  • motorcycle.py - Triển khai lớp con Motorcycle
  • rental.py - Triển khai lớp Rental
  • rentalagency.py - Triển khai lớp RentalAgency

Mỗi tệp có các chú thích TODO chi tiết hướng dẫn bạn triển khai. Hãy làm theo các chú thích này để đảm bảo code của bạn đáp ứng mọi yêu cầu.

Tự làm thử

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

# Xử lý toàn diện các test case
test_case = input()

if test_case == "create_vehicles":
    # Kiểm tra việc tạo phương tiện
    car = Car("C123", "Toyota", "Camry", 50, 5)
    bike = Motorcycle("M456", "Honda", "CBR", 35, 600)
    
    print(car)
    print(bike)

elif test_case == "rental_workflow":
    # Kiểm tra quy trình thuê xe
    agency = RentalAgency("Quick Rentals")
    
    # Thêm phương tiện
    car = Car("C123", "Toyota", "Camry", 50, 5)
    bike = Motorcycle("M456", "Honda", "CBR", 35, 600)
    agency.add_vehicle(car)
    agency.add_vehicle(bike)
    
    # Kiểm tra các phương tiện hiện có
    print(f"Available before rental: {len(agency.available_vehicles())}")
    
    # Thuê một phương tiện
    rental_id = agency.rent_vehicle("C123", "John Doe", 3)
    print(f"Rental created: {rental_id}")
    
    # Kiểm tra các phương tiện hiện có sau khi thuê
    print(f"Available after rental: {len(agency.available_vehicles())}")
    
    # Trả phương tiện
    agency.return_vehicle(rental_id)
    
    # Kiểm tra các phương tiện hiện có sau khi trả
    print(f"Available after return: {len(agency.available_vehicles())}")

elif test_case == "rental_cost":
    # Kiểm tra cách tính chi phí thuê
    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":
    # Kiểm tra các phương thức của 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":
    # Kiểm tra quan hệ kế thừa
    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":
    # Kiểm tra hành vi đa hình
    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":
    # Kiểm tra các trường hợp đặc biệt khi thuê xe
    agency = RentalAgency("Edge Rentals")
    
    # Thử thuê một xe không tồn tại
    print(f"Rent non-existent: {agency.rent_vehicle('X999', 'John Doe', 3)}")
    
    # Thêm một ô tô rồi cho thuê
    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}")
    
    # Thử thuê lại chính chiếc ô tô đó
    print(f"Rent unavailable car: {agency.rent_vehicle('C123', 'Jane Smith', 2)}")
    
    # Thử trả một lượt thuê không tồn tại
    print(f"Return non-existent rental: {agency.return_vehicle('R999')}")
    
    # Trả xe của lượt thuê hợp lệ
    print(f"Return valid rental: {agency.return_vehicle(rental_id)}")
    
    # Thử trả lại cùng một lượt thuê
    print(f"Return completed rental: {agency.return_vehicle(rental_id)}")

elif test_case == "agency_operations":
    # Kiểm tra toàn diện hoạt động của đại lý
    agency = RentalAgency("Full Service Rentals")
    
    # Thêm 5 xe
    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())}")
    
    # Cho thuê 3 xe
    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())}")
    
    # Nhận lại 2 xe
    agency.return_vehicle(rental_ids[0])
    agency.return_vehicle(rental_ids[1])
    
    print(f"Available after returns: {len(agency.available_vehicles())}")
    
    # In ra các xe hiện có
    print("Available vehicles:")
    for v in agency.available_vehicles():
        print(f"  {v}")

elif test_case == "stress_test":
    # Kiểm thử khả năng chịu tải với nhiều xe
    agency = RentalAgency("Stress Test Rentals")
    
    # Thêm 100 xe
    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)
    
    # Thuê 50 xe
    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())}")
    
    # Trả lại tất cả xe đã thuê
    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":
    # Kiểm thử với đầu vào không hợp lệ
    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()}")

Tất cả bài học trong Lập trình hướng đối tượng

Tự luyện tập: Trình biên dịch Python online