Java Reference
In-Depth Information
case DEBIT:
// Debits require even more checks...
try {
Thread.sleep(150);
} catch(InterruptedException e) {
System.out.println(e);
}
balance -= transaction.getAmount();
// Decrement
the balance
break;
default:
// We should
never get here
System.out.println("Invalid transaction");
System.exit(1);
}
transaction.getAccount().setBalance(balance);
// Restore
the A/C balance
}
}
Directory "BankOperation 1"
How It Works
The Bank class is very simple. It keeps no records of anything locally as the accounts are identified separ-
ately, and it has only one method that carries out a transaction. The Transaction object provides all the
information about what the transaction is and the account to which it applies. You can define the possible
types of transactions with the following enumeration:
// Bank account transaction types
public enum TransactionType {DEBIT, CREDIT }
You have provided only for debit and credit operations on an account, but the enum type and the switch
statement could easily be extended to accommodate other types of transactions. Both of the transactions
supported involve some delay while the standard nameless checks and verifications that all banks have
are carried out. The delay is simulated by calling the sleep() method belonging to the Thread class.
Of course, during this time, other things in other threads may be going on. There are no instance variables
to initialize in a Bank object, so you don't need a constructor. Because the Bank object works using a
Transaction object, let's define the class for that next.
TRY IT OUT: Defining a Transaction on an Account
The Transaction class can represent any transaction on an account. You can define the class as follows:
Search WWH ::




Custom Search