Challenge

Project 3

Project 3

Aim:

The aim of this project is to create an object-oriented library for tracking automobile maintenance. The project provides a flexible framework for managing various types of vehicles, including Gas, Diesel, and Electric cars. It allows users to add, update, list, and remove vehicle entries at runtime. Each vehicle is represented by its make, model, year, and odometer reading.

Description:

The project defines a hierarchy of classes, including the base class "Vehicle" with common attributes and abstract methods for fuel type and maintenance checks. Subclasses "GasCar" and "DieselCar" inherit from "Vehicle" and provide specific implementations for gas and diesel vehicles, respectively. "ElectricCar" is another subclass that extends "Vehicle" for electric cars, introducing a battery percentage attribute and a unique maintenance check related to recharging.

A "VehicleInventory" class manages a collection of vehicles, enabling users to perform operations like adding, updating, listing, and removing vehicles. The main program provides a user-friendly menu to interact with the vehicle inventory, making it easy to check for maintenance requirements specific to each vehicle type. In summary, this project offers a comprehensive solution for tracking and managing the maintenance of different types of vehicles, promoting code reusability, modularity, and extensibility while catering to the specific needs of each vehicle category.

C++ program for tracking automobile maintenance, various types of pointers, classes, functions, and other C++ features are used. Here's a description of these elements:

Pointers:

Vehicle* and its derived classes (GasCar*, DieselCar*, and ElectricCar*): These are used as pointers to objects of different vehicle types. Polymorphism is achieved through pointers to the base class Vehicle, which allows handling various vehicle types uniformly.

Classes:

Vehicle: The base class representing common attributes and behaviors for all vehicles.

GasCar, DieselCar, ElectricCar: Derived classes representing specific types of vehicles and providing their own implementations.

VehicleInventory: A class for managing a collection of vehicles, including operations to add, update, list, and remove vehicles.

Functions:

main(): The main function where the program execution starts. It handles the user interface, menu, and the interaction with the vehicle inventory.

getMenuChoice(): A function to get and validate the user's menu choice.

getYear(): A function to get and validate the year input.

getNewOdometerReading(): A function to get and validate the odometer reading input.

getNewType(): A function to get and validate the fuel type input.

getNewMake(): A function to get and validate the make input.

getNewModel(): A function to get and validate the model input.

selectCar(): A function to select a vehicle from the inventory list.

Enums:

FuelType: An enumeration to represent different fuel types (GAS, DIESEL, ELECTRIC).

Dynamic Memory Management:

new: Dynamic memory allocation is used to create objects of different vehicle types, such as GasCar, DieselCar, and ElectricCar.

delete: Dynamic memory deallocation is used when removing vehicles from the inventory.

Inheritance and Polymorphism:

Inheritance is used to create a hierarchy of classes, where subclasses inherit attributes and methods from the base class (Vehicle).

Polymorphism is achieved through pointers to the base class, allowing different vehicle types to be treated uniformly.

Object-Oriented Principles:

Abstraction, encapsulation, and inheritance are utilized to model the behavior of different vehicle types, promoting code reusability and maintainability.

I/O Streams:

std::cin and std::cout are used for user input and output, enabling interaction with the program.

Loop Structures:

do-while loops are used for menu choices, input validation, and error handling.

Conditional Statements:

if-else statements are used for making decisions and providing different outputs based on conditions.

Vector Container:

The std::vector container is used to store and manage a collection of vehicle pointers within the VehicleInventory class.

Error Handling:

Input validation and error handling are implemented using conditional statements and loops to ensure valid user input.

Code:

/*
 * service.c++
 *
 *  Created on: Oct 17, 2023
 *      Author: laxma
 */
#include 
#include 
#include 
#include 
#include 

enum FuelType {
    GAS,
    DIESEL,
    ELECTRIC
};

class Vehicle {
protected:
    std::string Make;
    std::string Model;
    int Year;
    double Odometer;

public:
    Vehicle(const std::string& make, const std::string& model, int year, double odometer)
        : Make(make), Model(model), Year(year), Odometer(odometer) {}

