Answer to Question #201570 in C++ for SWB

Question #201570

Write a C++ Programme in which Car Parking Reservation System is based on a concept to generate and maintain parking details with their total charge. Before stepping into the main system a user has to pass through a login system to get access, then only he/she can use all the features of the system which includes maintaining arrival car, view all cars, parking charges, and another one is Departure of the car.

Car Arrival

1.      Total number of cars arrived.

2.      Total parking charges for each car based on the time parking area is used

3.      Car Departure

4.      VIP Discount

1
Expert's answer
2021-06-02T13:14:28-0400
#include<iostream>
#include<conio.h>
#include<string>
#include<vector>



using namespace std;



class Car{
private:
	string driverName;
	string carNo;
	float hoursStay;
	float discount;
public:
	//constructor
	Car(){}


	//constructor
	Car(string driverName,string carNo,float hoursStay,float discount){
		this->driverName=driverName;
		this->carNo=carNo;
		this->hoursStay=hoursStay;
		this->discount=discount;
	}
	//Calculate parking charge
	float calculateParkingCharge(){
		return hoursStay*5.0;
	}


	//Calculates discount
	float calculateDiscount(float parkingCharge){
		return parkingCharge*this->discount;
	}
	~Car(){}
	//Prints info
	void printInfo(){
		float disc=0;
		float parkingCharge=this->calculateParkingCharge();
		cout<<"The driver name: "<<this->driverName<<"\n";
		cout<<"The car no: "<<this->carNo<<"\n";
		cout<<"The hours stay: "<<this->hoursStay<<"\n";
		cout<<"The parking charge (subtotal): "<<parkingCharge<<"\n";
		if(discount!=0.0){
			cout<<"The discount: "<<calculateDiscount(parkingCharge)<<"\n";
			disc=calculateDiscount(parkingCharge);
		}
		cout<<"The parking charge (total): "<<(parkingCharge-disc)<<"\n";


	}
	string getCarNo(){
		return carNo;
	}
};


int main(){
	bool isLogin=false;
	cout <<"Car Parking Reservation System Login\n\n";
	while(!isLogin){
		string username ="";
		string password ="";
		char ch;
		cout << "Enter username: ";
		getline(cin,username);
		cout << "Enter password: ";
		ch = getch();
		while(ch != 13){
			password+=ch;
			cout << '*';
			ch = getch();
		}
		if(username.compare("admin")==0){
			if(password.compare("admin")==0){
				isLogin=true;
			}else{
				cout<<"\n\nWrong password. Try again.\n\n";
			}
		}else{
			cout<<"\n\nWrong username. Try again.\n\n";
		}
	}


	if(isLogin){
		cout<<"\n\n";
		int menuChoice=-1;
		string driverName;
		string carNo;
		float hoursStay;
		float discount;
		vector<Car> cars;


		while(menuChoice!=5){
			cout<<"1. Car arrival\n";
			cout<<"2. Total number of cars arrived.\n";
			cout<<"3. Total parking charges for each car based on the time parking area is used\n";
			cout<<"4. Car departure\n";
			cout<<"5. Exit\n";
			cout<<"Your choice: ";
			cin>>menuChoice;
			cin.ignore();
			if(menuChoice==1){
				cout<<"Enter the driver name: ";
				getline(cin,driverName);
				cout<<"Enter the car no: ";
				getline(cin,carNo);
				hoursStay=-1;
				while(hoursStay<=0){
					cout<<"Enter the hours of stay: ";
					cin>>hoursStay;
				}
				cin.ignore();
				string answer="";
				while(answer.compare("y")!=0 && answer.compare("Y")!=0 &&
					answer.compare("n")!=0 && answer.compare("N")!=0){
						cout<<"Are you a V.I.P (y/n): ";
						getline(cin,answer);	
				}
				discount=0.0;
				if(answer.compare("y")==0 || answer.compare("Y")==0){
					discount=-1;
					while(discount<=0){
						cout<<"Enter the discount %(0-100]: ";
						cin>>discount;
					}
					discount=discount/100.0;
				}
				cars.push_back(Car(driverName,carNo,hoursStay,discount));
				cout<<"\nA new car has been arrived.\n\n";
			}else if(menuChoice==2){
				if(cars.empty()){
					cout<<"\nCar Parking is empty.\n\n";
				}else{
					cout<<"\nTotal number of cars arrived: "<<cars.size()<<"\n\n";
				}
			}else if(menuChoice==3){
				if(cars.empty()){
					cout<<"\nCar Parking is empty.\n\n";
				}else{
					float totalParkingCharge=0.0;
					for(int i=0;i<cars.size();i++){
						cars[i].printInfo();
						cout<<"\n";
						totalParkingCharge+=cars[i].calculateParkingCharge();
					}
					cout<<"\nTotal parking charge: "<<totalParkingCharge<<"\n\n";
				}


			}else if(menuChoice==4){
				if(cars.empty()){
					cout<<"\nCar Parking is empty.\n\n";
				}else{
					cout<<"Enter the car number you want to get depart: ";
					getline(cin,carNo);
					int carIndex=-1;
					for(int i=0;i<cars.size();i++){
						if(cars[i].getCarNo().compare(carNo)==0){
							carIndex=i;
							break;
						}
					}
					if(carIndex!=-1){
						cars.erase(cars.begin()+carIndex);
						cout<<"\nThe car has been departed.\n\n";
					}else{
						cout<<"\nThe car does not exist.\n\n";
					}
				}
			}else if(menuChoice==5){
				//exit
			}else{
				cout<<"\nWrong menu item.\n\n";
			}
		}
	}


	system("pause");
}








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