Challenge

Project 2

1.

Objective:

  • To create an object-oriented library for tracking automobile maintenance.
  • The library involves designing and implementing several classes to handle different types of bikes or cars and their maintenance tasks.

Step-by-Step Process:

  1. Vehicle Class: This class represents a basic vehicle with common attributes such as make, model, year, and odometer reading. It provides getter and setter methods for these attributes. Additionally, it has maintenance activities like checking tire rotation and providing car information.

  2. Fuel Types: An enumeration Type is used to represent different fuel types (GAS, DIESEL, ELECTRIC).

  3. GasCar, DieselCar, ElectricCar: These classes inherit from the Vehicle class and represent specific types of cars. They implement the getType() and getFuelType() methods, and the ElectricCar class also has methods for checking tire rotation and charger power. These classes handle the specific behavior and properties of each car type.

  4. FuelVehicle Class: This abstract class extends the Vehicle class and adds the checkOil() method and a pure virtual method getFuelType() that should be implemented by the subclasses.

  5. Main Function: This is the driver code that interacts with the library. It provides a menu for users to choose different operations such as listing, adding, updating, or removing vehicles, as well as performing maintenance tasks like tire rotation, oil change, and recharge. It includes methods to retrieve user inputs for vehicle details.

Let's make the program section by section:

Section 1: Header Inclusions and Enum Definition

#include <iostream>
#include <vector>
#include <string>

enum Type {
    GAS,
    DIESEL,
    ELECTRIC
};

This section includes necessary header files (iostream, vector, and string) and defines an enumeration Type to represent different fuel types: GAS, DIESEL, and ELECTRIC.

Section 2: Base Vehicle Class

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) {}

    const std::string& getMake() const { return make; }
    const std::string& getModel() const { return model; }
    int getYear() const { return year; }
    int getOdometer() const { return odometer; }

    virtual void checkTireRotation() {
        // ...
    }

    virtual void getCarInformation() const {
        // ...
    }

    virtual Type getType() = 0;
};

This section defines the base class Vehicle. It has protected attributes for vehicle details (make, model, year, odometer). It provides a constructor to initialize these attributes and getter methods to access them. The class declares virtual methods checkTireRotation() and getCarInformation() for maintenance tasks and car information display, respectively. The getType() method is declared as a pure virtual method to be implemented by derived classes.

Section 3: Derived Car Classes

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() override { return 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() override { return DIESEL; }
};

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

    void checkTireRotation() override {
        // ...
    }

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

This section defines three derived classes: GasCar, DieselCar, and ElectricCar, each inheriting from the Vehicle base class. These classes implement the getType() method to return the corresponding fuel type and provide specific behavior for checkTireRotation() if applicable.

Section 4: CarMaintenanceTracker Class

class CarMaintenanceTracker {
private:
    std::vector<Vehicle*> vehicles;

public:
    ~CarMaintenanceTracker() {
        for (Vehicle* vehicle : vehicles) {
            delete vehicle;
        }
    }

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

    void listVehicles() const {
        for (const Vehicle* vehicle : vehicles) {
            vehicle->getCarInformation();
        }
    }
};

This section defines the CarMaintenanceTracker class responsible for managing the collection of vehicles. It has a private vector of pointers to Vehicle, which stores the vehicles. The destructor ensures proper memory cleanup by deleting the dynamically allocated vehicle objects. The class provides methods to add vehicles and list them.

Section 5: Main Function

int main() {
    CarMaintenanceTracker tracker;

    while (true) {
        std::cout << "Menu:\n"
                  << "   0. Quit.\n"
                  << "   1. List inventory.\n"
                  << "   2. Add Gas Car.\n"
                  << "   3. Add Diesel Car.\n"
                  << "   4. Add Electric Car.\n"
                  << "Select an option: ";

        int choice;
        std::cin >> choice;

        // Handling user choices
        if (choice == 0) {
            break;
        } else if (choice == 1) {
            tracker.listVehicles();
        } else if (choice >= 2 && choice <= 4) {
            // Handling vehicle addition based on user choice
            std::string make, model;
            int year, odometer;
            
            std::cout << "Enter make: ";
            std::cin >> make;
            
            std::cout << "Enter model: ";
            std::cin >> model;
            
            std::cout << "Enter year: ";
            std::cin >> year;
            
            std::cout << "Enter odometer reading: ";
            std::cin >> odometer;
            
            // Creating appropriate vehicle based on user choice
            if (choice == 2) {
                GasCar* gasCar = new GasCar(make, model, year, odometer);
                tracker.addVehicle(gasCar);
            } else if (choice == 3) {
                DieselCar* dieselCar = new DieselCar(make, model, year, odometer);
                tracker.addVehicle(dieselCar);
            } else if (choice == 4) {
                ElectricCar* electricCar = new ElectricCar(make, model, year, odometer);
                tracker.addVehicle(electricCar);
            }
        } else {
            std::cout << "Invalid choice. Please select a valid option.\n";
        }
    }

    return 0;
}

This section contains the main function, where the program execution starts. It creates an instance of CarMaintenanceTracker called tracker to manage the vehicles. The main loop displays a menu, takes user input, and responds based on the user's choices. This section handles the user's menu choices and interacts with the CarMaintenanceTracker class to perform the desired actions based on the user's input. It includes code for listing vehicles, adding new vehicles, and displaying error messages for invalid choices. The actual vehicle addition code for each choice is not complete here.

