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.
Every vehicle should have the following details:
-
Make
-
Model
-
Year
-
Odometer reading
The library interfaces or the API’s (Application Program Interface) should be capable of adding, updating, listing and removing the entries at runtime.
Consider there are three classes such as
-
Vehicle
-
This class includes protected members such as Make, Model, Year and Odometer. These members are initialized using the class constructor.
-
The functions include getter and setter functions of Make(), Model(), Year() and Odometer().
-
Maintenance activities that should be done are checkTireRotation(), getCarInformation()
-
Let the three fuel types be
enum Type{
GAS,
DIESEL,
ELECTRIC
};
-
Let there be a pure virtual method to get the fuel type i.e.,
-
virtual Type getType() = 0;
-
GasCar
-
This is inherited from the vehicle class and has functions getType() and getFuelType()
-
DieselCar
-
This class is also a subclass of vehicle and the functions include getFuelType() and getType()
-
ElectricCar
-
This is a subclass of Vehicle class and methods include
-
checkTireRotation()- based on odometer reading, we decide whether the car needs to rotate tires or not.
-
checkChargerPower- based on odometer reading this advices to recharge power
-
getType() – to get the fuel type
-
FuelVehicle
-
This is an abstract class that inherits vehicle class. The methods in this class include checkOil() and pure virtual function getFuelType()
Maintenance activities that has to be done are as follows,
-
Check tire rotation: Let the tire cycle be 50,000 km. Calculate the distance the car has run and if this distance run is at 50k km, then advise rotation, else tires are good to go. Also, calculate the remainder of each 50k km, If remainder >=0 then advice to rotate tires.
-
Check Power: Let the distance be 400km, If the distance the car has run is at 400km, then advise battery to be recharged else your vehicle is good to go.
Now that we have learnt about the classes to be created, we need to write our driver code i.e., main.cpp
-
In the main class, initialize the inventory and ask the user to select whether to add, update, list or delete the Vehicle list.
-
addVehicle() – to add new vehicle to inventory
-
ListVehicle()- to list the vehicles in the inventory
-
UpdateVehicle()- to update the vehicle in the inventory list
-
RemoveVehicle()- to remove the vehicle from the inventory list
-
Tire Rotation() – Check if tires need rotation
-
OilChange()- check if car requires oil change
-
Recharge() – check if vehicle needs recharge
-
Check for the user input, 0-1-2-3-4-5-6-7, Functions will be called based on the input provided by the user. If the input is wrong, ask again.
-
Menu should be as below,
|
<< " 0. Quit.\n" |
|
|
<< " 1. List inventory.\n" |
|
|
<< " 2. Add vehicle.\n" |
|
|
<< " 3. Update vehicle\n" |
|
|
<< " 4. Remove vehicle\n" |
|
|
<< " 5. Tire Rotation\n" |
|
|
<< " 6. Oil Change\n" |
|
|
<< " 7. Recharge\n" |
|
-
Retrieve the user input on the vehicle’s make, model, year, type and odometer.
-
getNewMake() – If input is blank, request again
-
getNewModel() – If input is blank, request again
-
getYear() – If input doesn’t have 4 digits, request again
-
getNewOdometer()- Input should be only numericals
-
getNewType() – Input should be within 0-2, else request again
-
Retrieve the selected car from the user input
-
selectCar() – Input should be within the size of vehicle list, else request again
-
getNewOdometerReading() – New odometer reading is used to calculate the oil change, tire rotation, power recharge etc.
Ans-
#include <iostream>
#include <vector>
#include <string>
#include <limits>
enum Type {
GAS,
DIESEL,
ELECTRIC
};
class Vehicle {
protected:
std::string make;
std::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) {}
std::string getMake() const { return make; }
std::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 checkPower() const = 0;
virtual void printCarInformation() const {
std::cout << "Make: " << make << ", Model: " << model << ", Year: " << year << ", Odometer: " << odometer << " km\n";
}
};
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 {
int tireCycle = 50000;
int remainder = odometer % tireCycle;
if (remainder >= 0)
std::cout << "Tire rotation advised.\n";
else
std::cout << "Tires are good to go.\n";
}
void checkPower() const override {
int powerCycle = 400;
if (odometer % powerCycle == 0)
std::cout << "Recharge advised.\n";
else
std::cout << "Vehicle is good to go.\n";
}
};
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 {
int tireCycle = 50000;
int remainder = odometer % tireCycle;
if (remainder >= 0)
std::cout << "Tire rotation advised.\n";
else
std::cout << "Tires are good to go.\n";
}
void checkPower() const override {
int powerCycle = 400;
if (odometer % powerCycle == 0)
std::cout << "Recharge advised.\n";
else
std::cout << "Vehicle is good to go.\n";
}
};
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 {
int tireCycle = 50000;
int remainder = odometer % tireCycle;
if (remainder >= 0)
std::cout << "Tire rotation advised.\n";
else
std::cout << "Tires are good to go.\n";
}
void checkPower() const override {
int powerCycle = 400;
if (odometer % powerCycle == 0)
std::cout << "Recharge advised.\n";
else
std::cout << "Vehicle is good to go.\n";
}
};
class MaintenanceSystem {
private:
std::vector<Vehicle*> vehicles;
public:
~MaintenanceSystem() {
for (auto& vehicle : vehicles)
delete vehicle;
}
void addVehicle(Vehicle* vehicle) {
vehicles.push_back(vehicle);
}
void listVehicles() const {
for (const auto& vehicle : vehicles) {
vehicle->printCarInformation();
}
}
void updateVehicle(int index, int newOdometerReading) {
if (index >= 0 && index < vehicles.size()) {
vehicles[index]->setOdometer(newOdometerReading);
}
}
void removeVehicle(int index) {
if (index >= 0 && index < vehicles.size()) {
delete vehicles[index];
vehicles.erase(vehicles.begin() + index);
}
}
void tireRotation(int index) const {
if (index >= 0 && index < vehicles.size()) {
vehicles[index]->checkTireRotation();
}
}
void oilChange(int index) const {
if (index >= 0 && index < vehicles.size()) {
// Implementation of oil change check
}
}
void recharge(int index) const {
if (index >= 0 && index < vehicles.size()) {
vehicles[index]->checkPower();
}
}
};
int getUserInput() {
int choice;
std::cout << "Enter your choice: ";
std::cin >> choice;
while (std::cin.fail() || choice < 0 || choice > 7) {
std::cout << "Invalid input. Please enter a number between 0 and 7: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin >> choice;
}
return choice;
}
std::string getStringInput(const std::string& message) {
std::string input;
std::cout << message;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, input);
return input;
}
int getIntegerInput(const std::string& message) {
int input;
std::cout << message;
std::cin >> input;
while (std::cin.fail()) {
std::cout << "Invalid input. Please enter a valid integer: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin >> input;
}
return input;
}
Type getFuelTypeFromInput(int input) {
switch (input) {
case 0:
return Type::GAS;
case 1:
return Type::DIESEL;
case 2:
return Type::ELECTRIC;
default:
return Type::GAS;
}
}
int main() {
MaintenanceSystem maintenanceSystem;
while (true) {
std::cout << "0. Quit.\n"
<< "1. List inventory.\n"
<< "2. Add vehicle.\n"
<< "3. Update vehicle.\n"
<< "4. Remove vehicle.\n"
<< "5. Tire Rotation.\n"
<< "6. Oil Change.\n"
<< "7. Recharge.\n";
int choice = getUserInput();
switch (choice) {
case 0:
return 0;
case 1:
maintenanceSystem.listVehicles();
break;
case 2: {
std::string make = getStringInput("Enter make: ");
std::string model = getStringInput("Enter model: ");
int year = getIntegerInput("Enter year: ");
int odometer = getIntegerInput("Enter odometer reading: ");
int fuelTypeChoice = getIntegerInput("Enter fuel type (0 for GAS, 1 for DIESEL, 2 for ELECTRIC): ");
Type fuelType = getFuelTypeFromInput(fuelTypeChoice);
switch (fuelType) {
case Type::GAS:
maintenanceSystem.addVehicle(new GasCar(make, model, year, odometer));
break;
case Type::DIESEL:
maintenanceSystem.addVehicle(new DieselCar(make, model, year, odometer));
break;
case Type::ELECTRIC:
maintenanceSystem.addVehicle(new ElectricCar(make, model, year, odometer));
break;
default:
std::cout << "Invalid fuel type.\n";
break;
}
break;
}
case 3: {
int index = getIntegerInput("Enter the index of the vehicle to update: ");
int newOdometerReading = getIntegerInput("Enter new odometer reading: ");
maintenanceSystem.updateVehicle(index, newOdometerReading);
break;
}
case 4: {
int index = getIntegerInput("Enter the index of the vehicle to remove: ");
maintenanceSystem.removeVehicle(index);
break;
}
case 5: {
int index = getIntegerInput("Enter the index of the vehicle for tire rotation check: ");
maintenanceSystem.tireRotation(index);
break;
}
case 6: {
int index = getIntegerInput("Enter the index of the vehicle for oil change check: ");
maintenanceSystem.oilChange(index);
break;
}
case 7: {
int index = getIntegerInput("Enter the index of the vehicle for power recharge check: ");
maintenanceSystem.recharge(index);
break;
}
default:
std::cout << "Invalid choice.\n";
break;
}
}
return 0;
}
This code defines the classes for different types of cars, a maintenance system, and a simple command-line interface for interacting with the maintenance system. It allows users to add, update, list, and remove vehicles, as well as perform maintenance tasks such as tire rotation, oil change, and power recharge.