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:

  1. Make

  2. Model

  3. Year

  4. 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

  1. Vehicle

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

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

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

    4. Let the three fuel types be

enum Type{

GAS,

DIESEL,

ELECTRIC

};

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

    1. virtual Type getType() = 0;

  1. GasCar

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

  2. DieselCar

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

  3. ElectricCar

    1. This is a subclass of Vehicle class and methods include

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

      2. checkChargerPower- based on odometer reading this advices to recharge power

      3. getType() – to get the fuel type

  4. FuelVehicle

    1. 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,

  1. 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.

  2. 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

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

    1. addVehicle() – to add new vehicle to inventory

    2. ListVehicle()- to list the vehicles in the inventory

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

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

    5. Tire Rotation() – Check if tires need rotation

    6. OilChange()- check if car requires oil change

    7. Recharge() – check if vehicle needs recharge

  2. 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.

  3. 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"

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

    1. getNewMake() – If input is blank, request again

    2. getNewModel() – If input is blank, request again

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

    4. getNewOdometer()- Input should be only numericals

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

  2. Retrieve the selected car from the user input

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

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

Aim:

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.

The code is:

#include
#include

using namespace std;


class Vehicle{
	
	protected:
		string make,model;
		int year;
		long int odometer;
		
	public:
		Vehicle(string _make = "", string _model="",int _year=0, int _odo=0){
			make = _make;
			model = _model;
			year = _year;
			odometer = _odo;
		}
		
		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(int val){
			odometer = val;
		}
		
