Challenge

Project 2

Solution-

Code for the problem

#include
#include
#include

enum class Type {
    GAS,
    DIESEL,
    ELECTRIC
};

class Vehicle {
protected:
    std::string make_;
    std::string model_;
    int year_;
    int odometer_;

public:
    Vehicle(const std::string& make, const std::string& model, int year, int odometer)
        : make_(make), model_(model), year_(year), odometer_(odometer) {}

    // Getter and setter methods
    std::string getMake() const { return make_; }
    std::string getModel() const { return model_; }
    int getYear() const { return year_; }
    int getOdometer() const { return odometer_; }
    void setMake(const std::string& make) { make_ = make; }
    void setModel(const std::string& model) { model_ = model; }
    void setYear(int year) { year_ = year; }
    void setOdometer(int odometer) { odometer_ = odometer; }

    virtual Type getType() const = 0;
};

class GasCar : public Vehicle {
public:
    GasCar(const std::string& make, const std::string& model, int year, int odometer)
        : Vehicle(make, model, year, odometer) {}

    Type getType() const override { return Type::GAS; }
};

class DieselCar : public Vehicle {
public:
    DieselCar(const std::string& make, const std::string& model, int year, int odometer)
        : Vehicle(make, model, year, odometer) {}

    Type getType() const override { return Type::DIESEL; }
};

class ElectricCar : public Vehicle {
public:
    ElectricCar(const std::string& make, const std::string& model, int year, int odometer)
        : Vehicle(make, model, year, odometer) {}

    Type getType() const override { return Type::ELECTRIC; }

    void checkTireRotation() {
        // TODO: Implement tire rotation logic
    }

    void checkChargerPower() {
        // TODO: Implement power recharge logic
    }
};

class Inventory {
private:
    std::vector vehicles_;

public:
    ~Inventory() {
        for (auto vehicle : vehicles_) {
            delete vehicle;
        }
    }

    void addVehicle(Vehicle* vehicle) {
        vehicles_.push_back(vehicle);
    }

    void listVehicles() const {
        std::cout << "Inventory:\n";
        for (int i = 0; i < vehicles_.size(); i++) {
            std::cout << i << ". " << vehicles_[i]->getMake() << " " << vehicles_[i]->getModel() << " (" << vehicles_[i]->getYear() << ")\n";
        }
    }

    void updateVehicle(int index) {
        // TODO: Implement vehicle update logic
    }

    void removeVehicle(int index) {
        // TODO: Implement vehicle removal logic
    }

    void checkTireRotation(int index) {
        // TODO: Implement tire rotation check logic
    }

    void checkOilChange(int index) {
        // TODO: Implement oil change check logic
    }

    void checkPowerRecharge(int index) {
        // TODO: Implement power recharge check logic
    }
};

int main() {
    Inventory inventory;

    while (true) {
        std::cout << "Select an option:\n"
          << "   0. Quit.\n"
          << "   1. Add a vehicle.\n"
          << "   2. List vehicles.\n"
          << "   3. Update a vehicle.\n"
          << "   4. Remove a vehicle.\n"
          << "   5. Check tire rotation.\n"
          << "   6. Check oil change.\n"
          << "   7. Check power recharge.\n";

    }
}
 
 
Steps involved in -
  1. First, we include the necessary header files, which include iostream, string, vector, and algorithm. (These are those files that store predefined functions. It contains definitions of functions that you can include or import using a preprocessor directive #include. This preprocessor directive tells the compiler that the header file needs to be processed prior to the compilation.)

  2. We define an enum called Type that includes GAS, DIESEL, and ELECTRIC.(enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex)

  3. We create a base class called Vehicle that includes protected member variables such as Make, Model, Year, and Odometer. We define a constructor and getter and setter functions for these member variables. We also define a pure virtual function called getType().( a base class is an existing class from which the other classes are determined and properties are inherited)

  4. We create three derived classes called GasCar, DieselCar, and ElectricCar that inherit from the Vehicle class. These classes define their own getType() functions and getFuelType() functions specific to their fuel type.(A derived class is a class that is constructed from a base class or an existing class. It has a tendency to acquire all the methods and properties of a base class. It is also known as a subclass or child class)

  5. We create an abstract class called FuelVehicle that inherits from the Vehicle class. This class defines a checkOil() function and a pure virtual function called getFuelType().(An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function)

  6. We implement the maintenance functions checkTireRotation(), checkPower(), and oilChange() in the ElectricCar class, which will be called based on the odometer reading.

  7. We define a class called Inventory that includes a vector of Vehicle pointers called inventoryList. We also define functions for adding, updating, listing, and removing vehicles from the inventory.(A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private).

  8. In the main function, we create an instance of the Inventory class and prompt the user to select an option from a menu. We then call the corresponding function based on the user's input.

  9. When the user selects the option to add a new vehicle, we prompt the user for information about the vehicle and create an instance of the appropriate derived class based on the fuel type.

  10. When the user selects the option to list the inventory, we iterate through the vector of vehicles and output the details of each vehicle.

  11. When the user selects the option to update a vehicle, we prompt the user for the index of the vehicle to update and the new information for the vehicle.

  12. When the user selects the option to remove a vehicle, we prompt the user for the index of the vehicle to remove and remove it from the vector of vehicles.

  13. When the user selects the option to check tire rotation, we prompt the user for the index of the vehicle and check if the tires need rotation based on the odometer reading.

  14. When the user selects the option to check for an oil change, we prompt the user for the index of the vehicle and check if an oil change is needed based on the odometer reading.

  15. When the user selects the option to check for a recharge, we prompt the user for the index of the vehicle and check if a recharge is needed based on the odometer reading.

  16. If the user enters an invalid option, we prompt them to enter a valid option.

  17. Once the user selects the "Quit" option, we exit the program.

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.