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.

Code
#include
using namespace std;

int count=0;

enum type{
Gas,
Diesel,
Electric
}; //fuel type

class Vehicle //Base class Vehicle
{
protected:
string make;
string model;
int year;
int odometer;
type ty;

public:
Vehicle(string m,string mod,int y,int od):make(m),model(mod),year(y),odometer(od){}

string getMake()
{
return make;
}
string getModel()
{
return model;
}
int getYear()
{
return year;
}
int getOdometer()
{
return odometer;
}
void setMake(string m)
{
make=m;
}
void setModel(string mod)
{
model=mod;
}
void setYear(int y)
{
year=y;
}
void setOdometer(int o)
{
odometer=o;
}

virtual type getType() = 0; //abstract classes
virtual void check_power()=0;
virtual void getFueltype() = 0;
virtual void getcar_info()
{
cout<<"Make :"<<make<<endl;
cout<<"Model :"<<model<<endl;
cout<<"Year :"<<year<<endl;
cout<<"Odometer reading :"<<odometer<<endl;
}
virtual void check_tire_rotation() //function to check whether tire rotation is needed
{

if(odometer%50000==0 || odometer>=45000 )
{
cout<<"Rotate tires "<<endl;
}
else
{
cout<<"Tires are good to go"<<endl;
}
}
};
class Gas_car :public Vehicle //derieved class gas_car inherited from Vehicle
{
public:
Gas_car(string m,string mod,int y,int od ):Vehicle(m,mod,y,od){} //base class initializing
type getType()
{
return Gas;
}
void getFueltype()
{
cout<<"Gas powered vehicle"<<endl;;
}

void check_power() //function to check power
{
if(odometer%400==0)
{
cout<<"Recharge Battery"<<endl;
}
else
{
cout<<"Vehicle is good to go"<<endl;
}
}
};
class Diesel_car:public Vehicle //derieved class Diesel_car inherited from Vehicle
{
public:
Diesel_car(string m,string mod,int y,int od ):Vehicle(m,mod,y,od){} //base class initializing
type getType()
{
return Diesel;
}
void getFueltype()
{
cout<<"Diesel powered vehicle"<<endl;;
}

void check_power() //function to check power
{
if(odometer%400==0)
{
cout<<"Recharge Battery"<<endl;
}
else
{
cout<<"Vehicle is good to go"<<endl;
}
}
};
class Electric_car:public Vehicle //derieved class Electric_car inherited from Vehicle
{
public:
Electric_car(string m,string mod,int y,int od ):Vehicle(m,mod,y,od){} //base class initializing
type getType()
{
return Electric;
}
void getFueltype()
{
cout<<"Electric powered vehicle"<<endl;
}
void check_power() //function to check power
{
if(odometer%200==0)
{
cout<<"Recharge Battery"<<endl;
}
else
{
cout<<"Vehicle is good to go"<<endl;
}
}
};
class Fuelvehicle:public Vehicle ////derieved class Fuelvehicle inherited from Vehicle ,acts as abstract class
{
public:
virtual void getFueltype()=0;
void checkoil() //function to check whether oilchange is needed
{
if(odometer%1000 == 0)
{
cout<<"Oil needs to be changed"<<endl;
}
else{
cout<<"Vehicle is good to go"<<endl;
}
}
};

