Project 3
Project 3 -
Build an object-oriented library which can be used to track automobile maintenance. There can be different car types such as petrol, diesel, gas, electric etc., This library should be able to support at least three of the car types.
Maintenance tasks include activities like oil change or replace oil filter, replace air filter, replace fuel filter, tire rotations, infotainment system debug, replace spark plugs, replace the cabin or a/c filter, check the level and refill clutch fluid or brake fluid, examine belts, wipers, hoses, check the tire conditions, check the charging systems and battery.
Every vehicle should have the following details:
<!-- [if !supportLists]-->a. <!--[endif]-->Make
<!-- [if !supportLists]-->b. <!--[endif]-->Model
<!-- [if !supportLists]-->c. <!--[endif]-->Year
<!-- [if !supportLists]-->d. <!--[endif]-->Odometer reading
The library interfaces or the API’s (Application Program Interface) should be capable of adding, updating, listing and removing the entries at runtime.
Consider there are three classes such as
<!-- [if !supportLists]-->a. <!--[endif]-->Vehicle
<!-- [if !supportLists]-->a. <!--[endif]-->This class includes protected members such as Make, Model, Year and Odometer. These members are initialized using the class constructor.
<!-- [if !supportLists]-->b. <!--[endif]-->The functions include getter and setter functions of Make(), Model(), Year() and Odometer().
<!-- [if !supportLists]-->c. <!--[endif]-->Maintenance activities that should be done are checkTireRotation(), getCarInformation()
<!-- [if !supportLists]-->d. <!--[endif]-->Let the three fuel types be
enum Type{
GAS,
DIESEL,
ELECTRIC
};
<!-- [if !supportLists]-->e. <!--[endif]-->Let there be a pure virtual method to get the fuel type i.e.,
<!-- [if !supportLists]-->i. <!--[endif]-->virtual Type getType() = 0;
GasCar
a. This is inherited from the vehicle class and has functions getType() and getFuelType()
<!-- [if !supportLists]-->c. <!--[endif]-->DieselCar
<!-- [if !supportLists]--> . <!--[endif]-->a. This class is also a subclass of vehicle and the functions include getFuelType() and getType()
- ElectricCar
<!-- [if !supportLists]--> . <!--[endif]-->
a. This is a subclass of Vehicle class and methods include
<!-- [if !supportLists]--> i.<!--[endif]--> checkTireRotation()- based on odometer reading, we decide whether the car needs to rotate tires or not.
<!-- [if !supportLists]--> ii.<!--[endif]-->checkChargerPower- based on odometer reading this advices to recharge power
<!-- [if !supportLists]--> iii.<!--[endif]-->getType() – to get the fuel type
<!-- [if !supportLists]--> iv.<!--[endif]-->
<!-- [if !supportLists]-->e. <!--[endif]-->FuelVehicle
<!-- [if !supportLists]--> . <!--[endif]-->
<!-- [if !supportLists]-->a. <!--[endif]-->This is an abstract class that inherits vehicle class. The methods in this class include checkOil() and pure virtual function getFuelType()
Maintenance activities that has to be done are as follows,
<!-- [if !supportLists]-->a. <!--[endif]-->Check tire rotation: Let the tire cycle be 50,000 km. Calculate the distance the car has run and if this distance run is at 50k km, then advise rotation, else tires are good to go. Also, calculate the remainder of each 50k km, If remainder >=0 then advice to rotate tires.
<!-- [if !supportLists]-->b. <!--[endif]-->Check Power: Let the distance be 400km, If the distance the car has run is at 400km, then advise battery to be recharged else your vehicle is good to go.
Now that we have learnt about the classes to be created, we need to write our driver code i.e., main.cpp
<!-- [if !supportLists]-->a. <!--[endif]-->In the main class, initialize the inventory and ask the user to select whether to add, update, list or delete the Vehicle list.
<!-- [if !supportLists]-->a. <!--[endif]-->addVehicle() – to add new vehicle to inventory
<!-- [if !supportLists]-->b. <!--[endif]-->ListVehicle()- to list the vehicles in the inventory
<!-- [if !supportLists]-->c. <!--[endif]-->UpdateVehicle()- to update the vehicle in the inventory list
<!-- [if !supportLists]-->d. <!--[endif]-->RemoveVehicle()- to remove the vehicle from the inventory list
<!-- [if !supportLists]-->e. <!--[endif]-->Tire Rotation() – Check if tires need rotation
<!-- [if !supportLists]-->f. <!--[endif]-->OilChange()- check if car requires oil change
<!-- [if !supportLists]-->g. <!--[endif]-->Recharge() – check if vehicle needs recharge
<!-- [if !supportLists]-->b. <!--[endif]-->Check for the user input, 0-1-2-3-4-5-6-7, Functions will be called based on the input provided by the user. If the input is wrong, ask again.
<!-- [if !supportLists]-->c. <!--[endif]-->Menu should be as below,
|
|
<< " 0. Quit.\n" |
|
|
<< " 1. List inventory.\n" |
|
|
<< " 2. Add vehicle.\n" |
|
|
<< " 3. Update vehicle\n" |
|
|
<< " 4. Remove vehicle\n" |
|
|
<< " 5. Tire Rotation\n" |
|
|
<< " 6. Oil Change\n" |
|
|
<< " 7. Recharge\n" |
|
|
|
|
|
|
|
|
- Retrieve the user input on the vehicle’s make, model, year, type and odometer.
<!-- [if !supportLists]-->a. <!--[endif]-->getNewMake() – If input is blank, request again
<!-- [if !supportLists]-->b. <!--[endif]-->getNewModel() – If input is blank, request again
<!-- [if !supportLists]-->c. <!--[endif]-->getYear() – If input doesn’t have 4 digits, request again
<!-- [if !supportLists]-->d. <!--[endif]-->getNewOdometer()- Input should be only numericals
<!-- [if !supportLists]-->e. <!--[endif]-->getNewType() – Input should be within 0-2, else request again
<!-- [if !supportLists]-->e. <!--[endif]-->Retrieve the selected car from the user input
<!-- [if !supportLists]--> . <!--[endif]-->
<!-- [if !supportLists]-->a. <!--[endif]-->selectCar() – Input should be within the size of vehicle list, else request again
<!-- [if !supportLists]-->b. <!--[endif]-->getNewOdometerReading() – New odometer reading is used to calculate the oil change, tire rotation, power recharge etc.
Solution:
#include
#include
#include
using namespace std;
// Enum to represent fuel types
enum class Type {
GAS,
DIESEL,
ELECTRIC
};
// Base class for Vehicle
class Vehicle {
protected:
string make;
string model;
int year;
int odometer;
public:
Vehicle(const std::string& make, const std::string& model, int year, int odometer)
: make(make), model(model), year(year), odometer(odometer) {}
string getMake() const {
return make;
}
string getModel() const {
return model;
}
int getYear() const {
return year;
}
int getOdometer() const {
return odometer;
}
virtual Type getType() const = 0;
virtual void checkTireRotation() const = 0;
virtual void getCarInformation() const = 0;
};
// GasCar class
class GasCar : public Vehicle {
public:
GasCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() const override {
return Type::GAS;
}
void checkTireRotation() const override {
cout << "Checking tire rotation for Gas Car based on odometer reading: " << odometer>
// Implement tire rotation logic for gas car
}
void getCarInformation() const override {
cout << "Gas Car Information:" << std>
cout << "Make: " << make>
cout << "Model: " << model>
cout << "Year: " << year>
cout << "Odometer: " << odometer>
cout << "Fuel Type: Gas" << std>
}
};
// DieselCar class
class DieselCar : public Vehicle {
public:
DieselCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() const override {
return Type::DIESEL;
}
void checkTireRotation() const override {
cout << "Checking tire rotation for Diesel Car based on odometer reading: " << odometer>
// Implement tire rotation logic for diesel car
}
void getCarInformation() const override {
cout << "Diesel Car Information:" << std>
cout << "Make: " << make>
cout << "Model: " << model>
cout << "Year: " << year>
cout << "Odometer: " << odometer>
cout << "Fuel Type: Diesel" << std>
}
};
// ElectricCar class
class ElectricCar : public Vehicle {
public:
ElectricCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() const override {
return Type::ELECTRIC;
}
void checkTireRotation() const override {
cout << "Checking tire rotation for Electric Car based on odometer reading: " << odometer>
// Implement tire rotation logic for electric car
}
void getCarInformation() const override {
cout << "Electric Car Information:" << std>
cout << "Make: " << make>
cout << "Model: " << model>
cout << "Year: " << year>
cout << "Odometer: " << odometer>
cout << "Fuel Type: Electric" << std>
}
};
// Inventory class to manage vehicles
class Inventory {
private:
vector
public:
~Inventory() {
// Clean up the allocated memory
for (Vehicle* vehicle : vehicles) {
delete vehicle;
}
}
void addVehicle(Vehicle* vehicle) {
vehicles.push_back(vehicle);
}
void listVehicles() const {
cout << "Inventory:" << endl>
for (const Vehicle* vehicle : vehicles) {
vehicle->getCarInformation();
cout << "--------------------------------------" < } } void updateVehicle(int index, int newOdometer) { if (index >= 0 && index < vehicles>
vehicles[index]->getCarInformation(); vehicles[index]->checkTireRotation(); vehicles[index]->getCarInformation(); vehicles[index]->getCarInformation(); // Update the odometer reading vehicles[index]->getCarInformation(); } } void removeVehicle(int index) { if (index >= 0 && index < vehicles>
delete vehicles[index]; vehicles.erase(vehicles.begin() + index); } } }; // Function to display the menu and get user input int getMenuChoice() { int choice; cout << "Menu:\n" << "0. Quit.\n" << "1. List inventory.\n" << "2. Add vehicle.\n" << "3. Update vehicle.\n" << "4. Remove vehicle.\n" << "Enter your choice: "; cin >> choice; cout << std>
return choice; } // Function to get vehicle details from user void getVehicleDetails(std::string& make, std::string& model, int& year, int& odometer) { cout << "Enter make: "; cin.ignore(); getline(std::cin, make); cout << "Enter model: "; getline(std::cin, model); cout << "Enter year: "; cin >> year; cout << "Enter odometer reading: "; cin >> odometer; } int main() { Inventory inventory; while (true) { int choice = getMenuChoice(); if (choice == 0) { break; } else if (choice == 1) { inventory.listVehicles(); } else if (choice == 2) { int typeChoice; string make, model; int year, odometer; getVehicleDetails(make, model, year, odometer); cout << "Select fuel type:\n" << "0. Gas\n" << "1. Diesel\n" << "2. Electric\n" << "Enter your choice: "; cin >> typeChoice; Vehicle* vehicle = nullptr; if (typeChoice == 0) { vehicle = new GasCar(make, model, year, odometer); } else if (typeChoice == 1) { vehicle = new DieselCar(make, model, year, odometer); } else if (typeChoice == 2) { vehicle = new ElectricCar(make, model, year, odometer); } else { cout << "Invalid fuel type choice." << endl>
} if (vehicle != nullptr) { inventory.addVehicle(vehicle); cout << "Vehicle added to inventory." << endl>
} } else if (choice == 3) { int index, newOdometer; inventory.listVehicles(); cout << "Enter the index of the vehicle to update: "; cin >> index; cout << "Enter new odometer reading: "; cin >> newOdometer; inventory.updateVehicle(index, newOdometer); } else if (choice == 4) { int index; inventory.listVehicles(); cout << "Enter the index of the vehicle to remove: "; cin >> index; inventory.removeVehicle(index); cout << "Vehicle removed from inventory." << endl>
} else { cout << "Invalid choice. Please try again." << endl>
} cout << endl>
} return 0; } OUTPUT: Menu: 0. Quit. 1. List inventory. 2. Add vehicle. 3. Update vehicle. 4. Remove vehicle. Enter your choice: 2 Enter make: AR Enter model: SUV Enter year: 2020 Enter odometer reading: 1500 Select fuel type: 0. Gas 1. Diesel 2. Electric Enter your choice: 2 Vehicle added to inventory. Menu: 0. Quit. 1. List inventory. 2. Add vehicle. 3. Update vehicle. 4. Remove vehicle. Enter your choice: 3 Inventory: Electric Car Information: Make: AR Model: SUV Year: 2020 Odometer: 1500 Fuel Type: Electric -------------------------------------- Enter the index of the vehicle to update: Note: All the files related to the above code had attached and this code had done in online gdb also.