The small business Aggie Widget Factory business has been gr
The small business Aggie Widget Factory, business has been great! So, great in fact, that the money is coming in faster than founder can deposit it. He decides to hire two accountants to manage the money. So, now three people have the ability to deposit and withdraw money in the Aggie Widget account. Our job is to simulate how this bank account can be used by all three people. A withdrawal will be represented by: W$$ where $$ represents in dollars how much to withdraw. A deposit will be represented by: D$$ where $$ again represents in dollars how much to deposit. A balance inquiry will be represented by Q The program will input from a file named transactions.txt a series of events that occur. The first number will be the total number of transactions, each subsequent line will represent a transaction. For example: 10 Q D1000 D200 W500 D200 Q D10 D300 D500 W1000 Q Your job is simulate this system, using mutexes to insure accuracy
Solution
Using Mutexes to insure accuracy in Java program
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.concurrent.Semaphore;
public class SimaphoreTest {
// max 1 people
static Semaphore semaphore = new Semaphore(1);
static class MyLockerThread extends Thread {
String name = \"\";
static int account_bal = 1000;
MyLockerThread(String name) {
this.name = name;
}
public void run() {
try {
//System.out.println(name + \" : acquiring lock...\");
//System.out.println(name + \" : available Semaphore permits now: \"
// + semaphore.availablePermits());
semaphore.acquire();
//System.out.println(name + \" : got the permit!\");
try {
if(name.equalsIgnoreCase(\"Q\")){
System.out.println(\"Balance amount is :\"+account_bal);
}else if(name.startsWith(\"D\")){
String value = name.substring(1, name.length());
account_bal = account_bal + Integer.parseInt(value);
System.out.println(\"Final amount after deposit of \"+value + \" is : \"+account_bal);
}else if(name.startsWith(\"W\")){
String value = name.substring(1, name.length());
account_bal = account_bal - Integer.parseInt(value);
System.out.println(\"Final amount after withdrawl of \"+value + \" is : \"+account_bal);
}
} finally {
//System.out.println(name + \" : releasing lock...\");
semaphore.release();
//System.out.println(name + \" : available Semaphore permits now: \"
// + semaphore.availablePermits());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//System.out.println(\"Total available Semaphore permits : \"
// + semaphore.availablePermits());
Scanner txtscan;
try {
txtscan = new Scanner(new File(\"D:\\\\transactions.txt\"));
int iCount=0;
int tot_transactions = 0;
int trans_Count = 1;
while(txtscan.hasNextLine()){
String str = txtscan.nextLine();
if(iCount==0){
tot_transactions = Integer.parseInt(str);
iCount++;
}else{
if(trans_Count > tot_transactions){
break;
}
MyLockerThread t1 = new MyLockerThread(str);
t1.start();
Thread.sleep(1000);
trans_Count++;
iCount++;
}
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
transactions.txt
10
Q
D1000
D200
W500
D200
Q
D10
D300
D500
W1000
Q
------------------------------------------------------------------------------------------------------------------------------------------------
Result
Balance amount is :1000
Final amount after deposit of 1000 is : 2000
Final amount after deposit of 200 is : 2200
Final amount after withdrawl of 500 is : 1700
Final amount after deposit of 200 is : 1900
Balance amount is :1900
Final amount after deposit of 10 is : 1910
Final amount after deposit of 300 is : 2210
Final amount after deposit of 500 is : 2710
Final amount after withdrawl of 1000 is : 1710


