// Program Objarr
// J Harvey, 2002
// This is a rough outline solution to the Bank Account problem using arrays of objects
// It needs refinement as suggested by the comments in the code
// Add your own refinements, eg add a button to restart the display of stored objects
// Or add code to search for a particular account number
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Objarr extends Applet implements ActionListener
{
Button createBut, dispNextBut;
TextField T1, T2, T3, T4;
Label L1, L2, L3;
int storeCounter=0, displayCounter=0; // indexes to point to start of array initially
BankAccount A1;
BankAccount[] store = new BankAccount[25];
// Array to store BankAccount objects
public void init()
{
createBut = new Button("Create");
createBut.addActionListener(this);
add(createBut);
dispNextBut = new Button ("Display Next account");
dispNextBut.addActionListener(this);
add(dispNextBut);
T1=new TextField(12);
T2=new TextField(12);
T3=new TextField(12);
T4=new TextField(12);
L1=new Label("Name");
L2=new Label("Account No");
L3=new Label("Balance");
add(L1);
add(T1);
add(L2);
add(T2);
add(L3);
add(T3);
add(T4); // LAYOUT MANAGER NEEDED!!
}
public void paint(Graphics g)
{
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == createBut)
{
// THIS LINE NEEDS CHANGING IF CONSTRUCTOR CHANGES
A1 = new BankAccount(T1.getText(), Integer.parseInt(T2.getText()));
T1.setText("");
T2.setText("");
T3.setText("");
T4.setText("Account created");
for(int i=0; i<100000000; i++); // delay loop
T4.setText("");
store[storeCounter] = A1;
storeCounter++; // updates the index for storing next object
}
if (ae.getSource() == dispNextBut)
{
T1.setText(store[displayCounter].getName());
// ADD CODE TO DISPLAY ACCOUNT NUMBER AND BALANCE
T4.setText("");
displayCounter++; // updates the index for displaying next object
// ADD CODE TO CHECK FOR END OF ARRAY
// OTHERWISE PROGRAM WILL EVENTUALLY FAIL
}
} // end actionPerformed
public class BankAccount
{
int accountNumber;
String name;
double balance;
// ADD OTHER VARIABLES PLUS THEIR SET/GET METHODS
// ADD CODE TO CONSTRUCTOR TO ALLOW BALANCE TO BE INPUT
public BankAccount(String nameIn, int acNumIn)
{
balance = 0.0;
name = nameIn;
accountNumber = acNumIn;
}
public String getName()
{
return name;
}
public double getBalance()
{
return balance;
}
public int getAccNo()
{
return accountNumber;
}
} // end class bankAccount
} // end class Objarr
|