/* BankLine.java is a simple class that implements a bank queue. * * Started by: Charles Hoot, for Hands On Java. * Date: July, 2000       * Finished by: * * It is responsible for knowing the average amount of time * customers spent waiting in the line. *****************************************************************/import java.util.*;import Customer;class BankLine extends Object{	private long myTotalWaitTime;	private int myCustomersServed;	private Vector theLine;	private boolean tellAll;	private String myName;	/*****************************************************************	 * BankLine constructor	 *	 * 	 * PostCondition: created a Vector to hold the customers	 *				  total wait time is zero	 *				  no customers have been served	 *****************************************************************/	public BankLine(String name, boolean verbose){		myName = name;		tellAll = verbose;		theLine = new Vector();		myTotalWaitTime = 0;		myCustomersServed = 0;	}			/*****************************************************************	 * accessors	 *	 *****************************************************************/	public double totalWaitTime(){		return myTotalWaitTime;	}		public int myCustomersServed(){		return myCustomersServed;	}		public double averageWaitTime(){		return (double) myTotalWaitTime/myCustomersServed;	}	/*****************************************************************	 * addCustomer - adds a customer to the line	 *	 * Input: a customer to be added	 * PostCondition: customer was added at the end of the line	 *				  customer was informed that they are in line	 *****************************************************************/	synchronized public void addCustomer( Customer newCustomer){		theLine.addElement(newCustomer);		newCustomer.inLine();		if(tellAll)			System.out.println("Customer " + newCustomer.name() 						+ " was added to line " + myName);	}			/*****************************************************************	 * provideCustomer - gets the first customer in line	 *	 * Output: the first customer in line or null if none	 * PostCondition: customer was removed from the line	 *				  customer was asked for their time in line	 *				  total wait time and number served are updated	 *****************************************************************/	synchronized public Customer provideCustomer(){			if (theLine.size() == 0) return null;				//get the first customer in line		Customer firstInLine = (Customer) theLine.elementAt(0);		theLine.removeElementAt(0);				myTotalWaitTime += firstInLine.outOfLine();		myCustomersServed++;		if(tellAll)			System.out.println("Customer " + firstInLine.name() 						+ " was removed from the line " + myName);				return firstInLine;	}			/*****************************************************************	 * toString - provide a report on the line	 *	 * Output: a report string	 *****************************************************************/	public String toString(){		return "Bank Line - waiting currently: " + theLine.size() 		   + "\n             customers served: " + myCustomersServed 		   + "\n            average wait time: " + averageWaitTime();			}	} // end of class