void addVehicle(Vehicle** vehicle,int max) //to add new vehicles in the inventory list
{
if(count<max)
{
cout<<"Enter the Vehicle type n0.Gas n1.Diesel n2.Electic"<<endl;
int t,year,odometer;
string make,model;
type ty;
cin>>t;
ty=static_cast(t); //typecasting int to enum type
cout<<"Enter the Vehicle make"<<endl;
cin>>make;
cout<<"Enter the Vehicle Model"<<endl;
cin>>model;
cout<<"Enter the year of manufacture"<<endl;
cin>>year;
cout<<"Enter the Odometer reading"<<endl;
cin>>odometer;

switch(ty)
{
case Gas:
{
vehicle[count++]=new Gas_car(make,model,year,odometer);
break;
}
case Diesel:
{
vehicle[count++]=new Diesel_car(make,model,year,odometer);
break;
}
case Electric:
{
vehicle[count++]=new Electric_car(make,model,year,odometer);
break;
}
default:
{
cout<<"Error "<<endl;
break;
}
}
}
}
void listVehicle(Vehicle **vehicle) //to get Vehicle details in the inventory list
{
for(int i=0;i<count;i++)
{
cout<<i+1<<".->";
vehicle[i]->getcar_info();
Fuelvehicle *fuelvehicle=static_cast<Fuelvehicle*>(vehicle[i]); //getfueltype is defined in derieved class,so to access the function,we perform static cast to convert vehicle* to fuelvehicle*

cout<<"This is a ";
fuelvehicle->getFueltype();
}
}
void updateVehicle(Vehicle **vehicle) //to update vehicle details in the inventory list
{
int sel;
cout<<"Enter the Vehicle no in the inventory list"<<endl;
listVehicle(vehicle);
cin>>sel;
string make;
string model;
int year,odometer;
if(sel>0 && sel<=count)
{
cout<<"Enter the updated Vehicle make"<<endl;
cin>>make;
cout<<"Enter the updated Vehicle Model"<<endl;
cin>>model;
cout<<"Enter the updated year of manufacture"<<endl;
cin>>year;
cout<<"Enter the updated Odometer reading"<<endl;
cin>>odometer;
vehicle[sel-1]->setMake(make);
vehicle[sel-1]->setModel(model);
vehicle[sel-1]->setYear(year);
vehicle[sel-1]->setOdometer(odometer);
}
}
void removeVehicle(Vehicle **vehicle) //to add remove vehicle in the inventory list
{
int i;
cout<<"Enter the vehicle no in the inventory list"<<endl;
listVehicle(vehicle);
cin>>i;
if(i>0 && i<=count)
{
delete vehicle[i-1];
for(int j=i-1;j<count;j++)
{
vehicle[j]=vehicle[j+1];
}
}
}
void tireRotation(Vehicle **vehicle) //to check tire rotation of vehicles in the inventory list
{
int i;
cout<<"Enter the vehicle no in the inventory list"<<endl;
listVehicle(vehicle);
cin>>i;
if(i>0 && i<=count)
{
vehicle[i-1]->check_tire_rotation();
}
}
void oil_change(Vehicle **vehicle) //to check oil chnge of vehicles in the inventory list
{
int i;
cout<<"Enter the vehicle no in the inventory list"<<endl;
listVehicle(vehicle);
cin>>i;
if(i>0 && i<=count)
{
Fuelvehicle* fuelVehicle = static_cast<Fuelvehicle*>(vehicle[i-1]);
fuelVehicle->checkoil();

}
}
void recharge(Vehicle **vehicle) //to check battery recharge of vehicles in the inventory list
{
int i;
cout<<"Enter the vehicle no in the inventory list"<<endl;
listVehicle(vehicle);
cin>>i;
if(i>0 && i<=count)
{
vehicle[i-1]->check_power();
}
}
int main() //main function
{
int choice=9; //random choice but not in list
const int max=5;
Vehicle *vehicle[max];//max of 5 vehicles can be added in inventory
while(choice!=0)
{

cout<<"Menun";
cout<<"1.Add Vehiclen2.List Vehiclen3.Update Vehiclen4.Remove Vehiclen5.Tire Rotationn6.Oil changen7.Rechrgen0.Quit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
addVehicle(vehicle,max);
break;
}
case 2:
{
listVehicle(vehicle);
break;
}
case 3:
{
updateVehicle(vehicle);
break;
}
case 4:
{
removeVehicle(vehicle);
break;
}
case 5:
{
tireRotation(vehicle);
break;
}
case 6:
{
oil_change(vehicle);
break;
}
case 7:
{
recharge(vehicle);
break;
}
case 0:
{
cout<<"Exiting"<<endl;
break;
}
default:
cout<<"Wrong selection,try again"<<endl;
}
}

for(int i=0;i<count;i++){ //to deallocate memory after implemntation when it goes out of scope

delete vehicle[i]

}
return 0;
}

OUTPUT

The menu appears and user can enter the requrired function.Suppose if user selects Add Vehicle,

the menu asks to enter fuel type and then the vehicle details .After entering it returns to main menu.

If user selects List vehicle,then the entered vehicle details in the inventory will appear.

If the user selects Update vehicle,the menu shows entered vehicles and asks to enter the no of corresponding vehicle to update,then it asks to enter the modified vehicle details.

If the user selects Remove vehicle,the menu shows entered vehicles and asks to enter the no of corresponding vehicle to remove.

If the user selects Tire Rotation,the menu shows entered vehicles and asks to enter the no of corresponding vehicle to check whether the entered vehicle requires the tire rotation.

If the user selects Oil Change,the menu shows entered vehicles and asks to enter the no of corresponding vehicle to check whether the entered vehicle requires Oil change.

If the user selects Recharge,the menu shows entered vehicles and asks to enter the no of corresponding vehicle to check whether the entered vehicle requires Recharge

User enters the Tire Rotation(to check whether tire rotation is needed)

User enters Oil chage (to check whether oil change is needed)

User enters Recharge (to check whether Recharge is needed)

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.