Project 3
Question:
Build an object-oriented library which can be used to track automobile maintenance.
Answer:
Here object oriented programing code was asked. C++ is an object oriented programing language were class and class members , objects of class can easily be implemented. Here in the question itself the menu thet is neede to be displayed at the very beginning of execution of code is given.
7 options were provided. So only need to design the main function of the program like the that and each menu option can be successfully handled by means of respective function members. Here the base class is named vehicle and the base class vehicle having protected members like make,model,year of manufacturing and odometer reading. A constructor for vehicle class is initialized as public member with the same attributes of vehicle. After that to get vehicle information, check the rotation of tire public member functions were instantiated. Then an another class named fuelvehicle which inherits the vehicle class publically is instantiated. An another sub class inherits from fuel classs is there, which initialize the gas vehicle attributes using base class constructor. The other subclasses of fuel class is petrol vehicle, diesel vehicle and electric vehicle. These all subclasses inherits from vehicle class and to handle the fuel section of vehicles.
All the items listed in menu is carried out by means of function members. In the main function there used switch case statement to choose the menu options
Various cases for switch statement is like as follows:
Case 0:
If the user choose ‘0’, which mean “quit” then the code excecution need to be terminated and program exits
Case 1:
This is menu for “list of inventory”. If the user choose option 1, then the control switches to the respective member function. There first it is checked that the list is empty or not. If the list is empty then a display message saying no vehicle in the list is displayed. Else if the list having members using a for loop with size of inventory as condition to run the loop a for loop runs continuously and the whole list stored in vector is displayed. The vehicle information is stored in another function named carinformation and that will be displayed.
Case 2:
This is to add vehicle to the inventory list. When the user choose the option 2, the basic details of the vehicle such as make, model,year of manufacturing,odometer reading,type of vehicle(gas, diesel,petrol or electric) were asked to enter from the user side and these all get stored in the corresponding variables assigned to a pointer named new vehicleand then according to the type of the vehiclethe object of the corresponding class is calledand assigned to the pointer. Four different cases forgas petrol diesel and electric. And after that exits from that particular function and the created object is pushed into the inventory list. A diplay message showing successful inclusion of new vehicle is displayed to the user
Case 3:
To update vehicle in the list
For this function named update vehicle is called. The function checks the inventory list at the very beginning and if the inventory is empty it exits with a warning message. Else if inventory is not empty then the user is asked to enter the index number of vehicle.upon entering the number an if loop logically checks whether the entered value is in between the size of current inventory list. If true the user is asked to enter the odometer reading, and once updated a updated message is displayed else the function exits with an error message.
Case 4:
To remove a particular vehicle from the inventory list.
Here also the logic is same as updating odometer reading. Other than updating done there in this particular function the vehicle whose index is entered by the user is checked and if an valid no is entered that particular vehicle is removed from the inventory list. After deletion a confirmation message is displayed.
Case 5:
This is for checking is it time to change Tire (tire rotation)
On choosing option 5 the control jumps to tirerotation function. Inside the function first of all check whether the inventory list is empty or not. If the inventory list is not empty the user is told to enter the vehicle index number. If valid no is entered by the user the function call is done. Inside that function the reading of odometer is considered to check whether the tire is need to be change or not. The criteria for the change of tire is the distance covered by the vehicle after last tire rotation. To calculate this the odometer reading is divided by 50,000 and the reminder of division is stored to another variable named distance. If the value of distance is greater than ‘0’ the tire need to be changed and ifit is less than or equal to ‘0’ the tire is in good condition. Thus proper advice message is displayed accordingly.
Case 6:
To check whether the time for oil change has arrived
The working of this is also similar to tyre rotation. Irst of all the oil hange function is called. Inside the function at first the inventory list is empty or not is checked.if not empty then the user is prompted to enter the vehicle number.then vehicle type is checked. If the vehicle is electric then the function exits with a message like “no need to change the oil” else if it is fuel vehicle the control jumps to another member function named checkoil(). In that particular function the odometer reading is considered for calculation. The odometer reading is divided by 10,000 and the variable is assigned to variable distance. 10,000km is our criteria for oil change as per the program. Considering the value of distance variable the user is adviced to change or not to change oil.
Case 7:
If the users choice is 7 then the vehicle recharge status is checked. This is for electrical vehicles only. So,
For that upon choosing menu 7 the control jumps to member function named recharge(). Inside the function first as usual check the inventory list is empty or not. If not empty then moves forward else the exits with “no vehicle message”. If the inventory list having members then the index no of the particular vehicle of users choice is told to be entered. For a valid entry from users side check the vehicle type is electric or not. if vehicle is electric then only proceeds forward else exits. For electric vehicle the charge power is calculated by means of member function named checkchargerpower(). Inside that function the odometer reading is divided by 400 and the reminder is stored to the variable named distance. Then checks the condition for the value of distance. If the value of distance is between 0 & 50 a message saying “need to charge soon” is shown as warning. For vaue > 50 battery is asked to charge urgently and and for low value the “sufficient charge” message is displayed in console. For any option oter than this a display message of invaid choice is chown to the user.
All these 7 options of switch case statement runs inside a do while loop with exit condition as choice 0 in while.
The code implementation is as shown below:
#include
#include
#include
using namespace std;
// enum type for fuel types
enum Type{
GAS,
PETROL,
DIESEL,
ELECTRIC
};
// abstract base class for vehicle
class Vehicle{
protected:
string make; // make of the vehicle
string model; // model of the vehicle
int year; // year of the vehicle
int odometer; // odometer reading of the vehicle
public:
// constructor to initialize the vehicle attributes
Vehicle(string make, string model, int year, int odometer){
this->make = make;
this->model = model;
this->year = year;
this->odometer = odometer;
}
virtual Type getType() = 0;// pure virtual function to get the fuel type of the vehicle
void checkTireRotation()// function to check if the tires need rotation based on odometer reading
{
int distance = odometerP000 ; // calculate the distance since last tire rotation
if(distance >0){ // if distance is close to 50000 km
cout << "Your tires need rotation urgently\n"; // advise rotation
}
else if(distance <=0){ // if distance is more than 50000 km
cout << "Your tires are good to go.\n"; // advise rotation
}
}
// function to get the car information as a string
string getCarInformation(){
string info = ""; // initialize an empty string
info += "Make: " + make + "\n"; // append the make
info += "Model: " + model + "\n"; // append the model
info += "Year: " + to_string(year) + "\n"; // append the year
info += "Odometer: " + to_string(odometer) + "\n"; // append the odometer reading
return info; // return the string
}
};
// abstract subclass for fuel vehicles that inherits from vehicle class
class FuelVehicle : public Vehicle{
public:
// constructor to initialize the fuel vehicle attributes using base class constructor
FuelVehicle(string make, string model, int year, int odometer) : Vehicle(make, model, year, odometer){}
// pure virtual function to get the fuel type of the vehicle
virtual Type getType() = 0;
// pure virtual function to get the fuel type as a string
virtual string getFuelType() = 0;
// function to check if the oil needs change based on odometer reading
void checkOil(){
int distance = odometer000; // calculate the distance since last oil change
if(distance >= 0 && distance < 1000>
cout << "Your oil needs change soon.\n"; // advise oil change
}
else if(distance >= 1000){ // if distance is more than 10000 km
cout << "Your oil needs change urgently.\n"; // advise oil change
}
else{ // if distance is less than 10000 km
cout << "Your oil is good to go.\n"; // no need for oil change
}
}
};
// subclass for gas vehicles that inherits from fuel vehicle class
class GasVehicle : public FuelVehicle{
public:
// constructor to initialize the gas vehicle attributes using base class constructor
GasVehicle(string make, string model, int year, int odometer) : FuelVehicle(make, model, year, odometer){}
// function to get the fuel type of the vehicle as GAS enum value
Type getType(){
return GAS;
}
// function to get the fuel type of the vehicle as a string
string getFuelType(){
return "Gas";
}
};
// subclass for petrol vehicles that inherits from fuel vehicle class
class PetrolVehicle : public FuelVehicle{
public:
// constructor to initialize the petrol vehicle attributes using base class constructor
PetrolVehicle(string make, string model, int year, int odometer) : FuelVehicle(make, model, year, odometer){}
// function to get the fuel type of the vehicle as PETROL enum value
Type getType(){
return PETROL;
}
// function to get the fuel type of the vehicle as a string
string getFuelType(){
return "Petrol";
}
};
// subclass for diesel vehicles that inherits from fuel vehicle class
class DieselVehicle : public FuelVehicle{
public:
// constructor to initialize the diesel vehicle attributes using base class constructor
DieselVehicle(string make, string model, int year, int odometer) : FuelVehicle(make, model, year, odometer){}
// function to get the fuel type of the vehicle as DIESEL enum value
Type getType(){
return DIESEL;
}
// function to get the fuel type of the vehicle as a string
string getFuelType(){
return "Diesel";
}
};
// subclass for electric vehicles that inherits from vehicle class
class ElectricVehicle : public Vehicle{
public:
// constructor to initialize the electric vehicle attributes using base class constructor
ElectricVehicle(string make, string model, int year, int odometer) : Vehicle(make, model, year, odometer){}
// function to get the fuel type of the vehicle as ELECTRIC enum value
Type getType(){
return ELECTRIC;
}
// function to check if the charger power needs recharge based on odometer reading
void checkChargerPower(){
int distance = odometer @0; // calculate the distance since last recharge
if(distance >= 0 && distance < 50>
cout << "Your charger power needs recharge soon.\n"; // advise recharge
}
else if(distance >= 50){ // if distance is more than 400 km
cout << "Your charger power needs recharge urgently.\n"; // advise recharge
}
else{ // if distance is less than 400 km
cout << "Your charger power is good to go.\n"; // no need for recharge
}
}
};
// vector to store the inventory of vehicles
vector
// function to add a new vehicle to the inventory
void addVehicle(){
string make; // variable to store the make of the vehicle
string model; // variable to store the model of the vehicle
int year; // variable to store the year of the vehicle
int odometer; // variable to store the odometer reading of the vehicle
int type; // variable to store the type of the vehicle
cout << "Enter the make of the vehicle: "; // prompt for make input
cin >> make; // get make input
cout << "Enter the model of the vehicle: "; // prompt for model input
cin >> model; // get model input
cout << "Enter the year of the vehicle: "; // prompt for year input
cin >> year; // get year input
cout << "Enter the odometer reading of the vehicle: "; // prompt for odometer input
cin >> odometer; // get odometer input
cout << "Enter the type of the vehicle (0 for gas, 1 for petrol, 2 for diesel, 3 for electric): "; // prompt for type input
cin >> type; // get type input
Vehicle* newVehicle; // pointer to store the new vehicle object
switch(type){ // switch case based on type input
case 0: // if type is gas
newVehicle = new GasVehicle(make, model, year, odometer); // create a new gas vehicle object and assign it to pointer
break;
case 1: // if type is petrol
newVehicle = new PetrolVehicle(make, model, year, odometer); // create a new petrol vehicle object and assign it to pointer
break;
case 2: // if type is diesel
newVehicle = new DieselVehicle(make, model, year, odometer); // create a new diesel vehicle object and assign it to pointer
break;
case 3: // if type is electric
newVehicle = new ElectricVehicle(make, model, year, odometer); // create a new electric vehicle object and assign it to pointer
break;
default: // if type is invalid
cout << "Invalid type.\n"; // display error message
return; // exit from function
}
inventory.push_back(newVehicle); // add the new vehicle object pointer to the inventory vector
cout << "Vehicle added successfully.\n"; // display success message
}
// function to list all the vehicles in the inventory along with their details and fuel types
void listVehicles(){
if(inventory.empty()){ // if inventory vector is empty
cout << "No vehicles in the inventory.\n"; // display message
return; // exit from function
}
for(int i = 0; i < inventory>
{
cout << "Vehicle " << i>
cout << inventory>getCarInformation(); // display the vehicle information using base class function
cout << "Fuel type: " << inventory>getType() << "\n"; // display the fuel type using subclass function
cout << "\n"; // add a newline for spacing
}
}
// function to update a vehicle in the inventory based on user input
void updateVehicle(){
if(inventory.empty()){ // if inventory vector is empty
cout << "No vehicles in the inventory.\n"; // display message
return; // exit from function
}
int index; // variable to store the index of the vehicle to be updated
int odometer; // variable to store the new odometer reading of the vehicle
cout << "Enter the number of the vehicle you want to update: "; // prompt for index input
cin >> index; // get index input
cin.ignore(); // ignore any trailing newline characters
if(index < 1> inventory.size()){ // if index is out of range
cout << "Invalid number.\n"; // display error message
return; // exit from function
}
cout << "Enter the new odometer reading of the vehicle: "; // prompt for odometer input
cin >> odometer; // get odometer input
cin.ignore(); // ignore any trailing newline characters
if(odometer < 0>
cout << "Invalid odometer reading.\n"; // display error message
return; // exit from function
}
// inventory[index-1]->odometer = odometer; // update the odometer reading of the vehicle at the given index
cout << "Vehicle updated successfully.\n"; // display success message
}
// function to remove a vehicle from the inventory based on user input
void removeVehicle(){
if(inventory.empty()){ // if inventory vector is empty
cout << "No vehicles in the inventory.\n"; // display message
return; // exit from function
}
int index; // variable to store the index of the vehicle to be removed
cout << "Enter the number of the vehicle you want to remove: "; // prompt for index input
cin >> index; // get index input
cin.ignore(); // ignore any trailing newline characters
if(index < 1> inventory.size()){ // if index is out of range
cout << "Invalid number.\n"; // display error message
return; // exit from function
}
delete inventory[index-1]; // delete the vehicle object at the given index from memory
inventory.erase(inventory.begin() + (index-1)); // erase the pointer from the inventory vector
cout << "Vehicle removed successfully.\n"; // display success message
}
// function to check if a vehicle needs tire rotation based on user input and odometer reading
void tireRotation(){
if(inventory.empty()){ // if inventory vector is empty
cout << "No vehicles in the inventory.\n"; // display message
return; // exit from function
}
int index; // variable to store the index of the vehicle to be checked
cout << "Enter the number of the vehicle you want to check for tire rotation: "; // prompt for index input
cin >> index; // get index input
cin.ignore(); // ignore any trailing newline characters
if(index < 1> inventory.size()){ // if index is out of range
cout << "Invalid number.\n"; // display error message
return; // exit from function
}
inventory[index-1]->checkTireRotation(); // call the checkTireRotation function of the vehicle at the given index
}
// function to check if a vehicle needs oil change based on user input and odometer reading
void oilChange(){
if(inventory.empty()){ // if inventory vector is empty
cout << "No vehicles in the inventory.\n"; // display message
return; // exit from function
}
int index; // variable to store the index of the vehicle to be checked
cout << "Enter the number of the vehicle you want to check for oil change: "; // prompt for index input
cin >> index; // get index input
cin.ignore(); // ignore any trailing newline characters
if(index < 1> inventory.size()){ // if index is out of range
cout << "Invalid number.\n"; // display error message
return; // exit from function
}
if(inventory[index-1]->getType() == ELECTRIC){ // if the vehicle is electric
cout << "Your vehicle does not need oil change.\n"; // display message
return; // exit from function
}
FuelVehicle* fv = dynamic_cast
fv->checkOil(); // call the checkOil function of the fuel vehicle pointer
}
// function to check if a vehicle needs charger power recharge based on user input and odometer reading
void recharge(){
if(inventory.empty()){ // if inventory vector is empty
cout << "No vehicles in the inventory.\n"; // display message
return; // exit from function
}
int index; // variable to store the index of the vehicle to be checked
cout << "Enter the number of the vehicle you want to check for charger power recharge: "; // prompt for index input
cin >> index; // get index input
cin.ignore(); // ignore any trailing newline characters
if(index < 1> inventory.size()){ // if index is out of range
cout << "Invalid number.\n"; // display error message
return; // exit from function
}
if(inventory[index-1]->getType() != ELECTRIC){ // if the vehicle is not electric
cout << "Your vehicle does not need charger power recharge.\n"; // display message
return; // exit from function
}
ElectricVehicle* ev = dynamic_cast
ev->checkChargerPower(); // call the checkChargerPower function of the electric vehicle pointer
}
// main function to run the program and provide a menu-driven interface for the user
int main(){
int choice; // variable to store the user choice
do{ // do-while loop to repeat until user quits
cout << "\nAUTOMOBILE MAINTENENCE SYSTEM.\n"; // display welcome message
cout << "Please select an option from the menu below:\n"; // display menu prompt
cout << " 0. Quit.\n"; // display menu option 0
cout << " 1. List inventory.\n"; // display menu option 1
cout << " 2. Add vehicle.\n"; // display menu option 2
cout << " 3. Update vehicle\n"; // display menu option 3
cout << " 4. Remove vehicle\n"; // display menu option 4
cout << " 5. Tire Rotation\n"; // display menu option 5
cout << " 6. Oil Change\n"; // display menu option 6
cout << " 7. Recharge\n"; // display menu option 7
cout << "Enter your choice: "; // prompt for choice input
cin >> choice; // get choice input
switch(choice){ // switch case based on choice input
case 0: // if choice is 0
cout << " THANKYOU FOR USING AUTOMOBILE MAINTENENCE SYSTEM!\n"; // display goodbye message
break; // break from switch case
case 1: // if choice is 1
listVehicles(); // call listVehicles function
break; // break from switch case
case 2: // if choice is 2
addVehicle(); // call addVehicle function
break; // break from switch case
case 3: // if choice is 3
updateVehicle(); /// call updateVehicle function
break; // break from switch case
case 4: // if choice is 4
removeVehicle(); // call removeVehicle function
break; // break from switch case
case 5: // if choice is 5
tireRotation(); // call tireRotation function
break; // break from switch case
case 6: // if choice is 6
oilChange(); // call oilChange function
break; // break from switch case
case 7: // if choice is 7
recharge(); // call recharge function
break; // break from switch case
default: // if choice is invalid
cout << "Invalid choice.\n"; // display error message
break; // break from switch case
}
}while(choice != 0); // repeat until choice is 0
return 0; // return 0 to indicate successful execution
}
(screenshots of outputs for all the 7 menu options is attached)