Let's Combine all sections and make single program:

#include <iostream>
#include <vector>
#include <string>

// Enum to represent different fuel types
enum Type {
    GAS,
    DIESEL,
    ELECTRIC
};

// Base class representing a Vehicle
class Vehicle {
protected:
    std::string make;
    std::string model;
    int year;
    int odometer;

public:
    // Constructor to initialize vehicle details
    Vehicle(const std::string& make, const std::string& model, int year, int odometer)
        : make(make), model(model), year(year), odometer(odometer) {}

    // Getter methods for vehicle attributes
    const std::string& getMake() const { return make; }
    const std::string& getModel() const { return model; }
    int getYear() const { return year; }
    int getOdometer() const { return odometer; }

    // Virtual methods for maintenance tasks
    virtual void checkTireRotation() {
        // ...
    }

    virtual void getCarInformation() const {
        // ...
    }

    virtual Type getType() = 0;  // Pure virtual method for fuel type
};

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

    // Implementing getType method for GasCar
    Type getType() override { return GAS; }
};

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

    // Implementing getType method for DieselCar
    Type getType() override { return DIESEL; }
};

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

    // Implementing checkTireRotation method for ElectricCar
    void checkTireRotation() override {
        // ...
    }

    // Implementing getType method for ElectricCar
    Type getType() override { return ELECTRIC; }
};

// CarMaintenanceTracker class to manage vehicles
class CarMaintenanceTracker {
private:
    std::vector<Vehicle*> vehicles;

public:
    // Destructor to free memory
    ~CarMaintenanceTracker() {
        for (Vehicle* vehicle : vehicles) {
            delete vehicle;
        }
    }

    // Method to add a vehicle
    void addVehicle(Vehicle* vehicle) {
        vehicles.push_back(vehicle);
    }

    // Method to list all vehicles
    void listVehicles() const {
        for (const Vehicle* vehicle : vehicles) {
            vehicle->getCarInformation();
        }
    }
};

int main() {
    CarMaintenanceTracker tracker;

    while (true) {
        std::cout << "Menu:\n"
                  << "   0. Quit.\n"
                  << "   1. List inventory.\n"
                  << "   2. Add Gas Car.\n"
                  << "   3. Add Diesel Car.\n"
                  << "   4. Add Electric Car.\n"
                  << "Select an option: ";

        int choice;
        std::cin >> choice;

        // Handling user choices
        if (choice == 0) {
            break;
        } else if (choice == 1) {
            tracker.listVehicles();
        } else if (choice >= 2 && choice <= 4) {
            // Handling vehicle addition based on user choice
            std::string make, model;
            int year, odometer;
            
            std::cout << "Enter make: ";
            std::cin >> make;
            
            std::cout << "Enter model: ";
            std::cin >> model;
            
            std::cout << "Enter year: ";
            std::cin >> year;
            
            std::cout << "Enter odometer reading: ";
            std::cin >> odometer;
            
            // Creating appropriate vehicle based on user choice
            if (choice == 2) {
                GasCar* gasCar = new GasCar(make, model, year, odometer);
                tracker.addVehicle(gasCar);
            } else if (choice == 3) {
                DieselCar* dieselCar = new DieselCar(make, model, year, odometer);
                tracker.addVehicle(dieselCar);
            } else if (choice == 4) {
                ElectricCar* electricCar = new ElectricCar(make, model, year, odometer);
                tracker.addVehicle(electricCar);
            }
        } else {
            std::cout << "Invalid choice. Please select a valid option.\n";
        }
    }

    return 0;
}

Full Program Explanation:

  1. Header Inclusions and Enum Definition: The program starts by including necessary header files and defining an enumeration Type to represent different fuel types.

  2. Base Vehicle Class: This section defines the base class Vehicle, which has protected attributes for make, model, year, and odometer reading. It provides a constructor to initialize these attributes and getter methods to access them. It also declares virtual methods for maintenance tasks such as tire rotation and obtaining car information. The getType method is declared as a pure virtual method to be implemented by derived classes.

  3. Derived Car Classes: This section defines three derived classes GasCar, DieselCar, and ElectricCar, each inheriting from Vehicle. These classes implement the getType method and provide additional functionality, such as the checkTireRotation method for ElectricCar.

  4. CarMaintenanceTracker Class: This section defines the CarMaintenanceTracker class responsible for managing the collection of vehicles. It has a vector of pointers to Vehicle, which will store the vehicles in the tracker. The destructor ensures proper memory cleanup. The class provides methods to add vehicles and list them.

  5. Main Function: The main function is where the program execution starts. It creates an instance of CarMaintenanceTracker called tracker to manage the vehicles. The main loop displays a menu, takes user input, and responds based on the user's choices.

    • If the user chooses to quit (0), the program breaks out of the loop and terminates.
    • If the user chooses to list inventory (1), the program calls the listVehicles method to display vehicle information.
    • If the user chooses to add a vehicle (2-4), the program prompts the user for vehicle details, creates an appropriate type of vehicle, and adds it to the tracker.
  6. Handling User Choices: This section handles the user's menu choices and interacts with the CarMaintenanceTracker class to perform the desired actions.

Overall, the program structure is designed to create and manage different types of vehicles using object-oriented principles, with user interaction provided through the console menu. It allows adding vehicles of different types, listing their information, and exiting the program.

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.