Java Reference
In-Depth Information
An implementation of the class
WaitList
appears in Listing 10-2. The definition of the method
simulate
contains print statements to help you follow the simulation. The other methods in the
class are straightforward.
LISTING 10-2
The class
WaitLine
/** Simulates a waiting line. */
public
class
WaitLine
{
private
QueueInterface<Customer> line;
private
int
numberOfArrivals;
private
int
numberServed;
private
int
totalTimeWaited;
public
WaitLine()
{
line =
new
LinkedQueue<Customer>();
reset();
}
// end default constructor
/** Simulates a waiting line with one serving agent.
@param duration the number of simulated minutes
@param arrivalProbability a real number between 0 and 1, and the
probability that a customer arrives at
a given time
@param maxTransactionTime the longest transaction time for a
customer */
public
void
simulate(
int
duration,
double
arrivalProbability,
int
maxTransactionTime)
{
int
transactionTimeLeft = 0;
for
(
int
clock = 0; clock < duration; clock++)
{
if
(Math.random() < arrivalProbability)
{
numberOfArrivals++;
int
transactionTime = (
int
)(Math.random()
* maxTransactionTime + 1);
Customer nextArrival =
new
Customer(clock, transactionTime,
numberOfArrivals);
line.enqueue(nextArrival);
System.out.println("Customer " + numberOfArrivals
+ " enters line at time " + clock
+ ". Transaction time is "
+ transactionTime);
}
// end if
if
(transactionTimeLeft > 0)