Write a program that simulates a checkout line at a supermarket. The line will s
ID: 639684 • Letter: W
Question
Write a program that simulates a checkout line at a supermarket. The line will simply be a counter, indicating the number of people in that line. On average, a customer will need to get in line for check out every two minutes. The customer will go to the shortest line. On average, each customer will require five minutes to check out.
So with each one-minute cycle, a random number 0 or 1 will be generated. If it is a zero, nothing happens. If it is a 1, the shortest line will have a customer added to the line. (A customer comes to check out, on average, every two minutes.) Also, a second random number from 0 through 4 will be generated for each line that has at least one customer. (A customer checks out from each line, on average, every five minutes.) If it is a 0 through 3, nothing will happen. If it is a 4, the count of customers in the line will decrease by 1.
Run your program with three checkout lines for 720 minutes (12 hours), and answer each of the following:
a) What is the maximum number of customers in any line at any time?
b) What is the average line length?
c) How many minutes is each line empty?
Vary the number of checkout lines. What do you believe would be the optimal number of lines? Why? What are the values associated with all test variations?
Run your simulation (using a for loop) 50 times.
At the end of the run, display on the screen three values:
Explanation / Answer
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
// import edu.colorado.simulations.BooleanSource;
// import edu.colorado.simulations.Line ;
// import edu.colorado.simulations.Averager;
******************************************************************************/
public class LineWait
{
public static void main(String[ ] args)
{
final int LINETIME = 100 + (int)(Math.random() * ((400) + 1));
final double ARRIVALPROB = (Math.random() * ((.1) + 1));
final int TOTALTIME = 6000;
lineSimulate(LINETIME, ARRIVALPROB, TOTALTIME);
}
public static void lineSimulate
(int lineTime, double arrivalProb, int totalTime)
{
Queue<Integer> arrivalTimes = new LinkedList<Integer>( );
Queue<Integer> arrivalTimes2 = new LinkedList<Integer>( );
Queue<Integer> arrivalTimes3 = new LinkedList<Integer>( );
Queue<Integer> arrivalTimes4 = new LinkedList<Integer>( );
Queue<Integer> arrivalTimes5 = new LinkedList<Integer>( );
int next;
BooleanSource arrival = new BooleanSource(arrivalProb);
Line number = new Line(lineTime);
Line number2 = new Line(lineTime);
Line number3 = new Line(lineTime);
Line number4 = new Line(lineTime);
Line number5 = new Line(lineTime);
Averager waitTimes = new Averager( );
Averager waitTimes2 = new Averager();
Averager waitTimes3 = new Averager();
Averager waitTimes4 = new Averager();
Averager waitTimes5 = new Averager();
int currentSecond;
// Write the parameters to System.out.
System.out.println("Seconds to wait in line " + lineTime);
System.out.print("Probability of customer arrival during a second: ");
System.out.println(arrivalProb);
System.out.println("Total simulation seconds: " + totalTime);
// Check the precondition:
if (lineTime <= 0 || arrivalProb < 0 || arrivalProb > 1 || totalTime < 0)
throw new IllegalArgumentException("Values out of range");
//I BELIEVE THE PROBLEM IS BELOW THIS POINT
for (currentSecond = 0; currentSecond < totalTime; currentSecond++)
{ // Simulate the passage of one second of time.
// Check whether a new customer has arrived.
if (arrival.query( ))
{
//if(number.isBusy() && number2.isBusy() && number3.isBusy() && number4.isBusy() && number5.isBusy() )
//{
if(arrivalTimes.size() > arrivalTimes2.size() && arrivalTimes.size() > arrivalTimes3.size() && arrivalTimes.size() > arrivalTimes4.size() && arrivalTimes.size() > arrivalTimes5.size())
{
arrivalTimes.add(currentSecond);
System.out.println("Test");
}
else if(arrivalTimes2.size() > arrivalTimes.size() && arrivalTimes2.size() > arrivalTimes3.size() && arrivalTimes2.size() > arrivalTimes4.size() && arrivalTimes2.size() > arrivalTimes5.size())
{
arrivalTimes2.add(currentSecond);
System.out.println("Test");
}
else if(arrivalTimes3.size() > arrivalTimes.size() && arrivalTimes3.size() > arrivalTimes2.size() && arrivalTimes3.size() > arrivalTimes4.size() && arrivalTimes3.size() > arrivalTimes5.size())
{
arrivalTimes3.add(currentSecond);
System.out.println("Test");
}
else if(arrivalTimes4.size() > arrivalTimes.size() && arrivalTimes4.size() > arrivalTimes3.size() && arrivalTimes4.size() > arrivalTimes2.size() && arrivalTimes4.size() > arrivalTimes5.size())
{
arrivalTimes4.add(currentSecond);
System.out.println("Test");
}
else{arrivalTimes5.add(currentSecond);}
//}
}
// Check whether we can put the person into a line.
if ((!number.isBusy( )) && (!arrivalTimes.isEmpty( )))
{
next = arrivalTimes.remove( );
waitTimes.addNumber(currentSecond - next);
number.startMoving( );
}
if ((!number2.isBusy( )) && (!arrivalTimes2.isEmpty( )))
{
next = arrivalTimes2.remove( );
waitTimes2.addNumber(currentSecond - next);
number2.startMoving( );
}
if ((!number3.isBusy( )) && (!arrivalTimes2.isEmpty( )))
{
next = arrivalTimes2.remove( );
waitTimes3.addNumber(currentSecond - next);
number3.startMoving( );
}
if ((!number4.isBusy( )) && (!arrivalTimes2.isEmpty( )))
{
next = arrivalTimes2.remove( );
waitTimes4.addNumber(currentSecond - next);
number4.startMoving( );
}
if ((!number5.isBusy( )) && (!arrivalTimes2.isEmpty( )))
{
next = arrivalTimes2.remove( );
waitTimes5.addNumber(currentSecond - next);
number5.startMoving( );
}
// Subtract one second from the remaining time in the current li
number.reduceRemainingTime( );
number2.reduceRemainingTime( );
number3.reduceRemainingTime( );
number4.reduceRemainingTime( );
number5.reduceRemainingTime( );
}
//I BELIEVE THE PROBLEM IS ABOVE THIS POINT
// Write the summary information about the simulation.
System.out.println(" Customers served Line1: " + waitTimes.howManyNumbers( ));
if (waitTimes.howManyNumbers( ) > 0)
System.out.println("Average wait Line1: " + waitTimes.average( ) + " sec");
System.out.println(" Customers served Line2: " + waitTimes2.howManyNumbers( ));
if (waitTimes.howManyNumbers( ) > 0)
System.out.println("Average wait Line2: " + waitTimes2.average( ) + " sec");
System.out.println(" Customers served Line3: " + waitTimes3.howManyNumbers( ));
if (waitTimes.howManyNumbers( ) > 0)
System.out.println("Average wait Line3: " + waitTimes3.average( ) + " sec");
System.out.println(" Customers served Line4: " + waitTimes4.howManyNumbers( ));
if (waitTimes.howManyNumbers( ) > 0)
System.out.println("Average wait Line4: " + waitTimes4.average( ) + " sec");
System.out.println(" Customers served Line5: " + waitTimes5.howManyNumbers( ));
if (waitTimes.howManyNumbers( ) > 0)
System.out.println("Average wait Line5: " + waitTimes5.average( ) + " sec");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.