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. 

Every vehicle should have the following details:

  1. Make
  2. Model
  3. Year
  4. Odometer reading

#include

#include

class Vehicle {

public:

    Vehicle(const std::string& make, const std::string& model, int year, int odometer)

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

    virtual void performMaintenance() const = 0;

    void displayDetails() const {

        std::cout << "Make: " << make>

        std::cout << "Model: " << model>

        std::cout << "Year: " << year>

        std::cout << "Odometer: " << odometer>

    }

protected:

    std::string make;

    std::string model;

    int year;

    int odometer;

};

class PetrolCar : public Vehicle {

public:

    PetrolCar(const std::string& make, const std::string& model, int year, int odometer)

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

    void performMaintenance() const override {

        std::cout << "Maintenance tasks for Petrol Car:" << std>

        std::cout << "- Oil change" << std>

        std::cout << "- Replace oil filter" << std>

        // Add other tasks specific to petrol cars

    }

};

class DieselCar : public Vehicle {

public:

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

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

    void performMaintenance() const override {

        std::cout << "Maintenance tasks for Diesel Car:" << std>

        std::cout << "- Oil change" << std>

        std::cout << "- Replace oil filter" << std>

        // Add other tasks specific to diesel cars

    }

};

class ElectricCar : public Vehicle {

public:

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

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

    void performMaintenance() const override {

        std::cout << "Maintenance tasks for Electric Car:" << std>

        std::cout << "- Check the charging system" << std>

        std::cout << "- Examine battery" << std>

        // Add other tasks specific to electric cars

    }

};

int main() {

    PetrolCar petrolCar("Toyota", "Corolla", 2020, 30000);

    DieselCar dieselCar("Volkswagen", "Passat", 2019, 45000);

    ElectricCar electricCar("Tesla", "Model S", 2022, 20000);

    petrolCar.displayDetails();

    petrolCar.performMaintenance();

    dieselCar.displayDetails();

    dieselCar.performMaintenance();

    electricCar.displayDetails();

    electricCar.performMaintenance();

    return 0;

}

Output :

Make: Toyota

Model: Corolla

Year: 2020

Odometer: 30000 miles

Maintenance tasks for Petrol Car:

- Oil change

- Replace oil filter

Make: Volkswagen

Model: Passat

Year: 2019

Odometer: 45000 miles

Maintenance tasks for Diesel Car:

- Oil change

- Replace oil filter

Make: Tesla

Model: Model S

Year: 2022

Odometer: 20000 miles

Maintenance tasks for Electric Car:

- Check the charging system

- Examine battery

 

 

 

  OUTPUT :

 Maintenance activities that has to be done are as follows,

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

 #include

int main() {

    // Get the current distance run and remaining distance for tire rotation

    int distanceRun;

    std::cout << "Enter the current distance your car has run (in km): ";

    std::cin >> distanceRun;

    int tireCycle = 50000;

    int remainder = tireCycle - (distanceRun % tireCycle);

    if (remainder >= 0) {

        std::cout << "It's time to rotate your tires. Remainder: " << remainder>

    } else {

        std::cout << "Your tires are good to go.\n";

    }

    // Check power

    int powerDistance = 400;

    if (distanceRun == powerDistance) {

        std::cout << "It's time to recharge your battery.\n";

    } else {

        std::cout << "Your vehicle's power is good to go.\n";

    }

    return 0;

}

 

 Now that we have learnt about the classes to be created, we need to write our driver code i.e., main.cpp

  1. In the main class, initialize the inventory and ask the user to select whether to add, update, list or delete the Vehicle list.

    1. addVehicle() – to add new vehicle to inventory

    2. ListVehicle()- to list the vehicles in the inventory

    3. UpdateVehicle()- to update the vehicle in the inventory list

    4. RemoveVehicle()- to remove the vehicle from the inventory list

    5. Tire Rotation() – Check if tires need rotation

    6. OilChange()- check if car requires oil change

    7. Recharge() – check if vehicle needs recharge

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

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

 
 
 
  1. Retrieve the user input on the vehicle’s make, model, year, type and odometer.

    1. getNewMake() – If input is blank, request again

    2. getNewModel() – If input is blank, request again

    3. getYear() – If input doesn’t have 4 digits, request again

    4. getNewOdometer()- Input should be only numericals

    5. getNewType() – Input should be within 0-2, else request again

  2. Retrieve the selected car from the user input

    1. selectCar() – Input should be within the size of vehicle list, else request again

    2. getNewOdometerReading() – New odometer reading is used to calculate the oil change, tire rotation, power recharge etc.


 #include

#include

#include

class Vehicle {

public:

    Vehicle(const std::string& make, const std::string& model, int year, int odometer, int type)

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

    std::string getMake() const { return make; }

    std::string getModel() const { return model; }

    int getYear() const { return year; }

    int getOdometer() const { return odometer; }

