167k views
3 votes
Write an application to pre-sell a limited number of theatre tickets. Each buyer can buy as many as 6 tickets. No more than 178 tickets can be sold. Implement a program called TikStar that prompts the user for the desired number of tickets and displays the number of remaining tickets. Repeat until all tickets have been sold, and then display the total number of buyers.

1 Answer

3 votes

Answer:

THe required code is given below:

Step-by-step explanation:

import java.util.Scanner;

public class TicketSeller {

public static int count=0;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int order; //number of ticket ordered

int numberTickets; //total number of available tickets

System.out.println("Input the number of tickets you want to sell: ");

numberTickets = scanner.nextInt();

//continue until there are no tickets remaining

while (numberTickets>0)

{

System.out.println("\\Input requested tickets: ");

order = scanner.nextInt();

if (order<=numberTickets) //check if the ordered number is less than(or equal to) the available tickets

{

numberTickets= processOrder(order,numberTickets);

count++; //increase the count of number of buyers

}

else System.out.println("Too many tickets requested, please try again.");

}

System.out.println("You have had "+count+ " buyers.");

}

public static int processOrder(int order, int numberTickets){

Scanner scanner = new Scanner(System.in);

if(order>4) //check if order is more than 4

{

System.out.println("Too many tickets requested, please try again.");

System.out.println("\\Input requested tickets: "); //ask to input number of tickets again

order = scanner.nextInt();

numberTickets= processOrder(order,numberTickets);

count++; //increase the count of number of buyers

}

else

{

numberTickets=numberTickets-order; //subtract the number of ordered tickets from available tickets

System.out.println("Thank you for your purchase. There are "+numberTickets+ " tickets remaining.\\");

}

return numberTickets;

}

User Rey
by
4.6k points