Java Reference
In-Depth Information
The bank in the model is actually a computer that performs operations on the account, and the account is
stored separately. Each clerk can communicate directly with the bank. You define four classes that you use
in the program to model banking operations:
• A Bank class to represent the bank's computer
• An Account class to represent the account at the bank
• A Transaction class to represent a transaction on the account — a debit or a credit, for example
• A Clerk class to represent a bank clerk
You also define a class containing the method main() that starts the process off and determines how it all
works.
WARNING As you develop the code, you won't necessarily get it right the first time, but
you will improve as you find out more about how to program using threads. This will expose
some of the sorts of errors and complications that can arise when you're programming using
threads. I've included the incremental changes to the example as separate programs in the
code download numbered sequentially.
TRY IT OUT: Defining a Bank Class
The bank computer is the agent that performs the operations on an account so you start with that. You
can define the Bank class that represents the bank computer as follows:
// Define the bank
public class Bank {
// Perform a transaction
public void doTransaction(Transaction transaction) {
int balance = transaction.getAccount().getBalance(); // Get
current balance
switch(transaction.getTransactionType()) {
case CREDIT:
// Credits require a lot of checks...
try {
Thread.sleep(100);
} catch(InterruptedException e) {
System.out.println(e);
}
balance += transaction.getAmount();
// Increment
the balance
break;
Search WWH ::




Custom Search