Click here to Skip to main content
15,867,326 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:

Make
Model
Year
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

Vehicle
This class includes protected members such as Make, Model, Year and Odometer. These members are initialized using the class constructor.
The functions include getter and setter functions of Make(), Model(), Year() and Odometer().
Maintenance activities that should be done are checkTireRotation(), getCarInformation()
Let the three fuel types be
enum Type{

GAS,

DIESEL,

ELECTRIC

};

Let there be a pure virtual method to get the fuel type i.e.,
virtual Type getType() = 0;
GasCar
This is inherited from the vehicle class and has functions getType() and getFuelType()
DieselCar
This class is also a subclass of vehicle and the functions include getFuelType() and getType()
ElectricCar
This is a subclass of Vehicle class and methods include
checkTireRotation()- based on odometer reading, we decide whether the car needs to rotate tires or not.
checkChargerPower- based on odometer reading this advices to recharge power
getType() – to get the fuel type
FuelVehicle
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,

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.
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

In the main class, initialize the inventory and ask the user to select whether to add, update, list or delete the Vehicle list.
addVehicle() – to add new vehicle to inventory
ListVehicle()- to list the vehicles in the inventory
UpdateVehicle()- to update the vehicle in the inventory list
RemoveVehicle()- to remove the vehicle from the inventory list
Tire Rotation() – Check if tires need rotation
OilChange()- check if car requires oil change
Recharge() – check if vehicle needs recharge
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.
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.
getNewMake() – If input is blank, request again
getNewModel() – If input is blank, request again
getYear() – If input doesn’t have 4 digits, request again
getNewOdometer()- Input should be only numericals
getNewType() – Input should be within 0-2, else request again
Retrieve the selected car from the user input
selectCar() – Input should be within the size of vehicle list, else request again
getNewOdometerReading() – New odometer reading is used to calculate the oil change, tire rotation, power recharge etc.

What I have tried:

C++
#include <iostream>
#include <string>
#include <vector>

using namespace std;

enum class FuelType {
    PETROL,
    DIESEL,
    GAS,
    ELECTRIC
};

class Vehicle {
protected:
    string make_;
    string model_;
    int year_;
    int odometer_;

public:
    Vehicle(string make, string model, int year, int odometer)
        : make_(make), model_(model), year_(year), odometer_(odometer) {}

    string getMake() const { return make_; }
    void setMake(string make) { make_ = make; }

    string getModel() const { return model_; }
    void setModel(string model) { model_ = model; }

    int getYear() const { return year_; }
    void setYear(int year) { year_ = year; }

    int getOdometer() const { return odometer_; }
    void setOdometer(int odometer) { odometer_ = odometer; }

    virtual FuelType getFuelType() = 0;
};
//abstract class - containing one pure virtual method
class FuelVehicle : public Vehicle
{
 public: //access specifier
 int quarts; //data member
 void checkOil() //member function
 {
 cout<<endl<<" enter="" oil="" level="" (in="" quarts)="" :="" ";
="" cin="">>quarts;
 if(quarts <= 1)
 {
 cout<<endl<<" engine="" oil="" level="" :="" low";
="" }
="" else
="" cout<<endl<<"="" is="" fine";
="" friend="" void="" oilchange(fuelvehicle="" &oil);="" function="" declaration
};

class="" gascar="" public="" vehicle="" {
public:
="" gascar(string="" make,="" string="" model,="" int="" year,="" odometer)
="" vehicle(make,="" odometer)="" {}

="" fueltype="" getfueltype()="" override="" {="" return="" fueltype::gas;="" }
};

class="" dieselcar="" dieselcar(string="" fueltype::diesel;="" electriccar="" electriccar(string="" fueltype::electric;="" }

="" checktirerotation()="" {
="" if="" (odometer_="" %="" 50000="=" 0)="" cout="" <<="" "it's="" time="" for="" a="" tire="" rotation."="" endl;
="" }="" else=""> 0) {
            cout << "You should rotate your tires soon." << endl;
        }
    }

    void checkPower() {
        if (odometer_ % 400 == 0) {
            cout << "Your car needs to be recharged." << endl;
        } else {
            cout << "Your car is good to go." << endl;
        }
    }
};

class MaintenanceLibrary {
private:
    vector<vehicle*> inventory_;

public:
    void addVehicle(Vehicle* vehicle) {
        cout << "Enter the make of the vehicle: ";
        cin >> vehicle->make_;
        cout << "Enter the model of the vehicle: ";
        cin >> vehicle->model_;
        cout << "Enter the year of the vehicle: ";
        cin >> vehicle->year_;
        cout << "Enter the odometer reading of the vehicle: ";
        cin >> vehicle->odometer_;
        inventory_.push_back(vehicle);
        cout << "Vehicle added to inventory.\n";
    }

