Menu
Coddy logo textTech

Serviço de Aluguel de Veículos

Parte da seção Object Oriented Programming do Journey de Python da Coddy. Lição 64 de 64.

challenge icon

Desafio

Médio

Neste desafio, você implementará um sistema de aluguel de veículos. O sistema modela veículos, aluguéis e uma agência de aluguel com herança e encapsulamento adequados.

  • vehicle.py - Implemente a classe base Vehicle
  • car.py - Implemente a subclasse Car
  • motorcycle.py - Implemente a subclasse Motorcycle
  • rental.py - Implemente a classe Rental
  • rentalagency.py - Implemente a classe RentalAgency

Cada arquivo contém comentários TODO detalhados orientando sua implementação. Siga esses comentários para garantir que seu código atenda a todos os requisitos.

Experimente você mesmo

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

# Manipulador abrangente de casos de teste
test_case = input()

if test_case == "create_vehicles":
    # Testar a criação de veículos
    car = Car("C123", "Toyota", "Camry", 50, 5)
    bike = Motorcycle("M456", "Honda", "CBR", 35, 600)
    
    print(car)
    print(bike)

elif test_case == "rental_workflow":
    # Testar o fluxo de trabalho de aluguel
    agency = RentalAgency("Quick Rentals")
    
    # Adicionar veículos
    car = Car("C123", "Toyota", "Camry", 50, 5)
    bike = Motorcycle("M456", "Honda", "CBR", 35, 600)
    agency.add_vehicle(car)
    agency.add_vehicle(bike)
    
    # Verificar veículos disponíveis
    print(f"Available before rental: {len(agency.available_vehicles())}")
    
    # Alugar um veículo
    rental_id = agency.rent_vehicle("C123", "John Doe", 3)
    print(f"Rental created: {rental_id}")
    
    # Verificar veículos disponíveis após o aluguel
    print(f"Available after rental: {len(agency.available_vehicles())}")
    
    # Devolver veículo
    agency.return_vehicle(rental_id)
    
    # Verificar veículos disponíveis após a devolução
    print(f"Available after return: {len(agency.available_vehicles())}")

elif test_case == "rental_cost":
    # Testar o cálculo do custo de aluguel
    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":
    # Testar métodos da classe 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":
    # Testar relações de heranç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":
    # Testar comportamento polimórfico
    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":
    # Testar casos extremos de aluguel
    agency = RentalAgency("Edge Rentals")
    
    # Tentar alugar veículo inexistente
    print(f"Rent non-existent: {agency.rent_vehicle('X999', 'John Doe', 3)}")
    
    # Adicionar um carro e alugá-lo
    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}")
    
    # Tentar alugar o mesmo carro novamente
    print(f"Rent unavailable car: {agency.rent_vehicle('C123', 'Jane Smith', 2)}")
    
    # Tentar devolver aluguel inexistente
    print(f"Return non-existent rental: {agency.return_vehicle('R999')}")
    
    # Devolver o aluguel válido
    print(f"Return valid rental: {agency.return_vehicle(rental_id)}")
    
    # Tentar devolver o mesmo aluguel novamente
    print(f"Return completed rental: {agency.return_vehicle(rental_id)}")

elif test_case == "agency_operations":
    # Testar operações abrangentes da agência
    agency = RentalAgency("Full Service Rentals")
    
    # Adicionar 5 veículos
    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())}")
    
    # Alugar 3 veículos
    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())}")
    
    # Devolver 2 veículos
    agency.return_vehicle(rental_ids[0])
    agency.return_vehicle(rental_ids[1])
    
    print(f"Available after returns: {len(agency.available_vehicles())}")
    
    # Imprimir veículos disponíveis
    print("Available vehicles:")
    for v in agency.available_vehicles():
        print(f"  {v}")

elif test_case == "stress_test":
    # Teste de estresse com muitos veículos
    agency = RentalAgency("Stress Test Rentals")
    
    # Adicionar 100 veículos
    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)
    
    # Alugar 50 veículos
    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())}")
    
    # Devolver todos os aluguéis
    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":
    # Testar com entradas inválidas
    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()}")

Todas as lições de Object Oriented Programming

Pratique por conta própria: Compilador de Python online