Project 3
Program Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Enum to represent fuel types
enum class Type {
GAS,
DIESEL,
ELECTRIC
};
// Base class for Vehicle
class Vehicle {
protected:
string make;
string model;
int year;
int odometer;
public:
Vehicle(const std::string& make, const std::string& model, int year, int odometer)
: make(make), model(model), year(year), odometer(odometer) {}
string getMake() const {
return make;
}
string getModel() const {
return model;
}
int getYear() const {
return year;
}
int getOdometer() const {
return odometer;
}
virtual Type getType() const = 0;
virtual void checkTireRotation() const = 0;
virtual void getCarInformation() const = 0;
};
// GasCar class
class GasCar : public Vehicle {
public:
GasCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() const override {
return Type::GAS;
}
void checkTireRotation() const override {
cout << "Checking tire rotation for Gas Car based on odometer reading: " << odometer << std::endl;
// Implement tire rotation logic for gas car
}
void getCarInformation() const override {
cout << "Gas Car Information:" << std::endl;
cout << "Make: " << make << std::endl;
cout << "Model: " << model << std::endl;
cout << "Year: " << year << std::endl;
cout << "Odometer: " << odometer << std::endl;
cout << "Fuel Type: Gas" << std::endl;
}
};
// DieselCar class
class DieselCar : public Vehicle {
public:
DieselCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() const override {
return Type::DIESEL;
}
void checkTireRotation() const override {
cout << "Checking tire rotation for Diesel Car based on odometer reading: " << odometer << std::endl;
// Implement tire rotation logic for diesel car
}
void getCarInformation() const override {
cout << "Diesel Car Information:" << std::endl;
cout << "Make: " << make << std::endl;
cout << "Model: " << model << std::endl;
cout << "Year: " << year << std::endl;
cout << "Odometer: " << odometer << std::endl;
cout << "Fuel Type: Diesel" << std::endl;
}
};
// ElectricCar class
class ElectricCar : public Vehicle {
public:
ElectricCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() const override {
return Type::ELECTRIC;
}
void checkTireRotation() const override {
cout << "Checking tire rotation for Electric Car based on odometer reading: " << odometer << std::endl;
// Implement tire rotation logic for electric car
}
void getCarInformation() const override {
cout << "Electric Car Information:" << std::endl;
cout << "Make: " << make << std::endl;
cout << "Model: " << model << std::endl;
cout << "Year: " << year << std::endl;
cout << "Odometer: " << odometer << std::endl;
cout << "Fuel Type: Electric" << std::endl;
}
};
// Inventory class to manage vehicles
class Inventory {
private:
vector<Vehicle*> vehicles;
public:
~Inventory() {
// Clean up the allocated memory
for (Vehicle* vehicle : vehicles) {
delete vehicle;
}
}
void addVehicle(Vehicle* vehicle) {
vehicles.push_back(vehicle);
}
void listVehicles() const {
cout << "Inventory:" << endl;
for (const Vehicle* vehicle : vehicles) {
vehicle->getCarInformation();
cout << "--------------------------------------" <<endl;
}
}
void updateVehicle(int index, int newOdometer) {
if (index >= 0 && index < vehicles.size()) {
vehicles[index]->getCarInformation();
vehicles[index]->checkTireRotation();
vehicles[index]->getCarInformation();
vehicles[index]->getCarInformation();
// Update the odometer reading
vehicles[index]->getCarInformation();
}
}
void removeVehicle(int index) {
if (index >= 0 && index < vehicles.size()) {
delete vehicles[index];
vehicles.erase(vehicles.begin() + index);
}
}
};
// Function to display the menu and get user input
int getMenuChoice() {
int choice;
cout << "Menu:\n"
<< "0. Quit.\n"
<< "1. List inventory.\n"
<< "2. Add vehicle.\n"
<< "3. Update vehicle.\n"
<< "4. Remove vehicle.\n"
<< "Enter your choice: ";
cin >> choice;
cout << std::endl;
return choice;
}
// Function to get vehicle details from user
void getVehicleDetails(std::string& make, std::string& model, int& year, int& odometer) {
cout << "Enter make: ";
cin.ignore();
getline(std::cin, make);
cout << "Enter model: ";
getline(std::cin, model);
cout << "Enter year: ";
cin >> year;
cout << "Enter odometer reading: ";
cin >> odometer;
}
int main() {
Inventory inventory;
while (true) {
int choice = getMenuChoice();
if (choice == 0) {
break;
} else if (choice == 1) {
inventory.listVehicles();
} else if (choice == 2) {
int typeChoice;
string make, model;
int year, odometer;
getVehicleDetails(make, model, year, odometer);
cout << "Select fuel type:\n"
<< "0. Gas\n"
<< "1. Diesel\n"
<< "2. Electric\n"
<< "Enter your choice: ";
cin >> typeChoice;
Vehicle* vehicle = nullptr;
if (typeChoice == 0) {
vehicle = new GasCar(make, model, year, odometer);
} else if (typeChoice == 1) {
vehicle = new DieselCar(make, model, year, odometer);
} else if (typeChoice == 2) {
vehicle = new ElectricCar(make, model, year, odometer);
} else {
cout << "Invalid fuel type choice." << endl;
}
if (vehicle != nullptr) {
inventory.addVehicle(vehicle);
cout << "Vehicle added to inventory." << endl;
}
} else if (choice == 3) {
int index, newOdometer;
inventory.listVehicles();
cout << "Enter the index of the vehicle to update: ";
cin >> index;
cout << "Enter new odometer reading: ";
cin >> newOdometer;
inventory.updateVehicle(index, newOdometer);
} else if (choice == 4) {
int index;
inventory.listVehicles();
cout << "Enter the index of the vehicle to remove: ";
cin >> index;
inventory.removeVehicle(index);
cout << "Vehicle removed from inventory." << endl;
} else {
cout << "Invalid choice. Please try again." << endl;
}
cout << endl;
}
return 0;
}
