HOMEWORK - FALL 2023
HOMEWORK 3 - Monday, October 23rd by 7PM (China time).
REMINDERS:
- Be sure your code follows the coding style for CSE214.
- Make sure you read the warnings about academic dishonesty. Remember, all work you submit for homework or exams MUST be your own work.
- Login to your grading account and click "Submit Assignment" to upload and submit your assignment.
- You may use any Java API class that you wish.
- You may use Scanner, InputStreamReader, or any other class that you wish for keyboard input.
In some theme-parks, visitors, rather than waiting in lines, pick up a "fast-pass" for a certain ride at a certain time to allow them to do other things while they wait. In this assignment, you will be asked to write the fast-pass system for a theme park.
The theme park will simultaneously keep track of which rides it has (via RideInfo objects), and who it has as visitors (via VisitorInfo objects). Each ride should keep track of its own fast-pass line (using a linked-list-based queue), and each visitor should keep track of their own fast-pass status. Every visitor is allowed only one fast-pass at a time.
The queues will be implemented by encapsulating a VisitorInfo object within a VisitorInfoNode object. The VisitorInfoNodes will then be linked together in the RideInfo class.
1. Write a fully documented class named VisitorInfo with the instance variables described below:
- private String name
The name of the visitor. - private boolean hasAFastPass
The fast-pass status of the visitor. Set to true if the visitor already has a fast pass for a ride, false otherwise. Should initialize to false.
- Mutator and Accessor methods for all instance variables.
- public boolean equals(Object obj)
Checks if the given Object is equal to the current VisitorInfo instance. Note that obj may not necessarily be an instance of VisitorInfo.
2. Write a fully documented class named VisitorInfoNode with the instance variables described below:
- private VisitorInfo data
An instance of VisitorInfo, which describes the visitor. - private VisitorInfoNode link
A link to the next VisitorInfoNode in the list.
- The usual getData/setData and getLink/setLink methods.
3. Write a fully documented class named RideInfo that implements a queue using a singly-linked-list of VisitorInfoNode's. RideInfo should include the following instance variables:
- private VisitorInfoNode front
The head of the linked list - private VisitorInfoNode rear
The tail of the linked list - private String name
The name of the ride - private int capacity
The capacity of the ride
- A constructor that takes in the name and capacity of the RideInfo instance.
- public VisitorInfoNode dequeue() throws EmptyQueueException
Dequeues VisitorInfoNode from the queue. Should be O(1). Throws a EmptyQueueException if the queue is empty - public void enqueue(VisitorInfoNode enqueueMe) throws FullQueueException
Enqueues VisitorInfoNode in the queue. Should be O(1). Throws a FullQueueException if the queue is full (i.e. there are capacity number of nodes). - public String toString()
Should return a String in the following format: RideName: passenger1name, passenger2name, ... , passengerNname - public int size()
Returns the size of the queue ie. the number of people waiting for the ride. You may create an instance variable that keeps track of the size of the queue, or compute the size manually every time. - public boolean equals(Object obj)
Checks if the given Object is equal to the current RideInfo instance. Note that obj may not necessarily be an instance of RideInfo; if it is, you do not need to compare the queues. - Mutator and Accessor methods for all instance variables.
4. Write a fully documented class named FastPassHandler with instance variables described below:
- private List<VisitorInfo> visitorList = new ArrayList<VisitorInfo>()
The list of passengers. - private List<RideInfo> rideList = new ArrayList<RideInfo>()
The list of rides.
- public void createRide(String rideName, int rideCapacity)
Creates a new RideInfo instance with the given name and capacity, and inserts it into rideList. - public void createVisitor(String visitorName)
Creates a new VisitorInfo instance with the given name, and inserts it into visitorList. - public void getFastPass(String visitorName, String rideName)
throws AlreadyHasFastPassException, FullQueueException, InvalidArgumentException
Gets a fast-pass for the given visitor for the given ride. This method should first check if visitorName is the name of a visitor in visitorList - if it's not, a new visitorInfo instance should be created and inserted into the list; if it is, the visitorInfo instance should be retrieved from visitorList. If the visitor already has a fast-pass (ie. hasAFastPass==true), an AlreadyHasFastPassException should be thrown. Also, if the ride does not exist, an InvalidArgumentExceptionexception should be thrown. Otherwise, set that visitor's hasAFastPass to true, and insert that visitor's visitorInfo instance into the appropriate RideInfo instance, taken from rideList. - public void allowVisitorsToTakeRide(String rideName, int numVisitors) throws InvalidArgumentException
Allows a number of visitors to take a ride. If the rideName doesn't exist, throw an IllegalArgumentException. This method should dequeue the given number of visitors from the RideInfo object, setting each VisitorInfo's hasAFastPass to false. numVisitors should be between 1 and the number of people waiting for the ride, inclusive.
5. Write a driver class named ThemePark. This class will contain a static FastPassHandler object, and a static main method which displays the menu and accepts the following commands:
- F (Get a Fast-pass)
Get a fast-pass for a visitor. Input: Name of visitor; Name of ride . - L (Print the list of visitors)
Print the list of visitors in the ThemePark, along with their fast-pass status. - P (Print the list of rides)
Print the list of rides in the ThemePark. - Q (Quit Program)
Quit program - R (Add a Ride)
Add a ride to the ThemePark. Input: Name of ride; Capacity of the ride - T (Take Ride)
Allow a number of visitors to take a ride. The number should be generated randomly, from 1 to the number of visitors waiting for the ride. Input: Name of ride - V (Print the list of visitors for a given ride)
Print the list of visitors waiting for a given ride. Input: Name of ride
Exceptions
Following exceptions are recommended, but not required. You can create your own exception class(es) if you want. Just make sure that you document all the exceptions that you use.
- AlreadyHasFastPassException
Thrown when a visitor who already has a fast-pass tries to obtain another one. - FullQueueException
Thrown when the queue is full. - EmptyQueueException
Thrown when a queue is empty.
INPUT FORMAT
- All names and menu operations are case-insensitive. You may use the equalsIgnoreCase() method of the String class for comparisons.
- USE THE INPUT COMMANDS SHOWN ABOVE for accessing menu options. Do not make up your own commands, such as using numbers for menu options. You will lose points if you do.
- You must check for invalid menu options.
- You may assume capacity is a valid number greater than 0.
- Visitor's name is a string no longer than 25 characters. You are not required to validate the size. The visitor's name may or may not contain spaces.
- RideName is a string no longer than 25 characters. It won't contain spaces, but it may contain special characters indicating the time that the ride will be taken (ie. FreeFall-11:00 and FreeFall-11:15 should have separate queues). You are not required to validate the size.
- Since, according to the specifications, two different people with the same name will be considered the same person, we will not be testing how your program handles duplicate names.
OUTPUT FORMAT
- Be sure your prompts and error messages for the user are clear and understandable.
- It is NOT required to sort either of the lists. They can be displayed in any order.
- Be sure to have display the menu after each operation.
SAMPLE INPUT/OUTPUT
Blue text is computer output. Black text is User input. Green text is comments.