/* $Id: ATM.java 1613 2011-04-28 04:02:39Z terescoj $ */

/**
 * Class to represent ATM which withdraws money from a bank
 * Written 11/26/99 by Kim Bruce
 * Changed 3/16/00 by Barbara Lerner
 * Introduced a random pause in the critical section to make interference more probable.
 * Changed 5/7/02 by Jim Teresco to include a longer delay to better see
 * what is happening
 * 
 * @author additional updates, Jim Teresco, Siena College, Fall 2011
 */

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

public class ATM extends ActiveObject {

    private RandomIntGenerator pauseGenerator =
        new RandomIntGenerator(1, 10);

    private Account account;
    private int change;         	// Amount of each transaction
    private Label info;		// Where write description of transactions
    private int total = 0;

    // Store parameters and start running
    public ATM(Account anAccount, int aChange, Label aInfo) {
        account = anAccount;
        change = aChange;
        info = aInfo;
        start();
    }

    // Repeatedly withdraw "change" from account at bank.
    public void run() {
        for (int i = 0; i < 10; i++) {
            int balance = account.getBalance();
            pause(pauseGenerator.nextValue());
            account.setBalance(balance + change);

            total = total + change;
            info.setText("" + total);
            pause(400);
        }
    }
}
