Project 3
#include <iostream> // include statndard library for using input output functions
#include <vector> // we include vector header file for using daynamic array and vector inbuilds functions
#include <limits> // include limit library for limits
using namespace std; // use standard namespace
enum Type
{
GAS, // element 0;
DIESEL, // element 1;
ELECTRIC // element 2;
};
class Vehicle // base class vehicle
{
protected: // protected data members inside the base class
string make; // for vehicle make
string model;// for vehicle model
int year;// for vehicle manufacturing year
int odometer;// for vehicle odometer reading
public:// public specifier
//vehicle base class constructor and initializing them
Vehicle(string make_, string model_, int year_, int odometer_)
: make(make_), model(model_), year(year_), odometer(odometer_) {}
void setMake(string make_) // setter function for make
{
make = make_;
}
string getMake() const // getter function for make
{
return make;
}
void setModel(string model_)// setter function for model
{
model = model_;
}
string getModel() const// getter function for model
{
return model;
}
void setYear(int year_)// setter function for year
{
year = year_;
}
int getYear() const// getter function for year
{
return year;
}
void setOdometer(int odometer_)// setter function for odometer current range
{
odometer = odometer_;
}
int getOdometer() const// getter function for odometer current range
{
return odometer;
}
// pure vertual function
// we are going to use many times these function through out the program
// thats why we have defined it pure virtual with initialized the value 0;
virtual Type getType() = 0;
virtual void checkTireRotation() = 0;
virtual void getCarInformation() = 0;
virtual ~Vehicle() {} // base class vehicle destructor
};
class GasCar : public Vehicle // derived class gascar inherit from base class vehicle
{
public:
// Constructor for GasCar class that calls the constructor of the parent class and passes the arguments to it
GasCar(string make_, string model_, int year_, int odometer_)
: Vehicle(make_, model_, year_, odometer_) {}
// Function that returns the fuel type of the car as GAS
Type getType()
{
return GAS;
}
// Function that checks if the tires of the car need to be rotated based on the odometer reading
void checkTireRotation()
{
if (odometer >= 50000)
{
cout << "Time to rotate tires on your gas car.\n";
}
else
{
cout << " vehicle is good to go\n";
}
}
// Function that gives the car information of GasCar
void getCarInformation()
{
cout << " Make: " << make << endl;
cout << " Model: " << model << endl;
cout << " Year: " << year << endl;
cout << " Odometer Range: " << odometer << endl;
cout << " Fule Type: Gas " << endl;
}
// Function that checks if the oil of the car need to be changed based on the oil level
void oilChange()
{
int oilLevel;
cout << " Enetr the current level of Oil of vehicle in ML" << endl;
cin >> oilLevel;
cout << " Checking oil level...." << endl;
if (oilLevel >= 1000)
{
cout << " vehicle is good to go" << endl;
}
else
{
cout << " change the oil\n";
}
}
};
class DieselCar : public Vehicle// derived class dieselcar inherit from base class vehicle
{
public:
// Constructor for dieselCar class that calls the constructor of the parent class and passes the arguments to it
DieselCar(string make_, string model_, int year_, int odometer_)
: Vehicle(make_, model_, year_, odometer_) {}
// Function that returns the fuel type of the car as DIESEL;
Type getType()
{
return DIESEL;
}
// Function that checks if the tires of the car need to be rotated based on the odometer reading
void checkTireRotation()
{
if (odometer >= 50000)
{
cout << "Time to rotate tires on your gas car.\n";
}
else
{
cout << " vehicle is good to go\n";
}
}
// Function that gives the car information of DIESEL CAR
void getCarInformation()
{
cout << " Make: " << make << endl;
cout << " Model: " << model << endl;
cout << " Year: " << year << endl;
cout << " Odometer Range: " << odometer << endl;
cout << " Fule Type: Diesel " << endl;
}
};
class ElectricCar : public Vehicle// derived class electriccar inherit from base class vehicle
{
public:
// Constructor for ElectricCar class that calls the constructor of the parent class and passes the arguments to it
ElectricCar(string make_, string model_, int year_, int odometer_)
: Vehicle(make_, model_, year_, odometer_) {}
// Function that returns the fuel type of the car as electric
Type getType()
{
return ELECTRIC;
}
// Function that checks if the tires of the car need to be rotated based on the odometer reading
void checkTireRotation()
{
if (odometer >= 50000)
{
cout << "Time to rotate tires on your gas car.\n";
}
else
{
cout << " vehicle is good to go\n";
}
}
// Function that gives the car information of ELECTRIC CAR
void getCarInformation()
{
cout << " Make: " << make << endl;
cout << " Model: " << model << endl;
cout << " Year: " << year << endl;
cout << " Odometer Range: " << odometer << endl;
cout << " Fule Type: Diesel " << endl;
}
// Function that checks if the recharge of the car need to be charged based on the KM you have to go
void recharge()
{
int KM;
cout << " Enetr the number of KM " << endl;
cin >> KM;
cout << " Checking charging level...." << endl;
if (KM >= 00)
{
cout << " battery to be recharged" << endl;
}
else
{
cout << " vehicle is good to go\n";
}
}
};
// function for add vehicle in vector list garage
Vehicle *addVehicle()
{
// variable declaration to hold user input
string make;
string model;
int year;
int odometer;
int choice;
// prompt ask to user which car information you want to add
cout << " Select Vehicle Type: \n"
<< "1. GasCar \n"
<< "2. DieselCar\n"
<< "3. ElectricCar \n";
cin >> choice;
// Use while loops to validate user input for make, model, year, and odometer reading
// Prompt the user to enter make until a non-empty string is entered
while (true)
{
cout << "Enter make: " << endl;
getline(cin >> ws, make);
if (!make.empty())
break;
}
// Prompt the user to enter model until a non-empty string is entered
while (true)
{
cout << "Enter model: " << endl;
getline(cin >> ws, model);
if (!model.empty())
break;
}
// Prompt the user to enter year until a valid year is entered
while (true)
{
cout << "Enter year: " << endl;
if (cin >> year && year >= 1900 && year <= 2023)
break;
cout << "Invalid input, please try again.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
//the user to enter odometer until a valid range is entered
while (true)
{
cout << "Enter odometer Reading: " << endl;
if (cin >> odometer && odometer >= 0)
break;
cout << "Invalid input, please try again.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
switch (choice)
{
case 1:
// Create a new vehicle object based on user choice and input
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;
}
}
// This is the main function of the program.
int main()
{
// create a vector name garage pointer variable data type
vector<Vehicle *> garage;
// Declare a boolean variable 'running' and set it to true.
bool running = true;
int choice_;
// Begin a while loop which continues until the user chooses to quit the program.
while (running)
{
cout << "\n *******WELCOME TO MAINTAIN AUTOMOTIVE GARAGE*******" << endl;
cout << "\nSelect an option:\n"
<< " 1. Add vehicle\n"
<< " 2. List 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:
{
// If the user chooses to add a vehicle, call the addVehicle() function to create
//a new Vehicle object and add it to the garage.
Vehicle *vehicle = addVehicle();
garage.push_back(vehicle);
break;
}
case 2:
{
// If the user chooses to print the garage, loop through each vehicle in the garage and call
//its getCarInformation() function to display its information.
for (Vehicle *vehicle : garage)
{
vehicle->getCarInformation();
}
break;
}
case 3:
// this case allow to user to update the vehicle
{
int index;
cout << " enter the index which you want to update" << endl;
cin >> index;
if (index >= 0 && index < garage.size())
{
Vehicle *vehicle = garage[index];
string make;
string model;
int year;
int odometer;
while (true)
{
cout << "Enter make: " << endl;
getline(cin >> ws, make);
if (!make.empty())
break;
}
while (true)
{
cout << "Enter model: " << endl;
getline(cin >> ws, model);
if (!model.empty())
break;
}
while (true)
{
cout << "Enter year: " << endl;
if (cin >> year && year >= 1900 && year <= 2023)
break;
cout << "Invalid input, please try again.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
while (true)
{
cout << "Enter odometer Reading: " << endl;
if (cin >> odometer && odometer >= 0)
break;
cout << "Invalid input, please try again.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
vehicle->setMake(make);
vehicle->setModel(model);
vehicle->setYear(year);
vehicle->setOdometer(odometer);
}
else
{
cout << "Invalid Index you entered " << endl;
}
break;
}
// this case allow to user to remove the vehicle fro garage list
case 4:
{
int index;
cout << " enter the index which you want to Remove" << endl;
cin >> index;
if (index >= 0 && index < garage.size())
{
delete garage[index];
garage.erase(garage.begin() + index);
}
else
{
cout << "invalid Index";
}
break;
}
case 5:
// this case allow to user to check the vehicle tire from garage list
{
int index;
cout << " enter the index of vehicle which you want to check Tire Rotation" << endl;
cin >> index;
if (index >= 0 && index < garage.size())
{
garage[index]->checkTireRotation();
}
else
{
cout << " Invalid Index Try again!" << endl;
}
break;
}
case 6:
{
// this case allow to user to check the vehicle oil from garage list valid car where use oil
// otherwise it will show you your car is not oil car
int index;
cout << " enter the index of vehicle which you want to check Oil Level" << endl;
cin >> index;
// Read in the index entered by the user.
// Check if the index is valid and within the bounds of the vector.
if (index >= 0 && index < garage.size())
{
// Check if the vehicle at the given index is of type GAS.
if (garage[index]->getType() == GAS)
{
// Use dynamic_cast to downcast the Vehicle pointer to a GasCar pointer.
GasCar *gascar = dynamic_cast<GasCar *>(garage[index]);
// Check if the downcast was successful.
if (gascar != nullptr)
{
// Call the changeOil() function on the gasCar object.
gascar->oilChange();
}
else
{
cout << " Vehicle is not a gas car" << endl;
}
}
else
{
cout << " Vehicle is not a gas car" << endl;
}
}
else
{
cout << "Invalid index.\n";
}
break;
}
case 7:
{
// this case allow to user to check the vehicle recharge from garage list
int index;
cout << " enter the index of vehicle which you want to check Recharge" << endl;
cin >> index;
if (index >= 0 && index < garage.size())
{
if (garage[index]->getType() == ELECTRIC)
{
ElectricCar *electriccar = dynamic_cast<ElectricCar *>(garage[index]);
if (electriccar != nullptr)
{
electriccar->recharge();
}
else
{
// If the vehicle is not of type GAS, print an error message.
cout << " Vehicle is not a electric car" << endl;
}
}
else
{
cout << " Vehicle is not a electric car" << endl;
}
}
else
{
cout << "Invalid index.\n";
}
break;
}
case 0:
{
// This case handles the user choosing to exit the program.
cout << "Exiting.... good bye...";
running = false;
break;
}
default:
{
cout << "Invalid choice.\n";
break;
}
}
}
// iterates through the 'garage' vector using a range-based for
//loop and assigns each element to 'vehicle'
for (Vehicle *vehicle : garage)
{
// deletes each element of 'garage', which must have been dynamically allocated
delete vehicle;
}
garage.clear();// removes all elements from the 'garage' vector
return 0;
}