Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram help: Grocery Line simulation in Java
    primarykey
    data
    text
    <p>I have a fairly complex (well, for me it is) program that is supposed to simulate 5 lines in a grocery store. A person goes up to the line and finds one that is empty and then proceeds to checkout. If all of the lines are full, the person joins the line with the shortest wait time. This program has been adapted from another one that was in my book (thus all of the comments). I was able to get this to work with just 2 lines but with 5 I am unable to get it to work correctly.</p> <pre><code>// FILE: Carwash.java // This program illustrates the use of the lineSimulate method which uses // a simple queue to simulate cars waiting at a car wash. 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; /****************************************************************************** * The &lt;CODE&gt;CarWash&lt;/CODE&gt; Java application illustrates the use of * the &lt;CODE&gt;lineSimulate&lt;/CODE&gt; method. * The illustration uses the following values: * &lt;CODE&gt; * &lt;br&gt;lineTime = 240 * &lt;br&gt;arrivalTime = 0.0025 * &lt;br&gt;totalTime = 6000 * &lt;/CODE&gt; * * &lt;p&gt;&lt;dt&gt;&lt;b&gt;Java Source Code for this class:&lt;/b&gt;&lt;dd&gt; * &lt;A HREF="../applications/CarWash.java"&gt; * http://www.cs.colorado.edu/~main/applications/CarWash.java * &lt;/A&gt; * * @author Michael Main * &lt;A HREF="mailto:main@colorado.edu"&gt; (main@colorado.edu) &lt;/A&gt; * * @version * Jun 12, 1998 ******************************************************************************/ public class LineWait { /** * The main method activates &lt;CODE&gt;lineSimulate&lt;/CODE&gt; with the values: * &lt;CODE&gt; * &lt;br&gt;lineTime = 240 * &lt;br&gt;arrivalTime = 0.0025 * &lt;br&gt;totalTime = 6000 * &lt;/CODE&gt; * &lt;BR&gt;The &lt;CODE&gt;String&lt;/CODE&gt; argument (&lt;CODE&gt;args&lt;/CODE&gt;) is not used in * this implementation. **/ 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); } /** * Simulate the running of a car washer for a specified amount of time. * @param &lt;CODE&gt;lineTime&lt;/CODE&gt; * the number of seconds required to wash one car * @param &lt;CODE&gt;arrivalProb&lt;/CODE&gt; * the probability of a customer arriving in any second, for example * 0.1 is 10% * @param &lt;CODE&gt;totalTime&lt;/CODE&gt; * the total number of seconds for the simulation * &lt;dt&gt;&lt;b&gt;Precondition:&lt;/b&gt;&lt;dd&gt; * &lt;CODE&gt;lineTime&lt;/CODE&gt; and &lt;CODE&gt;totalTime&lt;/CODE&gt; are positive; * &lt;CODE&gt;arrivalProb&lt;/CODE&gt; lies in the range 0 to 1. * &lt;dt&gt;&lt;b&gt;Postcondition:&lt;/b&gt;&lt;dd&gt; * The method has simulated a car wash where &lt;CODE&gt;lineTime&lt;/CODE&gt; is the * number of seconds needed to wash one car, &lt;CODE&gt;arrivalProb&lt;/CODE&gt; is * the probability of a customer arriving in any second, and * &lt;CODE&gt;totalTime&lt;/CODE&gt; is the total number of seconds for the * simulation. Before the simulation, the method has written its three * parameters to &lt;CODE&gt;System.out&lt;/CODE&gt;. After the simulation, the method * has written two pieces of information to &lt;CODE&gt;System.out&lt;/CODE&gt;: * (1) The number of cars washed, and (2) The average waiting time for * customers that had their cars washed. (Customers that are still in the * queue are not included in this average). * @exception java.lang.IllegalArgumentException * Indicates that one of the arguments violates the precondition. **/ public static void lineSimulate (int lineTime, double arrivalProb, int totalTime) { Queue&lt;Integer&gt; arrivalTimes = new LinkedList&lt;Integer&gt;( ); Queue&lt;Integer&gt; arrivalTimes2 = new LinkedList&lt;Integer&gt;( ); Queue&lt;Integer&gt; arrivalTimes3 = new LinkedList&lt;Integer&gt;( ); Queue&lt;Integer&gt; arrivalTimes4 = new LinkedList&lt;Integer&gt;( ); Queue&lt;Integer&gt; arrivalTimes5 = new LinkedList&lt;Integer&gt;( ); 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 &lt;= 0 || arrivalProb &lt; 0 || arrivalProb &gt; 1 || totalTime &lt; 0) throw new IllegalArgumentException("Values out of range"); //I BELIEVE THE PROBLEM IS BELOW THIS POINT for (currentSecond = 0; currentSecond &lt; totalTime; currentSecond++) { // Simulate the passage of one second of time. // Check whether a new customer has arrived. if (arrival.query( )) { //if(number.isBusy() &amp;&amp; number2.isBusy() &amp;&amp; number3.isBusy() &amp;&amp; number4.isBusy() &amp;&amp; number5.isBusy() ) //{ if(arrivalTimes.size() &gt; arrivalTimes2.size() &amp;&amp; arrivalTimes.size() &gt; arrivalTimes3.size() &amp;&amp; arrivalTimes.size() &gt; arrivalTimes4.size() &amp;&amp; arrivalTimes.size() &gt; arrivalTimes5.size()) { arrivalTimes.add(currentSecond); System.out.println("Test"); } else if(arrivalTimes2.size() &gt; arrivalTimes.size() &amp;&amp; arrivalTimes2.size() &gt; arrivalTimes3.size() &amp;&amp; arrivalTimes2.size() &gt; arrivalTimes4.size() &amp;&amp; arrivalTimes2.size() &gt; arrivalTimes5.size()) { arrivalTimes2.add(currentSecond); System.out.println("Test"); } else if(arrivalTimes3.size() &gt; arrivalTimes.size() &amp;&amp; arrivalTimes3.size() &gt; arrivalTimes2.size() &amp;&amp; arrivalTimes3.size() &gt; arrivalTimes4.size() &amp;&amp; arrivalTimes3.size() &gt; arrivalTimes5.size()) { arrivalTimes3.add(currentSecond); System.out.println("Test"); } else if(arrivalTimes4.size() &gt; arrivalTimes.size() &amp;&amp; arrivalTimes4.size() &gt; arrivalTimes3.size() &amp;&amp; arrivalTimes4.size() &gt; arrivalTimes2.size() &amp;&amp; arrivalTimes4.size() &gt; 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( )) &amp;&amp; (!arrivalTimes.isEmpty( ))) { next = arrivalTimes.remove( ); waitTimes.addNumber(currentSecond - next); number.startMoving( ); } if ((!number2.isBusy( )) &amp;&amp; (!arrivalTimes2.isEmpty( ))) { next = arrivalTimes2.remove( ); waitTimes2.addNumber(currentSecond - next); number2.startMoving( ); } if ((!number3.isBusy( )) &amp;&amp; (!arrivalTimes2.isEmpty( ))) { next = arrivalTimes2.remove( ); waitTimes3.addNumber(currentSecond - next); number3.startMoving( ); } if ((!number4.isBusy( )) &amp;&amp; (!arrivalTimes2.isEmpty( ))) { next = arrivalTimes2.remove( ); waitTimes4.addNumber(currentSecond - next); number4.startMoving( ); } if ((!number5.isBusy( )) &amp;&amp; (!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("\nCustomers served Line1: " + waitTimes.howManyNumbers( )); if (waitTimes.howManyNumbers( ) &gt; 0) System.out.println("Average wait Line1: " + waitTimes.average( ) + " sec"); System.out.println("\nCustomers served Line2: " + waitTimes2.howManyNumbers( )); if (waitTimes.howManyNumbers( ) &gt; 0) System.out.println("Average wait Line2: " + waitTimes2.average( ) + " sec"); System.out.println("\nCustomers served Line3: " + waitTimes3.howManyNumbers( )); if (waitTimes.howManyNumbers( ) &gt; 0) System.out.println("Average wait Line3: " + waitTimes3.average( ) + " sec"); System.out.println("\nCustomers served Line4: " + waitTimes4.howManyNumbers( )); if (waitTimes.howManyNumbers( ) &gt; 0) System.out.println("Average wait Line4: " + waitTimes4.average( ) + " sec"); System.out.println("\nCustomers served Line5: " + waitTimes5.howManyNumbers( )); if (waitTimes.howManyNumbers( ) &gt; 0) System.out.println("Average wait Line5: " + waitTimes5.average( ) + " sec"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload