Computer Science 252
Problem Solving with Java

Spring 2015, The College of Saint Rose

ATM2 Demo

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



ATM2 BlueJ Project

Click here to download a BlueJ project for ATM2.


ATM2 Source Code

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


ATM.java

/* $Id: ATM.java 2269 2013-12-01 02:33:37Z 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, Spring 2011
 * Additional updates, CSC 252, The College of Saint Rose, Fall 2013
 * 
 */

import java.util.Random;
import javax.swing.JLabel;

public class ATM extends Thread {

    private static final int MAX_PAUSE = 10;
    private static final int NUM_TRANSACTIONS = 1000;

    private Random pauseGenerator = new Random();

    private Account account;
    private int change;             // Amount of each transaction
    private JLabel info;            // Where to display description of transactions
    private int total = 0;

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

        start();
    }

    // Repeatedly withdraw "change" from account at bank.
    // This is effectively a deposit when "change" is negative.
    public void run() {

        for (int i = 0; i < NUM_TRANSACTIONS; i++) {

            account.changeBalance(change);

            total = total + change;
            info.setText("" + total);

            // demonstrate both with and without sleep
            //try {
            //    sleep(400);
            //}
            //catch (Exception e) {}
        }
    }
}

Bank.java

/* $Id: Bank.java 2269 2013-12-01 02:33:37Z 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
 * Further updated for Swing and to remove dependency on Objectdraw
 * by Jim Teresco, The College of Saint Rose, Fall 2013, CSC 252
 */

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

// note that since this class does not need ObjectDraw but does have Java Swing
// components, we extend "JApplet" instead of "WindowController"
public class Bank extends JApplet implements ActionListener {

    private static final int INITIAL_BALANCE = 1000;        // Initial balance in bank account
    private static final int TRANSACTION_AMOUNT = 100;      // how much to deposit/withdraw per xaction

    private JLabel currentBalanceLabel;
    private JLabel withdrawTotalLabel;
    private JLabel depositTotalLabel;

    // Set up window layout and two ATM threads
    // The init method is the JApplet equivalent of a WindowController's begin method
    public void init() {

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

        setLayout(new GridLayout(0, 1));
        this.setFont(new Font("System", Font.PLAIN, 18));

        add(startBalance);

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

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

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

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

    }

    // Run the demo each time the 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 javax.swing.JLabel;

/* $Id: Account.java 2269 2013-12-01 02:33:37Z terescoj $ */

/**
 * A simple bank account.  Still NSFCA (not safe for concurrent access).
 * 
 * @author original unknown
 * Updated for Java Swing, Jim Teresco, The College of Saint Rose, Fall 2013
 */

public class Account {

    private int balance;
    private JLabel display;  // label on screen for balance

    public Account(int start, JLabel aDisplay) {
        
        balance = start;
        display = aDisplay;
        display.setText("" + balance);
    }

    public int getBalance() {
        return balance;
    }

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

       // alternate:
        //int newBalance = balance + amount;
        //display.setText("" + newBalance);
        //balance = newBalance;
    }
}