Wednesday 6 July 2016

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected.
Model this tollbooth with a class called tollBooth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected.
A constructor initializes both of these to 0. A member function called payingCar() increments the car total and adds 0.50 to the cash total.
Another function, called nopayCar(),increments the car total but adds nothing to the cash total. Finally,a member function called display() displays the two totals. Make appropriate member functions const......


#include <iostream>
#include <conio.h>

using namespace std;

char getWhatTheyWant();

class tollBooth{
private:
unsigned int numCars;
double amount;
public:
tollBooth(){
numCars = 0;
amount = 0;
}
void payingCar(){
numCars++;
amount+=0.50;
}
void noPayCar(){
numCars++;
}
void display() {
cout << "Number Of Total Cars: " << numCars << endl;
cout << "Total Amount: " << amount << endl;
}
};

int main(){
tollBooth booth;
char whatTheyWant;
whatTheyWant = getWhatTheyWant();
while(whatTheyWant!='s'){
switch(whatTheyWant){
case('p'):
cout << "Paid" << endl << endl;
booth.payingCar();
whatTheyWant = getWhatTheyWant();
break;
case('n'):
cout << "Not Paid" << endl << endl;
booth.noPayCar();
whatTheyWant = getWhatTheyWant();
break;
case('s'):
cout << "Result" << endl << endl;
break;
default:
cout << "Invalid Input" << endl;
}
}
booth.display();
}

char getWhatTheyWant(){
char a;
cout << "Enter 'p' To Pay And Pass" << endl;
cout << "Enter 'n' To Pass Without Paying" << endl;
cout << "Press 's' To Show Result And Exit" << endl;
cout << "Make Your Choice" << endl;
a = getch();
return a;
}












No comments:

Post a Comment