Project 3
Build an object-oriented library which can be used to track automobile maintenance. There can be different car types such as petrol, diesel, gas, electric etc., This library should be able to support at least three of the car types.
Maintenance tasks include activities like oil change or replace oil filter, replace air filter, replace fuel filter, tire rotations, infotainment system debug, replace spark plugs, replace the cabin or a/c filter, check the level and refill clutch fluid or brake fluid, examine belts, wipers, hoses, check the tire conditions, check the charging systems and battery.
#include
#include
enum Type {
GAS,
DIESEL,
ELECTRIC
};
class Vehicle {
protected:
std::string make;
std::string model;
int year;
double odometer;
public:
Vehicle(const std::string& make, const std::string& model, int year, double odometer)
: make(make), model(model), year(year), odometer(odometer) {}
const std::string& getMake() const { return make; }
const std::string& getModel() const { return model; }
int getYear() const { return year; }
double getOdometer() const { return odometer; }
virtual Type getType() = 0;
virtual void checkTireRotation() = 0;
virtual void checkOilChange() = 0;
virtual void checkBatteryCharge() = 0;
void getCarInformation() {
std::cout << "Make: " << make << std::endl;
std::cout << "Model: " << model << std::endl;
std::cout << "Year: " << year << std::endl;
std::cout << "Odometer Reading: " << odometer << " km" << std::endl;
}
};
class GasCar : public Vehicle {
public:
GasCar(const std::string& make, const std::string& model, int year, double odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() override { return GAS; }
void checkTireRotation() override {
if (static_cast
std::cout << "Tire rotation advised." << std::endl;
} else {
int remainder = static_cast
if (remainder >= 0) {
std::cout << "Tire rotation advised." << std::endl;
} else {
std::cout << "Tire rotation not required." << std::endl;
}
}
}
void checkOilChange() override {
if (static_cast
std::cout << "Oil change advised." << std::endl;
} else {
std::cout << "No oil change required." << std::endl;
}
}
void checkBatteryCharge() override {
if (static_cast
std::cout << "Battery recharge advised." << std::endl;
} else {
std::cout << "No recharge required." << std::endl;
}
}
};
class DieselCar : public Vehicle {
public:
DieselCar(const std::string& make, const std::string& model, int year, double odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() override { return DIESEL; }
void checkTireRotation() override {}
void checkOilChange() override {
if (static_cast
std::cout << "Oil change advised." << std::endl;
} else {
std::cout << "No oil change required." << std::endl;
}
}
void checkBatteryCharge() override {
if (static_cast
std::cout << "Battery recharge advised." << std::endl;
} else {
std::cout << "No recharge required." << std::endl;
}
}
};
class ElectricCar : public Vehicle {
public:
ElectricCar(const std::string& make, const std::string& model, int year, double odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() override { return ELECTRIC; }
void checkTireRotation() override {
if (static_cast
std::cout << "Tire rotation advised." << std::endl;
} else {
int remainder = static_cast
if (remainder >= 0) {
std::cout << "Tire rotation advised." << std::endl;
} else {
std::cout << "Tire rotation not required." << std::endl;
}
}
}
void checkOilChange() override {
if (static_cast
std::cout << "Oil change advised." << std::endl;
} else {
std::cout << "No oil change required." << std::endl;
}
}
void checkBatteryCharge() override {
if (static_cast
std::cout << "Battery recharge advised." << std::endl;
} else {
std::cout << "No recharge required." << std::endl;
}
}
};
class Inventory {
private:
std::vector
public:
void addVehicle(Vehicle* vehicle) {
vehicles.push_back(vehicle);
}
size_t size() const {
return vehicles.size();
}
Vehicle* getVehicle(int index) {
if (index >= 0 && index < vehicles.size()) {
return vehicles[index];
}
return nullptr;
}
void listVehicles() {
for (size_t i = 0; i < vehicles.size(); i++) {
std::cout << "Vehicle " << i + 1 << std::endl;
vehicles[i]->getCarInformation();
std::cout << "Fuel Type: " << getTypeString(vehicles[i]->getType()) << std::endl;
std::cout << "Maintenance Checks:" << std::endl;
vehicles[i]->checkTireRotation();
vehicles[i]->checkOilChange();
vehicles[i]->checkBatteryCharge();
std::cout << "--------------------------" << std::endl;
}
}
void updateVehicle(int index, double newOdometer) {
if (index >= 0 && index < vehicles.size()) {
vehicles[index]->getCarInformation();
std::cout << "Fuel Type: " << getTypeString(vehicles[index]->getType()) << std::endl;
std::cout << "Updated Odometer Reading: " << newOdometer << " km" << std::endl;
std::cout << "Maintenance Checks:" << std::endl;
vehicles[index]->checkTireRotation();
vehicles[index]->checkOilChange();
vehicles[index]->checkBatteryCharge();
} else {
std::cout << "Invalid vehicle index." << std::endl;
}
}
void removeVehicle(int index) {
if (index >= 0 && index < vehicles.size()) {
delete vehicles[index];
vehicles.erase(vehicles.begin() + index);
std::cout << "Vehicle removed from inventory." << std::endl;
} else {
std::cout << "Invalid vehicle index." << std::endl;
}
}
static std::string getTypeString(Type type) {
switch (type) {
case GAS: return "Gas";
case DIESEL: return "Diesel";
case ELECTRIC: return "Electric";
default: return "Unknown";
}
}
};
int main() {
Inventory inventory;
while (true) {
std::cout << "Select an option:\n"
<< "0. Quit\n"
<< "1. List inventory\n"
<< "2. Add vehicle\n"
<< "3. Update vehicle\n"
<< "4. Remove vehicle\n"
<< "5. Check Tire Rotation\n"
<< "6. Check Oil Change\n"
<< "7. Check Battery Charge\n"
<< "Enter your choice: ";
int choice;
std::cin >> choice;
if (choice == 0) {
break;
}
if (choice == 1) {
inventory.listVehicles();
} else if (choice == 2) {
std::string make, model;
int year, fuelType;
double odometer;
Type type;
std::cout << "Enter make: ";
std::cin >> make;
std::cout << "Enter model: ";
std::cin >> model;
std::cout << "Enter year: ";
std::cin >> year;
std::cout << "Enter odometer reading: ";
std::cin >> odometer;
do {
std::cout << "Enter fuel type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): ";
std::cin >> fuelType;
} while (fuelType < 0 || fuelType > 2);
type = static_cast
Vehicle* vehicle = nullptr;
if (type == GAS) {
vehicle = new GasCar(make, model, year, odometer);
} else if (type == DIESEL) {
vehicle = new DieselCar(make, model, year, odometer);
} else if (type == ELECTRIC) {
vehicle = new ElectricCar(make, model, year, odometer);
}
if (vehicle) {
inventory.addVehicle(vehicle);
std::cout << "Vehicle added to inventory." << std::endl;
}
} else if (choice == 3) {
int index;
double newOdometer;
std::cout << "Enter the index of the vehicle to update: ";
std::cin >> index;
if (index < 1 || index > inventory.size()) {
std::cout << "Invalid vehicle index." << std::endl;
continue;
}
std::cout << "Enter the new odometer reading: ";
std::cin >> newOdometer;
inventory.updateVehicle(index - 1, newOdometer);
} else if (choice == 4) {
int index;
std::cout << "Enter the index of the vehicle to remove: ";
std::cin >> index;
if (index < 1 || index > inventory.size()) {
std::cout << "Invalid vehicle index." << std::endl;
continue;
}
inventory.removeVehicle(index - 1);
} else if (choice == 5) {
int index;
std::cout << "Enter the index of the vehicle to check tire rotation: ";
std::cin >> index;
if (index < 1 || index > inventory.size()) {
std::cout << "Invalid vehicle index." << std::endl;
continue;
}
std::cout << "Tire Rotation Check for Vehicle " << index << std::endl;
inventory.getVehicle(index - 1)->checkTireRotation();
} else if (choice == 6) {
int index;
std::cout << "Enter the index of the vehicle to check oil change: ";
std::cin >> index;
if (index < 1 || index > inventory.size()) {
std::cout << "Invalid vehicle index." << std::endl;
continue;
}
std::cout << "Oil Change Check for Vehicle " << index << std::endl;
inventory.getVehicle(index - 1)->checkOilChange();
} else if (choice == 7) {
int index;
std::cout << "Enter the index of the vehicle to check battery charge: ";
std::cin >> index;
if (index < 1 || index > inventory.size()) {
std::cout << "Invalid vehicle index." << std::endl;
continue;
}
std::cout << "Battery Charge Check for Vehicle " << index << std::endl;
inventory.getVehicle(index - 1)->checkBatteryCharge();
} else {
std::cout << "Invalid choice. Please try again." << std::endl;
}
}
return 0;
}

In above program first step is to add vehicle as shown in the above image
next step add another vehicle


Then check for tire rotation as shown in the above image

check for the oil change required

we can aslo check for the battery cahnge