Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a tollbooth on a bridge. Cars passing by the booth are expected to pay

ID: 3830741 • Letter: C

Question

Consider a tollbooth on 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 manager 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 int to hold the total number of cars, and a type float 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 member function, called nopayCar(), increments the car total but adds nothing to the cash total. Finally, a member function called display() displays the two totals.

WAP in C++ to simulate and test this class. The program should present the user with three options in every run of the program:
1. to simulate the scenario of a car passing and paying the toll
2. to simulate the scenario of a car passing and not paying the toll
3. to display the results of total cars passed and total toll collected

Once the user selects a particular option from above, the program will execute the functionalities related to that particular option. For example, if they select say option one, then your program needs to call the member function payingCar() and so forth.

After this, the user should be asked whether or not they want to repeat the program. If yes, then the three options above needs to be presented again, else the program will terminate.


Input Case to be tested:
Program Run 1: Car passes and pays the toll
Program Run 2: Car passes and pays the toll
Program Run 3: Car passes and pays the toll
Program Run 4: Car passes and does not pay the toll
Program Run 5: Car passes and does not pay the toll
Program Run 6: Display results

References:
From your lecture book: Problem Solving with C++
Chapter 10.2 : Classes.
Display 10.3 and 10.4 : For basic declaration of classes and using data member, member functions
Display 10.6: For how to use Constructors

Explanation / Answer

Here is the C++ program for the above scenario:

#include <iostream>

using namespace std;

class tollboth

{

private:

int null,pcar,wpcar;

float tax;

public:

tollboth()

{

tax=0;

pcar=0;

wpcar=0;

null=0;

}

void payingcar(int a)

{

/*pcar=a;*/

pcar=pcar+a;

for(null;null<=pcar;null++)

{

tax=tax+0.50;

}

}

void withoutpay(int b)

{

wpcar=wpcar+b;

}

void showdata()

{

cout<<"Total no of payed cars are : "<<pcar<<endl;

cout<<"Total tax is : "<<tax<<endl;

cout<<"Total no of not payed cars are : "<<wpcar<<endl;

}

};

int main()

{

tollboth tb;

char press,input;

int a,b;

do

{

cout<<"Press 1 for car pay tax"<<endl;

cout<<"Press 2 for car not pay tax"<<endl;

cout<<"Press 3 for total tax and Exit"<<endl;

cin>>press;

switch(press)

{

case '1':

{

cout<<"Enter No of the cars pay tax"<<endl;

cin>>a;

tb.payingcar(a);

break;

}

case '2':

{

cout<<"Enter No of cars not pay tax"<<endl;

cin>>b;

tb.withoutpay(b);

break;

}   

}

cout<<"Press y to continue and n for terminate"<<endl;

cin>>input;

}

while(input=='y');

tb.showdata();

system("pause");

return 0;

}