    virtual ~Vehicle() {}

    const std::string& getMake() const {
        return Make;
    }

    const std::string& getModel() const {
        return Model;
    }

    int getYear() const {
        return Year;
    }

    double getOdometer() const {
        return Odometer;
    }

    virtual FuelType getType() const = 0;
    virtual void doMaintenanceCheck() = 0;
};

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

    FuelType getType() const override {
        return GAS;
    }

    void doMaintenanceCheck() override {
        std::cout << "Performing maintenance check for Gas Car" << std xss=removed xss=removed> vehicles;

public:
    ~VehicleInventory() {
        for (auto vehicle : vehicles) {
            delete vehicle;
        }
    }

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

    void listVehicles() const {
        std::cout << "Vehicle Inventory:" << std xss=removed>getMake() << " " << vehicles>getModel() << std>doMaintenanceCheck();
        }
    }

    void removeVehicle(size_t index) {
        if (index < vehicles>> choice;

        if (std::cin.fail()) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits::max(), '\n');
        } else if (choice < 0> 7) {
            std::cout << "Invalid choice. Please try again." << std>> year;

        if (std::cin.fail() || year < 1000> 9999) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits::max(), '\n');
            std::cout << "Invalid input. Please enter a 4-digit year." << std>> odometer;

        if (std::cin.fail() || odometer < 0>::max(), '\n');
            std::cout << "Invalid input. Please enter a non-negative number." << std>> type;

        if (std::cin.fail() || type < 0> 2) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits::max(), '\n');
            std::cout << "Invalid input. Please enter 0, 1, or 2." << std>(type);
}

std::string getNewMake() {
    std::string make;
    do {
        std::cout << "Enter the make: ";
        std::cin.ignore();
        std::getline(std::cin, make);
        if (make.empty()) {
            std::cout << "Make cannot be empty. Please try again." << std>> index;

        if (std::cin.fail() || index >= inventory.getVehicleCount()) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits::max(), '\n');
            std::cout << "Invalid input. Please enter a valid index." << std xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>> batteryPercentage;

                        if (std::cin.fail() || batteryPercentage < 0> 100) {
                            std::cin.clear();
                            std::cin.ignore(std::numeric_limits::max(), '\n');
                            std::cout << "Invalid input. Please enter a valid battery percentage." << std> 0) {
                    size_t selectedCar = selectCar(inventory);
                    // Perform maintenance check for the selected vehicle
                    inventory.performMaintenanceCheck(selectedCar);
                } else {
                    std::cout << "No vehicles in the inventory to update." << std> 0) {
                    size_t selectedCar = selectCar(inventory);
                    inventory.removeVehicle(selectedCar);
                } else {
                    std::cout << "No vehicles in the inventory to remove." << std> 0) {
                    size_t selectedCar = selectCar(inventory);
                    // Implement tire rotation check for the selected vehicle here
                    Vehicle* selectedVehicle = inventory.getVehicle(selectedCar);
                    std::cout << "Tire rotation check for " << selectedVehicle>getMake() << " " << selectedVehicle>getModel() << std> 0) {
                    size_t selectedCar = selectCar(inventory);
                    // Implement oil change check for the selected vehicle here (only for Gas and Diesel cars)
                    Vehicle* selectedVehicle = inventory.getVehicle(selectedCar);
                    if (selectedVehicle->getType() == GAS || selectedVehicle->getType() == DIESEL) {
                        std::cout << "Oil change check for " << selectedVehicle>getMake() << " " << selectedVehicle>getModel() << std> 0) {
                    size_t selectedCar = selectCar(inventory);
                    // Implement recharge check for the selected vehicle (Electric cars)
                    Vehicle* selectedVehicle = inventory.getVehicle(selectedCar);
                    if (selectedVehicle->getType() == ELECTRIC) {
                        ElectricCar* electricCar = dynamic_cast(selectedVehicle);
                        electricCar->checkChargerPower();
                    } else {
                        std::cout << "Recharge check not applicable for non-Electric cars." << std xss=removed>


Here, I'm using below Vehicles to obtain the output:

Gas Cars:

Make: Ford, Model: Mustang, Year: 2022

Make: Toyota, Model: Camry, Year: 2023

Diesel Cars:

Make: BMW, Model: X5, Year: 2023

Make: Mercedes-Benz, Model: E-Class, Year: 2022

Electric Cars:

Make: Tesla, Model: Model 3, Year: 2022

Make: Nissan, Model: Leaf, Year: 2023

Output:

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 1

Vehicle Inventory:

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 2

Enter the make: Ford

Enter the model: Mustang

Enter the year (4 digits): 2022

Enter the new odometer reading: 85650

Enter the fuel type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): 0

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 2

Enter the make: Toyota

Enter the model: Camry

Enter the year (4 digits): 2023

Enter the new odometer reading: 15480

Enter the fuel type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): 0

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 2

Enter the make: BMW

Enter the model: X5

Enter the year (4 digits): 2023

Enter the new odometer reading: 5640

Enter the fuel type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): 1

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 2

Enter the make: Mercedes-Benz

Enter the model: E-Class

Enter the year (4 digits): 2022

Enter the new odometer reading: 125360

Enter the fuel type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): 1

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 2

Enter the make: Tesla

Enter the model: Model 3

Enter the year (4 digits): 2022

Enter the new odometer reading: 75624

Enter the fuel type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): 2

