Project 3
Hi,
I have submitted the answer which is ok in my knowledge.
Thank you,
#include<iostream>
#include<string.h>
#define MAX_DISTANCE 50000
#define CHARGE 400
#define CURRENT_YEAR 2024
using namespace std;
float reminder=0;
int D=0,E=0,G=0;
typedef struct vechicles {
string make;
string model;
int year;
int odometer;
struct vechicles *address;
} car;
car *gash=NULL;
car *dieselh=NULL;
car *electrich=NULL;
car *temp1=NULL;
car *temp2=NULL;
typedef enum Type {
GAS,
ELECTRIC,
DIESEL
} cartype;
class Vehicle {
protected:
string make;
string model;
int year;
int odometer;
public:
Vehicle(string make_, string model_, int year_, int odometer_) {
make=make_;
model=model_;
year=year_;
odometer=odometer_;
}
string Make() {
return make;
}
string Model() {
return model;
}
int Year() {
return year;
}
int Odometer() {
return odometer;
}
string getNewMake(car *ma) {
return ma->make;
}
string getNewModel(car *mo) {
return mo->model;
}
int getYear(car *ye) {
return ye->year;
}
int getNewOdometer(car *od) {
return od->odometer;
}
void Listvehicle(car *g, car *e, car* d) {
if(g==NULL) {
cout<<"\n-->No GAS vehicles are there\n";
} else {
cout<<"\nGas Vehicles : "<<G;
while(g!=NULL) {
cout<<"\n______________________________________\n";
cout<<"MAKE : "<<getNewMake(g)<<endl;
cout<<"Model : "<<getNewModel(g)<<endl;
cout<<"Year of Manufacturing : "<<getYear(g)<<endl;
cout<<"Odometer reading : "<<getNewOdometer(g)<<endl;
cout<<"______________________________________\n";
g=g->address;
}
}
if(e==NULL) {
cout<<"\n-->No ELECTRIC vehicles are there\n";
} else {
cout<<"\nElectric Vehicles : "<<E;
while(e!=NULL) {
cout<<"\n......................................\n";
cout<<"MAKE : "<<getNewMake(e)<<endl;
cout<<"Model : "<<getNewModel(e)<<endl;
cout<<"Year of Manufacturing : "<<getYear(e)<<endl;
cout<<"Odometer reading : "<<getNewOdometer(e)<<endl;
cout<<"......................................\n";
e=e->address;
}
}
if(d==NULL) {
cout<<"\n-->No DIESEL vehicles are there\n";
} else {
cout<<"\nDiesel Vehicles : "<<D;
while(d!=NULL) {
cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"MAKE : "<<getNewMake(d)<<endl;
cout<<"Model : "<<getNewModel(d)<<endl;
cout<<"Year of Manufacturing : "<<getYear(d)<<endl;
cout<<"Odometer reading : "<<getNewOdometer(d)<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
d=d->address;
}
}
}
void addvehicle(int e,car *temp) {
if(temp==NULL) {
car *temp1= new car;
temp1->make=Make();
temp1->model=Model();
temp1->year=Year();
temp1->odometer=Odometer();
temp1->address=NULL;
if(e==cartype(GAS)) {
gash=temp1;
} else if(e==cartype(ELECTRIC)) {
electrich=temp1;
} else if(e==cartype(DIESEL)) {
dieselh=temp1;
}
} else {
car *temp1= new car;
temp1->make=Make();
temp1->model=Model();
temp1->year=Year();
temp1->odometer=Odometer();
if(e==cartype(GAS)) {
temp1->address=gash;
gash=temp1;
} else if(e==cartype(ELECTRIC)) {
temp1->address=electrich;
electrich=temp1;
} else if(e==cartype(DIESEL)) {
temp1->address=dieselh;
dieselh=temp1;
}
}
cout<<"VEHICLE IS ADDED IN LIST\n"<<endl;
}
void update(car *temp, int pos) {
int data,s;
car *head=NULL;
if(temp==NULL) {
cout<<"List is empty!!!\n";
} else {
{
if(temp==gash) {
s=G;
head=gash;
} else if(temp==electrich) {
s=E;
head=electrich;
} else if(temp=dieselh) {
s=D;
head=dieselh;
}
if(pos<=s&&pos>=0) {
cout<<"YOU CAN UPDATE ODOMETER ONLY\n";
while(pos>0) {
head=head->address;
pos--;
}
cout<<"You have choosen "<make<<" "<model<<endl;
cout<<"Enter the ODOMETER value : ";
cin>>data;
head->odometer=data;
cout<<"VEHICLE IS UPDATED NOW!!!\n";
} else
cout<<"In-valid position\n";
}
}
}
void deletion(car *temp, int pos) {
int s;
car *first=NULL;
car *second=NULL;
if(temp==NULL) {
cout<<"List is empty\n";
} else {
if(temp==gash) {
first=gash->address;
second=gash;
s=G;
} else if(temp==electrich) {
first=electrich->address;
second=electrich;
s=E;
} else if(temp=dieselh) {
first=dieselh->address;
second=dieselh;
s=D;
}
if(pos==0) {
second=second->address;
if(temp==gash)
gash=second;
else if(temp==electrich)
electrich=second;
else if(temp==dieselh)
dieselh=second;
cout<<"Vehicle has been deleted...\n";
} else if(pos<=s&&pos>=0) {
for(int i=0; i<pos-1; i++) {
first =first->address;
second=second->address;
}
second->address=first->address;
cout<<"Vehicle has been deleted...\n";
} else {
cout<<"In-valid position\n";
}
}
}
};
class GasCar: protected Vehicle {
public :
string x;
GasCar(string x, string y, int z, int w) : Vehicle(x, y, z, w) {
}
void getType() {
cout<<getNewMake(gash);
}
void getFuelType() {
cout<<"Gas is the fuel of this car\n";
}
};
class DieselCar : protected Vehicle {
public:
DieselCar(string x, string y, int z, int w) : Vehicle(x, y, z, w) {
}
void getType() {
cout<<"Diesel vehicle\n";
}
void getFuelType() {
cout<<"Diesel is the fuel of this car\n";
}
void checkOilChange(car *oil) {
if(oil==NULL) {
cout<<"List is empty\n";
} else {
while(oil!=NULL) {
if(oil->odometer>4500) {
cout<<"In "<make<<" "<model<<" -->Change oil"<<endl;
} else {
cout<<"In "<make<<" "<model<<" -->No need to change oil"<<endl;
}
oil=oil->address;
}
}
}
};
class ElectricCar : protected Vehicle {
public:
ElectricCar(string x, string y, int z, int w) : Vehicle(x, y, z, w) {
}
void getType() {
cout<<"Electric vehicle\n";
}
void getFuelType() {
cout<<"This vehicle operated by the Electricity\n";
}
void checkTireRotation(car *add) {
if(add==NULL) {
cout<<"List is empty\n";
} else {
while(add!=NULL) {
reminder =add->odometer/MAX_DISTANCE;
if(reminder>=0 && add->odometer>MAX_DISTANCE) {
cout<<"In "<make<<" "<model<<"--> Change the tires"<<endl;
} else {
cout<<"In "<make<<" "<model<<"--> Tires are good to go"<<endl;
}
add=add->address;
}
}
}
void checkChargerPower(car *power) {
if(power==NULL) {
cout<<"List is empty\n";
} else {
while(power!=NULL) {
if(power->odometer > CHARGE) {
cout<<"In "<make<<" "<model<<"-->Change the Battery"<<endl;
} else {
cout<<"In "<make<<" "<model<<"-->BATTERY IS HEALTHY\n";
}
power=power->address;
}
}
}
};
class FuelVehicle : public Vehicle {
public:
FuelVehicle(string x, string y, int z, int w) : Vehicle(x, y, z, w) {
}
void getType();
void getFuelType();
};
int main() {
string MAKE="", MODEL="";
int YEAR=0, ODOMETER=0, fuel, choice;
string type;
char update;
int pos,dpos;
Vehicle v(MAKE,MODEL,YEAR,ODOMETER);
FuelVehicle F(MAKE, MODEL,YEAR,ODOMETER);
GasCar g(MAKE,MODEL,YEAR,ODOMETER);
DieselCar d(MAKE,MODEL,YEAR,ODOMETER);
ElectricCar e(MAKE,MODEL,YEAR,ODOMETER);
Vehicle *v1=&F;
while(1) {
cout<<"Menu is given below : \n";
cout<<"---------------------\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. Tire Rotation\n";
cout<< " 6. Oil Change\n" ;
cout<< " 7. Recharge\n";
cout<<"Enter the choice would you like to do : ";
cin>>choice;
switch(choice) {
case 0:
exit(1);
break;
case 1:
fflush(stdout);
v.Listvehicle(gash, electrich, dieselh);
break;
case 2: {
fflush(stdout);
cout<<"::::GAS -- 0 :: ELECTRIC -- 1 :: DIESEL --2::::"<<endl;
cout<<"Enter Vehicle Type : ";
cin>>fuel;
if(fuel<=2) {
cout<<"Enter the vehicle make : ";
cin>>MAKE;
cout<<"Enter the vehicle model : ";
cin>>MODEL;
year:
cout<<"Enter the year of manufacturing : ";
cin>>YEAR;
if(YEAR<1000 || YEAR>CURRENT_YEAR ) {
cout<<"YEAR SHOULD BE 4 DIGIT AS WELL AS WITHIN THE CURRENT YEAR\n";
goto year;
}
cout<<"Enter the Odometer reading : ";
cin>>ODOMETER;
Vehicle v(MAKE,MODEL,YEAR,ODOMETER);
if (fuel==cartype(GAS)) {
v.addvehicle(fuel,gash);
G++;
} else if(fuel==cartype(ELECTRIC)) {
v.addvehicle(fuel,electrich);
E++;
} else if(fuel==cartype(DIESEL)) {
v.addvehicle(fuel,dieselh);
D++;
} else {
cout<<"Invalid option";
}
} else
cout<<"Wrong Option\n";
}
break;
case 3: {
cout<<"::::GAS -- 0 :: ELECTRIC -- 1 :: DIESEL --2::::"<<endl;
cout<<"\nEnter the fuel of the Vehicle : ";
cin>>fuel;
cout<<"Enter the vehicle position of the vehicle : ";
cin>>pos;
if(fuel==cartype(GAS)) {
v.update(gash,--pos);
} else if(fuel==cartype(ELECTRIC)) {
v.update(electrich,--pos);
}
if(fuel==cartype(DIESEL)) {
v.update(dieselh,--pos);
}
}
break;
case 4: {
cout<<"::::GAS -- 0 :: ELECTRIC -- 1 :: DIESEL --2::::"<<endl;
cout<<"\nEnter the fuel type : ";
cin>>fuel;
cout<<"Enter the position : ";
cin>>dpos;
if(fuel==cartype(GAS)) {
v.deletion(gash,--dpos);
G--;
} else if(fuel==cartype(ELECTRIC)) {
v.deletion(electrich,--dpos);
E--;
} else if(fuel==cartype(DIESEL)) {
v.deletion(dieselh,--dpos);
D--;
}
}
break;
case 5: {
e.checkTireRotation(electrich);
}
break;
case 6:
d.checkOilChange(dieselh);
break;
case 7:
cout<<"Battery status\n";
e.checkChargerPower(electrich);
break;
default :
cout<<"\nIn-valid option";
}
}
}