AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

# corejava-programming: Java Program-TollBooth

 Imagine a tollbooth at a bridge. Cars passing by the bridge are expected to pay a 50 rupees 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. Write a program to allow the user to press the key 1 to count a paying car, and 2 to count a nonpaying car. Pressing any other key should cause the program to print the output (as defined in Output format section) and then exit from the program. Output format: The output consists of 4 lines. First line of the output contains an integer which corresponds to the total number of cars. Second line of the output contains an integer which corresponds to total number of cars who paid. Third line of the output contains an integer which corresponds to total number of cars who have not paid Fourth line contains an integer which corresponds to the total amount of money collected in rupees. Sample Input: 1 1 2 1 2 1 2 1 1 1 5 Sample Output: 10 7 3 350 Explanation: In above case, total car is 10 among them 7 cars who paid whereas 3 car have not paid. For 7 paid cars, total cash collected is 350. corejava x 353programming x 168 Posted On : 2016-12-17 14:34:00.0 Aarti Kumar 5900 5

 Check This code and Let me know if this code satisfies the conditions or not. import java.util.*; class TollBooth { public static void main(String args[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int cnt=0,paidcnt=0; do { if(n==1||n==2) { if(n==1) { paidcnt++; } } cnt++; n=s.nextInt(); }while(n!=5); int Totalpaidcnt=cnt-paidcnt; System.out.println(cnt+"\n"+paidcnt+"\n"+Totalpaidcnt); System.out.println(paidcnt*50); } } Posted On : 2016-12-18 21:04:45 Satisfied : 2 Yes  0 No 5
Small changes in my code.validate the condition and let know if conditions are satisfied or not.

import java.util.*;
class TollBooth
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int cnt=0,paidcnt=0;
do
{
if(n==1||n==2)
{
if(n==1)
{
paidcnt++;
}
cnt++;
}
n=s.nextInt();
if(n==3||n==4||n>5)
{
break;
}
}while(n!=5);
int Totalpaidcnt=cnt-paidcnt;
System.out.println(cnt+"\n"+paidcnt+"\n"+Totalpaidcnt);
System.out.println(paidcnt*50);
}
}
Posted On :2016-12-19 23:46:54.0