/* $Id: Bank.java 1613 2011-04-28 04:02:39Z terescoj $ */
/**
Class that represents a bank with two ATM's making deposits and withdrawals.
This program was designed to illustrate problems with concurrency.
Written 11/26/99 by Kim Bruce.
Changed 3/16/99 by Barbara Lerner to use a different UI showing just current values, not
a transaction history.

@author updated by Jim Teresco, Siena College, Fall 2011
 */

import java.awt.*;
import java.awt.event.*;
import objectdraw.*;

public class Bank extends Controller implements ActionListener {

    private static final int INITIAL_BALANCE = 1000;		// Initial balance in bank account

    private Label startBalance = new Label("Starting balance:  " + INITIAL_BALANCE, Label.CENTER);
    private Label currentLabel = new Label("Current balance:  ", Label.RIGHT);
    private Label currentBalanceLabel = new Label("" + INITIAL_BALANCE + "     ", Label.LEFT);
    private Label withdrawLabel = new Label("Amount withdrawn:  ", Label.RIGHT);
    private Label withdrawTotalLabel = new Label("         0    ", Label.LEFT);
    private Label depositLabel = new Label("Amount deposited:  ", Label.RIGHT);
    private Label depositTotalLabel = new Label("         0    ", Label.LEFT);

    // Set up text area and two ATM threads
    public void begin() {
        setLayout(new GridLayout(0, 1));
        this.setFont(new Font("System", Font.PLAIN, 18));

        add(startBalance);

        Panel balancePanel = new Panel();
        balancePanel.add(currentLabel);
        balancePanel.add(currentBalanceLabel);
        add(balancePanel);

        Panel withdrawPanel = new Panel();
        withdrawPanel.add(withdrawLabel);
        withdrawPanel.add(withdrawTotalLabel);
        add(withdrawPanel);

        Panel depositPanel = new Panel();
        depositPanel.add(depositLabel);
        depositPanel.add(depositTotalLabel);
        add(depositPanel);

        Panel buttonPanel = new Panel();
        Button runIt = new Button("Run demo");
        buttonPanel.add(runIt,"South");
        add(buttonPanel);
        runIt.addActionListener(this);

    }

    // Rerun demo when button is pressed.
    public void actionPerformed(ActionEvent evt) {
        Account account = new Account(INITIAL_BALANCE, currentBalanceLabel);
        ATM atm1 = new ATM(account, 100, depositTotalLabel);
        ATM atm2 = new ATM(account, -100, withdrawTotalLabel);
    }
}
