import java.util.Random; public class CustomerProvider implements Runnable { private BankInterface theBank; private double ratio; private static int MAX_TRANSACTION_TIME = 6; private volatile boolean exit = false; /** * Create an instance of CustomerProvider * @param bi the bank to which the customers are going * @param fratio the ratio to time between customers to the time required for a customer. * A ratio of 2.0 means that, on average 2 customers arrive at the bank for every * one customer that can be handled by a single teller. */ public CustomerProvider(BankInterface bi, double fratio) { theBank=bi; ratio = fratio; } public void startCustomerFlow() { Thread t1 = new Thread(this); t1.start(); } public void stopCustomers() { exit=true; } @Override public void run() { Random r = new Random(); while (!exit) { double delay = (r.nextInt((MAX_TRANSACTION_TIME-1))+1) / ratio; int ptime = r.nextInt(MAX_TRANSACTION_TIME-1)+1; theBank.enteringCustomer(new Customer(ptime)); try { Thread.sleep(((int)delay)*100); } catch (Exception ee) {} } } }