Java Reference
In-Depth Information
Lastly, we output the results:
// Now output the results
System.out.println(
"Original balance : $" + initialBalance+"\n" +
"Total credits : $" + totalCredits+"\n" +
"Total debits : $" + totalDebits+"\n" +
"Final balance : $" + account.getBalance() + "\n" +
"Should be : $" + (initialBalance + totalCredits - totalDebits));
How It Works
The variables in the
main()
method track the total debits and credits, and record the initial account
balance. They are there to help us figure out what has happened after the transactions have been
processed. The number of times we debit and then credit the account is stored in
transactionCount
,
so the total number of transactions will be twice this value. We have added five further blocks of code to
perform the functions indicated by the comments, so let's now go through each of them in turn.
The
Account
object is created with the account number as 1 and with the initial balance stored in
initialBalance
. We pass the bank object,
theBank
, to the constructor for each of the
Clerk
objects, so that they can record it.
The
Thread
constructor requires an object of type
Runnable
, so we can just pass the
Clerk
objects in
the argument. There's no problem in doing this because the
Clerk
class implements the interface
Runnable
. You can always implicitly cast an object to a type that is any superclass of the object or any
interface type that the object class implements.
All the transactions are generated in the
for
loop. The handling of debits is essentially the same as the
handling of credits, so we'll only go through the code for the latter in detail. A random amount between $50
and $75 is generated for a credit transaction by using the
nextInt()
method for the
rand
object of type
Random
that we create. You'll recall that
nextInt()
returns an
int
value in the range 0 to one less than
the value of the argument, so by passing 26 to the method, we get a value between 0 and 25 returned. We
add 50 to this and, hey presto, we have a value between 50 and 75. We then use this amount to create a
Transaction
object that represents a credit for the account. To keep a check on the work done by the
clerks, we add this credit to the total of all the credits generated that is stored in the variable
totalCredits
.
This will allow us to verify whether or not the account has been updated properly.
Before we pass the transaction to
clerk1
, we must make sure that he or she isn't busy. Otherwise we
would overwrite the clerk's in-tray. The
while
loop does this. As long as the
isBusy()
method
returns
true
, we continue to call the
sleep()
method for a 25 millisecond delay, before we go round
and check again. When
isBusy()
returns
false
, we call the
doTransaction()
method for the clerk
with the reference to the
transaction
object as the argument. The
for
loop will run for 20 iterations,
so we'll generate 20 random transactions of each type.
The third
while
loop works in the same way as the previous check for a busy clerk - the loop
continues if either of the clerks is busy.
Lastly, we output the original account balance, the totals of credits and debits, and the final balance plus
what it should be for comparison. That's all we need in the method
main()
, so we're ready to give it a
whirl. Remember that all four classes need to be in the same directory.