Project 2
Que 1.
Solution:
//Preprocessor Section/Header Files
#include //Standard I/O Stream Library
#include //Standard C String Library
�
using namespace std;
�
//Define Class named Vehicle
class Vehicle
{
protected: //Access Specifier
//Protected Member Variable
string make; //The make of the vehicle
string model; //The model of the vehicle
int odo; //The odometer reading
int year; //The year of manufacture of the vehicle
�
public: //Access Specifier
�
� � // Setter methods for vehicle attributes
� � void setMake(string z)
� � {
� � make = z;
� � }
� � void setModel(string z)
� � {
� � model = z;
� � }
� � void setOdo(int z)
� � {
� � odo = z;
� � }
� � void setYear(int z)
� � {
� � year = z;
� � }
�
� � //Getter methods for vehicle attributes
� � string getMake()
� � {
� � return make;
� � }
� � string getModel()
� � {
� � return model;
� � }
� � int getOdo()
� � {
� � return odo;
� � }
� � int getYear()
� � {
� � return year;
� � }
�
� � //Declare function
� � void list()
� � {
� � � � cout<<"Make : "< � � � � cout<<"Model : "<< getModel()< � � � � cout<<"Odometer : "< � � � � cout<<"Year : "< � � } � � � //Declare function � � void add() //declare a Function add a new vehicle � � { � � � � string mak, mode; � � � � int od, yr; � � � � cout<<"Enter the make, model, odometer, year"< � � � � cin>>mak>>mode>>od>>yr; � � � � setMake(mak); � � � � setModel(mode); � � � � setOdo(od); � � � � setYear(yr); � � } � � � //Declare functiom � � void update() //Declare function update vehicle attributes � � { � � � � int n; � � � � cout<<"Give the choice"< � � � � cout<<"1. Make 2. Model 3. Odometer 4. Year"< � � � � cin>>n; � � � � � //Switch Case � � � � switch (n) � � � � { � � � � case 1: � � � � { � � � � � � string mak; � � � � � � cout<<"Enter new make : "; � � � � � � cin>>mak; � � � � � � setMake(mak); � � � � � � break; � � � � } � � � � � case 2: � � � � { � � � � � � string mode; � � � � � � cout<<"Enter new model : "; � � � � � � cin>>mode; � � � � � � setModel(mode); � � � � � � break; � � � � } � � � � � case 3: � � � � { � � � � � � int od; � � � � � � cout<<"Enter new odometer reading : "; � � � � � � cin>>od; � � � � � � setOdo(od); � � � � � � break; � � � � } � � � � � case 4: � � � � { � � � � � � int yr; � � � � � � cout<<"Enter new year : "; � � � � � � cin>>yr; � � � � � � setYear(yr); � � � � � � break; � � � � } � � � � � default: � � � � � � cout<<"Invalid choice"< � � � � } � � } � � � //Declare function � � void Remove() //declare functiom remove a vehicle by resetting attributes � � { � � � � make = "NULL"; � � � � model = "NULL"; � � � � odo = 0; � � � � year = 0; � � } }; � //Declare Function void checkTireRotation(int odometer) //Functions to check vehicle maintenance based on attributes { � � if (odometer >= 50000) � � { � � � � cout<<"Time to rotate tires on your gas car."< � � } � � else � � { � � � � cout<<"Tires are good to go."< � � } } � //declare Function void changeOil(int oilLevel) //Function to check changeoil of vehicle { � � cout<<"Checking for oil change..."< � � if (oilLevel >= 1000) � � { � � � � cout<<"No oil change needed."< � � } � � else � � { � � � � cout<<"Car needs an oil change."< � � } } � //Declare Function void recharge(int KM) { � � cout<<"Checking for battery charging..."< � � if (KM >= 400) � � { � � � � cout<<"Battery needs to be recharged."< � � } � � else � � { � � � � cout<<"Car is good to go."< � � } } � //Main Function int main() { � � Vehicle v[10]; //An array of Vehicle objects � � int var = 0; � � � 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. Check Tire Rotation."< � � � � cout<<"6. Change Oil."< � � � � cout<<"7. Recharge."< � � � � � int ch; � � � � cin>>ch; � � � � � //Switch Case � � � � switch (ch) � � � � { � � � � case 0: � � � � { � � � � � � exit(0); //Exit the program � � � � � � break; � � � � } � � � � � case 1: � � � � � � for (int i = 0; i < var; i++) � � � � � � { � � � � � � � � v[i].list(); //List details of all vehicles � � � � � � � � break; � � � � � � } � � � � � case 2: � � � � � � if (var < 10) � � � � � � { � � � � � � � � v[var].add(); // Add a new vehicle to the list � � � � � � � � var++; � � � � � � � � break; � � � � � � } � � � � � case 3: � � � � { � � � � � � int in; � � � � � � cout<<"Enter the index to update : "; � � � � � � cin>>in; � � � � � � if (in >= 0 && in < var) � � � � � � { � � � � � � � � v[in].update(); // Update attributes of a specific vehicle � � � � � � } � � � � � � else � � � � � � { � � � � � � � cout << "Invalid index." << endl; � � � � � � } � � � � � � break; � � � � } � � � � � case 4: � � � � { � � � � � � int in; � � � � � � cout<<"Enter the index to remove : "; � � � � � � cin>>in; � � � � � � if (in >= 0 && in < var) � � � � � � { � � � � � � � � v[in].Remove(); // Remove a vehicle from the list � � � � � � � � cout<<"Vehicle removed."< � � � � � � } � � � � � � else � � � � � � { � � � � � � � � cout<<"Invalid index."< � � � � � � } � � � � break; � � � � } � � � � � case 5: � � � � { � � � � � � cout<<"Enter the odometer reading : "; � � � � � � int odometer; � � � � � � cin>>odometer; � � � � � � checkTireRotation(odometer); //Check tire rotation maintenance � � � � � � break; � � � � } � � � � � case 6: � � � � { � � � � � � cout<<"Enter the oil level (in ml) : "; � � � � � � int oilLevel; � � � � � � cin >> oilLevel; � � � � � � changeOil(oilLevel); //Check oil change maintenance � � � � � � break; � � � � } � � � � � case 7: � � � � { � � � � � � cout<<"Enter the number of kilometers : "; � � � � � � int KM; � � � � � � cin >> KM; � � � � � � recharge(KM); //Check battery charging maintenance � � � � � � break; � � � � } � � � � � default: � � � � { � � � � � � cout<<"Invalid choice."< � � � � } � � � � } � � } � � � return 0; //End of program } Output: Enter the choice: 0. Quit. 1. List inventory. 2. Add vehicle. 3. Update vehicle. 4. Remove vehicle. 5. Check Tire Rotation. 6. Change Oil. 7. Recharge. 2 Enter the make, model, odometer, year Honda City 20000 2012 Enter the choice: 0. Quit. 1. List inventory. 2. Add vehicle. 3. Update vehicle. 4. Remove vehicle. 5. Check Tire Rotation. 6. Change Oil. 7. Recharge. 5 Enter the odometer reading : 20000 Tires are good to go. Enter the choice: 0. Quit. 1. List inventory. 2. Add vehicle. 3. Update vehicle. 4. Remove vehicle. 5. Check Tire Rotation. 6. Change Oil. 7. Recharge. 6 Enter the oil level (in ml) : 200 Checking for oil change... Car needs an oil change. Enter the choice: 0. Quit. 1. List inventory. 2. Add vehicle. 3. Update vehicle. 4. Remove vehicle. 5. Check Tire Rotation. 6. Change Oil. 7. Recharge. 1 Make : Honda Model : City Odometer : 20000 miles Year : 2012 Enter the make, model, odometer, year �