    int getType() const { return type; }

private:

    std::string make;

    std::string model;

    int year;

    int odometer;

    int type;

};

class Inventory {

public:

    void addVehicle(const Vehicle& vehicle) {

        vehicles.push_back(vehicle);

    }

    void listVehicles() {

        for (int i = 0; i < vehicles>

            std::cout << "Vehicle " << i>

                      << ", Year: " << vehicles>

        }

    }

    void updateVehicle(int index, int newOdometer) {

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

            vehicles[index] = Vehicle(vehicles[index].getMake(), vehicles[index].getModel(), vehicles[index].getYear(), newOdometer, vehicles[index].getType());

            std::cout << "Vehicle information updated." << std>

        } else {

            std::cout << "Invalid vehicle index." << std>

        }

    }

    void removeVehicle(int index) {

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

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

            std::cout << "Vehicle removed from the inventory." << std>

        } else {

            std::cout << "Invalid vehicle index." << std>

        }

    }

    bool checkTireRotation(int index, int newOdometer) {

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

            int previousOdometer = vehicles[index].getOdometer();

            return (newOdometer - previousOdometer >= 5000);

        } else {

            std::cout << "Invalid vehicle index." << std>

            return false;

        }

    }

    bool checkOilChange(int index, int newOdometer) {

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

            int previousOdometer = vehicles[index].getOdometer();

            return (newOdometer - previousOdometer >= 10000);

        } else {

            std::cout << "Invalid vehicle index." << std>

            return false;

        }

    }

    bool checkRecharge(int index, int newOdometer) {

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

            int previousOdometer = vehicles[index].getOdometer();

            return (newOdometer - previousOdometer >= 200);

        } else {

            std::cout << "Invalid vehicle index." << std>

            return false;

        }

    }

private:

    std::vector vehicles;

};

int main() {

    Inventory inventory;

    while (true) {

        std::cout << "Menu:\n"

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

                  << "Select an option: ";

        int choice;

        std::cin >> choice;

        if (choice == 0) {

            break;

        } else if (choice == 1) {

            inventory.listVehicles();

        } else if (choice == 2) {

            std::string make, model;

            int year, odometer, type;

            std::cin.ignore(); // Clear the input buffer

            std::cout << "Enter make: ";

            std::getline(std::cin, make);

            std::cout << "Enter model: ";

            std::getline(std::cin, model);

            std::cout << "Enter year: ";

            std::cin >> year;

            std::cout << "Enter odometer reading: ";

            std::cin >> odometer;

            std::cout << "Enter type (0 - Sedan, 1 - SUV, 2 - Truck): ";

            std::cin >> type;

            inventory.addVehicle(Vehicle(make, model, year, odometer, type));

        } else if (choice == 3) {

            int index, newOdometer;

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

            std::cin >> index;

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

            std::cin >> newOdometer;

            inventory.updateVehicle(index - 1, newOdometer);

        } else if (choice == 4) {

            int index;

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

            std::cin >> index;

            inventory.removeVehicle(index - 1);

        } else if (choice == 5 || choice == 6 || choice == 7) {

            int index, newOdometer;

            std::cout << "Enter the index of the vehicle: ";

            std::cin >> index;

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

            std::cin >> newOdometer;

            bool requiresMaintenance = false;

            if (choice == 5) {

                requiresMaintenance = inventory.checkTireRotation(index - 1, newOdometer);

                if (requiresMaintenance)

                    std::cout << "Tire rotation is recommended." << std>

                else

                    std::cout << "No tire rotation is needed." << std>

            } else if (choice == 6) {

                requiresMaintenance = inventory.checkOilChange(index - 1, newOdometer);

                if (requiresMaintenance)

                    std::cout << "Oil change is recommended." << std>

                else

                    std::cout << "No oil change is needed." << std>

            } else if (choice == 7) {

                requiresMaintenance = inventory.checkRecharge(index - 1, newOdometer);

                if (requiresMaintenance)

                    std::cout << "Recharge is recommended." << std>

                else

                    std::cout << "No recharge is needed." << std>

            }

        } else {

            std::cout << "Invalid choice. Please try again." << std>

        }

    }

    return 0;

}

 Output:

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Select an option: 2

Enter make: v3

Enter model: v11

Enter year: 2018

Enter odometer reading: 1520

Enter type (0 - Sedan, 1 - SUV, 2 - Truck): 3

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Select an option: 5

Enter the index of the vehicle: 5

Enter the new odometer reading: 1520

Invalid vehicle index.

No tire rotation is needed.

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Select an option: 6

Enter the index of the vehicle: 56

Enter the new odometer reading: 1520

Invalid vehicle index.

No oil change is needed.

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Select an option: 7

Enter the index of the vehicle: 52

Enter the new odometer reading: 1450

Invalid vehicle index.

No recharge is needed.

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Select an option: 0

 OUTPUT :

 

 

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.