Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this programming assignment, you are going to create a Ticket class that represents a Ticket for train travels from Chicagos Union Station to other

For this programming assignment, you are going to create a Ticket class that represents a Ticket for train travels from Chicagos Union Station to other cities. You will also create a Reservation class that will contain an array or collection of Ticket objects. Lastly, you will create a ReservationApp that manages train ticket sold.

Import the files:

Your first step will be to import the class file DataReader.java into your program 4 solution folder. You will also import the input data file, TicketData.txt, that have been provided for your programming assignment.

DataReader class

This class will be used to read an input file that has information for tickets already sold. The methods in this class are all static methods and should therefore be called in a static way. The ticketCount field stores the number of tickets already sold, and it also represents the number of Ticket objects stored in the Ticket array after the input file has been read.

Input data format (FYI only):

NOTE: Look at this class and see what it does.

Ticket class

Keeps track of the information for a ticket sold. This class should include the following:

Instance or class level fields

A field for first name

A field for last name

A field for reservation number

A field for destination city. Valid values for this field: Peoria, Normal, Joliet, Indianapolis, and St. Louis.

A field for credit card number

A field for credit card expiration date (format for date is mm/yyyy)

A field for credit card security code (number in the back of credit card number)

A field for price of the ticket

Methods

Constructor that accepts values for all instance fields

Getters for all instance fields

Setter for destination city and price

toString that formats ticket data as follows:

Klaus III, Mitchell Reservation number: 25532

Destination: INDIANAPOLIS Cost: $27.00

Credit card: xxxx-xxxx-xxxx-7777

Reservation class (will use aggregation)

The Reservation class will contain an array or collection of Ticket objects.

Instance or class level fields

An array of Ticket objects

A field for number of Ticket objects in the array

MethodsA default constructor.

DataReader class will be called from the default constructor.

Ticket array will get a reference to a partially filled array from method in DataReader that reads the input file into Ticket objects.

Set the count field with the value returned from the getter of the ticket count field in DataReader.

Getters for ticket count

A method to add a Ticket object to the array. This method accepts a Ticket object that will be added to the array.

A method that checks if there are still seats left on the plane by checking if there is room in the array to add another Ticket object. (Hint: which field and which attribute do you need to compare?)

A method to update a Ticket object. This method accepts reservation number and destination city (the only field that can be updated). The destination will be updated and the price will be updated using the value returned by the method that returns the price. If the reservation number is not found, display a message Reservation number entered is not found. Display the ticket information after a successful update.

A method that accepts the destination city and returns the price of the ticket. City and prices are as follows:

Peoria = $8.50

Normal = $12.75

Joliet = $9.77

Indianapolis = $24.25

St. Louis = $34

A method that accepts the reservation number and displays the information for the ticket with reservation number. If the reservation number is not found, display a message Reservation number entered is not found.

A method that displays the information for all the tickets in the array.

A private method that accepts the reservation number and searches the array for the Ticket object with the reservation number. This method returns the index of the Ticket object with the reservation number. Otherwise, it returns a -1.

ReservationApp class

This is the starting point for the application, which is the only class to contain a main method.

This will drive the menu driven application with the following menu options:

1 Buy a ticket

2 Update a ticket

3 Display ticket information

4 Display all ticket information

5 Quit

Options

Option 1 ask all the information needed for buying a ticket and add the Ticket object to the array. For reservation number, use the Random class to generate a random number with 5 digits and add C in front, ex. C12333.

Option 2 ask the user for the destination and the reservation number. Update the appropriate Ticket object in the array.

Option 3 Ask the user for the reservation number and display the information for the ticket with the reservation number

Option 4 display the information for all the tickets

Option 5 terminate

Display the menu every time a menu selection has been processed, whether valid or invalid menu choice is selected.

Required validation to be done

Validate the menu option entered. Display the message Invalid menu choice. Please select again. Re-display the menu and ask the user again to select an option.

Validate the credit card number entered and make sure that there are 16 digits. If not 16 digits, display message Invalid credit card number. and ask the user to enter another credit card number. Keep asking the user for new credit card number as long as entered value is invalid.

Here is DataReader.java

package edu.ilstu; import java.io.File; import java.io.FileNotFoundException; import java.util.Random; import java.util.Scanner; /** * This class reads the input file with ticke data - first name, last name, destination, reservation number, and * creadit card information. * * @author Patricia Matsuda * */ public class DataReader { private static int ticketCount; public static Ticket[] readInputFile(String filename) { Ticket[] ticketArray = new Ticket[50]; Scanner reader = null; String ccNumber, expDate, securityCode, destination, firstName, lastName,reservationNumber; double price; try { reader = new Scanner(new File(filename)); } catch (FileNotFoundException e) { System.out.println("File not found."); System.exit(1); } while(reader.hasNext()) { reservationNumber = reader.next(); reader.nextLine(); firstName = reader.nextLine(); lastName = reader.nextLine(); destination = reader.nextLine(); price = reader.nextDouble(); ccNumber = reader.next(); expDate = reader.next(); securityCode = reader.next(); ticketArray[ticketCount] = new Ticket(ccNumber, expDate, securityCode, destination, price, firstName, lastName, reservationNumber); ticketCount++; } return ticketArray; } /** * Returns the number of ticket already sold * * @return the number of tickets sold */ public static int getTicketCount() { return ticketCount; } }

Here is TicketData.txt

C1234 Annabelle Holland Peoria 8.50 1234567891234567 11/2018 253 C2553 Mitchell Klaus III Indianapolis 27.0 2222333355557777 08/2020 323 C2224 Mary Lee Samuelson st. louis 12.55 4545636389897555 02/2019 222

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions