public class WaitLine
1: /** Simulates a waiting line.
2: @author Frank M. Carrano
3: @author Timothy M. Henry
4: @version 4.0
5: */
6: public class WaitLine
7: {
8: private QueueInterface<Customer> line;
9: private int numberOfArrivals;
10: private int numberServed;
11: private int totalTimeWaited;
12:
13: public WaitLine()
14: {
15: line = new LinkedQueue<>();
16: reset();
17: } // end default constructor
18:
19: /** Simulates a waiting line with one serving agent.
20: @param duration The number of simulated minutes
21: @param arrivalProbability A real number between 0 and 1, and the
22: probability that a customer arrives at
23: a given time
24: @param maxTransactionTime the longest transaction time for a
25: customer */
26: public void simulate(int duration, double arrivalProbability,
27: int maxTransactionTime)
28: {
29: int transactionTimeLeft = 0;
30:
31: for (int clock = 0; clock < duration; clock++)
32: {
33: if (Math.random() < arrivalProbability)
34: {
35: numberOfArrivals++;
36: int transactionTime = (int)(Math.random() * maxTransactionTime + 1);
37: Customer nextArrival = new Customer(clock, transactionTime,
38: numberOfArrivals);
39: line.enqueue(nextArrival);
40: System.out.println("Customer " + numberOfArrivals +
41: " enters line at time " + clock +
42: ". Transaction time is " + transactionTime);
43: } // end if
44:
45: if (transactionTimeLeft > 0)
46: transactionTimeLeft--;
47: else if (!line.isEmpty())
48: {
49: Customer nextCustomer = line.dequeue();
50: transactionTimeLeft = nextCustomer.getTransactionTime() - 1;
51: int timeWaited = clock - nextCustomer.getArrivalTime();
52: totalTimeWaited = totalTimeWaited + timeWaited;
53: numberServed++;
54: System.out.println("Customer " + nextCustomer.getCustomerNumber() +
55: " begins service at time " + clock +
56: ". Time waited is " + timeWaited);
57: } // end if
58: } // end for
59: } // end simulate
60:
61: /** Displays summary results of the simulation. */
62: public void displayResults()
63: {
64: System.out.println();
65: System.out.println("Number served = " + numberServed);
66: System.out.println("Total time waited = " + totalTimeWaited);
67: double averageTimeWaited = ((double)totalTimeWaited) / numberServed;
68: System.out.println("Average time waited = " + averageTimeWaited);
69: int leftInLine = numberOfArrivals - numberServed;
70: System.out.println("Number left in line = " + leftInLine);
71: } // end displayResults
72:
73: /** Initializes the simulation. */
74: public final void reset()
75: {
76: line.clear();
77: numberOfArrivals = 0;
78: numberServed = 0;
79: totalTimeWaited = 0;
80: } // end reset
81: } // end WaitLine