Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
build a object oriented library (C++ code) 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.

What I have tried:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

enum class FuelType {
PETROL,
DIESEL,
GAS,
ELECTRIC
};

class Vehicle {
protected:
string make_;
string model_;
int year_;
int odometer_;

public:
Vehicle(string make, string model, int year, int odometer)
: make_(make), model_(model), year_(year), odometer_(odometer) {}

string getMake() const { return make_; }
void setMake(string make) { make_ = make; }

string getModel() const { return model_; }
void setModel(string model) { model_ = model; }

int getYear() const { return year_; }
void setYear(int year) { year_ = year; }

int getOdometer() const { return odometer_; }
void setOdometer(int odometer) { odometer_ = odometer; }

virtual FuelType getFuelType() = 0;
};
//abstract class - containing one pure virtual method
class FuelVehicle : public Vehicle
{
public: //access specifier
int quarts; //data member
void checkOil() //member function
{
cout<<endl<<" enter="" oil="" level="" (in="" quarts)="" :="" ";
="" cin="">>quarts;
if(quarts <= 1)
{
cout<<endl<<" engine="" oil="" level="" :="" low";
="" }
="" else
="" cout<<endl<<"="" is="" fine";
="" friend="" void="" oilchange(fuelvehicle="" &oil);="" function="" declaration
};

class="" gascar="" public="" vehicle="" {
public:
="" gascar(string="" make,="" string="" model,="" int="" year,="" odometer)
="" vehicle(make,="" odometer)="" {}

="" fueltype="" getfueltype()="" override="" {="" return="" fueltype::gas;="" }
};

class="" dieselcar="" dieselcar(string="" fueltype::diesel;="" electriccar="" electriccar(string="" fueltype::electric;="" }

="" checktirerotation()="" {
="" if="" (odometer_="" %="" 50000="=" 0)="" cout="" <<="" "it's="" time="" for="" a="" tire="" rotation."="" endl;
="" }="" else=""> 0) {
cout << "You should rotate your tires soon." << endl;
}
}

void checkPower() {
if (odometer_ % 400 == 0) {
cout << "Your car needs to be recharged." << endl;
} else {
cout << "Your car is good to go." << 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() {
MaintenanceLibrary library;
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;
library.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;
library.updateVehicle(index, make, model, year, odometer);
break;
}
case 4:
library.removeVehicle();
break;
case 5:
library.tireRotation();
break;
case 6:
library.oilChange();
break;
case 7:
library.recharge();
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (userInput != 0);

return 0;
}
Posted
Updated 10-Mar-23 19:29pm
Comments
Sanket Awasthy 10-Mar-23 23:07pm    
#include <iostream>
#include <string>
#include <vector>

using namespace std;

enum class FuelType {
PETROL,
DIESEL,
GAS,
ELECTRIC
};

class Vehicle {
protected:
string make_;
string model_;
int year_;
int odometer_;

public:
Vehicle(string make, string model, int year, int odometer)
: make_(make), model_(model), year_(year), odometer_(odometer) {}

string getMake() const { return make_; }
void setMake(string make) { make_ = make; }

string getModel() const { return model_; }
void setModel(string model) { model_ = model; }

int getYear() const { return year_; }
void setYear(int year) { year_ = year; }

int getOdometer() const { return odometer_; }
void setOdometer(int odometer) { odometer_ = odometer; }

virtual FuelType getFuelType() = 0;
};
//abstract class - containing one pure virtual method
class FuelVehicle : public Vehicle
{
public: //access specifier
int quarts; //data member
void checkOil() //member function
{
cout<<endl<<" enter="" oil="" level="" (in="" quarts)="" :="" ";
="" cin="">>quarts;
if(quarts <= 1)
{
cout<<endl<<" engine="" oil="" level="" :="" low";
="" }
="" else
="" cout<<endl<<"="" is="" fine";
="" friend="" void="" oilchange(fuelvehicle="" &oil);="" function="" declaration
};

class="" gascar="" public="" vehicle="" {
public:
="" gascar(string="" make,="" string="" model,="" int="" year,="" odometer)
="" vehicle(make,="" odometer)="" {}

="" fueltype="" getfueltype()="" override="" {="" return="" fueltype::gas;="" }
};

class="" dieselcar="" dieselcar(string="" fueltype::diesel;="" electriccar="" electriccar(string="" fueltype::electric;="" }

="" checktirerotation()="" {
="" if="" (odometer_="" %="" 50000="=" 0)="" cout="" <<="" "it's="" time="" for="" a="" tire="" rotation."="" endl;
="" }="" else=""> 0) {
cout << "You should rotate your tires soon." << endl;
}
}

void checkPower() {
if (odometer_ % 400 == 0) {
cout << "Your car needs to be recharged." << endl;
} else {
cout << "Your car is good to go." << 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() {
Sanket Awasthy 10-Mar-23 23:08pm    
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() {
MaintenanceLibrary library;
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;
library.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: ";
Sanket Awasthy 10-Mar-23 23:09pm    
cin >> year;
cout << "Enter the odometer reading of the vehicle: ";
cin >> odometer;
library.updateVehicle(index, make, model, year, odometer);
break;
}
case 4:
library.removeVehicle();
break;
case 5:
library.tireRotation();
break;
case 6:
library.oilChange();
break;
case 7:
library.recharge();
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (userInput != 0);

return 0;
}
Sanket Awasthy 10-Mar-23 23:10pm    
there are many errors in this code please resolve these errors i was not able to fix these errors by myself
FreedMalloc 10-Mar-23 23:56pm    
First off, all you've done is dumped a bunch of code pasted as text instead of using the code block tool so the formatting/indenting is very hard to follow.
Secondly, you don't show any errors. I'm supposed to take your poorly formatted code and try to compile it myself? I don't think you'll get much help with that.

I would suggest you compile the code. Then take each error one at a time and try to figure it out. The compiler should tell what line and column it doesn't like. I'll bet you can clear quite a few on your own. Then if there are still some you don't understand use the green Improve Question link to show 2 or 3 of those errors along with the relevant code. I think people here will be much more willing to help if you do that.

1 solution

We have no idea what errors you are seeing - that code looks corrupted so it probably doesn't match what you are trying to execute, and we can't tell if the original even compiled or not.
If it didn't compile, those are called syntax errors, and what you see may differ from what we see as we don't know what compiler and options you are using.
If it did compile, but crashed those are called run time errors, and since we can't compile your code we don't know what the errors are!
If it compile, and didn't crash but didn't do what you expected, those are called logic errors, and since we have no idea what your code is meant to do or what data it is trying to process, we can't help you with that either.

Without actual information on what type of errors you get, and where; what you expected to happen compared with what did; some idea what you tried to fix them - we can't help.
I'm going to give you two links which I'd like you to read:

Asking questions is a skill[^]
How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]

Please read them, the first should help you to get an answer, and the second may help you to fix your problem yourself.
I'm not saying we don't want to help you fix your problems - we are more than willing to help people - but we need to know what problem you are having before we can even begin!

Just dumping code on us and saying "it don't work" doesn't help anyone!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900