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
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.