Challenge

Project 3

Code:

#include

#include

using namespace std;

class Vehicle

{

protected:

    string make;

    string model;

    int odo;

    int year;

public:

    void setMake(string z) { make = z; }

    void setModel(string z) { model = z; }

    void setOdo(int z) { odo = z; }

    void setYear(int z) { year = z; }

    string getMake()  { return make; }

    string getModel()  { return model; }

    int getOdo() { return odo; }

    int getYear()  { return year; }

    void list() 

    {

        cout << "Make: " << getMake() << endl;

        cout << "Model: " << getModel() << endl;

        cout << "Odometer: " << getOdo() << " miles" << endl;

        cout << "Year: " << getYear() << endl;

    }

    void add()

    {

        string mak, mode;

        int od, ye;

        cout << "Enter the make, model, odometer, year" << endl;

        cin >> mak >> mode >> od >> ye;

        setMake(mak);

        setModel(mode);

        setOdo(od);

        setYear(ye);

    }

    void update()

    {

        int n;

        cout << "Give the choice" << endl;

        cout << "1. Make 2. Model 3. Odometer 4. Year" << endl;

        cin >> n;

        switch (n)

        {

        case 1:

        {

            string mak;

            cout << "Enter new make: ";

            cin >> mak;

            setMake(mak);

        }

        break;

        case 2:

        {

            string mode;

            cout << "Enter new model: ";

            cin >> mode;

            setModel(mode);

        }

        break;

        case 3:

        {

            int od;

            cout << "Enter new odometer reading: ";

            cin >> od;

            setOdo(od);

        }

        break;

        case 4:

        {

            int ye;

            cout << "Enter new year: ";

            cin >> ye;

            setYear(ye);

        }

        break;

        default:

            cout << "Invalid choice" << endl;

        }

    }

    void Remove()

    {

        make = "NULL";

        model = "NULL";

        odo = 0;

        year = 0;

    }

};

void checkTireRotation(int odometer)

{

    if (odometer >= 50000)

    {

        cout << "Time to rotate tires on your gas car." << endl;

    }

    else

    {

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

    }

}

void changeOil(int oilLevel)

{

    cout << "Checking for oil change..." << endl;

    if (oilLevel >= 1000)

    {

        cout << "No oil change needed." << endl;

    }

    else

    {

        cout << "Car needs an oil change." << endl;

    }

}

void recharge(int KM)

{

    cout << "Checking for battery charging..." << endl;

    if (KM >= 400)

    {

        cout << "Battery needs to be recharged." << endl;

    }

    else

    {

        cout << "Car is good to go." << endl;

    }

}

int main()

{

    Vehicle v[10];

    int var = 0;

    while (true)

    {

        cout << "Enter the choice:" << 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. Check Tire Rotation." << endl;

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

        cout << "7. Recharge." << endl;

        int ch;

        cin >> ch;

        switch (ch)

        {

        case 0:

            exit(0);

            break;

        case 1:

            for (int i = 0; i < var; i++)

                v[i].list();

            break;

        case 2:

            if (var < 10)

            {

                v[var].add();

                var++;

            }

            break;

        case 3:

        {

            int in;

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

            cin >> in;

            if (in >= 0 && in < var)

                v[in].update();

            else

                cout << "Invalid index." << endl;

        }

        break;

        case 4:

        {

            int in;

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

            cin >> in;

            if (in >= 0 && in < var)

            {

                v[in].Remove();

                cout << "Vehicle removed." << endl;

            }

            else

                cout << "Invalid index." << endl;

        }

        break;

        case 5:

            cout << "Enter the odometer reading: ";

            int odometer;

            cin >> odometer;

            checkTireRotation(odometer);

            break;

        case 6:

            cout << "Enter the oil level (in ml): ";

            int oilLevel;

            cin >> oilLevel;

            changeOil(oilLevel);

            break;

        case 7:

            cout << "Enter the number of kilometers: ";

            int KM;

            cin >> KM;

            recharge(KM);

            break;

        default:

            cout << "Invalid choice." << endl;

        }

    }

    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.