CS 231 Intro to Programming, Spring 2003
Lab 4: ATM/Bank Applet


In this lab you will gain more practice writing classes and creating a graphical user interface (GUI). You will write an ATM Applet to simulate a bank with three acocunts. The user will be able to select an account and then withdraw funds. Each time an amount is withdrawn. the current balance will be displayed.

Think about the design before you begin to write any code. The design pattern you are considering here is called the model-view-comtroller pattern. The view is what the user sees and interacts with, i.e. the user interface. The model is the underlying data or information. The controller is the interface between the two, i.e. the connection that allows the user, interacting with the view, to access and manipulate the underlying model.

The Interface

What sort of interface does your ATM need? At a minimum it will need:
  1. a button to request the current balance
  2. a place (TextField) to type the amount to withdraw
  3. a place (TextArea) for the ATM machine to report the status of the transaction.

Grab a piece of paper and make a quick sketch of what this interface might look like. You can do this without any knowledge of how the program will work.

The Model

What and how information will be stored? Typically, user information is kept in accounts so it is reasonable to create an Account class that will store information for each person. This information might include a name and a balance for each account.

What actions need to be performed on this information? This must at least include the ability to display (e.g. the name and balance) and withdraw an amount from the account.

Controller

How is your program going to access and manipulate all of the accounts. Typically, accounts are stored in a bank. When you want to withdraw funds, you must go to the bank, identify which account is yours, and then request a withdrawal. Thus it makes sense to have a Bank class that stores and manages all of the accounts. When a user requests a withdrawal (through the interface), the interface makes a request to the bank. Thus, the Bank class must contain all of the accounts and it must have actions that include requesting a withdrawal from a specific account. Thus it must know which is the account currently being considered.

Creating the Interface

In Visual Cafe (VC), create a new AWT Applet and save it to your H drive. Rename Applet1 to something more meaningful such as ATM_Applet. Let's begin by assuming that there are three accounts in the bank. Use the VC's Form Designer to create an interface that looks something like this. There should be 3 Buttons, 1 TextField, 1 Label for the TextField and one TextArea. You might want to rename these components to such things as accountButton1, accountButton2, accountButton3, amountTextField, amountLabel, and statusTestArea so that they are easier to find in the code.

Creating the Account Class

As already mentioned, an account should store information for a single account. Begin by defining the class as follows:


public class Account{
	String name = "Account";
	int balance = 1000;
}

Save the Account class. It should appear in the ATM_Applet project.

In order to access and change this information, we must add accessor methods for both the name and balance variables. For example, for the name variable, we need the following "get" and "set" methods:


public class Account{
	String name;
	int balance;

	public void setName(String nuName) {
		name = nuName;
	}
	public String getName() {
		return name;
	}
        
        // You must add the getBalance() and setBalance() methods here.       
 
        public String toString() {
	    return name + " has balance = " + balance;
	}
}

Add the above methods to code. Also add a "get" and "set" method for the balance variable. Note that we have also included a toString() method so that we can display what is stored in the account.

At this point it is important to test your account class to make sure it is working properly. To do this, we write what is called a driver program whose sole purpose is to create an Account object and to test the methods.

We can use the ATM_Applet as our driver class. We will set things up so that when the button for account 1 is pressed, an Account object will be created and its balance will be set. To do this, open up the ATM_Applet.java window in VC. In the "Objects" menu at the top of the file, choose accountButton1 (or whatever you named your button for account 1). Then in the Events/Methods menu, choose actionPerformed. VC will add code and take the cursor to the place where it says "// to do: code goes here." When the user presses the accountButton1 button, the program will execute the code that you place at this location. The code you will add to the accountButton1_ActionPerformed() method is shown below:

THIS GOES IN THE ATM_Applet CLASS
void accountButton1_ActionPerformed(java.awt.event.ActionEvent event)
{
	Account myAccount = new Account();
	myAccount.setName("James");
	statusTextArea.append("Old: " + myAccount.toString());
	myAccount.setBalance(1000);
	statusTextArea.append("New: " + myAccount.toString());	
}

Run the program to make sure that it works.

Now, in the Account class add a withdraw method to withdraw money from the account:

THIS GOES IN THE Account CLASS
public void withdraw(int amountToWithdraw) {
	    balance = balance - amountToWithdraw;
}

Test it in the ATM_Applet by sending a message to myAccount to withdraw some amount. Print the results in the status area.

The Bank Class

Recall that we want to have multiple accounts. These accounts will managed by a Bank. Create a new class called Bank. Add three Account Objects as data variables (see below). Since there are multiple accounts, the Bank needs to keep track of which account is currently being accessed. Add a variable of type Account that is called the currentAccount:

public class Bank {
    Account account1 = new Account();
    Account account2 = new Account();
    Account account3 = new Account();
    Account currentAccount = account1;   
}

Save this class into your project. The currentAccount variable may seem a bit odd. Note that it does not represent a fourth account, rather it always references one of the three accounts. In the picture below, it is referencing account1.

Add "set" methods (shown below) to the Bank class so that we can set the currentAccount to any one of the three accounts we wish. To withdraw money from the account referenced by currentAccount we also need to add a withdraw method to the Bank class. To view the balance we need to add a getBalance method:

THIS GOES IN THE Bank CLASS
public void setCurrentAccountTo1() {
        currentAccount = account1;
}
public void setCurrentAccountTo2() {
        currentAccount = account2;
}
public void setCurrentAccountTo3() {
        currentAccount = account3;
}
public void withdraw(int amount) {
        currentAccount.withdraw(amount);
}
public String getBalance() {
        return "$"+ currentAccount.getBalance() + "\n";
}

Connecting a Bank Object to Your Interface

To test your Bank class, we will again use the ATM_Applet as the driver class. Declare and create a Bank object as a data variable in your ATM_Applet class as shown below:


public class ATM_Applet extends Applet
{
    Bank myBank = new Bank();

    ...

}
In the ATM_Applet, remove the code you wrote for creating an Account object (i.e. the code in the accountButton1_ActionPerformed() method). Replace it with the code below which 1) sets the current account to account1, 2) reads the amount the user has typed into the amountTextField, and 3) withdraws that amount from account1:
THIS GOES IN THE ATM_Applet CLASS
void accountButton1_ActionPerformed(java.awt.event.ActionEvent event)
{
	myBank.setCurrentAccountTo1();
        int amount = Integer.parseInt(amountTextField.getText());
	myBank.withdraw(amount);
	statusTextArea.append(myBank.getBalance());
}

Just as you did for the accountButton1 button, add an actionPerformed event to your ATM_Applet for accountButton2 and accountButton3. For each of these buttons, add code similar to that shown above to set the current account and withdraws funds. Test your code.

Additional Tasks

Update your program so that there are 4 accounts instead of 3.

Add a constructor to your Account class so that the data variables can more easily be initialized. That is, rather than having to use "set" and "get" methods to set the initial values, you can just use your constructor when creating an Account object:

Account account1 = new Acount("Account 1",200); 
In the current program, it is possible to withdraw more funds than are available thus resulting in a negative balance. How would you modify your withdraw method in the Account class so that the user cannot overdraw an account (hint: use a guarded command pattern).

What to turn in


[top]

[CS 231 Home Page]