Challenge

Project 3

Solution :

//Preprocessor Section/Header files
#include //Standard input/output stream library
#include //Standard vector library
#include //Standard string library

using namespace std;

// Define an enumeration type to represent different fuel types
enum Type
{
    GAS, //Represents gas-powered vehicles
    DIESEL, //Represents diesel-powered vehicles
    ELECTRIC //Represents electric-powered vehicles
};


//Declare an class represents a vehicle
class Vehicle
{
protected:
    string make; //The make of the vehicle
    string model; //The model of the vehicle
    int year; //The manufacturing year of the vehicle
    int odometer; //The current odometer reading of the vehicle in km

public:
    //Constructor for the Vehicle class.
    Vehicle(string& make, string& model, int year, int odometer)
        : make(make), model(model), year(year), odometer(odometer) {}

    //Getter methods for the vehicle attributes
    string& getMake()
    {
        return make;
    }

    string& getModel()
    {
        return model;
    }

    int getYear()
    {
        return year;
    }

    int getOdometer()
    {
        return odometer;
    }

    //Setter methods for the vehicle attributes
    void setMake(string& make)
    {
        this->make = make;
    }

    void setModel(string& model)
    {
        this->model = model;
    }

    void setYear(int year)
    {
        this->year = year;
    }

    void setOdometer(int odometer)
    {
        this->odometer = odometer;
    }

    //Virtual method to get the fuel type
    virtual Type getType() = 0;

    virtual bool checkTireRotation() = 0;
};

//Declare an GasCar class, derived from Vehicle
class GasCar : public Vehicle
{
public:
    //Constructor for GasCar
    GasCar(string& make, string& model, int year, int odometer)
        : Vehicle(make, model, year, odometer) {}

    //Implementation of getType for GasCar
    Type getType()
    {
        return GAS;
    }

    void getFuelType()
    {
        cout << "Fuel Type : Gas" << endl>    }

    //Implementation of checkTireRotation for GasCar
    bool checkTireRotation()
    {
        int remainder = odometer P000;
        return remainder >= 0;
    }
};

//declare an DieselCar class, derived from Vehicle
class DieselCar : public Vehicle
{
public:
    //Constructor for DieselCar
    DieselCar(string& make, string& model, int year, int odometer)
        : Vehicle(make, model, year, odometer) {}

    //Implementation of getType for DieselCar
    Type getType()
    {
        return DIESEL;
    }

    void getFuelType()
    {
        cout << "Fuel Type: Diesel" << endl>    }

    //Implementation of checkTireRotation for DieselCar.
    bool checkTireRotation()
    {
        int remainder = odometer P000;
        return remainder >= 0;
    }
};

//Declare an class ElectricCar class, derived from Vehicle
class ElectricCar : public Vehicle
{
public:
    //Constructor for ElectricCar
    ElectricCar(string& make, string& model, int year, int odometer)
        : Vehicle(make, model, year, odometer) {}

    // Implementation of getType for ElectricCar
    Type getType()
    {
        return Type::ELECTRIC;
    }

    void getFuelType()
    {
        cout << "Fuel Type: Diesel" << endl>    }

    // Implementation of checkTireRotation for ElectricCar
    bool checkTireRotation()
    {
        int remainder = odometer P000;
        return remainder >= 0;
    }
};

//declare an class MaintenanceLibrary
class MaintenanceLibrary
{
private:
    vector vehicles;

public:
    //Destructor
    ~MaintenanceLibrary()
    {
        for (auto vehicle : vehicles)
        {
            delete vehicle;
        }
    }

    //Declare an function to add Vehicles to the library
    void addVehicle(Vehicle* vehicle)
    {
        vehicles.push_back(vehicle);
    }

    //Declare an function to list all vehicles in the library
    void listVehicles()
    {
        int index = 0;
        for (auto& vehicle : vehicles)
        {
            cout << " Vehicle " << index>            cout << " Make : " << vehicle>getMake() << ",";
            cout << " Model : " << vehicle>getModel() << ",";
            cout << " Year : " << vehicle>getYear() << ",";
            cout << " Odometer Reading : " << vehicle>getOdometer() << " km" << ",";
            cout << " Fuel Type : ";
            switch (vehicle->getType())
            {
                case GAS:
                    cout << "Gas\n";
                    break;
                case DIESEL:
                    cout << "Diesel\n";
                    break;
                case ELECTRIC:
                    cout << "Electric\n";
                    break;
            }
            index++;
        }
    }

