JAVA hwk help

rosasznwysui
05_TicketSystem.pdf

Page 1 of 7

ITP 109: Introduction to Java Programming

Assignment 05: Concert Ticket System Objective Demonstrate knowledge of using while and for loops.

Overview You are creating a system to track the number of tickets available and allow the user to buy tickets. There are two type of tickets – VIP tickets and General Floor tickets.

Procedure Open BlueJ and create a new project called Assignment05-userName, where username is your USC username (your email address without the @usc.edu) . Get the starter code from Blackboard. Read over and understand the code that already exists.

ConsoleMenu.java (modified from in class CoffeeShop code) Update the getUserChoice() method in the ConsoleMenu class to make the method loop in order to only allow the user to enter the numbers 1 through numOptions

TicketSystem.java This skeleton class does some of the setup for the ticket system program; it is up to you to complete this code.

Instance Variables There are 5 instance variables in this class. You should not create any more. private Scanner input; // Used to get input from the user private ConsoleMenu menu; // Used to display a menu and get input from the user private String concertName; // Name of the concert the user is buying tickets for private String venueName; //Name of the venue for the concert private final int TOTAL_ROWS; //total number of rows in the venue (set in the constructor) private final int VIP_ROWS; // number of VIP rows in the venue (set in the constructor) private int vipSeatsSold; // Needs to increase when the user buys VIP tickets private int generalSeatsSold; // Needs to increase when the user buys General Floor tickets

Class Variables There are 3 static constants that you will also use in your program, for setting up the menu and for defining a standard number of seats per row.

Constructors One constructor is provided for you, it is a version that takes 4 input variables and sets up the corresponding instance variables accordingly. In addition, the other 4 instance variables are initialized in this version of the constructor. Make sure you understand it.

Page 2 of 7

Overload the constructor to allow a user of the system of the Ticket System to just provide a concert & venue name. Also provide a default constructor and set up the concert as “The Killers” playing at the “Staples Center”. Do not simply copy and paste the code; you must use the programming construct this() as a method that allows one version of the constructors to call another. Each of the constructors that you provide should have only one statement.

Public Method The heart of your program is the run method. This method will control how the ticket system runs and will call helper methods to perform tasks.

public void run() • Print a welcome message that includes the eventName and the venue name. (Could be a method or code within run) • Print the menu and get input from the user. Use a while or do-while loop to do this until the user enters the option 4

(to EXIT). Don’t use a literal “magic “number 4; use the appropriate constant. o To print the menu, use the ConsoleMenu object. o Get the menu option by using the ConsoleMenu object. o Use a switch statement to call the corresponding helper method. For 1, call the printSeats method. For 2, call

the buyGeneralTickets method. For 3, call the buyVipTickets method. • Print a closing message that includes the eventName. (Could be a method or code within run)

Private Methods Create various helper methods to perform tasks. The following private methods are required. Create as many local variables in the methods as you need. You should not create any more instance variables. You may create other helper methods if you want.

private int vipSeatsAvailable() • Calculate and return the number of VIP seats available. Use the number of VIP rows, and the number of seats per

row to know the number of seats. Use the vipSeatsSold instance variable to figure out how many are available at any point during the program’s run time.

private int generalSeatsAvailable() • Calculate and return the number of Regular concert seats available. Use the total number of row, the number of VIP

rows, and the number of seats per row to know the number of general seats in total. Use the generalSeatsSold instance variable to figure out how many are available at any point during the program’s run time.

Page 3 of 7

private void printSeats() • Use nested for loops (Week 7 lecture) to print a diagram of the seats. Print a (V) for VIP seats and a (G) for General

Floor seats. Use the TOTAL_ROWS, SEATS_PER_ROW, and VIP_ROWS constants. • Follow the map with a print out of the number of VIP seats available and the number of General Floor seats

available. Use the vipSeatsAvailable and generalSeatsAvailable private helper methods. • BONUS: print an “x” instead of the (V) or (G) to indicate a seat that is sold.

private void printDashes(int num) • Use a for loop to print the number of dashes (-) based on the parameter num. (See the example ticket below to see

the what printed dashes does; if printDashes(50) is called, then the program loops to print 50 dashes in one row.)

private void printTicket(boolean isVip, int ticketsPurchased) • Call the printDashes method twice to print two lines of 40 dashes. • Print the concert name, followed by the venue name. (Include a tab character to indent the text) • If the isVip parameter is true, then print "VIP Tickets". In the next line, print the starting and ending seat numbers.

Use the vipSeatsSold instance variable and the ticketsPurchased parameter to calculate the seat numbers. (Note: printing seat numbers can be a little tricky. You will need to do a little problem-solving to figure out what seat numbers are sold. For example, if 4 vip seat are already sold, and somebody purchases 3 seats, then their seat numbers should be 5-7)

• If the isVip parameter is false, then print "\tGeneral Floor Tickets". In the next line, print the starting and ending seat numbers. Use the generalSeatsSoldinstance variable and the ticketsPurchased parameter to calculate them. (Similar thought process for seat numbers)

• Call the printDashes twice to print two lines of 40 dashes.

private void buyGeneralTickets() • If there are no general seats available, then print an appropriate message.

Page 4 of 7

• If there are general seats available, then tell the user how many seats are available and ask them how many seats they want to buy. Get their input. o If the user enters a number less than 1, then print an appropriate message. o If the user enters a number greater than the seats available, then print an appropriate message. o If the user enters a valid number, then call the appropriate code to update (“sell”) that number of general seats

and call the printTicket method with the appropriate parameters.

private void buyVipTickets() • If there are no vip seats available, then print an appropriate message. • If there are vip seats available, then tell the user how many seats are available and ask them how many seats they

want to buy. Get their input. o If the user enters a number less than 1, then print an appropriate message. o If the user enters a number greater than the seats available, then print an appropriate message. o If the user enters a valid number, then call the appropriate code to update (“sell”) that number of vip seats and

call the printTicket method with the appropriate parameters.

Page 5 of 7

You may create other helper methods if you want.

Page 6 of 7

Documentation Use Java doc comments before all methods to ensure good documentation of your code.

Code In Action (Sample Console Output) Use the previous and following screen shots to see test input and the corresponding results.

Deliverables • A compressed Assignment05-username folder containing two files (ConsoleMenu.java and TicketSystem.java). It

must be submitted through Blackboard. Here are the instructions for submission a) Navigate to your project folder. b) Create a compressed folder with all your code. c) Rename the zip file as follows:

assignment05_lastname_firstname d) Upload zip file to Blackboard site for our course.

Page 7 of 7

Grading Item Points ConsoleMenu.java: updating getUserChoice method 4

TicketSystem.java – constructor with 2 params 3 TicketSystem.java – default constructor 3 run method 10 printSeats 6 vipSeatsAvailable 2 generalSeatsAvailable 2 printDashes 4 printTicket 4 buyGeneralTickets 4 buyVipTickets 4 Good documentation 2 Overall style and proper submission 2 TOTAL 50 Bonus: printing out sold seats as “X” 5