Answer to Question #273613 in C++ for Joy Chepkwemoi

Question #273613

you are required to write a simple Vehicle Hite System to rent and return vehicle. The rental charges are based on the number of days a car is rented. You are required to write the Vehicle class using the specifications below: instance variables for storing vehicleID, status, dailyRates, nadaysRented, renterName, renterLicenseNumber. The status should be set to either '0' (on-loan) or 'A' (available). Provide accessor and mutator methods for instance variables vehicleID and status.


Write a constructor that takes as parameter vehicle_ID and daily_Rates and assigns the corresponding instance variables. The instance variable status should be set to 'A' (available) initially.

1
Expert's answer
2021-11-30T07:14:55-0500
#include<iostream>
#include<string>


using namespace std;


class Vehicle{
private:
	// instance variables for storing vehicleID, status, dailyRates, nadaysRented, renterName, renterLicenseNumber. 
	int vehicleID;
	char status;//set to either '0' (on-loan) or 'A' (available).
	float dailyRates;
	int nadaysRented;
	string renterName;
	string renterLicenseNumber;
public:
	Vehicle(){}
	// a constructor that takes as parameter vehicle_ID and daily_Rates and assigns the corresponding instance variables. 
	//The instance variable status should be set to 'A' (available) initially.
	Vehicle(int vehicle_ID,float daily_Rates){
		this->vehicleID=vehicle_ID;
		this->status='A';
		this->dailyRates=daily_Rates;
		this->nadaysRented=0;
		this->renterName="";
		this->renterLicenseNumber="";
	}


	// accessor and mutator methods for instance variables vehicleID and status.
	void setVehicleID(int vehicle_ID){
		this->vehicleID=vehicle_ID;
	}
	void setStatus(char status){
		this->status=status;
	}


	int getVehicleID(){
		return this->vehicleID;
	}
	char getStatus(){
		return this->status;
	}


};




int getVehicleById(Vehicle vehicles[],int totalVehicles,int vehicleID){
	for(int i=0;i<totalVehicles;i++){
		if(vehicles[i].getVehicleID()==vehicleID){
			return i;
		}
	}
	return -1;
}


int main()
{
	int ch;
	Vehicle vehicles[100];
	int totalVehicles=0;
	int vehicleID;
	char status;//set to either '0' (on-loan) or 'A' (available).
	float dailyRates;
	int nadaysRented;
	string renterName;
	string renterLicenseNumber;
	do{
		cout<<"1. Rent vehicle\n";
		cout<<"2. Return vehicle\n";
		cout<<"3. Exit\n";
		cout<<"Your choice: ";
		cin>>ch;
		if(ch==1){


			cout<<"Enter vehicle ID: ";
			cin>>vehicleID;
			cout<<"Enter vehicle daily rates: ";
			cin>>dailyRates;
			cin.ignore();
			cout<<"Enter renter name: ";
			getline(cin,renterName);
			cout<<"Enter renter license number: ";
			getline(cin,renterLicenseNumber);
			vehicles[totalVehicles]=Vehicle(vehicleID,dailyRates);
			totalVehicles++;
		}else if(ch==2){
			cout<<"Enter vehicle ID to return: ";
			cin>>vehicleID;
			int index=getVehicleById(vehicles,totalVehicles,vehicleID);
			if(index!=-1){
				for(int i=index;i<totalVehicles-1;i++){
					vehicles[i]=vehicles[i+1];
				}
				totalVehicles--;
				cout<<"\nThe vehicle has been returned.\n\n";
			}else{
				cout<<"\nThe vehicle ID does NOT exist.\n\n";
			}
		}else if(ch==3){
			//exit
		}


	}while(ch!=3);




	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS