Project 3
Aim:
- To implements an object-oriented library for tracking automobile maintenance
Objective:
Vehicle Management:
- Add Vehicles: Allow users to add new vehicles to the inventory with details such as make, model, year, and odometer reading.
- Update Vehicles: Enable updating existing vehicle information in the inventory.
- List Vehicles: Provide a list of all vehicles in the inventory with their details.
- Remove Vehicles: Allow users to remove vehicles from the inventory.
Maintenance Tracking:
- Check Tire Rotation: Determine if a vehicle needs tire rotation based on the odometer reading.
- Check Oil Change: For fuel vehicles, determine if an oil change is needed based on the odometer reading.
- Check Battery Recharge: For electric vehicles, determine if a battery recharge is needed based on the odometer reading.
User Interaction:
- Provide a user-friendly menu to interact with the system.
- Validate user inputs to ensure correct data entry for vehicle details and maintenance checks.
Source Code:
/*
* project 3.c++
*
* Created on: Jul 26, 2024
* Author: mages
*/
#include <iostream>
#include <vector>
#include <memory>
#include <string>
using namespace std;
enum Type {
GAS,
PETROL,
DIESEL,
ELECTRIC
};
class Vehicle {
protected:
string make;
string model;
int year;
int odometer;
public:
Vehicle(const string& make, const string& model, int year, int odometer)
: make(make), model(model), year(year), odometer(odometer) {}
virtual ~Vehicle() {}
string getMake() const { return make; }
string getModel() const { return model; }
int getYear() const { return year; }
int getOdometer() const { return odometer; }
void setMake(const string& make) { this->make = make; }
void setModel(const string& model) { this->model = model; }
void setYear(int year) { this->year = year; }
void setOdometer(int odometer) { this->odometer = odometer; }
virtual Type getType() = 0;
virtual void checkTireRotation() const {
if (odometer >= 50000) {
cout << "Tire rotation needed." << endl;
} else {
cout << "Tires are good to go." << endl;
}
}
virtual string getCarInformation() const {
return "Make: " + make + ", Model: " + model + ", Year: " + to_string(year) + ", Odometer: " + to_string(odometer);
}
};
class FuelVehicle : public Vehicle {
public:
FuelVehicle(const string& make, const string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
virtual void checkOil() const {
cout << "Checking oil..." << endl;
// Logic for checking oil
}
virtual Type getFuelType() = 0;
};
class GasCar : public FuelVehicle {
public:
GasCar(const string& make, const string& model, int year, int odometer)
: FuelVehicle(make, model, year, odometer) {}
Type getType() override { return GAS; }
Type getFuelType() override { return GAS; }
};
class PetrolCar : public FuelVehicle {
public:
PetrolCar(const string& make, const string& model, int year, int odometer)
: FuelVehicle(make, model, year, odometer) {}
Type getType() override { return GAS; }
Type getFuelType() override { return GAS; }
};
class DieselCar : public FuelVehicle {
public:
DieselCar(const string& make, const string& model, int year, int odometer)
: FuelVehicle(make, model, year, odometer) {}
Type getType() override { return DIESEL; }
Type getFuelType() override { return DIESEL; }
};
class ElectricCar : public Vehicle {
public:
ElectricCar(const string& make, const string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type getType() override { return ELECTRIC; }
void checkTireRotation() const override {
if (odometer >= 50000) {
cout << "Tire rotation needed." << endl;
} else {
cout << "Tires are good to go." << endl;
}
}
void checkChargerPower() const {
if (odometer >= 400) {
cout << "Recharge needed." << endl;
} else {
cout << "Battery is good to go." << endl;
}
}
};
void addVehicle(vector<shared_ptr<Vehicle>>& inventory) {
string make, model;
int year, odometer, type;
cout << "Enter make: ";
cin >> make;
cout << "Enter model: ";
cin >> model;
cout << "Enter year: ";
cin >> year;
cout << "Enter odometer reading: ";
cin >> odometer;
cout << "Enter type (0 = GAS, 1 = PETROL, 2 = DIESEL, 3 = ELECTRIC): ";
cin >> type;
shared_ptr<Vehicle> vehicle;
switch (type) {
case GAS:
vehicle = make_shared<GasCar>(make, model, year, odometer);
break;
case DIESEL:
vehicle = make_shared<DieselCar>(make, model, year, odometer);
break;
case ELECTRIC:
vehicle = make_shared<ElectricCar>(make, model, year, odometer);
break;
default:
cout << "Invalid type." << endl;
return;
}
inventory.push_back(vehicle);
cout << "Vehicle added." << endl;
}
void listVehicles(const vector<shared_ptr<Vehicle>>& inventory) {
for (const auto& vehicle : inventory) {
cout << vehicle->getCarInformation() << endl;
}
}
void updateVehicle(vector<shared_ptr<Vehicle>>& inventory) {
int index;
if (inventory.empty()) {
cout << "No vehicles in inventory." << endl;
return;
}
cout << "Enter vehicle index (0 to " << inventory.size() - 1 << "): ";
cin >> index;
if (index < 0 || index >= inventory.size()) {
cout << "Invalid index." << endl;
return;
}
string make, model;
int year, odometer;
cout << "Enter new make: ";
cin >> make;
cout << "Enter new model: ";
cin >> model;
cout << "Enter new year: ";
cin >> year;
cout << "Enter new odometer reading: ";
cin >> odometer;
inventory[index]->setMake(make);
inventory[index]->setModel(model);
inventory[index]->setYear(year);
inventory[index]->setOdometer(odometer);
cout << "Vehicle updated." << endl;
}
void removeVehicle(vector<shared_ptr<Vehicle>>& inventory) {
int index;
if (inventory.empty()) {
cout << "No vehicles in inventory." << endl;
return;
}
cout << "Enter vehicle index (0 to " << inventory.size() - 1 << "): ";
cin >> index;
if (index < 0 || index >= inventory.size()) {
cout << "Invalid index." << endl;
return;
}
inventory.erase(inventory.begin() + index);
cout << "Vehicle removed." << endl;
}
void checkTireRotation(const vector<shared_ptr<Vehicle>>& inventory) {
int index;
if (inventory.empty()) {
cout << "No vehicles in inventory." << endl;
return;
}
cout << "Enter vehicle index (0 to " << inventory.size() - 1 << "): ";
cin >> index;
if (index < 0 || index >= inventory.size()) {
cout << "Invalid index." << endl;
return;
}
inventory[index]->checkTireRotation();
}
void checkOilChange(const vector<shared_ptr<Vehicle>>& inventory) {
for (const auto& vehicle : inventory) {
FuelVehicle* fuelVehicle = dynamic_cast<FuelVehicle*>(vehicle.get());
if (fuelVehicle) {
fuelVehicle->checkOil();
} else {
cout << "This vehicle does not require an oil change." << endl;
}
}
}
void checkRecharge(const vector<shared_ptr<Vehicle>>& inventory) {
for (const auto& vehicle : inventory) {
ElectricCar* electricCar = dynamic_cast<ElectricCar*>(vehicle.get());
if (electricCar) {
electricCar->checkChargerPower();
} else {
cout << "This vehicle does not require recharging." << endl;
}
}
}
int main() {
vector<shared_ptr<Vehicle>> inventory;
int choice;
do {
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. Check tire rotation.\n";
cout << "6. Check oil change.\n";
cout << "7. Check recharge.\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 0:
break;
case 1:
listVehicles(inventory);
break;
case 2:
addVehicle(inventory);
break;
case 3:
updateVehicle(inventory);
break;
case 4:
removeVehicle(inventory);
break;
case 5:
checkTireRotation(inventory);
break;
case 6:
checkOilChange(inventory);
break;
case 7:
checkRecharge(inventory);
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 0);
return 0;
}
Output:
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 2
Enter make: marutisuzuki
Enter model: alto
Enter year: 2020
Enter odometer reading: 55000
Enter type (0 = GAS, 1 = PETROL, 2 = DIESEL, 3 = ELECTRIC): 0
Vehicle added.
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 2
Enter make: mahindra
Enter model: bolero
Enter year: 2014
Enter odometer reading: 45000
Enter type (0 = GAS, 1 = PETROL, 2 = DIESEL, 3 = ELECTRIC): 2
Vehicle added.
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 2
Enter make: BYD
Enter model: BYDSEAL
Enter year: 2022
Enter odometer reading: 50000
Enter type (0 = GAS, 1 = PETROL, 2 = DIESEL, 3 = ELECTRIC): 3
Vehicle added.
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 1
Make: marutisuzuki, Model: alto, Year: 2020, Odometer: 55000
Make: mahindra, Model: bolero, Year: 2014, Odometer: 45000
Make: BYD, Model: BYDSEAL, Year: 2022, Odometer: 50000
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 3
Enter vehicle index (0 to 2): 1
Enter new make: marutisuzuki
Enter new model: altoz
Enter new year: 2022
Enter new odometer reading: 55000
Vehicle updated.
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 4
Enter vehicle index (0 to 2): 1
Vehicle removed.
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 1
Make: marutisuzuki, Model: alto, Year: 2020, Odometer: 55000
Make: BYD, Model: BYDSEAL, Year: 2022, Odometer: 50000
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 5
Enter vehicle index (0 to 1): 0
Tire rotation needed.
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 6
Checking oil...
This vehicle does not require an oil change.
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 7
This vehicle does not require recharging.
Recharge needed.
0. Quit.
1. List inventory.
2. Add vehicle.
3. Update vehicle.
4. Remove vehicle.
5. Check tire rotation.
6. Check oil change.
7. Check recharge.
Enter your choice: 0
Explanation:
Enums and Classes:
- This enum defines three types of vehicles: GAS, PETROL, DIESEL, and ELECTRIC.
Class Vehicle:
Class FuelVehicle:
- GasCar inherits from FuelVehicle and implements the getType() and getFuelType() functions to return Type::GAS.
Class petrolCar:
- PetrolCar inherits from FuelVehicle and implements the getType() and getFuelType() functions to return Type::PETROL.
Class DieselCar:
- DieselCar inherits from FuelVehicle and implements the getType() and getFuelType() functions to return Type::DIESEL.
Class ElectricCar:
- ElectricCar inherits from Vehicle and implements the getType() function to return Type::ELECTRIC.
- It adds a function checkChargerPower() to check if the vehicle needs to recharge based on the odometer reading.
- It overrides checkTireRotation() to check for tire rotation.
Class Inventory:
- Inventory class manages a collection of vehicles using a vector of shared_ptr<Vehicle>.
- It provides functions to add, list, update, and remove vehicles from the inventory.
- getVehicle() returns a vehicle at a specific index.
Main Function:
- The main function provides a user interface to interact with the inventory and perform maintenance checks based on user input.
Screenshot of the Code:








Screenshot of the Output:
1)Adding the vehicle:


2)List Inventory:

3)Update Vehicle:

4)Remove Vehicle:

5)List Inventory:

6)Check Tire Rotation: 
7)Check Oil Change:

8)Check Recharge:

9)Quit:
