155k views
1 vote
write an application to presell a limited number of cinema tickets. each buyer can buy as many as 4 tickets. No more than 100 tickets can be sold. Implement a program called TicketSeller that prompts the user for the desired number of tickets and then displays the number of remaining ticket. Repeat until all tickets have been sold, and then display the total number of buyers

User Fram
by
5.8k points

1 Answer

6 votes

Answer:

The program for this question can be given as:

Program:

import java.util.*; //import package

public class TicketSeller //define class

{

public static void main(String ar[]) //main method

{

int totalTickets=100,userCount=0,remainingTickets=100,numTickets; // varaibale

try //try block

{

Scanner ob= new Scanner(System.in); //input by user

System.out.println("Total Tickets:"+totalTickets);

while(remainingTickets!=0) //loop

{

System.out.print("Enter no of Tickets to buy:"); //message.

numTickets=ob.nextInt(); //taking input

if(numTickets<=4) //conditional statements.

{

if(remainingTickets>=numTickets)

{

remainingTickets=remainingTickets-numTickets; //calculate remainingTickets

userCount++;

System.out.println("remaining tickets : "+remainingTickets+" user :"+userCount);

}

else

{

System.out.println("Invalid Input");

}

}

else

{

System.out.println("Invalid Input");

}

}

}

catch(Exception e) //catch block.

{

}

}

}

Output:

Total tickets:100

Enter no of Tickets to buy: 4

Remaining tickets : 96 User :1

Enter no of Tickets to buy: 4

Remaining tickets : 92 User :2

Enter no of Tickets to buy: 4

Remaining tickets : 88 User :3

.

.

.

Enter no of Tickets to buy: 4

Remaining tickets : 0 User :25

Step-by-step explanation:

In the above Program firstly we import the package for the user input. Then we declare the class that is (TicketSeller) given in question. In this class we declare the main method in the main method we declare the variable that is totalTickets, userCount, remainingTickets and numTickets .Then we use the exception handling. we use this for providing the validation from the user side that users don't insert value less the 4. In the exception handling, we use the multiple catch block with try block. Then we input from the user and calculate the values and in the last, we print all the values.

User Katelyn Gadd
by
6.5k points