Answers
Hello, I have answered a similar question once. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// TicketSeller.java
public class TicketSeller {
// private constant for storing maximum number of tickets per person
private static final int MAXIMUM_TICKETS_ALLOWED = 4;
// public constant for representing error state if too many tickets are
// requested.
Here -999 can be replaced by any (negative) number,.
public static final int TOO_MANY_TICKETS_REQUESTED = -999;
// total number of tickets available
private int numTickets;
// total number of legit buyers
private int numBuyers;
/**
* constructor to initialize number of tickets
*
* @param initialTicketAllotment
* - number of tickets available
*/
public TicketSeller(int initialTicketAllotment) {
this.numTickets = initialTicketAllotment;
numBuyers = 0;
}
/**
* method to request a specific number of tickets
*
* @param ticketRequest
* number of tickets wanted
* @return TOO_MANY_TICKETS_REQUESTED if user asks for tickets more than
* MAXIMUM_TICKETS_ALLOWED or more than available tickets. In all
* other cases, returns the number of tickets available
*/
public int requestTickets(int ticketRequest) {
if (ticketRequest > MAXIMUM_TICKETS_ALLOWED
|| ticketRequest > numTickets) {
// error
return TOO_MANY_TICKETS_REQUESTED;
}
// valid. subtracting tickets
numTickets -= ticketRequest;
// incrementing buyers
numBuyers++;
// returning tickets left
return numTickets;
}
/**
* returns the number of valid buyers
*
* @return the current value of numBuyers
*/
public int getNumberOfBuyers() {
return numBuyers;
}
}
// Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// scanner to read user input
Scanner scanner = new Scanner(System.in);
// getting initial number of tickets
System.out.print("Enter the total number of tickets: ");
int n = scanner.nextInt();
// creating a TicketSeller object
TicketSeller seller = new TicketSeller(n);
int remaining = n; // number of tickets remaining
// loops until all tickets are sold
while (remaining > 0) {
// asking for input
System.out.print("Input the number of tickets you want: ");
int tickets = scanner.nextInt();
// requesting tickets
int result = seller.requestTickets(tickets);
if (result == TicketSeller.TOO_MANY_TICKETS_REQUESTED) {
// error
System.out.println("Too many tickets requested, try again!");
} else {
// valid number of tickets
System.out.println("Thank you for your purchase! " + result
+ " number of tickets are remaining");
// updating number of tickets available
remaining = result;
}
}
System.out.println("Tickets have been sold out!");
// displaying number of buyers
System.out.println("Number of buyers: " + seller.getNumberOfBuyers());
}
}
/*OUTPUT*/
Enter the total number of tickets: 10
Input the number of tickets you want: 6
Too many tickets requested, try again!
Input the number of tickets you want: 2
Thank you for your purchase! 8 number of tickets are remaining
Input the number of tickets you want: 4
Thank you for your purchase! 4 number of tickets are remaining
Input the number of tickets you want: 2
Thank you for your purchase! 2 number of tickets are remaining
Input the number of tickets you want: 2
Thank you for your purchase! 0 number of tickets are remaining
Tickets have been sold out!
Number of buyers: 4
.