Question: Need help adding the other agents to my java code... Audit import java.util.Random; / / audit class run method public class Audit implements Runnable {

Need help adding the other agents to my java code...
Audit
import java.util.Random;
//audit class run method
public class Audit implements Runnable{
private static Random sleeptime = new Random();
private BankAccount Account; //reference to shared bank account object
private String threadName;
public Audit(BankAccount sharedAccount, String name){
Account = sharedAccount;
threadName = name;
}//close constructor
@Override
public void run(){
while(true){
try {
Account.audit(sleeptime.nextInt(), threadName);
//sleep random time then run audit
Thread.sleep(sleeptime.nextInt(300)); //sleep thread
}catch(InterruptedException exception){
exception.printStackTrace();
}//close catch
}//close while
}//close run
}//close class Audit
BankAccount....
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.io.IOException;
import java.io.FileWriter;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.concurrent.locks.Condition;
public class BankAccount implements BankBuffer{
//lock to control mutually exclusive access to bank account
private Lock accessLock = new ReentrantLock();
private Condition canWithdraw = accessLock.newCondition(); //signal to waiting withdraw thread
private Condition canDeposit = accessLock.newCondition(); //signal to waiting deposit thread
//variables
private int balance =0; //Start the simulation with a balance of $0
private static int transactionNumber =0;
private static int transactionsSinceAudit =0;
private static int auditCounter=0;
boolean occupied = false;
private static final int DEPOSIT_ALERT_LEVEL =350;
private static final int WITHDRAWAL_ALERT_LEVEL =75;
BankAccount(){
accessLock = new ReentrantLock();
canWithdraw = accessLock.newCondition();
canDeposit = accessLock.newCondition();
}//constructor
//deposit into bank account
@Override
public void deposit(int amount, String threadName){
accessLock.lock(); //lock bank account
try {
while(occupied){
canDeposit.await();
}
//logging for flag
if(amount >= DEPOSIT_ALERT_LEVEL){
flag(amount, threadName, "Deposit");
}else {
//make deposit
balance+=amount;
transactionNumber++;
auditCounter++;
//display balance
System.out.print(threadName+" deposits $"+amount+"\t\t\t\t");
System.out.println("\t\t\t\t\t\t(+) Balance is $"+balance+"\t\t\t\t\t\t\t\t"+ transactionNumber);
}
canWithdraw.signalAll(); //signal all waiting to make withdrawal
} catch(Exception exception){
exception.printStackTrace();
} finally {
accessLock.unlock();
}//close finally
}//close deposit
//withdraw
@Override
public void withdraw(int amount, String threadName){
accessLock.lock(); //lock bank account
try {
while(occupied){
canWithdraw.await();
}
if((balance - amount)<0){
//negative balance
System.out.println("\t\t (******) WITHDRAWAL BLOCKED - INSUFFICIENT FUNDS");
flag(amount, threadName, "Withdraw"); //flag log
canWithdraw.await();
} else {
balance -= amount;
transactionNumber++;
auditCounter++;
System.out.print("\t\t\t\t"+ threadName+" withdraws $"+ amount);
System.out.println("\t\t\t\t\t\t(-) Balance is $"+balance+"\t\t\t\t\t\t\t\t"+ transactionNumber);
//logging for flag
if(amount >= WITHDRAWAL_ALERT_LEVEL){
//flag(amount, threadName, "Withdraw");
}//close if
}//close else
canWithdraw.signalAll(); //signal all waiting to make withdrawal
} catch(Exception exception){
exception.printStackTrace();
} finally {
accessLock.unlock();
}//close finally
}//close withdraw
@Override
public void audit(int i, String threadName){
transactionsSinceAudit = auditCounter; //counts number of transactions since last audit
System.out.println("*******************************************************************************************************************
");
System.out.println("AUDITOR FINDS CURRENT ACCOUNT BALANCE TO BE: $"+ balance +"\t\t NUMBER OF TRANSACTIONS SINCE LAST AUDIT: "+ transactionsSinceAudit);
System.out.println("
*****************************************************************************************************************");
auditCounter=0; //reset counter
}//close audit
//AUXILIARY FUNCTIONS
//log flagged transactions to avoid money laundering
public void flag(int amount, String threadName, String transactionType){
//flag withdraw
if(transactionType == "Withdraw"){
System.out.println("
*** Flagged Transaction - Withdrawal Agent "+ threadName +" Made A Withdrawal In Excess of $"+ WITHDRAWAL_ALERT_LEVEL +".00 USD - See Flagged Transaction Log
");
WriteTransactionLog(amount, threadName, transactionType);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!