01: /**
02:    A bank account has a balance that can be changed by 
03:    deposits and withdrawals.
04: */
05: public class BankAccount
06: {
07:    /**
08:       Constructs a bank account with a given balance
09:       @param initialBalance the initial balance
10:    */
11:    public BankAccount(double initialBalance)
12:    {
13:       balance = initialBalance;
14:    }
15: 
16:    /**
17:       Deposits money into the bank account.
18:       @param amount the amount to deposit
19:    */
20:    public synchronized void deposit(double amount)
21:    {
22:       balance = balance + amount;
23:       System.out.println(Thread.currentThread().getName() 
24:          + " Depositing " + amount 
25:          + ", new balance is " + balance);
26:       notifyAll();
27:    }
28:    
29:    /**
30:       Withdraws money from the bank account.
31:       @param amount the amount to withdraw
32:    */
33:    public synchronized void withdraw(double amount)
34:       throws InterruptedException
35:    {
36:       while (balance < amount)
37:       {
38:          System.out.println(Thread.currentThread().getName() 
39:             + " Waiting...");         
40:          wait();
41:       }
42:       balance = balance - amount;
43:       System.out.println(Thread.currentThread().getName() 
44:          + " Withdrawing " + amount 
45:          + ", new balance is " + balance);
46:    }
47:    
48:    /**
49:       Gets the current balance of the bank account.
50:       @return the current balance
51:    */
52:    public double getBalance()
53:    {
54:       return balance;
55:    }
56:    
57:    private double balance;
58: }