The project name of this exercise is TicketSeller The purpos

The project name of this exercise is TicketSeller

The purpose of this assignment is to learn how to work with iteration and constants. Additionally, you get more practice on how to write all of your own code and Javadoc comments.

Problem Description

This problem is inspired by the book\'s Problem P6.10 on page 304.

You are to write an application to pre­sell a limited number of movie tickets. The simple user interface will be contained in TicketSeller.java.

Your TicketSeller.java should contain code to test your TicketSeller object.

Note:

Your requests for tickets will always be positive integers.

Getting Started

There is no code to copy for the assignment. You get to do it all! Don\'t forget to provide proper Javadoc documentation.

You\'ll need to create a method called public static int processOrder(int order, int numberTickets). The method determines if the order size is less than 4 and less than or equal to numberTickets.

We are going to do this exercise by writing the object that solves the problem first (in a source file called TicketSeller.java) and then testing it using code we write into TicketSeller.java. Using the techniques shown on the web page titled How to Start Every Project in this Class to create a source file called TicketSeller.java. This is where your code will go.

Testing Your Code

Your test code, in main for this project will be in TicketSeller.java and will implement a simple user interface. Here is a sample run:

Notice that only the valid ticket purchases make someone count as a buyer. Don\'t worry about other inputs; you\'ll always get positive integers.

Once you\'ve written your code, run the code by single clicking on TicketSeller.java in the package explorer and selecting Run->Run from the menu or using the keyboard shortcut. Examine the output. Does it do what you want? If not, how can you modify the code to do what you want?

Solution

// TicketSeller.java
import java.util.Scanner;

public class TicketSeller
{
public static int totalBuyers = 0;
public static int processOrder(int order, int numberTickets)
{
if (order < 4 && order > 0 && order <= numberTickets)
{
totalBuyers = totalBuyers + 1;
numberTickets = numberTickets - order;
if(numberTickets > 0)
System.out.println(\"Thank you for your purchase. There are \" + numberTickets +\" tickets remaining.\");
else
System.out.println(\"Thank you for your purchase. There are no tickets remaining.\");
}
else
{
System.out.println(\"Too many tickets requested, please try again.\");
}

return numberTickets;
}

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

System.out.print(\"Input the number of tickets you want to sell: \");
int numberTickets = scan.nextInt();

do
{
System.out.print(\"Input requested tickets: \");
int order = scan.nextInt();

numberTickets = processOrder(order,numberTickets);


} while (numberTickets > 0);

scan.close();
System.out.println(\"You have had \" + totalBuyers + \" buyers.\");
}
}

/*
output:

Input the number of tickets you want to sell: 8
Input requested tickets: 1
Thank you for your purchase. There are 7 tickets remaining.
Input requested tickets: 2
Thank you for your purchase. There are 5 tickets remaining.
Input requested tickets: 6
Too many tickets requested, please try again.
Input requested tickets: 9
Too many tickets requested, please try again.
Input requested tickets: 3
Thank you for your purchase. There are 2 tickets remaining.
Input requested tickets: 2
Thank you for your purchase. There are no tickets remaining.
You have had 4 buyers.


*/

The project name of this exercise is TicketSeller The purpose of this assignment is to learn how to work with iteration and constants. Additionally, you get mor
The project name of this exercise is TicketSeller The purpose of this assignment is to learn how to work with iteration and constants. Additionally, you get mor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site