Vehicle Rental Service
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 110 of 110.
Challenge
EasyCongratulations 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 aVehicleStateabstract class that defines how vehicles behave in different states. Include an abstract methodString getStatus()andbool canRent(). Then create three concrete states:AvailableState- returnsAvailableand allows rentingRentedState- returnsRentedand doesn't allow rentingMaintenanceState- returnsIn Maintenanceand doesn't allow renting
vehicle.dart: Create an abstractVehicleclass with afinalid (String),finalmodel (String), private_dailyRate(double) with a getter, and a private_state(VehicleState) initialized toAvailableState. Include:- A getter
statusthat returns the state's status bool isAvailable()- checks if the current state allows rentingvoid setState(VehicleState state)- changes the vehicle's state- An abstract method
double calculateRentalCost(int days) - An abstract method
String getRequirements()- returns any special requirements
toString()to return[Type] model (id) - statuswhere Type is the runtime type.- A getter
vehicles.dart: Create three vehicle types extendingVehicle:Car- standard rental cost is daily rate times days. Requirements:Valid driver's licenseMotorcycle- 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 aVehicleFactoryclass with a static methodVehicle create(String type, String id, String model, double dailyRate)that returns the appropriate vehicle based on type (car,motorcycle, ortruck). For unknown types, default to creating a Car.customer.dart: Create aCustomerclass with an id (String), name (String), and a private list of rental records (list of Strings). Include:addRental(String vehicleInfo)- adds a rental recordgetRentalCount()- returns the number of rentalsbool isEligible()- returns true if the customer has fewer than 3 active rentals
toString()to returnCustomer: name (id) - X rentals.rental_service.dart: Create aRentalServiceclass using generics with aList<Vehicle>for the fleet and aList<Customer>for customers. Include:addVehicle(Vehicle vehicle)- adds to fleet, printsAdded: vehicle.toString()registerCustomer(Customer customer)- adds customerrentVehicle(String customerId, String vehicleId, int days)- if customer exists, is eligible, and vehicle is available: changes vehicle state to Rented, adds rental to customer, printscustomerId 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, orVehicle not availablereturnVehicle(String vehicleId)- sets vehicle state to Available, printsvehicleId returnedsendToMaintenance(String vehicleId)- sets vehicle state to Maintenance, printsvehicleId sent to maintenancegetAvailableVehicles()- returns a list of available vehicles
main.dart: Demonstrate your rental service. Create aRentalServiceand 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
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 asAvailable vehicles: X. Return the car. Print the final available count.- A car:
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: 2Try 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
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor