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
Answer:
#include<iostream>
using namespace std;
enum Type {
GAS,
DIESEL,
ELECTRIC
};
class Vehicle {
protected:
std::string make;
std::string model;
int year;
int odometer;
public:
Vehicle(std::string make_, 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; }
void setOdometer(int odometer_) { odometer = odometer_; }
virtual Type getType() = 0;
virtual void checkTireRotation() {
int distance = odometer % 50000;
if (distance >= 0) {
std::cout << "Tire rotation advised." << std::endl;
}
else {
std::cout << "Tires are good to go." << std::endl;
}
}
virtual void checkPower() {
int distance = odometer % 400;
if (distance == 0) {
std::cout << "Battery recharge advised." << std::endl;
}
else {
std::cout << "Your vehicle is good to go." << std::endl;
}
}
virtual void getCarInformation() const {
std::cout << "Make: " << make << ", Model: " << model << ", Year: " << year << ", Odometer: " << odometer << std::endl;
}
};
class GasCar : public Vehicle {
public:
GasCar(std::string make_, std::string model_, int year_, int odometer_) : Vehicle(make_, model_, year_, odometer_) {}
Type getType() { return Type::GAS; }
void getFuelType() {
std::cout << "Fuel Type: Gas" << std::endl;
}
};
class DieselCar : public Vehicle {
public:
DieselCar(std::string make_, std::string model_, int year_, int odometer_) : Vehicle(make_, model_, year_, odometer_) {}
Type getType() { return Type::DIESEL; }
void getFuelType() {
std::cout << "Fuel Type: Diesel" << std::endl;
}
};
class ElectricCar : public Vehicle {
public:
ElectricCar(std::string make_, std::string model_, int year_, int odometer_) : Vehicle(make_, model_, year_, odometer_) {}
Type getType() { return Type::ELECTRIC; }
void checkTireRotation() {
int distance = odometer % 40000;
if (distance >= 0) {
std::cout << "Tire rotation advised." << std::endl;
}
else {
std::cout << "Tires are good to go." << std::endl;
}
}
void checkChargerPower() {
int distance = odometer % 300;
if (distance == 0) {
std::cout << "Charger power recharge advised." << std::endl;
}
else {
std::cout << "Your vehicle is good to go." << std::endl;
}
}
void getFuelType() {
std::cout << "Fuel Type: Electric" << std::endl;
}
};
class MaintenanceLibrary {
private:
vector<vehicle*> inventory_;
public:
void addVehicle(Vehicle* vehicle) {
cout << "Enter the make of the vehicle: ";
cin >> vehicle->make_;
cout << "Enter the model of the vehicle: ";
cin >> vehicle->model_;
cout << "Enter the year of the vehicle: ";
cin >> vehicle->year_;
cout << "Enter the odometer reading of the vehicle: ";
cin >> vehicle->odometer_;
inventory_.push_back(vehicle);
cout << "Vehicle added to inventory.\n";
}
void listInventory() {
if (inventory_.empty()) {
cout << "The inventory is empty." << endl;
return;
}
cout << "Inventory:" << endl;
for (size_t i = 0; i < inventory_.size(); i++) {
Vehicle* vehicle = inventory_[i];
cout << i << ". " << vehicle->getMake() << " " << vehicle->getModel() << " (" << vehicle->getYear() << ")" << endl;
}
}
void updateVehicle(int index, string make, string model, int year, int odometer) {
if (index >= inventory_.size()) {
cout << "Vehicle not found." << endl;
return;
}
Vehicle* vehicle = inventory_[index];
vehicle->setMake(make);
vehicle->setModel(model);
vehicle->setYear(year);
vehicle->setOdometer(odometer);
}
void removeVehicle() {
if (inventory_.empty()) {
cout << "Inventory is empty.\n";
return;
}
int vehicleIndex;
cout << "Enter the index of the vehicle to remove: ";
cin >> vehicleIndex;
if (vehicleIndex < 1 || vehicleIndex > inventory_.size()) {
cout << "Invalid vehicle index.\n";
return;
}
inventory_.erase(inventory_.begin() + vehicleIndex - 1);
cout << "Vehicle removed from inventory.\n";
}
void tireRotation() {
if (inventory_.empty()) {
cout << "Inventory is empty.\n";
return;
}
for (int i = 0; i < inventory_.size(); i++) {
Vehicle* vehicle = inventory_[i];
ElectricCar* electricCar = dynamic_cast<electriccar*>(vehicle);
if (electricCar != nullptr) {
electricCar->checkTireRotation();
}
}
}
void oilChange() {
if (inventory_.empty()) {
cout << "Inventory is empty.\n";
return;
}
for (int i = 0; i < inventory_.size(); i++) {
Vehicle* vehicle = inventory_[i];
GasCar* gasCar = dynamic_cast<gascar*>(vehicle);
DieselCar* dieselCar = dynamic_cast<dieselcar*>(vehicle);
if (gasCar != nullptr || dieselCar != nullptr) {
int odometer = vehicle->getOdometer();
if (odometer % 10000 == 0) {
cout << "Vehicle " << i << " needs an oil change.\n";
}
}
}
}
void recharge() {
if (inventory_.empty()) {
cout << "Inventory is empty.\n";
return;
}
for (int i = 0; i < inventory_.size(); i++) {
Vehicle* vehicle = inventory_[i];
ElectricCar* electricCar = dynamic_cast<electriccar*>(vehicle);
if (electricCar != nullptr) {
electricCar->checkPower();
}
}
}
};
vector<vehicle> inventory;
int main() {
Vehicle vec;
int userInput;
do {
cout << "Menu:\n";
cout << " 0. Quit.\n";
cout << " 1. List inventory.\n";
cout << " 2. Add vehicle.\n";
cout << " 3. Update vehicle.\n";
cout << " 4. Remove vehicle.\n";
cout << " 5. Tire rotation.\n";
cout << " 6. Oil change.\n";
cout << " 7. Recharge.\n";
cout << "Enter your choice: ";
cin >> userInput;
switch (userInput) {
case 0:
cout << "Exiting program.\n";
break;
case 1:
library.listInventory();
break;
case 2: {
string make, model;
int year, odometer;
cout << "Enter vehicle make: ";
cin >> make;
cout << "Enter vehicle model: ";
cin >> model;
cout << "Enter vehicle year: ";
cin >> year;
cout << "Enter vehicle odometer reading: ";
cin >> odometer;
vec.addVehicle(new Vehicle(make, model, year, odometer));
break;
}
case 3: {
int index;
string make, model;
int year, odometer;
cout << "Enter the index of the vehicle to update: ";
cin >> index;
cout << "Enter the make of the vehicle: ";
cin >> make;
cout << "Enter the model of the vehicle: ";
cin >> model;
cout << "Enter the year of the vehicle: ";
cin >> year;
cout << "Enter the odometer reading of the vehicle: ";
cin >> odometer;
vec.updateVehicle(index, make, model, year, odometer);
break;
}
case 4:
vec.removeVehicle();
break;
case 5:
vec.tireRotation();
break;
case 6:
vec.oilChange();
break;
case 7:
vec.recharge();
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (userInput != 0);
return 0;
}
Output:
