Menu
Coddy logo textTech

Vehicle Rental Service

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 110 of 110.

challenge icon

Challenge

Easy

Congratulations on reaching the final challenge of the OOP course! Let's build a comprehensive Vehicle Rental Service that brings together everything you've learned - abstract classes, inheritance, encapsulation, the State pattern, Factory pattern, and generics.

Your rental service will manage different vehicle types, track their availability states, handle rentals, and maintain customer records. You'll organize this across seven files:

  • vehicle_state.dart: Create a VehicleState abstract class that defines how vehicles behave in different states. Include an abstract method String getStatus() and bool canRent(). Then create three concrete states:
    • AvailableState - returns Available and allows renting
    • RentedState - returns Rented and doesn't allow renting
    • MaintenanceState - returns In Maintenance and doesn't allow renting
  • vehicle.dart: Create an abstract Vehicle class with a final id (String), final model (String), private _dailyRate (double) with a getter, and a private _state (VehicleState) initialized to AvailableState. Include:
    • A getter status that returns the state's status
    • bool isAvailable() - checks if the current state allows renting
    • void setState(VehicleState state) - changes the vehicle's state
    • An abstract method double calculateRentalCost(int days)
    • An abstract method String getRequirements() - returns any special requirements
    Override toString() to return [Type] model (id) - status where Type is the runtime type.
  • vehicles.dart: Create three vehicle types extending Vehicle:
    • Car - standard rental cost is daily rate times days. Requirements: Valid driver's license
    • Motorcycle - rental cost is daily rate times days with a 10% discount. Requirements: Motorcycle license, Age 21+
    • Truck - rental cost is daily rate times days plus a flat $50 fee. Requirements: Commercial license, Deposit required
  • vehicle_factory.dart: Create a VehicleFactory class with a static method Vehicle create(String type, String id, String model, double dailyRate) that returns the appropriate vehicle based on type (car, motorcycle, or truck). For unknown types, default to creating a Car.
  • customer.dart: Create a Customer class with an id (String), name (String), and a private list of rental records (list of Strings). Include:
    • addRental(String vehicleInfo) - adds a rental record
    • getRentalCount() - returns the number of rentals
    • bool isEligible() - returns true if the customer has fewer than 3 active rentals
    Override toString() to return Customer: name (id) - X rentals.
  • rental_service.dart: Create a RentalService class using generics with a List<Vehicle> for the fleet and a List<Customer> for customers. Include:
    • addVehicle(Vehicle vehicle) - adds to fleet, prints Added: vehicle.toString()
    • registerCustomer(Customer customer) - adds customer
    • rentVehicle(String customerId, String vehicleId, int days) - if customer exists, is eligible, and vehicle is available: changes vehicle state to Rented, adds rental to customer, prints customerId rented vehicleId for X days. Cost: $Y (Y formatted to 2 decimals). Otherwise print appropriate message: Customer not found, Customer not eligible, Vehicle not found, or Vehicle not available
    • returnVehicle(String vehicleId) - sets vehicle state to Available, prints vehicleId returned
    • sendToMaintenance(String vehicleId) - sets vehicle state to Maintenance, prints vehicleId sent to maintenance
    • getAvailableVehicles() - returns a list of available vehicles
  • main.dart: Demonstrate your rental service. Create a RentalService and use the factory to add three vehicles:
    • A car: V001, Toyota Camry, $45/day
    • A motorcycle: V002, Honda CBR, $35/day
    • A truck: V003, Ford F-150, $75/day
    Register a customer (C001, Alice Johnson) and print the customer. Print the truck's requirements. Rent the car to Alice for 3 days. Try to rent the car again (should fail). Send the motorcycle to maintenance. Print how many vehicles are available as Available vehicles: X. Return the car. Print the final available count.

Expected output:

Added: [Car] Toyota Camry (V001) - Available
Added: [Motorcycle] Honda CBR (V002) - Available
Added: [Truck] Ford F-150 (V003) - Available
Customer: Alice Johnson (C001) - 0 rentals
Commercial license, Deposit required
C001 rented V001 for 3 days. Cost: $135.00
Vehicle not available
V002 sent to maintenance
Available vehicles: 1
V001 returned
Available vehicles: 2

Try it yourself

import 'rental_service.dart';
import 'vehicle_factory.dart';
import 'customer.dart';

void main() {
  // TODO: Create a RentalService instance
  
  // TODO: Use VehicleFactory to create and add three vehicles:
  // - Car: V001, Toyota Camry, $45/day
  // - Motorcycle: V002, Honda CBR, $35/day
  // - Truck: V003, Ford F-150, $75/day
  
  // TODO: Register a customer (C001, Alice Johnson)
  
  // TODO: Print the customer
  
  // TODO: Print the truck's requirements (find truck and call getRequirements())
  
  // TODO: Rent the car (V001) to Alice (C001) for 3 days
  
  // TODO: Try to rent the car again (should fail - not available)
  
  // TODO: Send the motorcycle (V002) to maintenance
  
  // TODO: Print available vehicles count as 'Available vehicles: X'
  
  // TODO: Return the car (V001)
  
  // TODO: Print final available vehicles count
}

All lessons in Object Oriented Programming