    //Declare an function to check tire rotation for a specific vehicle
    bool checkTireRotation(int index)
    {
        if (index >= 0 && index < vehicles>        {
            return vehicles[index]->checkTireRotation();
        }
        else
        {
            cout << "Invalid index.\n";
            return false;
        }
    }

    //Declare an function to check oil change for a specific vehicle
    bool checkOilChange(int index)
    {
        if (index >= 0 && index < vehicles>        {
            return true;
        }
        else
        {
            cout << "Invalid index\n";
            return false;
        }
    }
};
//Main function
int main()
{
    MaintenanceLibrary maintenanceLibrary;

    // Main program loop for interacting with the maintenance library
    while (true)
    {
        cout << "Menu:\n"; //Display a menu
        cout << "0. Quit\n"; //Option to quit the program
        cout << "1. List inventory\n"; //Option to list the inventory of vehicles
        cout << "2. Add vehicle\n"; //Option to add a new vehicle to the inventory
        cout << "3. Update vehicle\n"; //Option to update vehicle information
        cout << "4. Remove vehicle\n"; //Option to remove a vehicle from the inventory
        cout << "5. Check Tire Rotation\n"; //Option to check for tire rotation service
        cout << "6. Check Oil Change\n"; //Option to check for an oil change service
        cout << "7. Check Recharge\n"; //Option to check for a recharge service

        cout << "Enter your choice: ";  //user to enter their choice

        int choice; //Declare an integer variable to store the user choice

        cin >> choice; //Read and store the user choice

        //Switch State
        switch (choice)
        {
            case 0: //for quit the program
            {
                return 0; //Quit the program
            }
            case 1: //Show list inventory
            {
                maintenanceLibrary.listVehicles();  //List all vehicles
                break;
            }
            case 2: //For add vehicle
            {
                //Adding a new vehicle to the library
                // Declare variabless
                string make, model; //Declare an String to store make & model
                int year, odometer; //Declare an integer variable to store year & odometer
                int fuelTypeChoice; //Declare an integer variable for fuel type choice

                //to enter the make of the vehicle and store it in the 'make' variable
                cout << "Enter make : ";
                cin >> make;

                //To enter the model of the vehicle and store it in the model variable
                cout << "Enter model : ";
                cin >> model;

                //To enter the year of the vehicle and store it in the year variable
                cout << "Enter year : ";
                cin >> year;

                //To enter the odometer reading of the vehicle and store it in the odometer variable
                cout << "Enter odometer reading : ";
                cin >> odometer;

                //To select the fuel type for the vehicle
                cout << "Select fuel type : \n";
                cout << "0. Gas\n";
                cout << "1. Diesel\n";
                cout << "2. Electric\n";

                //Store the user choice of fuel type in the fuelTypeChoice variable
                cin >> fuelTypeChoice;


                Vehicle* vehicle = nullptr;
                switch (fuelTypeChoice)
                {
                    case 0: //Case for select GasCar
                        vehicle = new GasCar(make, model, year, odometer);
                        break; //break statement
                    case 1: //Case for DieselCar
                        vehicle = new DieselCar(make, model, year, odometer);
                        break; //break statement
                    case 2: //Case for ElectricCar
                        vehicle = new ElectricCar(make, model, year, odometer);
                        break; //break statement
                    default:
                        cout << "Invalid fuel type choice \n";
                        break;
                }
                if (vehicle != nullptr)
                {
                    maintenanceLibrary.addVehicle(vehicle);
                }
                break; //break statement
            }

            case 5: //for check tire rotation of vehicle
            {
                //Check tire rotation for a specific vehicle
                int index;
                cout << "Enter the index of the vehicle to check tire rotation : ";
                cin >> index;
                if (maintenanceLibrary.checkTireRotation(index))
                {
                    cout << "Tire rotation is advised \n";
                }
                else
                {
                    cout << "Tire rotation is not advised \n";
                }
                break; //break statement
            }
            case 6: //for check oil status of vehicle
            {
                //Check oil change for a specific vehicle
                int index;
                cout << "Enter the index of the vehicle to check oil change : ";
                cin >> index;
                if (maintenanceLibrary.checkOilChange(index))
                {
                    cout << "Oil change is advised.\n";
                }
                else
                {
                    cout << "Oil change is not advised.\n";
                }
                break; //break statement
            }

            default: //Default case
            {
                cout << "Invalid choice. Please try again.\n";
            }
                break; //break statement
        }
    }

    return 0;  //Exit the program
}

Output :

Menu:
0. Quit
1. List inventory
2. Add vehicle
3. Update vehicle
4. Remove vehicle
5. Check Tire Rotation
6. Check Oil Change
7. Check Recharge
Enter your choice: 2
Enter make : Tata
Enter model : Nexon
Enter year : 2020
Enter odometer reading : 50000
Select fuel type : 
0. Gas
1. Diesel
2. Electric
2
Menu:
0. Quit
1. List inventory
2. Add vehicle
3. Update vehicle
4. Remove vehicle
5. Check Tire Rotation
6. Check Oil Change
7. Check Recharge
Enter your choice: 1
 Vehicle 1 : Make : Tata, Model : Nexon, Year : 2020, Odometer Reading : 50000 km, Fuel Type : Electric
Menu:
0. Quit
1. List inventory
2. Add vehicle
3. Update vehicle
4. Remove vehicle
5. Check Tire Rotation
6. Check Oil Change
7. Check Recharge
Enter your choice: 5
Enter the index of the vehicle to check tire rotation : 1
Invalid index.
Tire rotation is not advised 
Menu:
0. Quit
1. List inventory
2. Add vehicle
3. Update vehicle
4. Remove vehicle
5. Check Tire Rotation
6. Check Oil Change
7. Check Recharge
Enter your choice: 

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.