Java Reference
In-Depth Information
"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 bal-
ance. They are there to help you figure out what has happened after the transactions have been processed.
The number of times you debit and then credit the account is stored in
transactionCount
, so the total
number of transactions is twice this value. You 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
ini-
tialBalance
. You 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 you can just pass the
Clerk
objects in
the argument. There's no problem in doing this because the
Clerk
class implements the
Runnable
inter-
face. 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 I go through the code only 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 you create. You 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, you get a value between 0 and 25 returned.
You add 50 to this and, presto, you have a value between 50 and 75. You then use this amount to cre-
ate a
Transaction
object that represents a credit for the account. To keep a check on the work done
by the clerks, you add this credit to the total of all the credits generated, which is stored in the variable
totalCredits
. This enables you to verify whether or not the account has been updated properly.
Before you pass the transaction to
clerk1
, you must make sure that he or she isn't busy. Otherwise, you
would overwrite the clerk's in-tray. The
while
loop does this. As long as the
isBusy()
method returns
true
, you continue to call the
sleep()
method for a 25-millisecond delay, before you go round and
check again. When
isBusy()
returns
false
, you call the
doTransaction()
method for the clerk, with
the reference to the
transaction
object as the argument. The
for
loop runs for 20 iterations, so you
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, you 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 you need in the method
main()
, so you're ready to give it a
whirl. Remember that all four classes need to be in the same directory.
Running the Example
Now, if you run the example, the final balance is wrong. You should get results something like the following:
Original balance : $500
Total credits : $1295
Total debits : $880
Final balance : $212