Project 3
#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <limits>
using namespace std;
// Enum for fuel types
enum Type {
GAS,
DIESEL,
ELECTRIC
};
// Base class Vehicle
class Vehicle {
protected:
string make;
string model;
int year;
int odometer;
public:
Vehicle(string m, string mo, int y, int o) : make(m), model(mo), year(y), odometer(o) {}
virtual ~Vehicle() = default;
// Getters and Setters
string getMake() const { return make; }
string getModel() const { return model; }
int getYear() const { return year; }
int getOdometer() const { return odometer; }
void setMake(const string& m) { make = m; }
void setModel(const string& mo) { model = mo; }
void setYear(int y) { year = y; }
void setOdometer(int o) { odometer = o; }
virtual Type getType() = 0;
void getCarInformation() const {
cout << "Make: " << make << ", Model: " << model << ", Year: " << year << ", Odometer: " << odometer << " km\n";
}
virtual void checkTireRotation() const {
if (odometer % 50000 == 0) {
cout << "Tires need rotation.\n";
} else {
cout << "Tires are good to go.\n";
}
}
};
// Abstract class FuelVehicle
class FuelVehicle : public Vehicle {
public:
FuelVehicle(string m, string mo, int y, int o) : Vehicle(m, mo, y, o) {}
virtual Type getFuelType() = 0;
void checkOil() const {
if (odometer % 10000 == 0) {
cout << "Oil change required.\n";
} else {
cout << "Oil is good.\n";
}
}
};
// GasCar class
class GasCar : public FuelVehicle {
public:
GasCar(string m, string mo, int y, int o) : FuelVehicle(m, mo, y, o) {}
Type getType() override { return GAS; }
Type getFuelType() override { return GAS; }
};
// DieselCar class
class DieselCar : public FuelVehicle {
public:
DieselCar(string m, string mo, int y, int o) : FuelVehicle(m, mo, y, o) {}
Type getType() override { return DIESEL; }
Type getFuelType() override { return DIESEL; }
};
// ElectricCar class
class ElectricCar : public Vehicle {
public:
ElectricCar(string m, string mo, int y, int o) : Vehicle(m, mo, y, o) {}
Type getType() override { return ELECTRIC; }
void checkTireRotation() const override {
if (odometer % 50000 == 0) {
cout << "Tires need rotation.\n";
} else {
cout << "Tires are good to go.\n";
}
}
void checkChargerPower() const {
if (odometer % 400 == 0) {
cout << "Recharge battery.\n";
} else {
cout << "Battery is good.\n";
}
}
};
// Inventory management
class Inventory {
private:
vector<shared_ptr<Vehicle>> vehicles;
public:
void addVehicle(shared_ptr<Vehicle> vehicle) {
vehicles.push_back(vehicle);
}
void listVehicles() const {
if (vehicles.empty()) {
cout << "No vehicles in the inventory.\n";
return;
}
for (size_t i = 0; i < vehicles.size(); ++i) {
cout << i + 1 << ". ";
vehicles[i]->getCarInformation();
}
}
void removeVehicle(size_t index) {
if (index >= 1 && index <= vehicles.size()) {
vehicles.erase(vehicles.begin() + index - 1);
cout << "Vehicle removed.\n";
} else {
cout << "Invalid index.\n";
}
}
shared_ptr<Vehicle> selectVehicle(size_t index) {
if (index >= 1 && index <= vehicles.size()) {
return vehicles[index - 1];
}
return nullptr;
}
};
// Helper functions for input validation
int getIntInput(const string& prompt) {
int value;
while (true) {
cout << prompt;
cin >> value;
if (cin.fail() || value <= 0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please try again.\n";
} else {
return value;
}
}
}
string getStringInput(const string& prompt) {
string value;
while (true) {
cout << prompt;
cin >> value;
if (value.empty()) {
cout << "Input cannot be blank. Please try again.\n";
} else {
return value;
}
}
}
Type getTypeInput() {
int type;
while (true) {
cout << "Enter vehicle type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): ";
cin >> type;
if (type >= 0 && type <= 2) {
return static_cast<Type>(type);
} else {
cout << "Invalid input. Please try again.\n";
}
}
}
int main() {
Inventory inventory;
int choice;
do {
cout << "\nMenu:\n";
cout << "0. Quit\n";
cout << "1. List inventory\n";
cout << "2. Add vehicle\n";
cout << "3. Remove vehicle\n";
cout << "4. Tire Rotation\n";
cout << "5. Oil Change\n";
cout << "6. Recharge\n";
choice = getIntInput("Enter your choice: ");
switch (choice) {
case 0:
cout << "Exiting program.\n";
break;
case 1:
inventory.listVehicles();
break;
case 2: {
string make = getStringInput("Enter make: ");
string model = getStringInput("Enter model: ");
int year = getIntInput("Enter year: ");
int odometer = getIntInput("Enter odometer reading: ");
Type type = getTypeInput();
switch (type) {
case GAS:
inventory.addVehicle(make_shared<GasCar>(make, model, year, odometer));
break;
case DIESEL:
inventory.addVehicle(make_shared<DieselCar>(make, model, year, odometer));
break;
case ELECTRIC:
inventory.addVehicle(make_shared<ElectricCar>(make, model, year, odometer));
break;
}
break;
}
case 3: {
inventory.listVehicles();
int index = getIntInput("Enter the index of the vehicle to remove: ");
inventory.removeVehicle(index);
break;
}
case 4: {
inventory.listVehicles();
int index = getIntInput("Enter the index of the vehicle to check tire rotation: ");
auto vehicle = inventory.selectVehicle(index);
if (vehicle) {
vehicle->checkTireRotation();
} else {
cout << "Invalid vehicle selected.\n";
}
break;
}
case 5: {
inventory.listVehicles();
int index = getIntInput("Enter the index of the vehicle to check oil: ");
auto vehicle = inventory.selectVehicle(index);
auto fuelVehicle = dynamic_pointer_cast<FuelVehicle>(vehicle);
if (fuelVehicle) {
fuelVehicle->checkOil();
} else {
cout << "This vehicle does not use oil.\n";
}
break;
}
case 6: {
inventory.listVehicles();
int index = getIntInput("Enter the index of the vehicle to check recharge: ");
auto vehicle = inventory.selectVehicle(index);
auto electricCar = dynamic_pointer_cast<ElectricCar>(vehicle);
if (electricCar) {
electricCar->checkChargerPower();
} else {
cout << "This vehicle does not require a recharge.\n";
}
break;
}
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 0);
return 0;
}
OUTPUT:
0. Quit
1. List inventory
2. Add vehicle
3. Remove vehicle
4. Tire Rotation
5. Oil Change
6. Recharge
Enter your choice: 5
No vehicles in the inventory.
Enter the index of the vehicle to check oil: 5
This vehicle does not use oil.