Java Reference
In-Depth Information
// Create the bank and the clerks...
Bank theBank = new Bank(); //
Create a bank
Clerk clerk1 = new Clerk(theBank ); //
Create the first clerk
Clerk clerk2 = new Clerk(theBank ); //
Create the second clerk
// Create the accounts, and initialize total credits and debits
Account[] accounts = new Account[initialBalance.length];
for(int i = 0 ; i < initialBalance.length ; ++i) {
accounts[i] = new Account(i+1, initialBalance[i]); // Create
accounts
totalCredits[i] = totalDebits[i] = 0;
}
// Create the threads for the clerks as daemon, and start them off
// Create transactions randomly distributed between the
accounts...
// Wait until both clerks are done
// Now output the results...
}
}
Directory "BankOperation 3 - Multiple Accounts"
The shaded lines are where the changes occur. You now create an array of accounts in a loop, the number
of accounts being determined by the number of initial balances in the initialBalance array. Account
numbers are assigned successively starting from 1. The code for creating the bank and the clerks and for
creating the threads and starting them is exactly the same as before. The shaded comments that follow
the code indicate the other segments of code in main() that you need to modify.
The next piece you need to change is the creation and processing of the transactions:
// Create transactions randomly distributed between the accounts
Random rand = new Random();
Transaction transaction; // Stores a
transaction
int amount = 0; // Stores an
amount of money
int select = 0;
// Selects an
account
for(int i = 1 ; i <= transactionCount ; ++i) {
// Choose an account at random for credit operation
select = rand.nextInt(accounts.length);
Search WWH ::




Custom Search