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:

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{

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.

ANS :

// Header files

#include <iostream>

#include <string>

using namespace std;

//Assign max vehicles

const int MAX_VEHICLES = 100;

// Assign the class

class Vehicle

{

protected:

string make;

string model;

int year;

long int odometer;

static Vehicle v[MAX_VEHICLES];

static int vehCount;

public:

Vehicle(string _make = "", string _model = "", int _year = 0, long int _odometer = 0)

{

make = _make;

model = _model;

year = _year;

odometer = _odometer;

}

string getMake() {

return make;

}

string getModel() {

return model;

}

int getYear() {

return year;

}

long int getOdo() {

return odometer;

}

void setModel(string val) {

model = val;

}

void setMake(string val) {

make = val;

}

void setYear(int val) {

year = val;

}

void setOdo(long int val) {

odometer = val;

}

void checkTireRotation() {

int remainder = odometer - 50000;

if (remainder > 0)

cout << "Please replace tires. Tires in good condition. Remaining: " << (0 - remainder) << endl;

else

cout << "Tire rotation not required. Tires in good condition." << endl;

}

string getNewModel() {

string model;

cout << "Enter model: ";

cin >> model;

return model;

}

long int getNewOdometer() {

long int odo;

cout << "Enter odometer reading: ";

cin >> odo;

return odo;

}

int getNewType() {

int type = -1;

do {

cout << "Enter 0: Gas\n1. Disel\n2. Electric: ";

cin >> type;

} while (type < 0 || type > 2);

return type;

}

int getNewYear() {

int year;

cout <<"Enter year of manufactur: ";

cin >> year;

return year;

}

string getNewMake() {

string make;

cout <<"Enter the make: ";

cin >> make;

return make;

}

void addVehicle() {

if (vehCount >= MAX_VEHICLES) {

cout << "Inventory is full. Cannot add more vehicles." << endl;

return;

}

int type = getNewType();

string make = getNewMake();

string model = getNewModel();

long int odo = getNewOdometer();

int year = getNewYear();

v[vehCount].setMake(make);

v[vehCount].setModel(model);

v[vehCount].setOdo(odo);

v[vehCount].setYear(year);

vehCount++;

cout << "Added vehicle" << endl;

}

void updateVehicle() {

string make;

cout << "Enter the make of the vehicle you need to update: ";

cin >> make;

bool found = false;

for (int i = 0; i < vehCount; i++) {

if (v[i].getMake() == make) {

found = true;

int selection;

cout << "Enter the parameter to be updated:\n1. Year\n2. Odometer reading: ";

cin >> selection;

if (selection == 1) {

int newYear;

cout << "Enter new manufacturing year: ";

cin >> newYear;

v[i].setYear(newYear);

} else if (selection == 2) {

long int newOdo;

cout << "Enter new odometer reading: ";

cin >> newOdo;

v[i].setOdo(newOdo);

} else {

cout << "Invalid selection" << endl;

}

break;

}

}

if (!found)

cout << "Car make not found" << endl;

}

void removeVehicle() {

string make;

cout << "Enter the make of the vehicle you need to remove: ";

cin >> make;

bool found = false;

for (int i = 0; i < vehCount; i++) {

if (v[i].getMake() == make) {

found = true;

for (int j = i; j < vehCount - 1; j++) {

v[j] = v[j + 1];

}

vehCount--;

cout << "Vehicle removed" << endl;

break;

}

}

if (!found)

cout << "Car make not found" << endl;

}

void listVehicles() {

if (vehCount == 0) {

cout << "No vehicles in inventory" << endl;

return;

}

cout << "Inventory:" << endl;

cout << "Make\tModel\tOdometer\tYear" << endl;

for (int i = 0; i < vehCount; i++) {

cout << v[i].getMake() << "\t" << v[i].getModel() << "\t" << v[i].getOdo() << "\t" << v[i].getYear() << endl;

}

}

void oilChange() {

string make;

cout << "Enter the make of the vehicle you need to check oil: ";

cin >> make;

bool found = false;

for (int i = 0; i < vehCount; i++) {

if (v[i].getMake() == make) {

found = true;

cout << "Oil change not required" << endl;

break;

}

}

if (!found)

cout << "Car make not found" << endl;

}

void checkCharge() {

string make;

cout << "Enter the make of the vehicle you need to check battery: ";

cin >> make;

bool found = false;

for (int i = 0; i < vehCount; i++) {

if (v[i].getMake() == make) {

found = true;

cout << "Batteries are good to go" << endl;

break;

}

}

if (!found)

cout << "Car make not found" << endl;

}

};

Vehicle Vehicle::v[MAX_VEHICLES];

int Vehicle::vehCount = 0;

int main() {

Vehicle vehicle;

char choice;

do {

cout << "Choose the required option:\n";

cout << "0. Quit\n";

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

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

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

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

cout << "5. Check tire rotation\n";

cout << "6. Check oil\n";

cout << "7. Check battery\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case '0':

cout << "Quitting the program...\n";

break;

case '1':

vehicle.listVehicles();

break;

case '2':

vehicle.addVehicle();

break;

case '3':

vehicle.updateVehicle();

break;

case '4':

vehicle.removeVehicle();

break;

case '5':

vehicle.checkTireRotation();

break;

case '6':

vehicle.oilChange();

break;

case '7':

vehicle.checkCharge();

break;

default:

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

break;

}

cout << endl;

} while (choice != '0');

return 0;

}

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.