Project 3
CODE:
#include <iostream>
using namespace std;
enum Type{ //enum
GAS,
DIESEL,
ELECTRIC
};
class Vehicle //base class
{
public :
//data members
int Year, Odometer,Type;
string Make, Model;
void getMake() //member function
{
cout<<endl<<" Make Type :";
cin>>Make;
}
int getYear() //member function
{
cout<<endl<<" Year :";
cin>>Year;
return Year;
}
int getOdometer() //member function
{
cout<<endl<<" Odometer Reading :";
cin>>Odometer;
return Odometer;
}
void getModel() //member function
{
cout<<endl<<" Model :";
cin>>Model;
}
int getType()
{
cout<<endl<<" Type : ";
cin>>Type;
return Type;
}
//virtual Type getType() = 0;
friend void AddVehicle(Vehicle &add); // friend function declaration
friend void ListVehicle(Vehicle &list);
friend void UpdateVehicle(Vehicle &upd);
friend void RemoveVehicle(Vehicle &rmv);
};
//derived class GasCar
class GasCar : public Vehicle
{
};
//derived class DieselCar
class DieselCar : public Vehicle
{
};
//derived class ElectricCar
class ElectricCar : public Vehicle
{
public: //access specifier
int tire_c; //data member
void checkTireRotation() //member function
{
cout<<endl<<" Tire Cycle (in Kms) : ";
cin>>tire_c;
if(tire_c >= 50000)
{
cout<<endl<<" The tires need to be changed as their lifetime is over. ";
}
else
cout<<endl<<" The tires are in good condition. ";
}
void checkChargerPower() //member function
{
cout<<endl<<" Enter Distance Travelled (in Kms) : ";
cin>>Odometer;
if(Odometer >= 400)
{
cout<<endl<<" The battery needs to be changed. ";
}
else
cout<<endl<<" The battery is in good condition";
}
friend void TireRotation(ElectricCar &tire); // friend function declaration
friend void Recharge(ElectricCar &rchg); // friend function declaration
};
//abstract class - containing one pure virtual method
class FuelVehicle : public Vehicle
{
public: //access specifier
int level; //data member
void checkOil() //member function
{
cout<<endl<<" Enter oil Level : ";
cin>>level;
if(level <= 5)
{
cout<<endl<<" Engine Oil Level : Low";
}
else
cout<<endl<<" Engine Oil Level is Good";
}
friend void OilChange(FuelVehicle &oil); // friend function declaration
};
//friend function definitions (defining object for friend class and accessing the members)
void AddVehicle(Vehicle &add) //– to add new vehicle to inventory
{
cout<<endl<<" Enter Vehicle Details : "<<endl;
add.getMake();
add.getModel();
add.getYear();
cout<<endl<<" * The vehicle has been Added * ";
}
void ListVehicle(Vehicle &list) //- to list the vehicles in the inventory
{
cout<<endl<<" Tata : NexonEV : 2022 : Type - 2 (EV)";
cout<<endl<<" Toyota : Innova : 2019 : Type - 1 (Diesel)";
cout<<endl<<"Hyundai : NU2 :2023 : Type - 0 (Gassoline)";
}
void UpdateVehicle(Vehicle &upd) //- to update the vehicle in the inventory list
{
cout<<endl<<" Enter Vehicle Details : "<<endl;
upd.getMake();
upd.getModel();
upd.getYear();
cout<<endl<<" * The vehicle has been Updated * ";
}
void RemoveVehicle(Vehicle &rmv) //- to remove the vehicle from the inventory list
{
cout<<endl<<" Enter Vehicle Details : "<<endl;
rmv.getMake();
rmv.getModel();
rmv.getYear();
cout<<endl<<" * The vehicle has been Removed *";
}
void TireRotation(ElectricCar &tire) //– Check if tires need rotation
{
cout<<endl<<" Enter Vehicle Type : 0->Gas ; 1->Diesel ; 2->Electric :: ";
cin>>tire.Type;
if(tire.Type == 0)
{
cout<<endl<<" Enter Vehicle Details : "<<endl;
tire.checkTireRotation();
}
else if(tire.Type == 1)
{
cout<<endl<<" Enter Vehicle Details : "<<endl;
tire.checkTireRotation();
}
else if(tire.Type == 2)
{
cout<<endl<<" Enter Vehicle Details : "<<endl;
tire.checkTireRotation();
}
}
void OilChange(FuelVehicle &oil) //- check if car requires oil change
{
cout<<endl<<" Enter Vehicle Details : "<<endl;
oil.checkOil();
}
void Recharge(ElectricCar &rchg) //– check if vehicle needs recharge
{
cout<<endl<<" Enter Vehicle Type : 0->Gas ; 1->Diesel ; 2->Electric :: ";
cin>>rchg.Type;
if(rchg.Type == 2)
{
cout<<endl<<" Enter Vehicle Details : ";
rchg.checkChargerPower();
}
else
{
cout<<endl<<" * Cannot be checked * ";
}
}
int main() //main function
{
int option; //local variable
cout<<endl<<"CAR MAINTENANCE"<<endl;
//creating objects of each class
Vehicle ve;
GasCar gc;
DieselCar dc;
ElectricCar ec;
FuelVehicle fv;
cout<<endl<<" Select option ";
cout<<endl<< " 0. Quit";
cout<<endl<< " 1. List inventory";
cout<<endl<< " 2. Add vehicle";
cout<<endl<< " 3. Update vehicle";
cout<<endl<< " 4. Remove vehicle";
cout<<endl<< " 5. Tire Rotation";
cout<<endl<< " 6. Oil Change";
cout<<endl<< " 7. Recharge";
while(1) //infinite loop
{
cout<<endl<<" Option : ";
cin>>option;
switch(option) //switch- case conditional statement
{
case 0:
{
return 1;
break;
}
case 1:
{
ListVehicle(ve);
break;
}
case 2:
{
AddVehicle(ve);
break;
}
case 3:
{
UpdateVehicle(ve);
break;
}
case 4:
{
RemoveVehicle(ve);
break;
}
case 5:
{
TireRotation(ec);
break;
}
case 6:
{
OilChange(fv);
break;
}
case 7:
{
Recharge(ec);
break;
}
}
cout<<endl<<"____________________________"<<endl;
}
return 0; //returning zero to main function
}
OUTPUT:
CAR MAINTENANCE
Select option
0. Quit
1. List inventory
2. Add vehicle
3. Update vehicle
4. Remove vehicle
5. Tire Rotation
6. Oil Change
7. Recharge
Option : 1
Tata : NexonEV : 2022 : Type - 2 (EV)
Toyota : Innova : 2019 : Type - 1 (Diesel)
Hyundai : NU2 :2023 : Type - 0 (Gassoline)
____________________________
Option : 2
Enter Vehicle Details :
Make Type :Tata
Model :NexonEV
Year :2022
* The vehicle has been Added *
____________________________
Option : 3
Enter Vehicle Details :
Make Type :Hyundai
Model :NU2
Year :2023
* The vehicle has been Updated *
____________________________
Option : 4
Enter Vehicle Details :
Make Type :Hyundai
Model :NU2
Year :2023
* The vehicle has been Removed *
____________________________
Option : 5
Enter Vehicle Type : 0->Gas ; 1->Diesel ; 2->Electric :: 1
Enter Vehicle Details :
Tire Cycle (in Kms) : 58000
The tires need to be changed as their lifetime is over.
____________________________
Option : 5
Enter Vehicle Type : 0->Gas ; 1->Diesel ; 2->Electric :: 1
Enter Vehicle Details :
Tire Cycle (in Kms) : 40005
The tires are in good condition.
____________________________
Option : 6
Enter Vehicle Details :
Enter oil Level : 3
Engine Oil Level : Low
____________________________
Option : 6
Enter Vehicle Details :
Enter oil Level : 8
Engine Oil Level is Good
____________________________
Option : 7
Enter Vehicle Type : 0->Gas ; 1->Diesel ; 2->Electric :: 1
* Cannot be checked *
____________________________
Option : 7
Enter Vehicle Type : 0->Gas ; 1->Diesel ; 2->Electric ::
2
Enter Vehicle Details :
Enter Distance Travelled (in Kms) : 505
The battery needs to be changed.
____________________________
Option : 7
Enter Vehicle Type : 0->Gas ; 1->Diesel ; 2->Electric :: 2
Enter Vehicle Details :
Enter Distance Travelled (in Kms) : 450
The battery needs to be changed.
____________________________
Option : 7
Enter Vehicle Type : 0->Gas ; 1->Diesel ; 2->Electric :: 2
Enter Vehicle Details :
Enter Distance Travelled (in Kms) : 300
The battery is in good condition
____________________________