Monday, August 22, 2011

Test Bank


package com.evs.objava33.class14;

import java.io.RandomAccessFile;
import java.util.Random;

public class TestBank {

private static double initialBalance = 10000;
private static int noOfTrans = 100;
private static double totalCredit = 0.0;
private static double totalDebit = 0.0;

public static void main(String[] args) {
Bank bank = new Bank();
Account account = new Account(1111, initialBalance);

Clerk c1 = new Clerk(bank);
Clerk c2 = new Clerk(bank);

Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
t1.start();
t2.start();

Random rand = new Random();
Double amount = 0.0;
Transaction trans = null;

for (int i = 0; i < noOfTrans; i++) {
// Credit
amount = 50.0 + rand.nextInt(50);
trans = new Transaction(account, TransType.CREDIT, amount);
c1.doTransaction(trans);
totalCredit += amount;

// Debit
amount = 30.0 + rand.nextInt(30);
trans = new Transaction(account, TransType.DEBIT, amount);
c2.doTransaction(trans);
totalDebit += amount;
}

while (c1.isBusy() || c2.isBusy()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

System.out.println("Starting Balance: " + initialBalance);
System.out.println("Total Debits: " + totalDebit);
System.out.println("Total Credits: " + totalCredit);
System.out.println("Actual Balance: "
+ (initialBalance - totalDebit + totalCredit));
System.out.println("Account Balance: " + account.getBalance());

System.exit(0);
}
}

No comments:

Post a Comment