		void checkTireRotation(){
			int remainder = odometer-50000;
			if (remainder>0)
				cout<<"Please replace tires"<<endl; else="" cout<<"tire="" in="" good="" condition.="" remaining:="" "<<(0-remainder)<<endl;="" }="" void="" checkoil(){="" int="" remainder="odometer-5000;" if="" (remainder="">0)
				cout<<"Oil changed"<<endl; else="" cout<<"oil="" change="" not="" reqd"<<endl;="" }="" void="" getcarinformation(){="" checkchargerpower(){="" int="" distance="getOdo();" if="" (distance="">400)
				cout<<"Recharge battery"<<endl; else="" cout<<"batteries="" are="" good="" to="" go"<<endl;="" }="" enum="" type{="" gas,="" diesel,="" electric="" };="" vehicle="" v[10];="" int="" vehcount="0;" gascar:public="" vehicle{="" public:="" void="" gettype(){="" getfueltype(){="" dieselcar="" :public="" electriccar="" checktirerotation(){="" fuelvehicle:="" public="" checkoil(){="" virtual="" getfueltype()="0;" string="" getnewmake(){="" make;="" cout<<"enter="" make:="" "<<endl;="" cin="">>make;
	return make;
}


string getNewModel(){
	string model;
	cout<<"Enter model: "<<endl; cin="">>model;
	return model;
}


long int getNewOdometer(){
	long int odo;
	cout<<"Enter odometer reading: "<<endl; cin="">>odo;
	return odo;
}


int getYear(){
	int year;
	cout<<"Enter year of manufacture: "<<endl; cin="">>year;
	return year;
}


int getNewType(){
	int type=-1;
	do {
		cout<<"Enter 0: GAS\n1.DIESEL\n2.ELECTRIC"<<endl; cin="">>type;
	}while(type<0||type>2);
	
	return type;
}


void addVehicle(){
	int type = getNewType();
	string make = getNewMake();
	string model = getNewModel();
	long int odo = getNewOdometer();
	int year = getYear();
	
	v[vehCount].setMake(make);
	v[vehCount].setModel(model);
	v[vehCount].setOdo(odo);
	v[vehCount].setYear(year);
	
	vehCount++;
	
	cout<<"Added vehicle"<<endl; }="" void="" updatevehicle(){="" int="" selection;="" string="" make;="" bool="" flag="false;" cout<<"enter="" the="" make="" of="" vechicle="" you="" need="" to="" update:="" "<<endl;="" cin="">>make;
	for(int i=0;i<vehcount;i++){ if(v[i].getmake()="=" make){="" flag="true;" cout<<"enter="" the="" parameter="" to="" be="" updated:="" 1.="" year\n2.="" odometer="" reading"<<endl;="" cin="">>selection;
			if(selection==1){
				int newYear = getYear();
				v[i].setYear(newYear);
			}
			
			else if(selection==2){
				long int newOdo = getNewOdometer();
				v[i].setOdo(newOdo);
			}
			
			else
				cout<<"Invalid selection"<<endl; }="" if(!flag)="" cout<<"car="" make="" not="" found"<<endl;="" void="" removevehicle(){="" int="" selection;="" string="" make;="" bool="" flag="false;" cout<<"enter="" the="" of="" vechicle="" you="" need="" to="" update:="" "<<endl;="" cin="">>make;
	for(int i=0;i<vehcount;i++){ if(v[i].getmake()="=" make){="" flag="true;" v[i].setmake("");="" v[i].setmodel("");="" v[i].setyear(0);="" v[i].setodo(0);="" }="" if(!flag)="" cout<<"car="" make="" not="" found"<<endl;="" void="" listvehicles(){="" cout<<"make\tmodel\todo\tyear"<<endl;="" for(int="" i="0;i<vehCount;i++){" if(v[i].getyear()!="0)" cout<<v[i].getmake()<<"\t"<<v[i].getmodel()<<"\t"<<v[i].getodo()<<"\t"<<v[i].getyear()<<endl;="" checktirerotation(){="" int="" selection;="" string="" make;="" bool="" cout<<"enter="" the="" of="" vechicle="" you="" need="" to="" check="" tire="" rotation:="" "<<endl;="" cin="">>make;
	for(int i=0;i<vehcount;i++){ if(v[i].getmake()="=" make){="" v[i].checktirerotation();="" }="" void="" oilchange(){="" int="" selection;="" string="" make;="" bool="" flag="false;" cout<<"enter="" the="" make="" of="" vechicle="" you="" need="" to="" check="" oil:="" "<<endl;="" cin="">>make;
	for(int i=0;i<vehcount;i++){ if(v[i].getmake()="=" make){="" v[i].checkoil();="" }="" void="" checkcharge(){="" int="" selection;="" string="" make;="" bool="" flag="false;" cout<<"enter="" the="" make="" of="" vechicle="" you="" need="" to="" check="" battery:="" "<<endl;="" cin="">>make;
	for(int i=0;i<vehcount;i++){ if(v[i].getmake()="=" make){="" v[i].checkchargerpower();="" }="" int="" main(){="" short="" selection;="" cout<<"chose="" the="" required="" option:\n0.="" quit.\n1.="" list="" inventory.\n2.="" add="" vehicle.\n";="" cout<<"3.="" update="" vehicle\n4.="" remove="" vehicle\n5.="" tire="" rotation\n6.="" oil="" change\n7.="" recharge\n";="" cin="">>selection;
	while(selection!=0){
		
		
		switch(selection){
			case 1: listVehicles();
					break;
			case 2: addVehicle();
					break;
			case 3: updateVehicle();
					break;
			case 4: removeVehicle();
					break;
			case 5: checkTireRotation();
					break;
			case 6: oilChange();
					break;
			case 7: checkCharge();
					break;
			default: cout<<"Invalid selection"<<endl; }="" cout<<"chose="" the="" required="" option"<<endl;="" cin="">>selection;
	}
	
	
	return 0;
}</endl;></vehcount;i++){></vehcount;i++){></vehcount;i++){></vehcount;i++){></endl;></vehcount;i++){></endl;></endl;></endl;></endl;></endl;></endl;></endl;></endl;>

On running:

We can select the required option. First we add a new vehicle.

It asked for type-GAS/DIESEL/ELECTRIC and checks if entered type is correct

Then it asks for make, model, year and odometer reading.

Now we add one more vehicle and then list all vehicles

On selecting 1, all cars are listed.

Now we use option 3 to update a vehicle and then list all vehicles

It asks for the make of the vehicle to be updated and then update the year to 2005.

Now we can remove the vehicle with make "test2".

test2 has been removed

Checking tire rotation and oil:

Similarly we can check battery. On entering 0, the program quits:

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.