5.6 KiB
5.6 KiB
Workshop - Vehicle Management System
A simple hierarchy to learn OOP concepts like Interfaces, Inheritance, Polymorphism, and Abstraction.
Overview
This project simulates a vehicle management system using Object-Oriented Programming (OOP) principles. It demonstrates how real-world entities (like different types of cars) can be represented as classes, with shared characteristics and behaviors defined through interfaces, abstract classes, and inheritance.
Class Hierarchy
The project consists of the following classes and interfaces:
Vehicle(Abstract Class): The base abstract class representing the core properties of any vehicle.Serviceable(Interface): An interface defining the contract for vehicles that require maintenance and servicing.Car(Abstract Class): A subclass ofVehiclethat implements theServiceableinterface, representing common behaviors of cars.GasCar(Class): A concrete subclass ofCarrepresenting gasoline-powered vehicles.ElectricCar(Class): A concrete subclass ofCarrepresenting battery-powered electric vehicles.SportsCar(Class): A subclass ofGasCarrepresenting high-performance sports cars.VehicleInspector(Class): A utility class designed to inspect vehicles and demonstrate polymorphism and type-checking.
Class Diagram
+-------------------+ +-------------------+
| Vehicle | | Serviceable |
| (Abstract Class) | | (Interface) |
+-------------------+ +-------------------+
^ ^
| extends | implements
| |
+-----------------------------------+
|
+-------------------+
| Car |
| (Abstract Class) |
+-------------------+
^
| extends
+-----------------+-----------------+
| |
+-------------------+ +-------------------+
| GasCar | | ElectricCar |
| (Class) | | (Class) |
+-------------------+ +-------------------+
^ | |
| extends | implements | implements
| ⌄ ⌄
+-------------------+ +-------------------+ +-------------------+
| SportsCar | | Refuelable | | Chargeable |
| (Class) | | (Interface) | | (Interface) |
+-------------------+ +-------------------+ +-------------------+
What should you do?
Your task is to complete the implementation of the classes by finding the TODO comments in the codebase and fixing them:
1. Vehicle Class
- Generate getters for all fields
- Implement the
addMileage(double km)method with negative value validation - Define abstract method
getEnergyType()
2. Car Class
- Complete the constructor using
super - Override the
getEnergyType() - Override the
showDetails() - Implement
needsService(Vehicle v)andperformService()from the Serviceable interface
3. GasCar Class
- Override
getEnergyType() - Implement
refuel(double amount)with validation - Override
showDetails() - Override
Serviceable.javamethods
4. ElectricCar Class
- Override
getEnergyType() - Implement
charge(double amount)with validation - Override
showDetails() - Override
Serviceable.javamethods
5. SportsCar Class
- Override
showDetailsto include specific Sports Car feature alongside the base class details - Override
Serviceable.javamethods
6. VehicleInspector Class
- Complete the
inspect(Vehicle v)method usinginstanceof
Main Class
- Create at least 3 Vehicles
Example :
ElectricCar tesla = new ElectricCar(
"Tesla", "Model S", 2022, 1000,
100, 550, 50 // batteryCapacity, range
);
GasCar toyota = new GasCar(
"Toyota", "Corolla", 2018, 85000,
5, 50, "Petrol" // seats, fuelCapacity, fuelType
);
SportsCar ferrari = new SportsCar(
"Ferrari", "F8 Tributo", 2021, 12000,
2, 3.9, "oil", 710 // seats, engineSize, horsepower
);
- Create an array of all vehicles
- Show basic info of each vehicle
- Inspect each vehicle with instanceof logic
- Create an array of Serviceable
- Perform service for each Serviceable
Bonus
Add a HybridCar class
What to do?
- Create a
HybridCarclass that extendsCar. - It will need variables for both battery level and fuel level
- Implement
RefuelableandChargeabletogether - Service logic for HybridCar:
needsService(): return true if mileage > 10000
performService():
Print "Engine oil changed and battery system checked"
Reset fuelLevel to 100
Reset chargeLevel to 100
Workflow
- Fork the repository to your own Git account
- Clone your forked repository
- Create a new branch named
develop
git checkout -b develop
- Complete all TODO sections in the project
- Commit your changes with meaningful commit messages
- Push your branch to your repository
git push origin develop
- Create a Pull Request from
developtomainormaster