Challenge

Project 3

live (preeti):

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:

  1. Make

  2. Model

  3. Year

  4. 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

  1. Vehicle

    1. This class includes protected members such as Make, Model, Year and Odometer. These members are initialized using the class constructor.

    2. The functions include getter and setter functions of Make(), Model(), Year() and Odometer().

    3. Maintenance activities that should be done are checkTireRotation(), getCarInformation()

    4. Let the three fuel types be

enum Type{

GAS,

DIESEL,

ELECTRIC

};

  1. Let there be a pure virtual method to get the fuel type i.e.,

    1. virtual Type getType() = 0;

  1. GasCar

    1. This is inherited from the vehicle class and has functions getType() and getFuelType()

  2. DieselCar

    1. This class is also a subclass of vehicle and the functions include getFuelType() and getType()

  3. ElectricCar

    1. This is a subclass of Vehicle class and methods include

      1. checkTireRotation()- based on odometer reading, we decide whether the car needs to rotate tires or not.

      2. checkChargerPower- based on odometer reading this advices to recharge power

      3. getType() – to get the fuel type

  4. FuelVehicle

    1. 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,

  1. 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.

  2. 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

  1. In the main class, initialize the inventory and ask the user to select whether to add, update, list or delete the Vehicle list.

    1. addVehicle() – to add new vehicle to inventory

    2. ListVehicle()- to list the vehicles in the inventory

    3. UpdateVehicle()- to update the vehicle in the inventory list

    4. RemoveVehicle()- to remove the vehicle from the inventory list

    5. Tire Rotation() – Check if tires need rotation

    6. OilChange()- check if car requires oil change

    7. Recharge() – check if vehicle needs recharge

  2. 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.

  3. 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"

  1. Retrieve the user input on the vehicle’s make, model, year, type and odometer.

    1. getNewMake() – If input is blank, request again

    2. getNewModel() – If input is blank, request again

    3. getYear() – If input doesn’t have 4 digits, request again

    4. getNewOdometer()- Input should be only numericals

    5. getNewType() – Input should be within 0-2, else request again

  2. Retrieve the selected car from the user input

    1. selectCar() – Input should be within the size of vehicle list, else request again

    2. getNewOdometerReading() – New odometer reading is used to calculate the oil change, tire rotation, power recharge etc.

#include <iostream>// library for standard ip/op functions
#include <string>

using namespace std;
// Define a base class of car
class Car {
public: //public access modifier
Car(string make, string model, int year, string fuelType)
: make(make), model(model), year(year), fuelType(fuelType), mileage(0), lastOilChange(0) {}
// Constructor for the Vehicle class, initializes the make, model, year
string getMake() { return make; }
string getModel() { return model; }
int getYear() { return year; }
string getFuelType() { return fuelType; }
int getMileage() { return mileage; }

void addMileage(int miles) { mileage += miles; }
bool needsOilChange() {
if (fuelType == "electric") {
return false;
}
else if (mileage - lastOilChange >= 5000) {
return true;
}
else {
return false;
}
}
void performTask() {
cout << "Replacing the fuel filter..." << endl;

}
void doOilChange() { lastOilChange = mileage; }

private://private access modifier
string make;// the make of the car
string model;// the model of the car
int year;// the year the car was manufactured
string fuelType;// the fueltype get fuel type of car
int mileage;// the mileage get the mileage of car
int lastOilChange;// the lastoilchange of the car
};
// Define a class petrolCar that inherits from the car class
class PetrolCar : public Car {
public:
PetrolCar(string make, string model, int year)
: Car(make, model, year, "petrol") {} // Constructor for PetrolCar class that calls the constructor of the car class and passes the arguments to it
};
// Define a class DieselCar that inherits from the car class
class DieselCar : public Car {
public:
DieselCar(string make, string model, int year)
: Car(make, model, year, "diesel") {} // Constructor for DieselCar class that calls the constructor of the car class and passes the arguments to it
};
// Define a class ElectricCar that inherits from the car class
class ElectricCar : public Car {
public:
ElectricCar(string make, string model, int year)
: Car(make, model, year, "electric") {} // Constructor for ElectricCar class that calls the constructor of the car class and passes the arguments to it
};

