Challenge

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.

Answer:-

#include <iostream>  // Standard library functions
#include <string>
#include <limits>
using namespace std;  // Allows using standard library functions without prefixing with "std::"

// Enum to represent types of vehicles
enum class Type {
    GAS,
    DIESEL,
    ELECTRIC
};

// Base Class for Vehicles
class Vehicle {
protected:
    string make;    // Make of the vehicle
    string model;   // Model of the vehicle
    int year;       // Year of manufacture
    long int odometer;  // Odometer reading

public:
    // Function to add a new vehicle
    void addVehicle() {
        cout << "Adding a new vehicle..." << endl;
        cout << "Make: ";
        getline(cin, make);
        cout << "Model: ";
        getline(cin, model);
        year = getValidYear();  // Validate and set year
        odometer = getValidOdometer();  // Validate and set odometer
        cout << "Vehicle has been added." << endl;
    }

    // Function to list the inventory of vehicles
    void listInventory() const {
        cout << "List of vehicles:" << endl;
        cout << "Make: " << make << ", Model: " << model << ", Year: " << year << ", Odometer: " << odometer << endl;
    }

    // Function to update vehicle information
    void updateVehicle() {
        cout << "Updating vehicle..." << endl;
        cout << "Make: ";
        getline(cin, make);
        cout << "Model: ";
        getline(cin, model);
        year = getValidYear();
        odometer = getValidOdometer();
        cout << "Vehicle has been updated." << endl;
    }

    // Function to remove a vehicle
    void removeVehicle() {
        cout << "Removing vehicle..." << endl;
        make = "";
        model = "";
        year = 0;
        odometer = 0;
        cout << "Vehicle has been removed." << endl;
    }

    // Function to check tire rotation
    void checkTireRotation() const {
        int tire;
        cout << "Checking tire rotation..." << endl;
        cout << "Please enter odometer reading: ";
        cin >> tire;
        if (tire >= 50000) {
            cout << "Tire rotation advised." << endl;
        }
        else {
            cout << "Tires are good to go." << endl;
        }
    }

    // Function to check for oil change
    void checkOilChange() const {
        int quarts;
        cout << "Checking oil change..." << endl;
        cout << "Enter oil dipstick level (1 to 4): ";
        cin >> quarts;
        if (quarts <= 1) {
            cout << "Vehicle requires oil change." << endl;
        }
        else {
            cout << "Vehicle is good to go." << endl;
        }
    }

    // Function to check for recharge
    void checkRecharge() {
        cout << "Checking recharge..." << endl;
        cout << "Please enter odometer reading: ";
        cin >> odometer;
        if (odometer >= 400) {
            cout << "Recharge battery advised." << endl;
        }
        else {
            cout << "Vehicle is good to go." << endl;
        }
    }

private:
    // Function to get a valid year (4 digits)
    int getValidYear() {
        int input;
        do {
            cout << "Year: ";
            cin >> input;
            if (cin.fail() || to_string(input).length() != 4) {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Invalid input. Year should have 4 digits." << endl;
            }
            else {
                break;
            }
        } while (true);
        cin.ignore(); // Clear the buffer
        return input;
    }

    // Function to get a valid odometer reading (numeric value)
    long int getValidOdometer() {
        long int input;
        do {
            cout << "Odometer reading: ";
            cin >> input;
            if (cin.fail()) {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Invalid input. Please enter numerals only." << endl;
            }
            else {
                break;
            }
        } while (true);
        cin.ignore(); // Clear the buffer
        return input;
    }
};

int main() {
    Vehicle inventory;  // Creating an object of Vehicle class
    int choice;

    // Menu for the user to choose options
    cout << "Select your 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. Check oil change." << endl;
    cout << "7. Check recharge." << endl;

    // Main loop to handle user choices
    while (true) {
        cout << "Enter your choice: ";
        cin >> choice;
        cin.ignore();  // Clear the buffer

        switch (choice) {
            case 0:
                cout << "Thank you for visiting us! Have a safe day." << endl;
                return 0;
            case 1:
                inventory.listInventory();
                break;
            case 2:
                inventory.addVehicle();
                break;
            case 3:
                inventory.updateVehicle();
                break;
            case 4:
                inventory.removeVehicle();
                break;
            case 5:
                inventory.checkTireRotation();
                break;
            case 6:
                inventory.checkOilChange();
                break;
            case 7:
                inventory.checkRecharge();
                break;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }
    }

    return 0;
}

Output:

1)Output:

Select your choice

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: 0

Thank you for visiting us! Have a safe day.

2)Output:

Select your choice

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

List of vehicles:

Make: , Model: , Year: 0, Odometer: 0

Enter your choice: 2

Adding a new vehicle...

Make: Tata

Model: Nexon

Year: 2023

Odometer reading: 25000

Vehicle has been added.

Enter your choice: 1

List of vehicles:

Make: Tata, Model: Nexon, Year: 2023, Odometer: 25000

Enter your choice: 3

Updating vehicle...

Make: Tata

Model: Nexon

Year: 2021

Odometer reading: 36000

Vehicle has been updated.

Enter your choice: 1

List of vehicles:

Make: Tata, Model: Nexon, Year: 2021, Odometer: 36000

3)Output:

Select your choice

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

Adding a new vehicle...

Make: Tata

Model: Nexon

Year: 25000

Invalid input. Year should have 4 digits.

Year: 2023

Odometer reading: 25000

Vehicle has been added.

Enter your choice: 1

List of vehicles:

Make: Tata, Model: Nexon, Year: 2023, Odometer: 25000

Enter your choice: 3

Updating vehicle...

Make: Tata

Model: Nexon

Year: 2023

Odometer reading: 36000

Vehicle has been updated.

Enter your choice: 4

Removing vehicle...

Vehicle has been removed.

Enter your choice: 1

List of vehicles:

Make: , Model: , Year: 0, Odometer: 0

4)Output:

Select your choice

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

Checking tire rotation...

Please enter odometer reading: 49000

Tires are good to go.

Enter your choice: 5

Checking tire rotation...

Please enter odometer reading: 51000

Tire rotation advised.

5)Output:

Select your choice

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: 6

Checking oil change...

Enter oil dipstick level (1 to 4): 2

Vehicle is good to go.

Enter your choice: 6

Checking oil change...

Enter oil dipstick level (1 to 4): 1

Vehicle requires oil change.

6)Output:

Select your choice

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

Checking recharge...

Please enter odometer reading: 350

Vehicle is good to go.

Enter your choice: 470

Invalid choice. Please try again.

Enter your choice: 7

Checking recharge...

Please enter odometer reading: 470

Recharge battery advised.

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.