Introduction
A rental agency rents very different vehicles, but every rental follows the same steps: daily rate, discounts, distance charges and late fees. This project expresses the shared formula once in an abstract base class and lets each vehicle type supply its own rates — inheritance and polymorphism doing exactly what they are for.
Problem statement
Objectives
- Design an abstract, sealed class hierarchy
- Use the template-method pattern for a shared algorithm
- Override methods to vary behaviour by subtype
- Use an exhaustive pattern-matching switch over sealed types
Features
- Three vehicle types with their own pricing rules
- Weekly discount, late fees and truck insurance
- Rent/return workflow with receipts
- Revenue by vehicle type and licence requirements
Concepts demonstrated
- Inheritance
- Polymorphism
- Template method pattern
- Sealed hierarchies
- Exhaustive switches
System architecture
Vehicle is sealed abstract and permits Car, Bike and Truck. It declares abstract dailyRate and distanceCharge and a concrete rentalCost (the template method) that combines them with the weekly discount and late fees; Truck overrides rentalCost to add a fixed fee via super. RentalAgency holds the fleet, the active rentals and completed receipts; licenceNeeded switches over the sealed type with no default branch.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Vehicle.java
- The sealed abstract Vehicle and its three final subclasses with their pricing rules.
- src/RentalAgency.java
- Fleet, rentals, returns, availability and the exhaustive licence switch.
- src/Main.java
- Creates the fleet and runs the command loop and report.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
fleet quote C01 3 400 quote C03 7 100 quote B02 2 300 quote T01 1 120 rent C01 Asha 3 rent C01 Ravi 2 rent B02 Ravi 2 rent T01 Meera 1 rent C02 John 45 available car available return C01 400 0 return B02 300 1 return T01 120 0 return C03 10 0 report quit
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - BMW for 7 days: 4,500 × 7 × 0.85 = ₹26,775 (weekly discount).
- The Royal Enfield returned one day late: 2,400 + 600 km charge + 1,800 late fee = ₹4,800.
- The truck adds a ₹1,500 insurance fee to 4,250 + 1,260 = ₹7,010.
Common errors
class is not allowed to extend sealed class
Every subclass must be listed in permits and declared final, sealed or non-sealed.
Truck price misses the base charges
When overriding rentalCost, call super.rentalCost(...) and add to it.
Possible improvements
- Add electric vehicles with charging fees
- Support reservations for future dates
- Apply loyalty discounts per customer
Additional challenges
- 1Add a Van subclass and make the compiler point out every switch that must change.
- 2Track vehicle maintenance after every 5,000 km.
- 3Print a monthly utilisation report per vehicle.
Built and tested it yourself? Mark it to update your dashboard.