BankSimulation.java.html

  1  import java.util.LinkedList;
  2  import java.util.Queue;
  3  
  4  /**
  5     Simulation of customer traffic in a bank.
  6  */
  7  public class BankSimulation extends Simulation
  8  {
  9     private Customer[] tellers;
 10     private Queue<Customer> custQueue;
 11  
 12     private int totalCustomers;
 13     private double totalTime;
 14  
 15     private static final double INTERARRIVAL = 1; 
 16        // average of 1 minute between customer arrivals
 17     private static final double PROCESSING = 5; 
 18        // average of 5 minutes processing time per customer
 19  
 20     public BankSimulation(int numberOfTellers) 
 21     { 
 22        tellers = new Customer[numberOfTellers]; 
 23        custQueue = new LinkedList<Customer>();
 24        totalCustomers = 0;
 25        totalTime = 0;
 26     }
 27  
 28     /** 
 29         Adds a customer to the bank.
 30         @param c the customer
 31     */
 32     public void add(Customer c) 
 33     {
 34        boolean addedToTeller = false;
 35        for (int i = 0; !addedToTeller && i < tellers.length; i++)
 36        {
 37           if (tellers[i] == null)
 38           {  
 39              addToTeller(i, c);
 40              addedToTeller = true;
 41           }
 42        }
 43        if (!addedToTeller) { custQueue.add(c); }
 44  
 45        addEvent(new Arrival(getCurrentTime() + expdist(INTERARRIVAL)));
 46     }
 47  
 48     /**
 49        Adds a customer to a teller and schedules the departure event.
 50        @param i the teller number
 51        @param c the customer
 52     */
 53     private void addToTeller(int i, Customer c)
 54     {
 55        tellers[i] = c;
 56        addEvent(new Departure(getCurrentTime() + expdist(PROCESSING), i));
 57     }
 58     
 59     /** 
 60         Removes a customer from a teller.
 61         @param i teller position
 62     */
 63     public void remove(int i)
 64     {
 65        Customer c = tellers[i];
 66        tellers[i] = null;
 67  
 68        // Update statistics
 69        totalCustomers++; 
 70        totalTime = totalTime + getCurrentTime() - c.getArrivalTime();
 71  
 72        if (custQueue.size() > 0)
 73        {
 74           addToTeller(i, custQueue.remove());
 75        }
 76     }
 77  
 78     /**
 79        Displays tellers and queue.
 80     */
 81     public void display()
 82     {
 83        for (int i = 0; i < tellers.length; i++)
 84        {
 85           if (tellers[i] == null)
 86           {
 87              System.out.print(".");
 88           } 
 89           else
 90           {
 91              System.out.print("C");
 92           } 
 93        }
 94        System.out.print("<");
 95        int q = custQueue.size();
 96        for (int j = 1; j <= q; j++) { System.out.print("C"); }
 97        System.out.println();
 98     }
 99  
100     /** 
101         Displays a summary of the gathered statistics.
102     */
103     public void displaySummary()
104     {
105        double averageTime = 0;
106        if (totalCustomers > 0) 
107        { 
108           averageTime = totalTime / totalCustomers; 
109        }
110        System.out.println(totalCustomers + " customers. Average time "
111           + averageTime + " minutes.");
112     }
113  }