Computer Science 120
Introduction to Programming

Spring 2012, Siena College

ATM3 Demo

A working demo of ATM3 will appear below. Click inside the applet to interact with it.



ATM3 BlueJ Project

Click here to download a BlueJ project for ATM3.


ATM3 Source Code

The Java source code for ATM3 is below. Click on a file name to download it.


ATM.java

/**
 * 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
 */

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

public class ATM extends ActiveObject {

  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++) {

      account.changeBalance(change);

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

Bank.java

/* $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, Spring 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);
    }
}

Account.java


import java.awt.*;
/* $Id: Account.java 1613 2011-04-28 04:02:39Z terescoj $ */

/**
 * A Simple Bank Account.
 * 
 * @author unknown
 */


public class Account {

  private int balance;
  private Label display;

  public Account(int start, Label aDisplay) {
    balance = start;
    display = aDisplay;
  }

  synchronized public int getBalance() {
    return balance;
  }

  synchronized public void changeBalance(int amount) {
    int newBalance = balance + amount;
    display.setText("" + newBalance);
    balance = newBalance;
  }
}