Challenge

Project 3

Aim: 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.

Code:

#include <iostream> using namespace std; int vehCount = 0; class Vehicle { protected: string make,model; int year; long int odometer; int id; public: string getMake() { return make; } string getModel() { return model; } int getYear() { return year; } long int getOdo() { return odometer; } void setModel(string val) { model = val; } void setMake(string val) { make = val; } void setYear(int val) { year = val; } void setOdo(int val) { odometer = val; } void setId(int val) { id = val; } void checkTireRotation() { int remainder = odometer-50000; if (remainder>0) { cout<<"Please replace tyres"; } else { cout<<"\nno need to replace tyres yet"; } } void checkOil() { if (odometer>5000) cout<<"\nOil change required"; else cout<<"\nOil change not required"; } void checkCharge() { if (odometer>20000) cout<<"\nneed to Charge the battery"; else cout<<"\ncharging not required"; } int getId() { return id; } }; Vehicle car[20]; string getNewModel() { string model; cout<<"Enter model: "; cin>>model; return model; } string getNewMake() { string make; cout<<"Enter make: "; cin>>make; return make; } long int getNewOdometer() { long int odo; cout<<"Enter odometer reading: "; cin>>odo; return odo; } int getYear() { int year; cout<<"Enter year of manufacture: "; cin>>year; return year; } int getNewType() { int type; cout<<"Enter type\n0:GAS\n1.DIESEL\n2.ELECTRIC:\n"; cin>>type; cout<<endl; return type; } void addVehicle() { int type = getNewType(); string make = getNewMake(); string model = getNewModel(); long int odo = getNewOdometer(); int year = getYear(); car[vehCount].setMake(make); car[vehCount].setModel(model); car[vehCount].setOdo(odo); car[vehCount].setYear(year); car[vehCount].setId(vehCount+1); cout<<"Added vehicle"<<endl; vehCount++; } void listVehicles() { if (vehCount==0) { cout<<"No vehicles added yet!\n"; return; } for(int i=0;i<vehCount;i++) { cout<<"\n***************************************\n"; cout<<"ID: "<<car[i].getId()<<endl; cout<<"Make: "<<car[i].getMake()<<endl; cout<<"Model: "<<car[i].getModel()<<endl; cout<<"Odometer: "<<car[i].getOdo()<<endl; cout<<"Year: "<<car[i].getYear()<<endl; cout<<"***************************************\n"; } } void updateVehicle() { int id; int sel,year,odo; cout<<"Enter the id to be updated\n"; cin>>id; cout<<"Enter the parameter to be updated\n1.Year\n2.Odometer reading\n"; cin>>sel; cout<<endl; switch (sel) { case 1: year = getYear(); for (int i=0;i<vehCount;i++) { if (car[i].getId()==id) car[i].setYear(year); } break; case 2: odo = getNewOdometer(); for (int i=0;i<vehCount;i++) { if (car[i].getId()==id) car[i].setOdo(odo); } break; default: break; }; cout<<"\nUpdated details\n"; } void removeVehicle() { int id; cout<<"Enter the ID of the vehicle you want to remove"; cin>>id; for(int i=0;i<vehCount;i++) { if(car[i].getId()==id) { for(int j=i;j<vehCount-1;j++) { car[j]=car[j+1]; } break; } } vehCount--; cout<<"\nRemoved\n"; } void checkTireRotation() { int id; cout<<"Enter vehicle Id\n"; cin>>id; for(int i=0;i<vehCount;i++) { if(car[i].getId()==id) { car[i].checkTireRotation(); break; } } } void oilChange() { int id; cout<<"Enter vehicle Id\n"; cin>>id; for(int i=0;i<vehCount;i++) { if(car[i].getId()==id) { car[i].checkOil(); break; } } } void checkCharge() { int id; cout<<"Enter vehicle Id\n"; cin>>id; for(int i=0;i<vehCount;i++) { if(car[i].getId()==id) { car[i].checkCharge(); break; } } } int main() { int selection=10; while(selection!=0) { cout<<"\nEnter selection\n1.List Vehicles\n2.Add Vehicle\n3.Update Vehicle\n4.Remove Vehicle\n5.Check tire rotation\n6.Check oil\n7.Charge vehicle\n"; cin>>selection; cout<<endl; switch(selection) { case 1: listVehicles(); break; case 2: addVehicle(); break; case 3: updateVehicle(); break; case 4: removeVehicle(); break; case 5: checkTireRotation(); break; case 6: oilChange(); break; case 7: checkCharge(); break; default: cout<<"Invalid selection\n"; } } return 0; }

Output:





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.