Project 3
/*1)Object oriented library for automobile maintaince
date: 14/12/2023
Vishnu V Nair*/
#include <iostream>
#include <string>
using namespace std;
//enum class for Vehicle type
enum class Type
{
GAS,
DIESEL,
ELECTRIC
};
//Vehicle base class
class Vehicle
{
protected:
string make, model;
int year;
double odometer;
public:
//Constructor for Vehicle class
Vehicle(const string &ma, const string &mo, int y, double o)
{
make= ma;
model= mo;
year= y;
odometer= o;
}
//Get and set functiond for Make, Model, Year and Odometer//
//Make()
void setMake(const string &ma)
{
make= ma;
}
string getMake() const
{
return make;
}
//Model()
void setModel(const string &mo)
{
model= mo;
}
string getModel() const
{
return model;
}
//Year()
void setYear(int y)
{
year= y;
}
int getYear()
{
return year;
}
//Odometer()
void setOdometer(double o)
{
odometer= o;
}
int getOdometer()
{
return odometer;
}
virtual void getCar_Information()
{
cout << "Make: " << make << " Model: " << model << " Year: " << year << " Odometer: " << odometer << endl;
}
//Virtual functions
virtual void checkTire_Rotation()
{
double tyre_cycle= 50000.0; //Tyre cycle set to 50000
double distance_run= odometer / tyre_cycle;
double reminder= odometer - (distance_run * tyre_cycle);
cout << "Checking Car Rotation: ";
if(reminder >= 0)
{
cout << "Rotation needed" << endl;
}
else
{
cout << "Rotation not needed" << endl;
}
}
virtual Type getType()=0; //Pure virtual function for fuel type
};
//Class Gascar derived from Vehicle class
class Gascar: public Vehicle
{
public:
Gascar(const string &ma, const string &mo, int y, double o)
: Vehicle(ma, mo, y, o){}
Type getType() override
{
return Type::GAS;
}
void getFuelType()
{
cout << "Fuel type: Gas" << endl;
}
};
//Class Diselcar derived from Vehicle class
class Dieselcar: public Vehicle
{
public:
Dieselcar(const string &ma, const string &mo, int y, double o)
: Vehicle(ma, mo, y, o){}
Type getType() override
{
return Type::DIESEL;
}
void getFuelType()
{
cout << "Fuel type: Diesel" << endl;
}
};
//Class Electriccar derived from Vehicle class
class Electriccar: public Vehicle
{
public:
Electriccar(const string &ma, const string &mo, int y, double o)
: Vehicle(ma, mo, y, o){}
Type getType() override
{
return Type::ELECTRIC;
}
void checkTire_Rotation() override
{
Vehicle::checkTire_Rotation();
}
void check_Power()
{
double distance= 400.0;
if (odometer >= distance)
{
cout << "Battery needs recharging" << endl;
}
else
{
cout << "Vehicle is good to go" << endl;
}
}
};
//Abstract class Fuelvehicle inherits Vehicle class
class Fuelvehicle: public Vehicle
{
public:
virtual void check_oil()= 0;
virtual void get_Fueltype()= 0;
};
//Main class to check inventory
class Inventory
{
private:
static const int max_vehicle= 100;
Vehicle *vehicle[max_vehicle];
int no_ofvehicle;
public:
Inventory()
{
no_ofvehicle= 0;
}
//Function to add Vehicles to inventory
template<typename T>
void add_vehicle(T *new_vehicle)
{
if(no_ofvehicle < max_vehicle)
{
vehicle[no_ofvehicle]= new_vehicle;
no_ofvehicle++;
cout << "Vehicle added to inventory\n" << endl;
}
else
{
cout << "Inventory Full" << endl;
}
}
//Function to list Vehicles in inventory
void list_vehicle()
{
cout << "Vehicles in Inventory" << endl;
for(int i=0; i < no_ofvehicle; i++)
{
vehicle[i]->getCar_Information();
}
}
//Function to update inventory
void update_vehicle(int l, Vehicle *updated_vehicle)
{
if(l >= 0 && l < no_ofvehicle)
{
vehicle[l]= updated_vehicle;
cout << "Vehicle in list " << l << "is updated" << endl;
}
else
{
cout << "Invalid Option" << endl;
}
}
//Function to remove vehicle from inventory
void remove_vehicle(int i)
{
if(i >=0 && i < no_ofvehicle)
{
for(int j= i; j < no_ofvehicle - 1; j++)
{
vehicle[j]= vehicle[j + 1];
}
no_ofvehicle--;
cout << "Vehicle in list " << i << "is removed" << endl;
}
else
{
cout << "Invalid Option" << endl;
}
}
//Function to check tyre rotation
void tire_rotation()
{
cout << "Checking tyre rotation" << endl;
for(int i= 0; i < no_ofvehicle; i++)
{
vehicle[i]->checkTire_Rotation();
}
}
//Function to check Oil condition
void oil_change()
{
cout << "Checking Fuel level" << endl;
for(int i= 0; i < no_ofvehicle; i++)
{
Fuelvehicle *fvehicle= dynamic_cast<Fuelvehicle*>(vehicle[i]);
if(fvehicle != NULL)
{
fvehicle->check_oil();
}
}
}
//Function to check for recharge
void recharge()
{
cout << "Checking for Recharge" << endl;
for(int i= 0; i < no_ofvehicle; i++)
{
Electriccar* eCar= dynamic_cast<Electriccar*>(vehicle[i]);
if(eCar != NULL)
{
eCar->check_Power();
}
}
}
//Get function for no of vehicle in inventory
int get_novehicle()
{
return no_ofvehicle;
}
};
//Function to select choices and do operations
void switch_choice(Inventory i)
{
int choice;
bool quit= false;
while(!quit)
{
//Display menu options
cout << "Select Choice:-\n"
<< "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";
cout << "Choice: ";
cin >> choice;
//To handle user choices
switch(choice)
{
case 0:
quit= true;
break;
case 1:
i.list_vehicle();
break;
case 2:
{
string make, model;
int year, type;
double odometer;
cout << "Company: ";
cin >> make;
cout << "Model: ";
cin >> model;
cout << "Year: ";
cin >> year;
cout << "Odometer Reading: ";
cin >> odometer;
cout << "Select Fuel Type:\n"
<< "0. Gas\n"
<< "1. Diesel\n"
<< "2. Electric\n"
<< "Choice: ";
cin >> type;
//Adding details to specified type
if(type == 0)
{
Gascar* gCar = new Gascar(make, model, year, odometer);
i.add_vehicle(gCar);
}
else if(type == 1)
{
Dieselcar* dCar = new Dieselcar(make, model, year, odometer);
i.add_vehicle(dCar);
}
else if(type == 2)
{
Electriccar* eCar = new Electriccar(make, model, year, odometer);
i.add_vehicle(eCar);
}
else
{
cout << "Invalid Type\n";
}
break;
}
case 3:
{
int index;
cout << "Enter Vehicle Index to Update: ";
cin >> index;
if (index >= 0 && index < i.get_novehicle())
{
string make, model;
int year, type;
double odometer;
cout << "Company: ";
cin >> make;
cout << "Model: ";
cin >> model;
cout << "Year: ";
cin >> year;
cout << "Odometer Reading: ";
cin >> odometer;
cout << "Select Fuel Type:\n"
<< "0. Gas\n"
<< "1. Diesel\n"
<< "2. Electric\n"
<< "Choice: ";
cin >> type;
if (type == 0)
{
Gascar* gCar = new Gascar(make, model, year, odometer);
i.update_vehicle(index, gCar);
}
else if (type == 1)
{
Dieselcar* dCar = new Dieselcar(make, model, year, odometer);
i.update_vehicle(index, dCar);
}
else if (type == 2)
{
Electriccar* eCar = new Electriccar(make, model, year, odometer);
i.update_vehicle(index, eCar);
}
else
{
cout << "Invalid Type\n";
}
}
break;
}
case 4:
{
int index;
cout << "Enter Vehicle Index to Remove: ";
cin >> index;
i.remove_vehicle(index);
break;
}
case 5:
i.tire_rotation();
break;
case 6:
i.oil_change();
break;
case 7:
i.recharge();
break;
default:
cout << "Invalid Choice\n";
break;
}
}
}
/*Main Function*/
int main()
{
Inventory i; //Object creator
switch_choice(i); //Function for user choice
return 0;
}