#include <iostream>
#include <vector>
#include <limits>
using namespace std;
enum Type { //creating enum datatype
GAS,
DIESEL,
ELECTRIC
};
class Vehicle { //class Vehicle
protected:
string make;
string model;
int year;
int odometer;
public:
Vehicle(string make_, string model_, int year_, int odometer_) // constructor for class vehicale
: make(make_), model(model_), year(year_), odometer(odometer_) {}
void setMake(string make_) {
make = make_;
}
string getMake() const {
return make;
}
void setModel(string model_) {
model = model_;
}
string getModel() const {
return model;
}
void setYear(int year_) {
year = year_;
}
int getYear() const {
return year;
}
void setOdometer(int odometer_) {
odometer = odometer_;
}
int getOdometer() const {
return odometer;
}
virtual Type getType() = 0; //pure virtual function
virtual void checkTireRotation() = 0; //pure virtual function
virtual void getCarInformation() = 0; //pure virtual function
virtual ~Vehicle() {}
};
class GasCar : public Vehicle { //class gascar inherited with Vehicle
public:
GasCar(string make_, string model_, int year_, int odometer_)
: Vehicle(make_, model_, year_, odometer_) {}
Type getType() {
return GAS;
}
void checkTireRotation() {
int cycle = 50000;
int distance = odometer % cycle;
if (distance == 0 || distance >= 40000) {
cout << "Time to rotate tires on your gas car.\n";
} else {
cout << "Tires are good to go.\n";
}
}
void getCarInformation() {
cout << "Make: " << make << endl
<< "Model: " << model << endl
<< "Year: " << year << endl
<< "Odometer reading: " << odometer << endl
<< "Fuel type: Gas" << endl;
}
void changeOil() {
int oilLevel;
cout<<" Enter the level of oil in Ml: ";
cin>>oilLevel;
cout<<" checking for oil change \n";
if (oilLevel>= 1000){
cout<<"dont need oil change \n";
}
else{
cout<<"car needs oil change.. \n";
}
}
};
class DieselCar : public Vehicle { //class dieselcar inherited with Vehicle
public:
DieselCar(string make_, string model_, int year_, int odometer_)
: Vehicle(make_, model_, year_, odometer_) {}
Type getType() {
return DIESEL;
}
void checkTireRotation() {
int cycle = 50000;
int distance = odometer % cycle;
if (distance == 0 || distance >= 40000) {
cout << "Time to rotate tires on your diesel car.\n";
} else {
cout << "Tires are good to go.\n";
}
}
void getCarInformation() {
cout << "Make: " << make << endl
<< "Model: " << model << endl
<< "Year: " << year << endl
<< "Odometer reading: " << odometer << endl
<< "Fuel type: Diesel" << endl;
}
};
class ElectricCar : public Vehicle { //class electriccar inherited with Vehicle
public:
ElectricCar(string make_, string model_, int year_, int odometer_)
: Vehicle(make_, model_, year_, odometer_) {}
Type getType() {
return ELECTRIC;
}
void checkTireRotation() { // creating function for tirerotation
int cycle = 50000;
int distance = odometer % cycle;
if (distance == 0 || distance >= 40000) {
cout << "Time to rotate tires on your electric car.\n";
} else {
cout << "Tires are good to go.\n";
}
}
void getCarInformation() {
cout << "Make: " << make << endl
<< "Model: " << model << endl
<< "Year: " << year << endl
<< "Odometer reading: " << odometer << endl
<< "Fuel type: Electric" << endl;
}
void recharge() {
int KM;
cout<<"Enter number of Km ";
cin>>KM;
cout<<" checking for battery charging... \n";
if (KM>=400){
cout<<"battery to be recharged \n";
}
else{
cout<<"car is good to go.. \n";
}
}
};
Vehicle* addVehicle() { // function for add Vehicle
string make;
string model;
int year;
int odometer;
int choice;
cout << "Enter the make of the vehicle: ";
cin >> make;
cout << "Enter the model of the vehicle: ";
cin >>model;
cout << "Enter the year of the vehicle: ";
cin >> year;
cout << "Enter the odometer reading of the vehicle: ";
cin >> odometer;
cout << "Select vehicle type:\n"
<< "1. Gas\n"
<< "2. Diesel\n"
<< "3. Electric\n";
cin >> choice;
switch(choice) {
case 1:
return new GasCar(make, model, year, odometer);
case 2:
return new DieselCar(make, model, year, odometer);
case 3:
return new ElectricCar(make, model, year, odometer);
default:
cout << "Invalid choice, returning to main menu.\n";
return nullptr;
}
}
int main() {
vector<Vehicle*> inventory; // boject for Vehicle class
bool running = true;
int choice;
while (running) {
cout << "\nSelect an option:\n"
<< " 1. Add vehicle\n"
<< " 2. Print inventory\n"
<< " 3. Update vehicle\n"
<< " 4. Remove vehicle\n"
<< " 5. Tire Rotation\n"
<< " 6. Oil Change\n"
<< " 7. Recharge\n"
<< " 0. Quit\n";
cin >> choice;
switch (choice) {
case 1: {
Vehicle* vehicle = addVehicle();
inventory.push_back(vehicle);
break;
}
case 2:
for (Vehicle* vehicle : inventory) {
vehicle->getCarInformation();
}
break;
case 3: {
int index;
cout << "Enter the index of the vehicle you want to update: ";
cin >> index;
if (index >= 0 && index < inventory.size()) {
Vehicle* vehicle = inventory[index];
string make;
string model;
int year;
int odometer;
cout << "Enter the make of the vehicle: ";
cin >> make;
cout << "Enter the model of the vehicle: ";
cin >>model;
cout << "Enter the year of the vehicle: ";
cin >> year;
cout << "Enter the odometer reading of the vehicle: ";
cin >> odometer;
vehicle->setMake(make);
vehicle->setModel(model);
vehicle->setYear(year);
vehicle->setOdometer(odometer);
} else {
cout << "Invalid index.\n";
}
break;
}
case 4: {
int index;
cout << "Enter the index of the vehicle you want to remove: ";
cin >> index;
if (index >= 0 && index < inventory.size()) {
delete inventory[index];
inventory.erase(inventory.begin() + index);
} else {
cout << "Invalid index.\n";
}
break;
}
case 5: {
int index;
cout << "Enter the index of the vehicle you want to check: ";
cin >> index;
if (index >= 0 && index < inventory.size()) {
inventory[index]->checkTireRotation();
} else {
cout << "Invalid index.\n";
}
break;
}
case 6: {
int index;
cout << "Enter the index of the gas vehicle you want to change oil: ";
cin >> index;
if (index >= 0 && index < inventory.size()) {
if (inventory[index]->getType() == GAS) {
GasCar* gasCar = dynamic_cast<GasCar*>(inventory[index]);
if (gasCar != nullptr) {
gasCar->changeOil();
} else {
cout << "Vehicle is not a gas car.\n";
}
} else {
cout << "Vehicle is not a gas car.\n";
}
} else {
cout << "Invalid index.\n";
}
break;
}
case 7: {
int index;
cout << "Enter the index of the electric vehicle you want to recharge: ";
cin >> index;
if (index >= 0 && index < inventory.size()) {
if (inventory[index]->getType() == ELECTRIC) {
ElectricCar* electricCar = dynamic_cast<ElectricCar*>(inventory[index]);
if (electricCar != nullptr) {
electricCar->recharge();
} else {
cout << "Vehicle is not an electric car.\n";
}
} else {
cout << "Vehicle is not an electric car.\n";
}
} else {
cout << "Invalid index.\n";
}
break;
}
case 0:
cout<<"Exiting.... good bye..."<<endl;
running = false;
break;
default:
cout << "Invalid choice.\n";
break;
}
}
for (Vehicle* vehicle : inventory) {
delete vehicle;
}
inventory.clear();
return 0;
}