Challenge

Project 3

#include

#include

using namespace std;

enum Type {

    GAS,

    DIESEL,

    ELECTRIC

};

class Vehicle {

protected:

    string make;

    string model;

    int year;

    int odometer;

public:

    Vehicle(const string& make, const 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() = 0;

    virtual void checkTireRotation() = 0;

    void getCarInformation() { //getting car information

        cout << "Make: " << make>

        cout << "Model: " << model>

        cout << "Year: " << year>

        cout << "Odometer Reading: " << odometer>

    }

};

class GasCar : public Vehicle {

public:

    GasCar(const string& make, const string& model, int year, int odometer)

        : Vehicle(make, model, year, odometer) {}

    Type getType() override { return GAS; }

    void checkTireRotation() override {

        int remainder = odometer P000; //checking the distance travelled over 50,000km.

        if (remainder >= 0)

            cout << "Rotate the tires." << std>

        else

            cout << "Tires are good to go." << std>

    }

    string getFuelType() { return "Gas"; }

};

class DieselCar : public Vehicle {

public:

    DieselCar(const string& make, const string& model, int year, int odometer)

        : Vehicle(make, model, year, odometer) {}

    Type getType() override { return DIESEL; }

    void checkTireRotation() override {

        int remainder = odometer P000;

        if (remainder >= 0)

            cout << "Rotate the tires." << std>

        else

            cout << "Tires are good to go." << std>

    }

    string getFuelType() { return "Diesel"; }

};

class ElectricCar : public Vehicle {

public:

    ElectricCar(const string& make, const string& model, int year, int odometer)

        : Vehicle(make, model, year, odometer) {}

    Type getType() override { return ELECTRIC; }

    void checkTireRotation() override {

        int remainder = odometer P000;

        if (remainder >= 0)

            cout << "Rotate the tires." << endl>

        else

            cout << "Tires are good to go." << endl>

    }

    void checkChargerPower() {

        int remainder = odometer @0; // for elctrical vehicle's distance over 400km

        if (remainder == 0)

            cout << "Recharge the battery." << endl>

        else

            cout << "Your vehicle is good to go." << endl>

    }

};

class Inventory {

private:

    vector vehicles;

public:

    ~Inventory() {

        for (auto vehicle : vehicles)

            delete vehicle;

    }

    void addVehicle(Vehicle* vehicle) {

        vehicles.push_back(vehicle);

    }

    void listVehicles() {

        for (auto vehicle : vehicles) {

            vehicle->getCarInformation();

            std::cout << "Fuel Type: " << vehicle>getType() << std>

            std::cout << std>

        }

    }

    void updateVehicle(int index, int newOdometer) {

        if (index >= 0 && index < vehicles>

            Vehicle* vehicle = vehicles[index];

            vehicle->getCarInformation();

            vehicle->checkTireRotation();

            ElectricCar* electricCar = dynamic_cast(vehicle);

            if (electricCar)

                electricCar->checkChargerPower();

            else

                cout << "Cannot check charger power for this vehicle." << endl>

            vehicle->getCarInformation();

            vehicle->checkTireRotation();

        }

    }

    void removeVehicle(int index) {

        if (index >= 0 && index < vehicles>

            delete vehicles[index];

            vehicles.erase(vehicles.begin() + index);

        }

    }

    void performTireRotation(int index) {

        if (index >= 0 && index < vehicles>

            Vehicle* vehicle = vehicles[index];

            vehicle->checkTireRotation();

        }

    }

    void performOilChange(int index) {

        if (index >= 0 && index < vehicles>

            Vehicle* vehicle = vehicles[index];

            // Perform oil change specific to vehicle type (if applicable)

            GasCar* gasCar = dynamic_cast(vehicle);

            DieselCar* dieselCar = dynamic_cast(vehicle);

            if (gasCar)

                cout << "Perform oil change for gas car." << endl>

            else if (dieselCar)

                cout << "Perform oil change for diesel car." << endl>

            else

                cout << "Oil change not applicable for this vehicle type." << endl>

        }

    }

    void performRecharge(int index) {

        if (index >= 0 && index < vehicles>

            Vehicle* vehicle = vehicles[index];

            ElectricCar* electricCar = dynamic_cast(vehicle);

            if (electricCar)

                electricCar->checkChargerPower();

            else

                cout << "Recharge not applicable for this vehicle type." << endl>

        }

    }

};

int main()

{

    Inventory inventory;

  //The menu fot the Inventory.

    while (true) {

        cout << "Menu:" << endl>

        cout << "0. Quit" << endl>

        cout << "1. List inventory" << endl>

        cout << "2. Add vehicle" << endl>

        cout << "3. Update vehicle" << endl>

        cout << "4. Remove vehicle" << endl>

        cout << "5. Tire Rotation" << endl>

        cout << "6. Oil Change" << endl>

        cout << "7. Recharge" << endl>

        int choice;

        cin >> choice;

        if (choice == 0)

            break;

//conditional case for the choice operations in the menu system.

        switch (choice) {

            case 1:

                inventory.listVehicles();

                break;

            case 2: {

                string make, model;

                int year, odometer;

                int type;

                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;

                cout << "Enter type (0 - Gas, 1 - Diesel, 2 - Electric): ";

                cin >> type;

Vehicle* vehicle;

                switch (type) {

                    case 0:

                        vehicle = new GasCar(make, model, year, odometer);

                        break;

                    case 1:

                        vehicle = new DieselCar(make, model, year, odometer);

                        break;

                    case 2:

                        vehicle = new ElectricCar(make, model, year, odometer);

                        break;

                    default:

                        std::cout << "Invalid type. Vehicle not added." << std>

                        continue;

                }

    inventory.addVehicle(vehicle);

                break;

            }

            case 3: {

                int index, newOdometer;

                cout << "Enter the index of the vehicle to update: ";

                cin >> index;

                cout << "Enter the new odometer reading: ";

                cin >> newOdometer;

                inventory.updateVehicle(index, newOdometer);

                break;

            }

            case 4: {

                int index;

                cout << "Enter the index of the vehicle to remove: ";

                cin >> index;

                inventory.removeVehicle(index);

                break;

            }

            case 5: {

                int index;

                cout << "Enter the index of the vehicle for tire rotation: ";

                cin >> index;

                inventory.performTireRotation(index);

                break;

            }

            case 6: {

                int index;

                cout << "Enter the index of the vehicle for oil change: ";

                cin >> index;

                inventory.performOilChange(index);

                break;

            }

            case 7: {

                int index;

                cout << "Enter the index of the vehicle for recharge: ";

                cin >> index;

                inventory.performRecharge(index);

                break;

            }

            default:

                cout << "Invalid choice. Try again." << std>

                continue;

        }

    }

    return 0;

}

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.