Consider a tollbooth on a bridge. Cars passing by the booth are expected to pay
ID: 3833302 • 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 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 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
Explanation / Answer
TollBooth class implementation is given below which has two data members numCarsPassed (for tracking the number of cars that have gone by) and totalAmountCollected (for tracking the total amount of money collected). This class has three methods (namely, payingCar, nopayCar and display) as per requirement.
File: TollBooth.h
#include <iostream>
using namespace std;
class TollBooth
{
private:
int numCarsPassed;
float totalAmountCollected;
public:
TollBooth() : numCarsPassed(0), totalAmountCollected(0) {}
void payingCar()
{
numCarsPassed++;
totalAmountCollected += 0.5;
}
void nopayCar() { numCarsPassed++; }
void display()
{
cout << "Number of cars passed: " << numCarsPassed << endl;
cout << "Total amount collected: $" << totalAmountCollected << endl;
}
};
Test is a program to verify functionality of TollBooth class by providing the user options to perform any three methods supported by the TollBooth class. Sample execution output is also given below for reference.
File: Test.cpp
#include <iostream>
#include <algorithm>
#include "TollBooth.h"
using namespace std;
int main()
{
int option;
string repeat = "yes";
TollBooth tollBooth;
do
{
cout << "1. Car passes and pays the toll" << endl;
cout << "2. Car passes and does not pay the toll" << endl;
cout << "3. Display results" << endl;
cout << "Select an option (1 - 3): ";
cin >> option;
switch (option) {
case 1:
tollBooth.payingCar();
break;
case 2:
tollBooth.nopayCar();
break;
case 3:
tollBooth.display();
break;
default:
cout << "Please select a valid option" << endl;
continue;
}
cout << "Do you want to repeat? (yes / no) ";
cin >> repeat;
transform(repeat.begin(), repeat.end(), repeat.begin(), ::tolower);
} while (repeat == "yes");
return 0;
}
Compilation:
$ g++ Test.cpp
Sample Execution Output:
$ ./a.out
1. Car passes and pays the toll
2. Car passes and does not pay the toll
3. Display results
Select an option (1 - 3): 1
Do you want to repeat? (yes / no) yes
1. Car passes and pays the toll
2. Car passes and does not pay the toll
3. Display results
Select an option (1 - 3): 1
Do you want to repeat? (yes / no) yes
1. Car passes and pays the toll
2. Car passes and does not pay the toll
3. Display results
Select an option (1 - 3): 1
Do you want to repeat? (yes / no) yes
1. Car passes and pays the toll
2. Car passes and does not pay the toll
3. Display results
Select an option (1 - 3): 2
Do you want to repeat? (yes / no) yes
1. Car passes and pays the toll
2. Car passes and does not pay the toll
3. Display results
Select an option (1 - 3): 2
Do you want to repeat? (yes / no) yes
1. Car passes and pays the toll
2. Car passes and does not pay the toll
3. Display results
Select an option (1 - 3): 3
Number of cars passed: 5
Total amount collected: $1.5
Do you want to repeat? (yes / no) no