Some students from the college of business have decided that for their next big
ID: 3597855 • Letter: S
Question
Some students from the college of business have decided that for their next big money making scheme, they are going to open a computer science themed amusement park called 7 flags, "--more --flags --more --CSpuns". With rides like Kingda Knuth, Blue Scream of Death, i386 Tower of Terror, and big spinning wheel GeForce forecasted to be very popular, the entrepreneurs have decided to leverage technology to make waiting time more bearable (and allow visitors to spend time at the concession stands buying overpriced ice cream). They propose a system called the "Flash Drive Pass" which allows a visitor to book a ride on one of the park's amusements on their smartphone. Once the visitor has booked a ride, they are placed in a virtual line (hint… it's a Queue), where they wait until it's their turn. Once they come to the front of the Virtual Queue, they are placed into a limited-size holding queue and notified that they have to show up in person for the ride. Once they show up in person, they have around 5 minutes to wait until the ride is available. More wealthy customers will be able to make especially good use of the system because, for a fee, a customer will be able to simultaneously be in up to 2 (silver plan) or 3 (gold plan) different queues at once. If a customer is currently on a ride or in another holding queue when it is their turn to be dequeued, they are simply placed at the back of the queue that they were going to be dequeued from. You will be implementing a simulation of the amusement park's queue system so that the entrepreneurs can fine-tune their system.
Simulation details: we will assume that every customer will always be in as many virtual lines as possible (this means that regular customers join a line the second they get off a ride, and Gold and Silver customers are always on the maximum number of lines possible). A customer is considered "on a virtual line" until he/she has entered the holding queue (akin to physically showing up for a ride). The customer is then holding or on the ride, and will not be available until they are finished with the ride. A person's status should indicate if they are simply waiting on a virtual line (available) or on a given ride (on ride or holding). Once a customer is available, they choose with equal probability (if you do extra credit, this may change) one of the four rides to put themselves on a virtual line for (for gold and silver customers, it is possible they may want to take a ride multiple times, and therefore reserve multiple spots on the virtual line). In order to determine how big the rides should be and how big the holding queues should be, you will be creating a simulation where these parameters can be modified, and the number of customers of each type that are attending on a given day. If a Virtual Line is empty, then a user that reserves for a ride will get placed straight on the Holding Queue (unless the user is currently in another holding queue or ride), and if that is empty, then the user can go straight onto the ride. If a customer shows up to a ride partway through the ride sequence, they should not be let on until after the current ride has finished. We can imagine that loading and unloading the rides is instant (so when one ride terminates, a new one begins with new customers on board).
When you write your simulation, you should follow the following steps: Assign the gold customers their first ride, Assign silver customers their first ride, assign regular customers their ride. Then, assign gold a second ride, silver a second ride, and gold a third ride. Now there should be customers on rides, in Holding Queues, and in Virtual Lines and you can start the simulation. For each timestep of the simulation, update the time remaining for each ride. If the ride is over, remove the people from the ride, reset their status to available, and then assign them to a new line. Next, dequeue new riders from the ride's holding queue, change their status to onRide, and add them to the peopleOnRide list(if none available, look in the Virtual Line, but only add available customers, send all others to the back of the Virtual Line). Start the ride (reset the time) even if the ride is not full (or even if it's fully empty!).
After filling and starting the ride, try to dequeue riders from the Virtual Line. If they are not available, send them to the back of the line. Else, put them in the Holding Queue until either the Virtual Line is empty or the Holding Queue is full. Repeat this for all rides.
Required Classes
Status
Write a fully-documented enum named Status, which holds the status of a person. The possible values for each status are “Available”, “Holding” and “OnRide”
Enums are useful for switch statements, as well as for comparing and associating values with known categories.
Person
Write a fully-documented class named Person which contains an integer id, the max amount of lines they can be on, array or a list of the current lines that they are on and the current status of the person (an enum). This class must contain methods that allow the user to manipulate the instance variables (getters, setters, etc.)
private int number
private int maxLines
The maximum number of lines the Person can be on.
List<Ride> or Ride[] lines
Holds an instance of each ride the person is on.
enum Status
public Person(int number)
Default constructor of the Person class.
Preconditions
The number must be positive.
Throws
IllegalArgumentException if the number is not positive
VirtualLine
Write a fully-documented VirtualLine class which models a Queue. It must extend a class from the Java API (preferably LinkedList but anything can be used as long as it works). This class should contain the basic methods that allow you to enqueue or dequeue onto the line. Also there should be some sort of peek and isEmpty method that can tell other parts of the program if the queue is empty and tell what the top element is. You may use other java api methods as well if you find them advantageous.
public void enqueue(Person p)
Adds the specified Person to the rear of the VirtualLine
public Person dequeue()
Removes the Person at the front of the VirtualLine.
public Person peek()
Returns the student at the front of the VirtualLine without removing them from the Queue.
public boolean isEmpty()//THIS MUST BE INHERITED FROM THE JAVA API. DO NOT WRITE IT YOURSELF!
Returns true if the VirtualLine contains no people, returns false otherwise.
HoldingQueue
Write a fully-documented HoldingQueue class which models a Queue, and extends VirtualLine. This class will serve as the group of people who are up next for a ride. The difference between it and VirtualLine is that there is a limit to how many People can be in the HoldingQueue (maxSize). Your class should extend VirtualLine, which extends a class from the Java API (preferably LinkedList but anything can be used as long as it works). This class should contain the basic methods that allow you to enqueue or dequeue onto the line. Also there should be some sort of peek and isEmpty method that can tell other parts of the program if the queue is empty and tell what the top element is. There must be getters and setters for any instance variables that are required.
private int maxSize
This is the max size of the line at once.
Methods inherited from VirtualLine
public void enqueue(Person p)
public Person dequeue()
public Person peek()
public boolean isEmpty()
Ride
Write a fully-documented Ride object which will contain the duration of the ride in minutes, an instance of the VirtualLine Object, an instance of the HoldingQueue Object, a list or an array of all the people on the ride and methods to manipulate these methods (getters, setters, enqueue, dequeue)
private int duration
How many time steps the ride takes up.
private int timeLeft
How many minutes are left until the end of the ride cycle
private string Name
The name of the ride; used for printing
VirtualLine virtualLine
The Queue of people waiting for the ride.
HoldingQueue holdingQueue
The Queue of people who are next for the ride.
List<Person> or Person[ ] peopleOnRide
Changes person’s status to “onRide”, and “Available” after the ride is over.
Has a capacity specified at the start of of the simulation
SevenFlags
Write a fully-documented driver class called SevenFlags which runs the simulation. You will need to have four rides: Blue Scream of Death (BSOD), i386 Tower of Terror (ToT), GeForce (GF) and Kingda Knuth (KK). After getting input from the user, the simulation should print a minute-by minute breakdown as in the sample I/O. There is no need to sleep after each minute, the simulation may all be printed at once.
RandomGenerator
You must write a fully documented class that randomly selects a ride.
public static Ride selectRide(Ride[] rides)
This selects an element of the array passed in with equal probability for each one.
public static Ride selectRide(Ride[] rides, double[] probabilities) //Extra Credit
This selects a ride from the array of rides, the array passed in must give the probability for each ride, and they must add to 1 in total (ie: [.5, .2, .2, .1])
Explanation / Answer
Answer: See the code below:
1. Status enum: Status.java
--------------------------------------------
package sevenflags;
/**
* Status enum
*
*/
public enum Status {
Available,
Holding,
OnRide
}
-----------------------------------------
2. Person class: Person.java
--------------------------------------------
package sevenflags;
import java.util.ArrayList;
import java.util.List;
/**
* Person class
*
*/
public class Person {
private int number;
private int maxLines; //The maximum number of lines the Person can be on.
private List<Ride> lines; //Holds an instance of each ride the person is on.
private Status status; //current status
/**
* Default constructor
*/
public Person(int number) {
if (number < 0)
throw new IllegalArgumentException("Wrong value!");
else
{
this.number=number;
this.maxLines=10;
this.lines=new ArrayList<Ride>(maxLines);
this.status=Status.Available; //default status
}
}
/**
* @return the number
*/
public int getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(int number) {
this.number = number;
}
/**
* @return the maxLines
*/
public int getMaxLines() {
return maxLines;
}
/**
* @param maxLines the maxLines to set
*/
public void setMaxLines(int maxLines) {
this.maxLines = maxLines;
}
/**
* @return the lines
*/
public List<Ride> getLines() {
return lines;
}
/**
* @param lines the lines to set
*/
public void setLines(List<Ride> lines) {
this.lines = lines;
}
/**
* @return the status
*/
public Status getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(Status status) {
this.status = status;
}
}
---------------------------------------------------------------------
3. VirtualLine class: VirtualLine.java
-----------------------------------------------------------------
package sevenflags;
import java.util.LinkedList;
/**
* VirtualLine class
*
*/
public class VirtualLine extends LinkedList<Person>{
/**
* serial version id
*/
private static final long serialVersionUID = 1L;
/**
* Default constructor
*/
public VirtualLine() {
// TODO Auto-generated constructor stub
super();
}
/**
* Adds the specified Person to the rear of the VirtualLine
* @param p
*/
public void enqueue(Person p)
{
add(p);
}
/**
* Removes the Person at the front of the VirtualLine.
* @return
*/
public Person dequeue()
{
if(!isEmpty())
return removeFirst();
else
return null;
}
/**
* Returns the student at the front of the VirtualLine without removing them from the Queue.
*/
public Person peek()
{
if(!isEmpty())
return peekFirst();
else
return null;
}
/**
* Returns true if the VirtualLine contains no people, returns false otherwise.
*/
public boolean isEmpty()
{
return isEmpty();
}
}
------------------------------------------------------------
4. HoldingQueue class: HoldingQueue.java
----------------------------------------------------
package sevenflags;
/**
* HoldingQueue class
*
*/
public class HoldingQueue extends VirtualLine {
/**
* serial version id
*/
private static final long serialVersionUID = 1L;
private int maxSize; //This is the max size of the line at once.
//private int personsInQueue; //number of persons currently in queue
/**
* Default constructor
*/
public HoldingQueue(int maxSize) {
super();
this.maxSize=maxSize;
}
/* (non-Javadoc)
* @see sevenflags.VirtualLine#enqueue(sevenflags.Person)
*/
@Override
public void enqueue(Person p) {
// TODO Auto-generated method stub
if(size()<=maxSize)
super.enqueue(p);
else
System.out.println("Out of slots!!!");
}
/* (non-Javadoc)
* @see sevenflags.VirtualLine#dequeue()
*/
@Override
public Person dequeue() {
// TODO Auto-generated method stub
return super.dequeue();
}
/* (non-Javadoc)
* @see sevenflags.VirtualLine#peek()
*/
@Override
public Person peek() {
// TODO Auto-generated method stub
return super.peek();
}
/* (non-Javadoc)
* @see sevenflags.VirtualLine#isEmpty()
*/
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return super.isEmpty();
}
/**
* @return the maxSize
*/
public int getMaxSize() {
return maxSize;
}
/**
* @param maxSize the maxSize to set
*/
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
}
-------------------------------------------------------------
5. Ride class: Ride.java
-----------------------------------------------------------------
package sevenflags;
import java.util.LinkedList;
import java.util.List;
/**
* Ride class
*
*/
public class Ride {
private int duration; //How many time steps the ride takes up.
private int timeLeft; //How many minutes are left until the end of the ride cycle.
private String name; //The name of the ride; used for printing
private int capacity; //capacity of ride
private int maxHoldingSize; //max size for holding queue
private VirtualLine virtualLine; //The Queue of people waiting for the ride.
private HoldingQueue holdingQueue; //The Queue of people who are next for the ride.
private List<Person> peopleOnRide; //people on ride
/**
* @param name
*/
public Ride(String name) {
this.name = name;
this.duration=0;
this.timeLeft=duration;
this.capacity=0;
this.maxHoldingSize=0;
this.virtualLine=null;
this.holdingQueue=null;
this.peopleOnRide=null;
}
/**
* @param duration
* @param name
* @param capacity
*/
public Ride(int duration, String name, int capacity, int maxHoldingSize) {
this.duration = duration;
this.name = name;
this.capacity = capacity;
this.maxHoldingSize=maxHoldingSize;
this.timeLeft=duration;
this.virtualLine=new VirtualLine();
this.holdingQueue=new HoldingQueue(maxHoldingSize);
this.peopleOnRide=new LinkedList<Person>();
}
/**
* @return the duration
*/
public int getDuration() {
return duration;
}
/**
* @param duration the duration to set
*/
public void setDuration(int duration) {
this.duration = duration;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the capacity
*/
public int getCapacity() {
return capacity;
}
/**
* @param capacity the capacity to set
*/
public void setCapacity(int capacity) {
this.capacity = capacity;
}
/**
* @return the maxHoldingSize
*/
public int getMaxHoldingSize() {
return maxHoldingSize;
}
/**
* @param maxHoldingSize the maxHoldingSize to set
*/
public void setMaxHoldingSize(int maxHoldingSize) {
this.maxHoldingSize = maxHoldingSize;
}
/**
* @return the timeLeft
*/
public int getTimeLeft() {
return timeLeft;
}
/**
* @return the virtualLine
*/
public VirtualLine getVirtualLine() {
return virtualLine;
}
/**
* @return the holdingQueue
*/
public HoldingQueue getHoldingQueue() {
return holdingQueue;
}
/**
* @return the peopleOnRide
*/
public List<Person> getPeopleOnRide() {
return peopleOnRide;
}
/**
* @param virtualLine the virtualLine to set
*/
public void setVirtualLine(VirtualLine virtualLine) {
this.virtualLine = virtualLine;
}
/**
* @param holdingQueue the holdingQueue to set
*/
public void setHoldingQueue(HoldingQueue holdingQueue) {
this.holdingQueue = holdingQueue;
}
/**
* @param peopleOnRide the peopleOnRide to set
*/
public void setPeopleOnRide(List<Person> peopleOnRide) {
this.peopleOnRide = peopleOnRide;
}
/**
* adds a person to virtual line for ride
* @param p
*/
public void enqueueToVirtualLine(Person p)
{
virtualLine.enqueue(p);
}
/**
* removes a person from virtual line of ride
* @return
*/
public Person dequeueToVirtualLine()
{
return virtualLine.dequeue();
}
/**
* adds a person to holding queue for ride
* @param p
*/
public void enqueueToHoldingQueue(Person p)
{
holdingQueue.enqueue(p);
}
/**
* removes a person from holding queue of ride
* @return
*/
public Person dequeueToHoldingQueue()
{
return holdingQueue.dequeue();
}
/**
* adds a person to holding queue for ride
* @param p
*/
public void enqueueToRide(Person p)
{
if(peopleOnRide.size() <= capacity)
{
peopleOnRide.add(p);
p.setStatus(Status.OnRide);
}
else
System.out.println("Ride full!");
}
/**
* removes a person from holding queue of ride
* @return
*/
public Person dequeueToRide()
{
if(!peopleOnRide.isEmpty())
{
Person p = peopleOnRide.remove(0);
p.setStatus(Status.Available);
return p;
}
else
{
System.out.println("Ride empty!");
return null;
}
}
}
----------------------------------------------------------------
6. RandomGenerator class: RandomGenerator.java
--------------------------------------------------------------
package sevenflags;
/**
* RandomGenerator class
*
*/
public class RandomGenerator {
/**
* This selects an element of the array passed in with equal probability for each one.
* @param rides
* @return
*/
public Ride selectRide(Ride[] rides)
{
int totalRides=rides.length;
int selectedRide=(int)(Math.random()*totalRides+1);
return rides[selectedRide];
}
}
---------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.