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

In Java Adapted from book problem P4.30. page 194. Write an application to sell

ID: 674606 • Letter: I

Question

In Java

Adapted from book problem P4.30. page 194. Write an application to sell a limited number of cinema tickets. Each buyer can buy a limited number of tickets. Implement a program that: First, prompts the user for the maximum number of tickets available and the maximum number of tickets that can be sold to one buyer. Next, the program should repeatedly displays the number of remaining tickets and prompt the user for the desired number of tickets, until until all tickets have been sold. If the number of tickets desired by the user is invalid, the program should display an error message and ask again. After that it should display the total number of buyers. Name the project hw4_task1.

Explanation / Answer

import java.io.*;
import java.util.*;

class hw4_task1
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int total,avail,limit,reqd,buyers=0;
System.out.print("Enter the maximum number of tickets available : ");
total = in.nextInt();
System.out.print("Enter the maximum number of tickets that can be bought by one buyer : ");
limit = in.nextInt();
avail = total;
System.out.println(" Available tickets : "+total);
while(avail>0)
{
   System.out.print("How many tickets would you like? : ");
   reqd = in.nextInt();
   if(reqd<1||reqd>limit||reqd>avail)
   {
     System.out.print("Invalid. ");
   }
   else
   {
    buyers++;
    avail = avail-reqd;
    if(avail!=0)
    System.out.println(" Available tickets : "+avail);
   }
}
System.out.println(" All tickets sold");
System.out.println("Number of buyers : "+buyers);
System.out.println("Bye");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote