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 AuditBankAccount sharedAccount, String name
Account sharedAccount;
threadName name;
close constructor
@Override
public void run
whiletrue
try
Account.auditsleeptimenextInt threadName;
sleep random time then run audit
Thread.sleepsleeptimenextInt; sleep thread
catchInterruptedException 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.ioIOException;
import java.ioFileWriter;
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 ; Start the simulation with a balance of $
private static int transactionNumber ;
private static int transactionsSinceAudit ;
private static int auditCounter;
boolean occupied false;
private static final int DEPOSITALERTLEVEL ;
private static final int WITHDRAWALALERTLEVEL ;
BankAccount
accessLock new ReentrantLock;
canWithdraw accessLock.newCondition;
canDeposit accessLock.newCondition;
constructor
deposit into bank account
@Override
public void depositint amount, String threadName
accessLock.lock; lock bank account
try
whileoccupied
canDeposit.await;
logging for flag
ifamount DEPOSITALERTLEVEL
flagamount threadName, "Deposit";
else
make deposit
balanceamount;
transactionNumber;
auditCounter;
display balance
System.out.printthreadName deposits $amounttttt;
System.out.printlntttttt Balance is $balancetttttttt transactionNumber;
canWithdraw.signalAll; signal all waiting to make withdrawal
catchException exception
exception.printStackTrace;
finally
accessLock.unlock;
close finally
close deposit
withdraw
@Override
public void withdrawint amount, String threadName
accessLock.lock; lock bank account
try
whileoccupied
canWithdraw.await;
ifbalance amount
negative balance
System.out.printlntt WITHDRAWAL BLOCKED INSUFFICIENT FUNDS";
flagamount threadName, "Withdraw"; flag log
canWithdraw.await;
else
balance amount;
transactionNumber;
auditCounter;
System.out.printtttt threadName withdraws $ amount;
System.out.printlntttttt Balance is $balancetttttttt transactionNumber;
logging for flag
ifamount WITHDRAWALALERTLEVEL
flagamount threadName, "Withdraw";
close if
close else
canWithdraw.signalAll; signal all waiting to make withdrawal
catchException exception
exception.printStackTrace;
finally
accessLock.unlock;
close finally
close withdraw
@Override
public void auditint i String threadName
transactionsSinceAudit auditCounter; counts number of transactions since last audit
System.out.println
;
System.out.printlnAUDITOR FINDS CURRENT ACCOUNT BALANCE TO BE: $ balance tt NUMBER OF TRANSACTIONS SINCE LAST AUDIT: transactionsSinceAudit;
System.out.println
;
auditCounter; reset counter
close audit
AUXILIARY FUNCTIONS
log flagged transactions to avoid money laundering
public void flagint amount, String threadName, String transactionType
flag withdraw
iftransactionType "Withdraw"
System.out.println
Flagged Transaction Withdrawal Agent threadName Made A Withdrawal In Excess of $ WITHDRAWALALERTLEVEL USD See Flagged Transaction Log
;
WriteTransactionLogamount threadName, transactionType;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
