Monday, August 22, 2011

Assignment - Week 7

Continue Assignment of Week 6, and implement it as a client server address book. Additional attribute is to create a simple client; from which user can add/update/delete/view all address book entries. On server, there should be an audit of last added/update/delete/view entry, as who did what.

Note: Core Projects has been assigned in addition to this. Dead Line for Core Project is: 10-Sept-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);
}
}

Clerk


package com.evs.objava33.class14;

public class Clerk implements Runnable {

private Bank bank;
private Transaction transaction;

public Clerk(Bank bank) {
this.bank = bank;
}

public void run() {
while (true) {
while (isBusy() == false) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
bank.doTransaction(transaction);
transaction = null;
}
}

public void doTransaction(Transaction transaction) {
while (isBusy()) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.transaction = transaction;
}

public boolean isBusy() {
return transaction != null;
}

}

Bank


package com.evs.objava33.class14;

public class Bank {

public synchronized void doTransaction(Transaction transaction) {
switch (transaction.getTransType()) {
case DEBIT:
// synchronized (transaction.getAccount()) {
double balance = transaction.getAccount().getBalance();
balance -= transaction.getAmount();
transaction.getAccount().setBalance(balance);
// }
break;
case CREDIT:
// synchronized (this) {
balance = transaction.getAccount().getBalance();
balance += transaction.getAmount();
transaction.getAccount().setBalance(balance);
// }
break;
default:
System.out.println("Unknown Transaction Type ... ");
break;
}

}
}

Transaction


package com.evs.objava33.class14;

public class Transaction {

private Account account;
private TransType transType;
private Double amount;

public Transaction(Account account, TransType transType, Double amount) {
this.account = account;
this.transType = transType;
this.amount = amount;
}

/**
* @return the account
*/
public Account getAccount() {
return account;
}

/**
* @param account
*            the account to set
*/
public void setAccount(Account account) {
this.account = account;
}

/**
* @return the transType
*/
public TransType getTransType() {
return transType;
}

/**
* @param transType
*            the transType to set
*/
public void setTransType(TransType transType) {
this.transType = transType;
}

/**
* @return the amount
*/
public Double getAmount() {
return amount;
}

/**
* @param amount
*            the amount to set
*/
public void setAmount(Double amount) {
this.amount = amount;
}

public String toString() {
return "Transaction [account=" + account + ", transType=" + transType
+ ", amount=" + amount + "]";
}
}

Account


package com.evs.objava33.class14;

/**
 * THis class is used for Account holding purposes
 *
 * @author Masuds
 * @since 1.1
 */
public class Account {

private Integer accountNumber;
private Double balance;

/**
* THis is default Constructor
*
* @param accountNumber
*            - Bank Account Number
* @param balance
*            - Bank Account Balance
*/
public Account(Integer accountNumber, Double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}

/**
* @return the accountNumber
*/
public Integer getAccountNumber() {
return accountNumber;
}

/**
* @param accountNumber
*            the accountNumber to set
*/
public void setAccountNumber(Integer accountNumber) {
this.accountNumber = accountNumber;
}

/**
* @return the balance
*/
public Double getBalance() {
return balance;
}

/**
* @param balance
*            the balance to set
*/
public void setBalance(Double balance) {
this.balance = balance;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Account [accountNumber=" + accountNumber + ", balance="
+ balance + "]";
}
}

TransType


package com.evs.objava33.class14;

public enum TransType {
CREDIT, DEBIT;
}