Challenge

Project 3

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:

Make

Model

Year

Odometer reading

The library interfaces or the API’s (Application Program Interface) should be capable of adding, updating, listing and removing the entries at runtime.

Consider there are three classes such as

Vehicle

This class includes protected members such as Make, Model, Year and Odometer. These members are initialized using the class constructor.

The functions include getter and setter functions of Make(), Model(), Year() and Odometer().

Maintenance activities that should be done are checkTireRotation(), getCarInformation()

Let the three fuel types be

enum Type{

GAS,

DIESEL,

ELECTRIC

};

Let there be a pure virtual method to get the fuel type i.e.,

virtual Type getType() = 0;

GasCar

This is inherited from the vehicle class and has functions getType() and getFuelType()

DieselCar

This class is also a subclass of vehicle and the functions include getFuelType() and getType()

ElectricCar

This is a subclass of Vehicle class and methods include

checkTireRotation()- based on odometer reading, we decide whether the car needs to rotate tires or not.

checkChargerPower- based on odometer reading this advices to recharge power

getType() – to get the fuel type

FuelVehicle

This is an abstract class that inherits vehicle class. The methods in this class include checkOil() and pure virtual function getFuelType()

Maintenance activities that has to be done are as follows,

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.

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.

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

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

addVehicle() – to add new vehicle to inventory

ListVehicle()- to list the vehicles in the inventory

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

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

Tire Rotation() – Check if tires need rotation

OilChange()- check if car requires oil change

Recharge() – check if vehicle needs recharge

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.

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"

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

getNewMake() – If input is blank, request again

getNewModel() – If input is blank, request again

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

getNewOdometer()- Input should be only numericals

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

Retrieve the selected car from the user input

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

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

Answer :

here is a comprehensive object-oriented C++ library to manage automobile maintenance for different car types (Gas, Diesel, Electric). This program covers the addition, update, listing, and removal of vehicle entries at runtime and checks for maintenance activities such as tire rotation, oil change, and recharge.

File Structure

Vehicle.h

FuelVehicle.h

GasCar.h

DieselCar.h

ElectricCar.h

Inventory.h

main.cpp

Vehicle.h

#ifndef VEHICLE_H

#define VEHICLE_H

#include <string>

#include <iostream>

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

virtual ~Vehicle() = default;

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) { this->make = make; }

void setModel(const std::string& model) { this->model = model; }

void setYear(int year) { this->year = year; }

void setOdometer(int odometer) { this->odometer = odometer; }

virtual Type getType() const = 0;

virtual void checkTireRotation() const {

if (odometer % 50000 == 0) {

std::cout << "Tire rotation needed.\n";

} else {

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

}

}

void getCarInformation() const {

std::cout << "Make: " << make << ", Model: " << model << ", Year: " << year << ", Odometer: " << odometer << std::endl;

}

};

#endif // VEHICLE_H

FuelVehicle.h

#ifndef FUELVEHICLE_H

#define FUELVEHICLE_H

#include "Vehicle.h"

class FuelVehicle : public Vehicle {

public:

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

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

virtual void checkOil() const {

std::cout << "Checking oil...\n";

}

virtual Type getFuelType() const = 0;

};

#endif // FUELVEHICLE_H

GasCar.h

#ifndef GASCAR_H

#define GASCAR_H

#include "FuelVehicle.h"

class GasCar : public FuelVehicle {

public:

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

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

Type getType() const override {

return GAS;

}

Type getFuelType() const override {

return GAS;

}

};

#endif // GASCAR_H

DieselCar.h

#ifndef DIESELCAR_H

#define DIESELCAR_H

#include "FuelVehicle.h"

class DieselCar : public FuelVehicle {

public:

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

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

Type getType() const override {

return DIESEL;

}

std::string getFuelType() const override {

return "Diesel";

}

};

#endif // DIESELCAR_H

ElectricCar.h

#ifndef ELECTRICCAR_H

#define ELECTRICCAR_H

#include "Vehicle.h"

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 ELECTRIC;

}

void checkTireRotation() const override {

if (odometer >= 50000) {

std::cout << "Tires need rotation.\n";

} else {

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

}

}

void checkChargerPower() const {

if (odometer >= 400) {

std::cout << "Recharge needed.\n";

} else {

std::cout << "Battery is good.\n";

}

}

};

#endif // ELECTRICCAR_H

Inventory.h

#ifndef INVENTORY_H

#define INVENTORY_H

#include <vector>

#include <memory>

#include "Vehicle.h"

#include "GasCar.h"

#include "DieselCar.h"

#include "ElectricCar.h"