bool checkingyear(int year)
{
int count=0;
while(year!=0){
year=year/10;
count++;
}
if(count==4)
return true;
else
return false;

}

int main() {
int num ,year,addMileage;
char want;
string custName,make,model;

do{
again:
cout<<"Enter the what type of car 1.PetrolCar 2.DieselCar 3.ElectricCar"<<endl;
cin>>num;
switch(num){
case 1:{
cout<<"You selected peterolcar\n"<<"Enter your customer Name"<<endl;
cin>>custName;
cout<<"Enter the make type"<<endl;
cin>>make;
cout<<"Enter the car model"<<endl;
cin>>model;
year:
cout<<"Enter the car year"<<endl;
cin>>year;
if(checkingyear(year)){
}
else{
cout<<"The given car year is invalid please re-enter the year"<<endl;
goto year;
}
PetrolCar petrolCar(make,model,year);
cout<<"enter the mileage"<<endl;
cin>>addMileage;
petrolCar.addMileage(addMileage);

cout <<petrolCar.getMake()<< petrolCar.getModel()<< " " << petrolCar.getYear() << " mileage: " << petrolCar.getMileage() << endl;
if (petrolCar.needsOilChange()) {
petrolCar.doOilChange();
cout << "Replacing the oil filter..." << endl;
cout << "Oil changed for " << petrolCar.getMake()<< petrolCar.getModel()<< " " << petrolCar.getYear() << endl;
}
else {
cout << "Oil change not needed for " << petrolCar.getMake()<< petrolCar.getModel() << " " << petrolCar.getYear() << endl;
}
break;
}
case 2:{
cout<<"you selected dieselcar\n"<<"Enter your customer Name"<<endl;
cin>>custName;
cout<<"Enter the make type"<<endl;
cin>>make;
cout<<"Enter the car model"<<endl;
cin>>model;
year1:
cout<<"Enter the car year"<<endl;
cin>>year;
if(checkingyear(year)){
}
else{
cout<<"The given car year is invalid please re-enter the year"<<endl;
goto year1;
}
DieselCar dieselCar(make,model,year);
cout<<"enter the mileage"<<endl;
cin>>addMileage; dieselCar.addMileage(addMileage);
cout << dieselCar.getMake() <<dieselCar.getModel()<< " " <<dieselCar.getYear() << " mileage: " << dieselCar.getMileage() << endl;
if (dieselCar.needsOilChange()) {
dieselCar.doOilChange();
cout << "Replacing the oil filter..." << endl;
cout << "Oil changed for " << dieselCar.getMake() << " " << dieselCar.getModel() << " " << dieselCar.getYear() << endl;
}
else {
cout << "Oil change not needed for " << dieselCar.getMake();

break;
}
}
case 3:{

cout<<"You selected electrical\n"<<"Enter your customer Name"<<endl;
cin>>custName;
cout<<"Enter the make type"<<endl;
cin>>make;
cout<<"Enter the car model"<<endl;
cin>>model;
year2:
cout<<"Enter the car year"<<endl;
cin>>year;
if(checkingyear(year)){
}
else{
cout<<"The given car year is invalid please re-enter the year"<<endl;
goto year2;
}
ElectricCar electricCar(make,model,year);
cout<<"enter the mileage"<<endl;
cin>>addMileage;
electricCar.addMileage(addMileage);
cout<<electricCar.getMake()<<electricCar.getModel()<< electricCar.getYear()<< " mileage: "<<electricCar.getMileage() << endl;
break;
}
default:{
cout<<"Enter valid option"<<endl;
goto again;
}
}
cout<<"\nDid you want add another car detials (Y/N)"<<endl;
cin>>want;

}while((want=='y')||(want=='Y'));
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.