    void listInventory() {
        if (inventory_.empty()) {
            cout << "The inventory is empty." << endl;
            return;
        }

        cout << "Inventory:" << endl;

        for (size_t i = 0; i < inventory_.size(); i++) {
            Vehicle* vehicle = inventory_[i];
            cout << i << ". " << vehicle->getMake() << " " << vehicle->getModel() << " (" << vehicle->getYear() << ")" << endl;
        }
    }

    void updateVehicle(int index, string make, string model, int year, int odometer) {
        if (index >= inventory_.size()) {
            cout << "Vehicle not found." << endl;
            return;
        }

        Vehicle* vehicle = inventory_[index];
        vehicle->setMake(make);
        vehicle->setModel(model);
        vehicle->setYear(year);
        vehicle->setOdometer(odometer);
    }

    void removeVehicle() {
        if (inventory_.empty()) {
            cout << "Inventory is empty.\n";
            return;
        }
        int vehicleIndex;
        cout << "Enter the index of the vehicle to remove: ";
        cin >> vehicleIndex;
        if (vehicleIndex < 1 || vehicleIndex > inventory_.size()) {
            cout << "Invalid vehicle index.\n";
            return;
        }
        inventory_.erase(inventory_.begin() + vehicleIndex - 1);
        cout << "Vehicle removed from inventory.\n";
    }

    void tireRotation() {
        if (inventory_.empty()) {
            cout << "Inventory is empty.\n";
            return;
        }
        for (int i = 0; i < inventory_.size(); i++) {
            Vehicle* vehicle = inventory_[i];
            ElectricCar* electricCar = dynamic_cast<electriccar*>(vehicle);
            if (electricCar != nullptr) {
                electricCar->checkTireRotation();
            }
        }
    }

    void oilChange() {
        if (inventory_.empty()) {
            cout << "Inventory is empty.\n";
            return;
        }
        for (int i = 0; i < inventory_.size(); i++) {
            Vehicle* vehicle = inventory_[i];
            GasCar* gasCar = dynamic_cast<gascar*>(vehicle);
            DieselCar* dieselCar = dynamic_cast<dieselcar*>(vehicle);
            if (gasCar != nullptr || dieselCar != nullptr) {
                int odometer = vehicle->getOdometer();
                if (odometer % 10000 == 0) {
                    cout << "Vehicle " << i << " needs an oil change.\n";
                }
            }
        }
    }

    void recharge() {
        if (inventory_.empty()) {
            cout << "Inventory is empty.\n";
            return;
        }
        for (int i = 0; i < inventory_.size(); i++) {
            Vehicle* vehicle = inventory_[i];
            ElectricCar* electricCar = dynamic_cast<electriccar*>(vehicle);
            if (electricCar != nullptr) {
                electricCar->checkPower();
            }
        }
    }


};
vector<vehicle> inventory;

int main() {
    MaintenanceLibrary library;
    int userInput;

    do {
        cout << "Menu:\n";
        cout << " 0. Quit.\n";
        cout << " 1. List inventory.\n";
        cout << " 2. Add vehicle.\n";
        cout << " 3. Update vehicle.\n";
        cout << " 4. Remove vehicle.\n";
        cout << " 5. Tire rotation.\n";
        cout << " 6. Oil change.\n";
        cout << " 7. Recharge.\n";
        cout << "Enter your choice: ";
        cin >> userInput;

        switch (userInput) {
            case 0:
                cout << "Exiting program.\n";
                break;
            case 1:
                library.listInventory();
                break;
            case 2: {
                string make, model;
        int year, odometer;
        cout << "Enter vehicle make: ";
        cin >> make;
        cout << "Enter vehicle model: ";
        cin >> model;
        cout << "Enter vehicle year: ";
        cin >> year;
        cout << "Enter vehicle odometer reading: ";
        cin >> odometer;
        library.addVehicle(new Vehicle(make, model, year, odometer));

        break;
            }
            case 3: {
               int index;
               string make, model;
               int year, odometer;
               cout << "Enter the index of the vehicle to update: ";
               cin >> index;
               cout << "Enter the make of the vehicle: ";
               cin >> make;
               cout << "Enter the model of the vehicle: ";
               cin >> model;
               cout << "Enter the year of the vehicle: ";
               cin >> year;
               cout << "Enter the odometer reading of the vehicle: ";
               cin >> odometer;
               library.updateVehicle(index, make, model, year, odometer);
               break;
               }
            case 4:
               library.removeVehicle();
               break;
            case 5:
               library.tireRotation();
               break;
            case 6:
               library.oilChange();
               break;
            case 7:
               library.recharge();
               break;
            default:
               cout << "Invalid choice. Please try again.\n";
               }
} while (userInput != 0);

return 0;
}
Posted
Updated 10-Mar-23 14:43pm
v2
Comments
Dave Kreskowiak 10-Mar-23 13:08pm    
You never said what the problem is. All you did was dump your homework assignment and a ton of code. That's not a question.

Also, delete your Solution 1 post. You "answered" your own question with it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900