Project 3
Project-3:
Overview of the Project:
The overview of this project is as follows:
- This Project is a C++ program that defines a hierarchy of classes for managing a collection of vehicles and performing maintenance tasks on them.
- This project starts by defining an enumeration type `fuel_type`, which represents different types of fuel-based cars (GAS, DIESEL, ELECTRIC).
- Base class `Vehicle` is defined, which contains protected data members (make, model, year, odometer) and member functions to retrieve and update these values. The class also declares pure virtual functions for getting the fuel type and performing maintenance, as well as virtual functions for rotating tires, performing an oil change, and recharging the vehicle. It also includes virtual functions to check if tire rotation or battery recharge is needed.
- The Project then defines three derived classes: `GasCar`, `DieselCar`, and `ElectricCar`, which inherit from the `Vehicle` base class. Each derived class provides its implementations of the pure virtual functions to specify the fuel type and perform maintenance tasks specific to the respective car types.
- A class `MaintenanceLibrary` is defined, which manages a collection of vehicles using a vector. It provides member functions to add, remove, and update vehicles, as well as perform maintenance tasks such as rotating tires, performing an oil change, and checking tire rotation and battery recharge for each vehicle.
- The `main` function of the program creates an instance of the `MaintenanceLibrary` class and presents a menu to the user for interacting with the vehicle inventory. The user can select various options such as listing the inventory, adding a vehicle, updating vehicle information, performing maintenance tasks, and checking tire rotation or battery power.
Goals:
The goals for this project are as follows:
1. Develop a robust system for managing vehicle maintenance tasks.
2. Ensure that these maintenance tasks are executed correctly and timely for each vehicle in the inventory.
3. To accurately track and manage this information for each vehicle, allowing for easy retrieval and display of vehicle details.
4. Support and handle maintenance operations specific to each fuel type correctly.
5. Enhance the user interface, improve usability, and provide a seamless experience for users managing the vehicle inventory.
6. Implement robust error handling and input validation mechanisms to maintain data integrity.
7. Optimize these checks to minimize unnecessary maintenance operations and improve overall efficiency.
8. Ensure proper memory management, avoiding memory leaks and ensuring efficient use of system resources.
9. Enable better decision-making and provide insights into the overall maintenance and performance of the vehicles.
Key Highlights of the Project:
The key highlights of this project are as follows:
1. Vehicle Maintenance System: The code implements a system for managing vehicle maintenance tasks.
2. Fuel Types: The code supports Gas, Diesel, and Electric vehicles as distinct fuel types.
3. Maintenance Operations: Functions are included for rotating tires, performing oil changes, and recharging batteries.
4. User Interface: The code provides a menu-based interface for users to interact with the maintenance system.
5. Data Tracking: Vehicle information such as make, model, year, and odometer reading is stored and managed.
6. Dynamic Memory Allocation: The code uses dynamic memory allocation to manage the vehicle inventory.
7. Input Validation: The code ensures data integrity by validating user inputs and preventing data corruption.
8. Maintenance Checks: The code performs checks to determine when tire rotation or battery recharge is due.
9. Potential for Expansion: The code can be extended to include additional features such as reporting and analytics.
Deliverables of this project:
The deliverables of this project are as follows:
1. Vehicle Maintenance System: A fully functional system for managing vehicle maintenance tasks.
2. Menu-Based Interface: A user-friendly interface allowing users to interact with the maintenance system.
3. Vehicle Inventory Management: The ability to store and manage vehicle information such as make, model, year, and odometer reading.
4. Maintenance Operations: Functions for performing common maintenance tasks like rotating tires, oil changes, and battery recharging.
5. Fuel Type Support: Support for Gas, Diesel, and Electric vehicles as distinct fuel types.
6. Data Validation: Input validation to ensure data integrity and prevent data corruption.
7. Dynamic Memory Allocation: Efficient memory management for the vehicle inventory.
8. Maintenance Checks: Automatic checks to determine when tire rotation or battery recharge is due.
9. Extensibility: Potentials for future expansion, such as adding reporting and analytics features.
Expected Outcomes:
The expected outcomes of this project are as follows:
1. Successful Vehicle Maintenance: The code enables effective management of vehicle maintenance tasks, ensuring that vehicles are well-maintained and in optimal condition.
2. Streamlined Maintenance Process: The code provides a user-friendly interface that simplifies the maintenance process, making it easier for users to perform tasks such as tire rotation, oil changes, and battery recharging.
3. Accurate Vehicle Information: The code allows for the storage and management of vehicle details such as make, model, year, and odometer reading, ensuring accurate and up-to-date information.
4. Timely Maintenance Reminders: The code incorporates automatic checks to determine when maintenance tasks are due, providing timely reminders for activities like tire rotation or battery recharge.
5. Efficient Resource Allocation: The code utilizes dynamic memory allocation, optimizing memory usage and ensuring efficient resource allocation for the vehicle inventory.
6. Enhanced Data Integrity: The code includes data validation mechanisms to prevent data corruption and maintain data integrity throughout the maintenance system.
7. Support for Different Fuel Types: The code accommodates various fuel types such as Gas, Diesel, and Electric vehicles, enabling comprehensive support for different vehicle types.
8. Potential for Expansion: The code's modular structure and extensibility allow for potential future expansion, such as adding additional features like reporting, analytics, or integration with other systems.
Please find the code below:
/*
* p301.cpp
*
* Created on: 30 Jun 2023
* Author: chinmaykadam
*/
#include <iostream>
#include <string>
#include <vector>
//Defining enum type class for different fuel based cars
enum class fuel_type
{
GAS,
DIESEL,
ELECTRIC
};
//Defining a base class 'Vehicle'
class Vehicle
{
//Declaring its 4 protected data members
protected:
std::string make;
std::string model;
int year;
int odometer;
public:
//Using constructor to assign values to protected data members
Vehicle (const std::string & make, const std::string & model, int year,int odometer):make (make), model (model), year (year),odometer (odometer)
{}
//Defining function getMake() of return type string which returns the value of 'make'
std::string getMake () const
{
return make;
}
//Defining function getModel() of return type string which returns the value of 'model'
std::string getModel () const
{
return model;
}
//Defining function getYear() of return type integer which returns the value of 'year'
int getYear () const
{
return year;
}
//Defining function getOdometer() of return type integer which returns the value of 'odometer'
int getOdometer () const
{
return odometer;
}
// Setter method for odometer
void setOdometer (int newOdometer)
{
odometer = newOdometer;
}
// Pure virtual function to get the fuel type of the vehicle
virtual fuel_type getfuel_type () const = 0;
// Pure virtual function to perform maintenance on the vehicle
virtual void performMaintenance() = 0;
// Virtual function to rotate tires
virtual void rotateTires ()
{
std::cout << "Rotating tires for " << make << " " << model << std::endl;
}
// Virtual function to perform oil change
virtual void performOilChange ()
{
std::cout << "Performing oil change for " << make << " " << model << std::
endl;
}
// Virtual function to recharge the vehicle
virtual void recharge ()
{
std::cout << "Recharging " << make << " " << model << std::endl;
}
// Virtual function to check if tire rotation is needed
virtual void checkTireRotation ()
{
int tireCycle = 50000; // Tire rotation cycle in kilometers
int DistanceCovered = getOdometer() % tireCycle;
if (DistanceCovered == 0)
{
std::cout << "Time to rotate tires for " << make << " " << model << std::
endl;
}
else
{
std::
cout << "Tires are good to go for " << make << " " << model << std::
endl;
int BalanceDistance = tireCycle - DistanceCovered;
std::
cout << "Remaining distance for tire rotation: " << BalanceDistance <<
" km" << std::endl;
}
}
// Virtual function to check if battery recharge is needed
virtual void checkPower ()
{
int rechargeDistance = 400; // Recharge distance in kilometers
int DistanceCovered = getOdometer() % rechargeDistance;
if (DistanceCovered == 0)
{
std::
cout << "Time to recharge the battery for " << make << " " << model
<< std::endl;
}
else
{
std::
cout << "Your vehicle is good to go for " << make << " " << model <<
std::endl;
int BalanceDistance = rechargeDistance - DistanceCovered;
std::
cout << "Remaining distance for recharge: " << BalanceDistance << " km" <<
std::endl;
}
}
// Virtual destructor for the base class
virtual ~ Vehicle ()
{
}
};
class GasCar:public Vehicle
{
public:
// Constructor for GasCar class
GasCar (const std::string & make, const std::string & model, int year, int odometer):Vehicle (make, model, year, odometer)
{}
// Return the fuel type of GasCar
fuel_type getfuel_type () const override
{
return fuel_type::GAS;
}
// Perform maintenance on GasCar
void performMaintenance () override
{
std::cout << "Performing maintenance on GasCar" << std::endl;
}
// Rotate tires for GasCar
void rotateTires () override
{
std::cout << "Rotating tires for GasCar" << std::endl;
}
// Perform oil change for GasCar
void performOilChange () override
{
std::cout << "Performing oil change for GasCar" << std::endl;
}
};
class DieselCar:public Vehicle
{
public:
// Constructor for DieselCar class
DieselCar (const std::string & make, const std::string & model, int year,
int odometer):Vehicle (make, model, year, odometer)
{
}
// Return the fuel type of DieselCar
fuel_type getfuel_type () const override
{
return fuel_type::DIESEL;
}
// Perform maintenance on DieselCar
void performMaintenance () override
{
std::cout << "Performing maintenance on DieselCar" << std::endl;
}
// Rotate tires for DieselCar
void rotateTires () override
{
std::cout << "Rotating tires for DieselCar" << std::endl;
}
// Perform oil change for DieselCar
void performOilChange () override
{
std::cout << "Performing oil change for DieselCar" << std::endl;
}
};
class ElectricCar:public Vehicle
{
public:
// Constructor for ElectricCar class
ElectricCar (const std::string & make, const std::string & model, int year, int odometer):Vehicle (make, model, year, odometer)
{}
// Return the fuel type of ElectricCar
fuel_type getfuel_type () const override
{
return fuel_type::ELECTRIC;
}
// Perform maintenance on ElectricCar
void performMaintenance () override
{
std::cout << "Performing maintenance on ElectricCar" << std::endl;
}
// Recharge ElectricCar
void recharge () override
{
std::cout << "Recharging ElectricCar" << std::endl;
}
};
class MaintenanceLibrary
{
private:
std::vector < Vehicle * >inventory;
public:
// Destructor for MaintenanceLibrary class
~MaintenanceLibrary ()
{
// Delete each vehicle in the inventory
for (Vehicle * vehicle:inventory)
{
delete vehicle;
}
}
// Add a vehicle to the inventory
void addVehicle (Vehicle * vehicle)
{
inventory.push_back (vehicle);
}
// List all vehicles in the inventory
void listVehicles () const
{
int index = 0;
// Iterate over each vehicle in the inventory
for (const Vehicle * vehicle:inventory)
{
std::cout << "Index: " << index << std::endl;
std::cout << "Make: " << vehicle->getMake () << std::endl;
std::cout << "Model: " << vehicle->getModel () << std::endl;
std::cout << "Year: " << vehicle->getYear () << std::endl;
std::cout << "Odometer: " << vehicle->getOdometer () << std::endl;
std::cout << "Fuel Type: ";
// Print the fuel type based on the enum value
switch (vehicle->getfuel_type ())
{
case static_cast<int>(fuel_type::GAS):
std::cout << "Gas" << std::endl;
break;
case static_cast<int>(fuel_type::DIESEL):
std::cout << "Diesel" << std::endl;
break;
case static_cast<int>(fuel_type::ELECTRIC):
std::cout << "Electric" << std::endl;
break;
}
std::cout << std::endl;
++index;
}
}
// Update the odometer reading of a vehicle in the inventory
void updateVehicle(int index, int newOdometer)
{
if (index >= 0 && static_cast < size_t > (index) < inventory.size ())
{
Vehicle *vehicle = inventory[index];
vehicle->performMaintenance ();
vehicle->setOdometer (newOdometer);
}
}
// Remove a vehicle from the inventory
void removeVehicle (int index)
{
if (index >= 0 && static_cast < std::size_t > (index) < inventory.size ())
{
delete inventory[index];
inventory.erase (inventory.begin () + index);
}
}
// Rotate tires for a vehicle in the inventory
void rotateTires (int index)
{
if (index >= 0 && static_cast < std::size_t > (index) < inventory.size ())
{
Vehicle *vehicle = inventory[index];
vehicle->rotateTires ();
}
}
// Perform an oil change for a vehicle in the inventory
void performOilChange (int index)
{
if (index >= 0 && static_cast < std::size_t > (index) < inventory.size ())
{
Vehicle *vehicle = inventory[index];
vehicle->performOilChange ();
}
}
// Recharge an electric vehicle in the inventory
void recharge (int index)
{
if (index >= 0 && static_cast < std::size_t > (index) < inventory.size ())
{
Vehicle *vehicle = inventory[index];
vehicle->recharge ();
}
}
// Check if tire rotation is due for a vehicle in the inventory
void checkTireRotation(int index)
{
if (index >= 0 && static_cast < std::size_t > (index) < inventory.size ())
{
Vehicle *vehicle = inventory[index];
int currentOdometer = vehicle->getOdometer ();
int tireCycle = 50000;
int BalanceDistance = currentOdometer % tireCycle;
if (currentOdometer >= tireCycle && BalanceDistance >= 0)
{
std::cout << "Tire rotation is due for " << vehicle->
getMake () << " " << vehicle->getModel () << std::endl;
std::
cout << "Current odometer reading: " << currentOdometer << " km" <<
std::endl;
std::cout << "Tire rotation cycle: " << tireCycle << " km" << std::
endl;
std::cout << "Remaining distance for tire rotation: " << tireCycle -
BalanceDistance << " km" << std::endl;
}
else
{
std::cout << "Tires are good to go for " << vehicle->
getMake () << " " << vehicle->getModel () << std::endl;
std::
cout << "Current odometer reading: " << currentOdometer << " km" <<
std::endl;
std::cout << "Tire rotation cycle: " << tireCycle << " km" << std::
endl;
std::cout << "Remaining distance for tire rotation: " << tireCycle -
BalanceDistance << " km" << std::endl;
}
}
}
// Check if battery recharge is due for an electric vehicle in the inventory
void checkBatteryPower(int index)
{
if (index >= 0 && static_cast < std::size_t > (index) < inventory.size ())
{
Vehicle *vehicle = inventory[index];
int currentOdometer = vehicle->getOdometer ();
int powerCycle = 400;
int BalanceDistance = currentOdometer % powerCycle;
if (currentOdometer >= powerCycle && BalanceDistance == 0)
{
std::cout << "Battery recharge is due for " << vehicle->
getMake () << " " << vehicle->getModel () << std::endl;
std::
cout << "Current odometer reading: " << currentOdometer << " km" <<
std::endl;
std::
cout << "Battery recharge cycle: " << powerCycle << " km" << std::
endl;
}
else
{
std::cout << "Battery is good to go for " << vehicle->
getMake () << " " << vehicle->getModel () << std::endl;
std::
cout << "Current odometer reading: " << currentOdometer << " km" <<
std::endl;
std::
cout << "Battery recharge cycle: " << powerCycle << " km" << std::
endl;
std::
cout << "Remaining distance for battery recharge: " << powerCycle -
BalanceDistance << " km" << std::endl;
}
}
}
};
int main ()
{
MaintenanceLibrary library;
while (true)
{
// Display menu options
std::cout << "Menu:" << std::endl;
std::cout << "0. Quit." << std::endl;
std::cout << "1. List inventory." << std::endl;
std::cout << "2. Add vehicle." << std::endl;
std::cout << "3. Update vehicle." << std::endl;
std::cout << "4. Remove vehicle." << std::endl;
std::cout << "5. Check tire rotation." << std::endl;
std::cout << "6. Check battery power." << std::endl;
std::cout << "7. Rotate tires." << std::endl;
std::cout << "8. Perform oil change." << std::endl;
std::cout << "9. Recharge vehicle." << std::endl;
std::cout << "Select an option: ";
int option;
std::cin >> option;
if (option == 0)
{
break; // Exit the loop and quit the program
}
else if (option == 1)
{
library.listVehicles (); // List the vehicles in the inventory
} else if (option == 2)
{
// Add a new vehicle to the inventory
std::cout << "Enter vehicle make: ";
std::string make;
std::cin >> make;
std::cout << "Enter vehicle model: ";
std::string model;
std::cin >> model;
std::cout << "Enter vehicle year: ";
int year;
std::cin >> year;
std::cout << "Enter vehicle odometer reading: ";
int odometer;
std::cin >> odometer;
std::cout << "Select fuel type (0: Gas, 1: Diesel, 2: Electric): ";
int fuel_typeOption;
std::cin >> fuel_typeOption;
fuel_type fuel_type;
if (fuel_typeOption == 0)
{
fuel_type = fuel_type::GAS;
}
else if (fuel_typeOption == 1)
{
fuel_type = fuel_type::DIESEL;
}
else if (fuel_typeOption == 2)
{
fuel_type = fuel_type::ELECTRIC;
}
else
{
std::cout << "Invalid fuel type. Vehicle not added." << std::
endl;
continue; // Skip to the next iteration of the loop
}
// Create a new vehicle based on the selected fuel type
Vehicle* vehicle = nullptr;
switch (fuel_type)
{
case static_cast<int>(fuel_type::GAS):
vehicle = new GasCar (make, model, year, odometer);
break;
case static_cast<int>(fuel_type::DIESEL):
vehicle = new DieselCar (make, model, year, odometer);
break;
case static_cast<int>(fuel_type::ELECTRIC):
vehicle = new ElectricCar (make, model, year, odometer);
break;
}
library.addVehicle (vehicle); // Add the vehicle to the inventory
std::cout << "Vehicle added successfully." << std::endl;
}
else
if (option == 3)
{
// Update the odometer reading of a vehicle in the inventory
std::cout << "Enter index of the vehicle to update: ";
int index;
std::cin >> index;
std::cout << "Enter new odometer reading: ";
int newOdometer;
std::cin >> newOdometer;
library.updateVehicle (index, newOdometer);
}
else if (option == 4)
{
// Remove a vehicle from the inventory
std::cout << "Enter index of the vehicle to remove: ";
int index;
std::cin >> index;
library.removeVehicle (index);
}
else if (option == 5)
{
// Check tire rotation for a vehicle in the inventory
std::cout << "Enter index of the vehicle to check tire rotation: ";
int index;
std::cin >> index;
library.checkTireRotation (index);
}
else if (option == 6)
{
// Check battery power for an electric vehicle in the inventory
std::cout << "Enter index of the vehicle to check battery power: ";
int index;
std::cin >> index;
library.checkBatteryPower (index);
}
else if (option == 7)
{
// Rotate tires for a vehicle in the inventory
std::cout << "Enter index of the vehicle to rotate tires: ";
int index;
std::cin >> index;
library.rotateTires (index);
}
else if (option == 8)
{
// Perform an oil change for a vehicle in the inventory
std::cout << "Enter index of the vehicle to perform oil change: ";
int index;
std::cin >> index;
library.performOilChange (index);
}
else if (option == 9)
{
// Recharge the battery of an electric vehicle in the inventory
std::cout << "Enter index of the vehicle to recharge: ";
int index;
std::cin >> index;
library.recharge (index);
}
else
{
std::cout << "Invalid option. Try again." << std::endl;
}
std::cout << std::endl;
}
return 0;
}
All the necessary screenshots are attached herewith