Enter the battery percentage (0-100%): 60

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 2

Enter the make: Nissan

Enter the model: Leaf

Enter the year (4 digits): 2023

Enter the new odometer reading: 6954

Enter the fuel type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): 2

Enter the battery percentage (0-100%): 15

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 1

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. BMW 5

3. Mercedes-Benz -Class

4. Tesla odel 3

5. Nissan eaf

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 3

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. BMW 5

3. Mercedes-Benz -Class

4. Tesla odel 3

5. Nissan eaf

Enter the index of the vehicle: 3

Performing maintenance check for Diesel Car

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 5

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. BMW 5

3. Mercedes-Benz -Class

4. Tesla odel 3

5. Nissan eaf

Enter the index of the vehicle: 4

Tire rotation check for Tesla odel 3

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 4

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. BMW 5

3. Mercedes-Benz -Class

4. Tesla odel 3

5. Nissan eaf

Enter the index of the vehicle: 2

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 6

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 0

Oil change check for Ford ustang

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 6

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 1

Oil change check for Toyota amry

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 6

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 4

Oil change check not applicable for non-Gas/Diesel cars.

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 7

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 3

No recharging needed for Tesla odel 3

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 7

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 4

Advise recharging for Nissan eaf

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 7

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 4

Advise recharging for Nissan eaf

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 7

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 3

No recharging needed for Tesla odel 3

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 7

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 1

Recharge check not applicable for non-Electric cars.

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 4

Vehicle Inventory:

0. Ford ustang

1. Toyota amry

2. Mercedes-Benz -Class

3. Tesla odel 3

4. Nissan eaf

Enter the index of the vehicle: 1

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 1

Vehicle Inventory:

0. Ford ustang

1. Mercedes-Benz -Class

2. Tesla odel 3

3. Nissan eaf

 

Menu:

0. Quit

1. List inventory

2. Add vehicle

3. Update vehicle

4. Remove vehicle

5. Tire Rotation

6. Oil Change

7. Recharge

Enter your choice: 0

 

Result:

The result of this C++ project is a program that allows users to manage and maintain a fleet of vehicles with different fuel types (Gas, Diesel, Electric). Users can add, update, list, and remove vehicles, as well as perform maintenance checks specific to each vehicle type, such as oil change for Gas and Diesel cars and recharge checks for Electric cars. The program demonstrates object-oriented principles and uses dynamic memory management, user interaction, and validation to create a versatile vehicle inventory system.

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.