Java Reference
In-Depth Information
Try It Out - Defining a Bank Account
We can define an account as:
// Defines a customer account
public class Account {
// Constructor
public Account(int accountNumber, int balance) {
this.accountNumber = accountNumber; // Set the account number
this.balance = balance; // Set the initial balance
}
// Return the current balance
public int getBalance() {
return balance;
}
// Set the current balance
public void setBalance(int balance) {
this.balance = balance;
}
public int getAccountNumber() {
return accountNumber;
}
public String toString() {
return "A//C No. "+accountNumber+" : $"+balance;
}
private int balance; // The current account balance
private int accountNumber; // Identifies this account
}
How It Works
The Account class is also very simple. It just maintains a record of the amount in the account as a
balance, and provides methods for retrieving and setting the current balance. Operations on the account
are performed externally by the Bank object. We have a bit more than we need in the Account class at
the moment, but the methods we don't use in the current example may be useful later.
Try It Out - Defining a Bank Clerk
A clerk is a slightly more complicated animal. He or she retains information about the bank and details
of the current transaction and is responsible for initiating debits and credits on an account by
communication with the central bank. Each clerk will work independently of the others so they will
each be a separate thread:
public class Clerk implements Runnable {
private Bank theBank; // The employer - an electronic marvel
private Transaction inTray; // The in-tray holding a transaction
Search WWH ::




Custom Search