Project 3
ANS:
//In c++ we include iostream.
#include
//Include string
#include
//helps in avoiding the ambiguity
using namespace std;
//create class and name of the class
class vehicle
{
//here,protected access is using
protected:
//create members make,model,year & odometer
string make;
string model;
int year;
int odometer;
//under public create functions
public:
//members can't access under protected we need to create setter & getter functions
void setMake(string mak) //create a set function for make to access the data
{
make=mak; //give set name
}
void setModel(string mode) //create a set function for model to access the data
{
model=mode; //give set name
}
void setYear(int yea) //create a set function for year to access the data
{
year=yea; //give set name
}
void setOdometer(int odo) //create a set function for odometer to access the data
{
odometer=odo; //give set name
}
string getMake() //create get function for make
{
return make; //return make
}
string getModel() //create get function for make
{
return model; //return model
}
int getYear() //create get function for make
{
return year; //return year
}
int getOdometer() //create get function for make
{
return odometer; //return odometer
}
void list() //create function to list the vehicles in the inventory
{
cout<<"Make: "< cout<<"Model: "< cout<<"Year: "< cout<<"Odometer: "< } void add() //create function to add new vehicle to inventory { string maker,modelr; //create variables for string type int odo,ye; //create variables for integer type cout<<"Enter the make,model,year,odometer"< cin>>maker>>modelr>>odo>>ye; //to read the inputs of make,mode, year,odometer setMake(maker); //set function for make setModel(modelr); //set function for model setYear(ye); //set function for year setOdometer(odo); //set function for odometer } void update() //create function to update the vehicle in the inventory list { int x; //create variable x of int type cout<<"Enter your choice"< cout<<"1.make 2.model 3.year 4.odometer"< cin>>x; //to read the variable x switch(x) //create a switch case for user input { case 1: //if input is 1,make is update { string maker; //variable maker cout<<"Enter your new make: "; //enter new make name cin>>maker; //to read maker setMake(maker); //set function for make } break; case 2: //if input is 2,model is update { string modelr; //variable model cout<<"Enter new model: "; //enter new model name cin>>modelr; //to read modelr setModel(modelr); //set function for model } break; case 3: //if input is 3,year is update { int ye; //variable year cout<<"Enter new year: "; //enter new year cin>>ye; //to read input setYear(ye); //set function for year } case 4: //if input is 4,odometer is update { int odo; //variable odo cout<<"Enter new odometer reading: "; //enter new odometer reading cin>>odo; //to read the input setOdometer(odo); //set function for odometer } break; default: //if give any wrong input: default case cout<<"Invalid choice: "< } } void Remove() //create function to remove the vehicle from the inventory list { make="NULL"; //make in string type so,keep NULL to remove model="NULL"; //model in string type so,keep NULL to remove year=0; //year in int type so,keep 0 to remove odometer=0; //odometer in int type so,keep 0 to remove } }; void checkTireRotation(int odometer) //create function to Check if any tires need rotation { if(odometer>=50000) //condition: if the odometer reading is greater than or equal to 50000 { cout<<"Time to rotate tires on your gas car: "< } else //if condition not get satisfied { cout<<"Tires are good to go!"< } } void changeoil(int oillevel) //create function to check if car requires oil change { cout<<"check oil change is needed: "< if(oillevel>=1000) //condition: if oil level is greater than or equal to 1000 { cout<<"No oil change needed: "< } else //if condition not get satisfied { cout<<"The car needs to oil change: "< } } void recharge(int kilometer) //create function to check if vehicle needs recharge { cout<<"checking for any battery charging is needed: "< if(kilometer>=400) //condition: if kilometer is greater than or equal to 400 { cout<<"Battery needs to be charge: "< } else //if condition not get satisfied { cout<<"No charge is required: "< } } //main function or driver function int main() { vehicle v[10]; //create a array of size 10 int var=0; //Initialize var to 0 int ch; //create variable with ch while(true) //while true { cout<<"Enter the choice: "< cout<<"0.Quit."< cout<<"1.List inventory."< cout<<"2.Add vehicle."< cout<<"3.Update vehicle."< cout<<"4.Remove vehicle."< cout<<"5.Tire rotation."< cout<<"6.Oil change."< cout<<"7.Recharge."< cin>>ch; //to read input switch(ch) //create switch function { case 0: //if input is 0 exit(0); //finished the program exit break; case 1: //if user input is 1 for(int i=0;i
v[i].list(); //array to list the inventory case 2: //if user input is 2 if(var<10>
{ v[var].add(); //array to add the inventory var++; //var increased by 1 break; } case 3: //if user input is 3 { int in; //create variable in cout<<"Enter index to update: "; //print Enter index to update cin>>in; //to read the input if(in>=0&& in
v[in].update(); //array to update the vehicle else //if condition not get satisfied cout<<"Invalid index"< } break; case 4: //if user input is 4 { int in; //create variable in cout<<"Enter index to remove: "; //print Enter index to remove cin>>in; //to read the input if(in>=0 && in
{ v[in].Remove(); //array to remove index cout<<"Vehicle removed: "< } else //if condition not get satisfied cout<<"Invalid index"< } break; case 5: //if user input is 5 { cout<<"Enter odometer reading:"; //print Enter odometer reading int odometer; //create variable odometer cin>>odometer; //to read the input checkTireRotation(odometer); break; } case 6: //if user input is 6 { int oillevel; //create variable oil level cout<<"Enter the oil level(int ml):"; //print Enter the oil level(int ml) cin>>oillevel; //to read the input changeoil(oillevel); break; } case 7: //if user input is 7 { int km; //create a variable km cout<<"Enter the number of kilometers: "; //print Enter the number of kilometers cin>>km; //to read the input recharge(km); //recharge break; } default: //default case cout<<"Invalid choice."< } } return 0; } OUTPUT: 1. 2. 3. 4. 5. 6. 7.





