Project 3
#include
#include
#include // include the limits library for numeric limits
using namespace std;
//enumerated data type for vehicle fuel types
enum VehicleType {
GAS, // gas (element 0)
DIESEL, // diesel (element 1)
ELECTRIC // electric (element 2)
};
// Define a base class for vehicles
class Vehicle {
protected:
string make; // make of the vehicle
string model; // model of the vehicle
int year; // year the vehicle was manufactured
int odometer; // current odometer reading of the vehicle
public:
// Constructor of Vehicle class, to initialize the make, model, year, and odometer values
Vehicle(string make_, string model_, int year_, int odometer_)
: make(make_), model(model_), year(year_), odometer(odometer_) {}
//the setter has one parameter and does not return a value.
// Setter method for the make value
void setMake(string make_)
{
make = make_;
}
//A getter has no parameters and returns a value
// Getter method for the make value
string getMake() const
{
return make;
}
// Setter method for the model value
void setModel(string model_)
{
model = model_;
}
// Getter method for the model value
string getModel() const
{
return model;
}
// Setter method for the year value
void setYear(int year_)
{
year = year_;
}
// Getter method for the year value
int getYear() const
{
return year;
}
// Setter method for the odometer value
void setOdometer(int odometer_)
{
odometer = odometer_;
}
// Getter method for the odometer value
int getOdometer() const
{
return odometer;
}
// Pure virtual function to get the vehicle type,vehicle information
virtual VehicleType getType() = 0;
virtual void getCarInformation() = 0;
// Destructor for the Vehicle class
virtual ~Vehicle() {}
};
// inheriting gascar class from class vehicle
class GasCar : public Vehicle {
public:
// Constructor for GasCar class t
GasCar(string make_, string model_, int year_, int odometer_)
// Call the constructor of the base class 'Vehicle'
: Vehicle(make_, model_, year_, odometer_) {}
// Function to returns the fuel type of the car as GAS
VehicleType getType()
{
return GAS;
}
// Function to prints the make, model, year, odometer reading, and fuel type of the car
void getCarInformation()
{
cout << "Make: " << make << endl
<< "Model: " << model << endl
<< "Year: " << year << endl
<< "Odometer reading: " << odometer << endl
<< "Fuel type: Gas" << endl;
}
};
// inheriting DieselCar class from class vehicle
class DieselCar : public Vehicle
{
public:
// Constructor for DieselCar class
DieselCar(string make_, string model_, int year_, int odometer_)
// Call the constructor of the base class 'Vehicle'
: Vehicle(make_, model_, year_, odometer_) {}
// Function to returns the fuel type of the car as DIESEL
VehicleType getType() {
return DIESEL;
}
// Prints the information of the diesel car
void getCarInformation() {
cout << "Make: " << make << endl
<< "Model: " << model << endl
<< "Year: " << year << endl
<< "Odometer reading: " << odometer << endl
<< "Fuel type: Diesel" << endl;
}
};
// inheriting ElectricCar class from class vehicle
class ElectricCar : public Vehicle {
public:
// Constructor for ElectricCar class
ElectricCar(string make_, string model_, int year_, int odometer_)
// Call the constructor of the base class 'Vehicle'
: Vehicle(make_, model_, year_, odometer_) {}
// Function to returns the fuel type of the car as ELECTRIC
VehicleType getType() {
return ELECTRIC;
}
// Prints the information of the electric car
void getCarInformation() {
cout << "Make: " << make << endl
<< "Model: " << model << endl
<< "Year: " << year << endl
<< "Odometer reading: " << odometer << endl
<< "Fuel type: Electric" << endl;
}
};
// function to creates a new vehicle object based on user input and returns a pointer to that object.
Vehicle* addVehicle()
{
// Declare variables to hold user input
string make;
string model;
int year;
int odometer;
int choice;
// Prompt the user to select a vehicle type
cout << "Select vehicle type:n"
<< "1. Gasn"
<< "2. Dieseln"
<< "3. Electricn";
cin >> choice;
// Use while loops to validate user input for make, model, year, and odometer reading
//Runs until the user enter the valid input or non-empty input
while (true)
{
cout << "Enter make: ";
getline(cin >> ws, make);
if (!make.empty()) break;
}
//Runs until the user enter the valid input or non-empty input
while (true)
{
cout << "Enter model: ";
getline(cin >> ws, model);
if (!model.empty()) break;
}
// Runs until the user to enter year a valid year between 1900 and 2023 is entered
while (true)
{
cout << "Enter year: ";
if (cin >> year && year >= 1900 && year <= 2023) break;
cout << "Invalid input, please try again.n";
cin.clear();
cin.ignore(numeric_limits::max(), 'n');
}
// Runs until the user to enter odometer reading a valid non-negative integer is entered
while (true)
{
cout << "Enter odometer reading: ";
if (cin >> odometer && odometer >= 0) break;
cout << "Invalid input, please try again.n";
cin.clear();
cin.ignore(numeric_limits::max(), 'n');
}
// Create a new vehicle object based on user choice and input
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()
{
// Declare a vector of pointers to Vehicle objects called 'garage'.
vector<Vehicle*> garage;
// Declare a boolean variable 'running' and set it to true.
bool running = true;
// Declare an integer variable 'choice'.
int choice;
// while loop which continues to run until the user chooses to quit the program.
while (running)
{
// Display a menu of options for the user to choose from.
cout << "nSelect an option:n"
<< " 1. Add vehiclen"
<< " 2. List inventory n"
<< " 3. Update vehiclen"
<< " 4. Remove vehiclen";
// Get the user's choice.
cin >> choice;
// Use a switch statement to perform the appropriate action based on the user's 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:
{
int index;
cout << "Enter the index of the vehicle you want to update: ";
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: ";
getline(cin >> ws, make);
if (!make.empty()) break;
}
while (true)
{
cout << "Enter model: ";
getline(cin >> ws, model);
if (!model.empty()) break;
}
while (true)
{
cout << "Enter year: ";
if (cin >> year && year >= 1900 && year <= 2023) break;
cout << "Invalid input, please try again.n";
cin.clear();
cin.ignore(numeric_limits::max(), 'n');
}
while (true)
{
cout << "Enter odometer reading: ";
if (cin >> odometer && odometer >= 0) break;
cout << "Invalid input, please try again.n";
cin.clear();
cin.ignore(numeric_limits::max(), 'n');
}
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 < garage.size())
{
delete garage[index];
garage.erase(garage.begin() + index);
}
else
{
cout << "Invalid index.n";
}
break;
}
// This case handles the user choosing to exit the program.
case 0:
// Print a message indicating the program is exiting.
cout<<"Exiting.... good bye...";
running = false; // sets the variable 'running' to false, which will exit the while loop
break; // breaks out of the switch statement
default: // this code block executes if the user enters a choice that doesn't match any of the cases above
cout << "Invalid choice.n"; // prints an error message
break; // breaks out of the switch statement
}
}
for (Vehicle* vehicle : garage)
{
// iterates through the 'garage' vector using a range-based for loop and assigns each element to 'vehicle'
delete vehicle; // deletes each element of 'garage', which must have been dynamically allocated
}
garage.clear(); // removes all elements from the 'garage' vector
return 0; // exits the program with a return value of 0
}