class Inventory {

private:

std::vector<std::shared_ptr<Vehicle>> vehicles;

public:

void addVehicle(std::shared_ptr<Vehicle> vehicle) {

vehicles.push_back(vehicle);

}

void listVehicles() const {

for (const auto& vehicle : vehicles) {

vehicle->getCarInformation();

}

}

void updateVehicle(int index, const std::string& make, const std::string& model, int year, int odometer) {

if (index >= 0 && index < vehicles.size()) {

vehicles[index]->setMake(make);

vehicles[index]->setModel(model);

vehicles[index]->setYear(year);

vehicles[index]->setOdometer(odometer);

}

}

void removeVehicle(int index) {

if (index >= 0 && index < vehicles.size()) {

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

}

}

void checkTireRotation(int index) const {

if (index >= 0 && index < vehicles.size()) {

vehicles[index]->checkTireRotation();

}

}

void checkOilChange(int index) const {

auto fuelVehicle = std::dynamic_pointer_cast<FuelVehicle>(vehicles[index]);

if (fuelVehicle) {

fuelVehicle->checkOil();

} else {

std::cout << "Oil change not applicable.\n";

}

}

void checkRecharge(int index) const {

auto electricCar = std::dynamic_pointer_cast<ElectricCar>(vehicles[index]);

if (electricCar) {

electricCar->checkChargerPower();

} else {

std::cout << "Recharge not applicable.\n";

}

}

std::shared_ptr<Vehicle> getVehicle(int index) const {

if (index >= 0 && index < vehicles.size()) {

return vehicles[index];

}

return nullptr;

}

int size() const {

return vehicles.size();

}

};

#endif // INVENTORY_H

main.cpp

#include <iostream>

#include "Inventory.h"

void printMenu() {

std::cout << " 0. Quit.\n";

std::cout << " 1. List inventory.\n";

std::cout << " 2. Add vehicle.\n";

std::cout << " 3. Update vehicle.\n";

std::cout << " 4. Remove vehicle.\n";

std::cout << " 5. Tire Rotation.\n";

std::cout << " 6. Oil Change.\n";

std::cout << " 7. Recharge.\n";

}

int main() {

Inventory inventory;

int choice = -1;

while (choice != 0) {

printMenu();

std::cout << "Enter your choice: ";

std::cin >> choice;

if (choice == 1) {

inventory.listVehicles();

} else if (choice == 2) {

std::string make, model;

int year, odometer, type;

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

std::cin >> odometer;

std::cout << "Enter Type (0: Gas, 1: Diesel, 2: Electric): ";

std::cin >> type;

std::shared_ptr<Vehicle> vehicle;

if (type == GAS) {

vehicle = std::make_shared<GasCar>(make, model, year, odometer);

} else if (type == DIESEL) {

vehicle = std::make_shared<DieselCar>(make, model, year, odometer);

} else if (type == ELECTRIC) {

vehicle = std::make_shared<ElectricCar>(make, model, year, odometer);

}

if (vehicle) {

inventory.addVehicle(vehicle);

}

} else if (choice == 3) {

int index;

std::string make, model;

int year, odometer;

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

std::cin >> index;

std::cout << "Enter new Make: ";

std::cin >> make;

std::cout << "Enter new Model: ";

std::cin >> model;

std::cout << "Enter new Year: ";

std::cin >> year;

std::cout << "Enter new Odometer: ";

std::cin >> odometer;

inventory.updateVehicle(index, make, model, year, odometer);

} else if (choice == 4) {

int index;

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

std::cin >> index;

inventory.removeVehicle(index);

} else if (choice == 5) {

int index;

std::cout << "Enter index of vehicle to check tire rotation: ";

std::cin >> index;

inventory.checkTireRotation(index);

} else if (choice == 6) {

int index;

std::cout << "Enter index of vehicle to check oil change: ";

std::cin >> index;

inventory.checkOilChange(index);

} else if (choice == 7) {

int index;

std::cout << "Enter index of vehicle to check recharge: ";

std::cin >> index;

inventory.checkRecharge(index);

} else if (choice != 0) {

std::cout << "Invalid choice. Please try again.\n";

}

}

return 0;

}

Output :

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

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

Enter Model: Elxsi

Enter Year: 2024

Enter Odometer: 00005

Enter Type (0: Gas, 1: Diesel, 2: Electric): 2

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

Enter index of vehicle to check tire rotation: 20

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

Enter index of vehicle to check oil change: 00100

Explanation:

Vehicle Class: Defines the basic attributes and methods for a vehicle.

FuelVehicle Class: An abstract class that adds specific functionalities for fuel-powered vehicles.

GasCar, DieselCar, ElectricCar Classes: These inherit from Vehicle and FuelVehicle, implementing specific methods.

Inventory Class: Manages the collection of vehicles and their maintenance activities.

main.cpp: Provides a user interface to interact with the inventory, adding, updating, listing, and removing vehicles, and checking maintenance activities.

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.