SlideShare a Scribd company logo
1 of 16
Download to read offline
The program will read the file like this,
> java homework6/Bank small.txt 4
acct:0 bal:999 trans:1
acct:1 bal:1001 trans:1
acct:2 bal:999 trans:1
acct:3 bal:1001 trans:1
acct:4 bal:999 trans:1
acct:5 bal:1001 trans:1
acct:6 bal:999 trans:1
acct:7 bal:1001 trans:1
acct:8 bal:999 trans:1
acct:9 bal:1001 trans:1
acct:10 bal:999 trans:1
acct:11 bal:1001 trans:1
acct:12 bal:999 trans:1
acct:13 bal:1001 trans:1
acct:14 bal:999 trans:1
acct:15 bal:1001 trans:1
acct:16 bal:999 trans:1
acct:17 bal:1001 trans:1
acct:18 bal:999 trans:1
acct:19 bal:1001 trans:1
Each text file looks something like:
1 2 1
3 4 1
5 6 1
7 8 1
9 10 1
11 12 1
File Format: Each line in the external file represents a single transaction, and contains three
numbers: the id of the account from which the money is being transferred, the id of the account
to which the money is going, and the amount of money. For example the line:
17 6 104
indicates that $104 is being transferred from Account #17 to Account #6.
The test data provided includes transfers with the same from and to account numbers, so make
sure your program will work correctly for these transfers. For example:
5 5 40
Count these as two transactions for the account (one transaction taking money from the account
and one putting money into the account).
My goal is to pass each transaction into the queue, the queue will hold the transaction, the
worker will take the transaction, complete the deposit/withdraw, and update the balance of the
account accordingly. I am required to use BlockingQueue. My problem is that the program is not
running correctly. I need to fix the Bank class, how I start up the Bank in main thread, and also
work on Worker class.
More info:
Details
I recommend a design with four classes—Bank, Account, Transaction, and Worker. Both the
Account and Transactions classes are quite simple.
Account needs to store an id number, the current balance for the account, and the number of
transactions that have occurred on the account. Remember that multiple worker threads may be
accessing an account simultaneously and you must ensure that they cannot corrupt its data. You
may also want to override the toString method to handle printing of account information.
Transaction is a simple class that stores information on each transaction (see below for more
information about each transaction). If you’re careful you can treat the Transaction as
immutable. This means that you do not have to worry about multiple threads accessing it.
Remember an immutable object’s values never change, therefore its values are not subject to
corruption in a concurrent environment.
The Bank class maintains a list of accounts and the BlockingQueue used to communicate
between the main thread and the worker threads. The Bank is also responsible for starting up the
worker threads, reading transactions from the file, and printing out all the account values when
everything is done. Note: make sure you start up all the worker threads before reading the
transactions from the file.
I recommend making the Worker class is an inner class of the Bank class. This way it gets easy
access to the list of accounts and the queue used for communication. Workers should check the
queue for transactions. If they find a transaction they should process it. If the queue is empty,
they will wait for the Bank class to read in another transaction (you’ll get this behavior for free
by using a BlockingQueue). Workers terminate when all the transactions have been processed.
package Problem1;
public class Account {
private int id;
private int balance;
private int numoft;
public Account(int id, int balance, int numberOfTransactions)
{
this.id = id;
this.balance = 1000;
this.numoft = numberOfTransactions;
}
public int getBalance()
{
return balance;
}
public int getAccountNumber()
{
return id;
}
public int getNumOfTransactions()
{
return numoft;
}
public void deposite(int amount)
{
this.balance = this.balance + amount;
}
public void withdrawl(int amount)
{
this.balance = this.balance - amount;
}
public int addNumOfTrans(int amount)
{
return this.numoft = this.numoft + amount;
}
public String toString()
{
return "acct:" + id + " bal:" + balance + " trans:" + numoft + " ";
}
}
_________________________________________
package Problem1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/*
* Transaction class does the calculation job
*/
public class Transaction {
//private static File filename = new File("test2.txt");
// there can be two transactions, <--- goes to worker
private int accountTo;
private int accountFrom;
private int amount;
public Transaction(int accountTo, int accountFrom, int amount)
{
this.accountTo = accountTo;
this.accountFrom = accountFrom;
this.amount = amount;
}
public int getAccount1()
{
return accountTo;
}
public int getAccount2()
{
return accountFrom;
}
public int getTransferAmount()
{
return amount;
}
public String toString()
{
return accountTo + " " + accountFrom + " " + amount;
}
}
___________________________________
package Problem1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/*
* Bank would read the file
*/
public class Bank implements Runnable {
private ArrayList accounts; // the arraylist that has all the
// account
private ArrayList accountid; // the arraylist that hold all the
// account id so i can find
private ArrayList listOfT; // list of transaction
private int worker;
private File filename;
private BlockingQueue queue;
private final Transaction nullTrans = new Transaction(-1, 0, 0);
private int numThread;
public Bank(int numThread) {
this.numThread = numThread;
accounts = new ArrayList();
accountid = new ArrayList();
listOfT = new ArrayList();
}
public void addAccount(Account a) {
accounts.add(a);
}
// public Account replaceAccount(Account e)
// {
// return this.accounts.get(index) = e;
// }
public boolean checkAccounts(int id) {
for (Account b : accounts) {
if (b.getAccountNumber() == id) {
return true; // account exist
}
}
return false;
}
public Account find(int accountNumber) {
for (Account a : accounts) {
if (a.getAccountNumber() == accountNumber) // Found a match
return a;
}
// No match in the entire array list
throw new IllegalArgumentException("Account number " + accountNumber + " was not
found in the bank");
}
public String toString() {
for (Account a : accounts) {
// System.out.println("acct:" + a.getAccountNumber() + " bal:" +
// a.getBalance() + " trans:" + a.getNumOfTransactions());
return "acct:" + a.getAccountNumber() + " bal:" + a.getBalance() + " trans:" +
a.getNumOfTransactions();
}
return null;
}
public void readTransaction(Scanner in) {
int a1;
int a2;
int balance;
while (in.hasNext()) {
a1 = in.nextInt();
a2 = in.nextInt();
balance = in.nextInt();
if (!checkAccounts(a1)) {
Account newAccount = new Account(a1, 1000, 0);
addAccount(newAccount);
accountid.add(a1);
}
if (!checkAccounts(a2)) {
Account newAccount = new Account(a2, 1000, 0);
addAccount(newAccount);
accountid.add(a2);
}
listOfT.add(new Transaction(a1, a2, balance));
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
queue.put(listOfT.get(0));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// main thread
public static void main(String[] args) throws FileNotFoundException {
// start the worker's thread before transaction
//int thread = Integer.parseInt(args[1]);
// BlockingQueue q = new ArrayBlockingQueue(50);
int thread = 4; // numthread
Bank myBank = new Bank(thread);
myBank.queue = new ArrayBlockingQueue(50);
new Thread(myBank).start();
for (int i = 0; i < myBank.queue.size(); i++)
{
myBank.new Worker().start();
}
myBank.filename = new File("test2.txt");
Scanner in = new Scanner(myBank.filename);
myBank.readTransaction(in);
System.out.println(myBank.accounts);
// reading transactions
}
class Worker extends Thread {
// receive the transaction, add the number of transaction amount into
// queue
// queue will hold the transaction action
// find the from account and to account, run() method
// get the money
public void run() {
// TODO Auto-generated method stub
while (queue != nullTrans) {
try {
takeTransaction(queue.take());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void takeTransaction(Transaction t)
{
int a1;
int a2;
int balance;
a1 = t.getAccount1();
a2 = t.getAccount2();
balance = t.getTransferAmount();
Account account1 = find(a1);
Account account2 = find(a2);
account1.withdrawl(balance);
account2.deposite(balance);
account1.addNumOfTrans(1);
account2.addNumOfTrans(1);
accounts.remove(a1);
addAccount(account1);
accounts.remove(a2);
addAccount(account2);
}
}
}
// you put stuff in queue from bank,
// the worker receive it and do the transaction
/*
*
*/
Solution
public class Account {
private int id;
private int balance;
private int numoft;
public Account(int id, int balance, int numberOfTransactions)
{
this.id = id;
this.balance = 1000;
this.numoft = numberOfTransactions;
}
public int getBalance()
{
return balance;
}
public int getAccountNumber()
{
return id;
}
public int getNumOfTransactions()
{
return numoft;
}
public void deposite(int amount)
{
this.balance = this.balance + amount;
}
public void withdrawl(int amount)
{
this.balance = this.balance - amount;
}
public int addNumOfTrans(int amount)
{
return this.numoft = this.numoft + amount;
}
public String toString()
{
return "acct:" + id + " bal:" + balance + " trans:" + numoft + " ";
}
}
_________________________________________
package Problem1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/*
* Transaction class does the calculation job
*/
public class Transaction {
//private static File filename = new File("test2.txt");
// there can be two transactions, <--- goes to worker
private int accountTo;
private int accountFrom;
private int amount;
public Transaction(int accountTo, int accountFrom, int amount)
{
this.accountTo = accountTo;
this.accountFrom = accountFrom;
this.amount = amount;
}
public int getAccount1()
{
return accountTo;
}
public int getAccount2()
{
return accountFrom;
}
public int getTransferAmount()
{
return amount;
}
public String toString()
{
return accountTo + " " + accountFrom + " " + amount;
}
}
___________________________________
package Problem1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/*
* Bank would read the file
*/
public class Bank implements Runnable {
private ArrayList accounts; // the arraylist that has all the
// account
private ArrayList accountid; // the arraylist that hold all the
// account id so i can find
private ArrayList listOfT; // list of transaction
private int worker;
private File filename;
private BlockingQueue queue;
private final Transaction nullTrans = new Transaction(-1, 0, 0);
private int numThread;
public Bank(int numThread) {
this.numThread = numThread;
accounts = new ArrayList();
accountid = new ArrayList();
listOfT = new ArrayList();
}
public void addAccount(Account a) {
accounts.add(a);
}
// public Account replaceAccount(Account e)
// {
// return this.accounts.get(index) = e;
// }
public boolean checkAccounts(int id) {
for (Account b : accounts) {
if (b.getAccountNumber() == id) {
return true; // account exist
}
}
return false;
}
public Account find(int accountNumber) {
for (Account a : accounts) {
if (a.getAccountNumber() == accountNumber) // Found a match
return a;
}
// No match in the entire array list
throw new IllegalArgumentException("Account number " + accountNumber + " was not
found in the bank");
}
public String toString() {
for (Account a : accounts) {
// System.out.println("acct:" + a.getAccountNumber() + " bal:" +
// a.getBalance() + " trans:" + a.getNumOfTransactions());
return "acct:" + a.getAccountNumber() + " bal:" + a.getBalance() + " trans:" +
a.getNumOfTransactions();
}
return null;
}
public void readTransaction(Scanner in) {
int a1;
int a2;
int balance;
while (in.hasNext()) {
a1 = in.nextInt();
a2 = in.nextInt();
balance = in.nextInt();
if (!checkAccounts(a1)) {
Account newAccount = new Account(a1, 1000, 0);
addAccount(newAccount);
accountid.add(a1);
}
if (!checkAccounts(a2)) {
Account newAccount = new Account(a2, 1000, 0);
addAccount(newAccount);
accountid.add(a2);
}
listOfT.add(new Transaction(a1, a2, balance));
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
queue.put(listOfT.get(0));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// main thread
public static void main(String[] args) throws FileNotFoundException {
// start the worker's thread before transaction
//int thread = Integer.parseInt(args[1]);
// BlockingQueue q = new ArrayBlockingQueue(50);
int thread = 4; // numthread
Bank myBank = new Bank(thread);
myBank.queue = new ArrayBlockingQueue(50);
new Thread(myBank).start();
for (int i = 0; i < myBank.queue.size(); i++)
{
myBank.new Worker().start();
}
myBank.filename = new File("test2.txt");
Scanner in = new Scanner(myBank.filename);
myBank.readTransaction(in);
System.out.println(myBank.accounts);
// reading transactions
}
class Worker extends Thread {
// receive the transaction, add the number of transaction amount into
// queue
// queue will hold the transaction action
// find the from account and to account, run() method
// get the money
public void run() {
// TODO Auto-generated method stub
while (queue != nullTrans) {
try {
takeTransaction(queue.take());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void takeTransaction(Transaction t)
{
int a1;
int a2;
int balance;
a1 = t.getAccount1();
a2 = t.getAccount2();
balance = t.getTransferAmount();
Account account1 = find(a1);
Account account2 = find(a2);
account1.withdrawl(balance);
account2.deposite(balance);
account1.addNumOfTrans(1);
account2.addNumOfTrans(1);
accounts.remove(a1);
addAccount(account1);
accounts.remove(a2);
addAccount(account2);
}
}
}

More Related Content

Similar to The program will read the file like this, java homework6Bank sma.pdf

030 cpp streams
030 cpp streams030 cpp streams
030 cpp streamsHồ Lợi
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)aeden_brines
 
Introduction to trader bots with Python
Introduction to trader bots with PythonIntroduction to trader bots with Python
Introduction to trader bots with Pythonroskakori
 
Introduction to transaction processing concepts and theory
Introduction to transaction processing concepts and theoryIntroduction to transaction processing concepts and theory
Introduction to transaction processing concepts and theoryZainab Almugbel
 
ProjectReport - Maurya,Shailesh
ProjectReport - Maurya,ShaileshProjectReport - Maurya,Shailesh
ProjectReport - Maurya,Shaileshsagar.247
 
Study Notes: Google Percolator
Study Notes: Google PercolatorStudy Notes: Google Percolator
Study Notes: Google PercolatorGao Yunzhong
 
Tech_Implementation of Complex ITIM Workflows
Tech_Implementation of Complex ITIM WorkflowsTech_Implementation of Complex ITIM Workflows
Tech_Implementation of Complex ITIM Workflows51 lecture
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transactionpatinijava
 
Dependency injection - the right way
Dependency injection - the right wayDependency injection - the right way
Dependency injection - the right wayThibaud Desodt
 
Junit in mule demo
Junit in mule demoJunit in mule demo
Junit in mule demoSudha Ch
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping newaprilyyy
 
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdfProgramming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdfssuser6254411
 

Similar to The program will read the file like this, java homework6Bank sma.pdf (20)

Dbms module iii
Dbms module iiiDbms module iii
Dbms module iii
 
030 cpp streams
030 cpp streams030 cpp streams
030 cpp streams
 
My programming final proj. (1)
My programming final proj. (1)My programming final proj. (1)
My programming final proj. (1)
 
Introduction to trader bots with Python
Introduction to trader bots with PythonIntroduction to trader bots with Python
Introduction to trader bots with Python
 
Bot builder v4 HOL
Bot builder v4 HOLBot builder v4 HOL
Bot builder v4 HOL
 
Introduction to transaction processing concepts and theory
Introduction to transaction processing concepts and theoryIntroduction to transaction processing concepts and theory
Introduction to transaction processing concepts and theory
 
ProjectReport - Maurya,Shailesh
ProjectReport - Maurya,ShaileshProjectReport - Maurya,Shailesh
ProjectReport - Maurya,Shailesh
 
Thread
ThreadThread
Thread
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Study Notes: Google Percolator
Study Notes: Google PercolatorStudy Notes: Google Percolator
Study Notes: Google Percolator
 
Tech_Implementation of Complex ITIM Workflows
Tech_Implementation of Complex ITIM WorkflowsTech_Implementation of Complex ITIM Workflows
Tech_Implementation of Complex ITIM Workflows
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transaction
 
Dependency injection - the right way
Dependency injection - the right wayDependency injection - the right way
Dependency injection - the right way
 
Junit in mule demo
Junit in mule demoJunit in mule demo
Junit in mule demo
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdfProgramming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
 

More from ivylinvaydak64229

For the hypothesis test H0 = 5 against H1 5 with variance unkn.pdf
For the hypothesis test H0  = 5 against H1   5 with variance unkn.pdfFor the hypothesis test H0  = 5 against H1   5 with variance unkn.pdf
For the hypothesis test H0 = 5 against H1 5 with variance unkn.pdfivylinvaydak64229
 
Early in 2017 scientists have discovered a new family of eukaryotic b.pdf
Early in 2017 scientists have discovered a new family of eukaryotic b.pdfEarly in 2017 scientists have discovered a new family of eukaryotic b.pdf
Early in 2017 scientists have discovered a new family of eukaryotic b.pdfivylinvaydak64229
 
Do you believe great leaders are born or madeSolutioni believe.pdf
Do you believe great leaders are born or madeSolutioni believe.pdfDo you believe great leaders are born or madeSolutioni believe.pdf
Do you believe great leaders are born or madeSolutioni believe.pdfivylinvaydak64229
 
Every year, the viral strains included in vaccinations for the flu a.pdf
Every year, the viral strains included in vaccinations for the flu a.pdfEvery year, the viral strains included in vaccinations for the flu a.pdf
Every year, the viral strains included in vaccinations for the flu a.pdfivylinvaydak64229
 
Describe the role of different types of genomic changes in the evolut.pdf
Describe the role of different types of genomic changes in the evolut.pdfDescribe the role of different types of genomic changes in the evolut.pdf
Describe the role of different types of genomic changes in the evolut.pdfivylinvaydak64229
 
Describe the Darwinian theory of evolutionDescribe the Darwi.pdf
Describe the Darwinian theory of evolutionDescribe the Darwi.pdfDescribe the Darwinian theory of evolutionDescribe the Darwi.pdf
Describe the Darwinian theory of evolutionDescribe the Darwi.pdfivylinvaydak64229
 
Consider any organization where you’ve worked in the past, where you.pdf
Consider any organization where you’ve worked in the past, where you.pdfConsider any organization where you’ve worked in the past, where you.pdf
Consider any organization where you’ve worked in the past, where you.pdfivylinvaydak64229
 
Analyze the detected attacks and create a report that describes each.pdf
Analyze the detected attacks and create a report that describes each.pdfAnalyze the detected attacks and create a report that describes each.pdf
Analyze the detected attacks and create a report that describes each.pdfivylinvaydak64229
 
Collect 50 or more paired quantitative data items. You may use a met.pdf
Collect 50 or more paired quantitative data items. You may use a met.pdfCollect 50 or more paired quantitative data items. You may use a met.pdf
Collect 50 or more paired quantitative data items. You may use a met.pdfivylinvaydak64229
 
Assume you have decided to implement DFS so remote sites can access .pdf
Assume you have decided to implement DFS so remote sites can access .pdfAssume you have decided to implement DFS so remote sites can access .pdf
Assume you have decided to implement DFS so remote sites can access .pdfivylinvaydak64229
 
Are the following events SOURCES or USES of cashDecrease in Accou.pdf
Are the following events SOURCES or USES of cashDecrease in Accou.pdfAre the following events SOURCES or USES of cashDecrease in Accou.pdf
Are the following events SOURCES or USES of cashDecrease in Accou.pdfivylinvaydak64229
 
A. What are two advantages that the use of green fluorescent protein.pdf
A. What are two advantages that the use of green fluorescent protein.pdfA. What are two advantages that the use of green fluorescent protein.pdf
A. What are two advantages that the use of green fluorescent protein.pdfivylinvaydak64229
 
A species has a diploid number of 2n. Meiosis I fails during spermato.pdf
A species has a diploid number of 2n. Meiosis I fails during spermato.pdfA species has a diploid number of 2n. Meiosis I fails during spermato.pdf
A species has a diploid number of 2n. Meiosis I fails during spermato.pdfivylinvaydak64229
 
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdfA New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdfivylinvaydak64229
 
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdfA protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdfivylinvaydak64229
 
What were the driving forces behind the creation of the FAA and ICAO.pdf
What were the driving forces behind the creation of the FAA and ICAO.pdfWhat were the driving forces behind the creation of the FAA and ICAO.pdf
What were the driving forces behind the creation of the FAA and ICAO.pdfivylinvaydak64229
 
write two paragraphs on the polices to reduce income inequality and .pdf
write two paragraphs on the polices to reduce income inequality and .pdfwrite two paragraphs on the polices to reduce income inequality and .pdf
write two paragraphs on the polices to reduce income inequality and .pdfivylinvaydak64229
 
Where might you find the gametophytes of… Where might you find the g.pdf
Where might you find the gametophytes of… Where might you find the g.pdfWhere might you find the gametophytes of… Where might you find the g.pdf
Where might you find the gametophytes of… Where might you find the g.pdfivylinvaydak64229
 
What single , unique characteristic of a protist would be conside.pdf
What single , unique characteristic of a protist would be conside.pdfWhat single , unique characteristic of a protist would be conside.pdf
What single , unique characteristic of a protist would be conside.pdfivylinvaydak64229
 
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdfWhat are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdfivylinvaydak64229
 

More from ivylinvaydak64229 (20)

For the hypothesis test H0 = 5 against H1 5 with variance unkn.pdf
For the hypothesis test H0  = 5 against H1   5 with variance unkn.pdfFor the hypothesis test H0  = 5 against H1   5 with variance unkn.pdf
For the hypothesis test H0 = 5 against H1 5 with variance unkn.pdf
 
Early in 2017 scientists have discovered a new family of eukaryotic b.pdf
Early in 2017 scientists have discovered a new family of eukaryotic b.pdfEarly in 2017 scientists have discovered a new family of eukaryotic b.pdf
Early in 2017 scientists have discovered a new family of eukaryotic b.pdf
 
Do you believe great leaders are born or madeSolutioni believe.pdf
Do you believe great leaders are born or madeSolutioni believe.pdfDo you believe great leaders are born or madeSolutioni believe.pdf
Do you believe great leaders are born or madeSolutioni believe.pdf
 
Every year, the viral strains included in vaccinations for the flu a.pdf
Every year, the viral strains included in vaccinations for the flu a.pdfEvery year, the viral strains included in vaccinations for the flu a.pdf
Every year, the viral strains included in vaccinations for the flu a.pdf
 
Describe the role of different types of genomic changes in the evolut.pdf
Describe the role of different types of genomic changes in the evolut.pdfDescribe the role of different types of genomic changes in the evolut.pdf
Describe the role of different types of genomic changes in the evolut.pdf
 
Describe the Darwinian theory of evolutionDescribe the Darwi.pdf
Describe the Darwinian theory of evolutionDescribe the Darwi.pdfDescribe the Darwinian theory of evolutionDescribe the Darwi.pdf
Describe the Darwinian theory of evolutionDescribe the Darwi.pdf
 
Consider any organization where you’ve worked in the past, where you.pdf
Consider any organization where you’ve worked in the past, where you.pdfConsider any organization where you’ve worked in the past, where you.pdf
Consider any organization where you’ve worked in the past, where you.pdf
 
Analyze the detected attacks and create a report that describes each.pdf
Analyze the detected attacks and create a report that describes each.pdfAnalyze the detected attacks and create a report that describes each.pdf
Analyze the detected attacks and create a report that describes each.pdf
 
Collect 50 or more paired quantitative data items. You may use a met.pdf
Collect 50 or more paired quantitative data items. You may use a met.pdfCollect 50 or more paired quantitative data items. You may use a met.pdf
Collect 50 or more paired quantitative data items. You may use a met.pdf
 
Assume you have decided to implement DFS so remote sites can access .pdf
Assume you have decided to implement DFS so remote sites can access .pdfAssume you have decided to implement DFS so remote sites can access .pdf
Assume you have decided to implement DFS so remote sites can access .pdf
 
Are the following events SOURCES or USES of cashDecrease in Accou.pdf
Are the following events SOURCES or USES of cashDecrease in Accou.pdfAre the following events SOURCES or USES of cashDecrease in Accou.pdf
Are the following events SOURCES or USES of cashDecrease in Accou.pdf
 
A. What are two advantages that the use of green fluorescent protein.pdf
A. What are two advantages that the use of green fluorescent protein.pdfA. What are two advantages that the use of green fluorescent protein.pdf
A. What are two advantages that the use of green fluorescent protein.pdf
 
A species has a diploid number of 2n. Meiosis I fails during spermato.pdf
A species has a diploid number of 2n. Meiosis I fails during spermato.pdfA species has a diploid number of 2n. Meiosis I fails during spermato.pdf
A species has a diploid number of 2n. Meiosis I fails during spermato.pdf
 
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdfA New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
 
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdfA protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
 
What were the driving forces behind the creation of the FAA and ICAO.pdf
What were the driving forces behind the creation of the FAA and ICAO.pdfWhat were the driving forces behind the creation of the FAA and ICAO.pdf
What were the driving forces behind the creation of the FAA and ICAO.pdf
 
write two paragraphs on the polices to reduce income inequality and .pdf
write two paragraphs on the polices to reduce income inequality and .pdfwrite two paragraphs on the polices to reduce income inequality and .pdf
write two paragraphs on the polices to reduce income inequality and .pdf
 
Where might you find the gametophytes of… Where might you find the g.pdf
Where might you find the gametophytes of… Where might you find the g.pdfWhere might you find the gametophytes of… Where might you find the g.pdf
Where might you find the gametophytes of… Where might you find the g.pdf
 
What single , unique characteristic of a protist would be conside.pdf
What single , unique characteristic of a protist would be conside.pdfWhat single , unique characteristic of a protist would be conside.pdf
What single , unique characteristic of a protist would be conside.pdf
 
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdfWhat are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
 

Recently uploaded

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

The program will read the file like this, java homework6Bank sma.pdf

  • 1. The program will read the file like this, > java homework6/Bank small.txt 4 acct:0 bal:999 trans:1 acct:1 bal:1001 trans:1 acct:2 bal:999 trans:1 acct:3 bal:1001 trans:1 acct:4 bal:999 trans:1 acct:5 bal:1001 trans:1 acct:6 bal:999 trans:1 acct:7 bal:1001 trans:1 acct:8 bal:999 trans:1 acct:9 bal:1001 trans:1 acct:10 bal:999 trans:1 acct:11 bal:1001 trans:1 acct:12 bal:999 trans:1 acct:13 bal:1001 trans:1 acct:14 bal:999 trans:1 acct:15 bal:1001 trans:1 acct:16 bal:999 trans:1 acct:17 bal:1001 trans:1 acct:18 bal:999 trans:1 acct:19 bal:1001 trans:1 Each text file looks something like: 1 2 1 3 4 1 5 6 1 7 8 1 9 10 1 11 12 1 File Format: Each line in the external file represents a single transaction, and contains three numbers: the id of the account from which the money is being transferred, the id of the account to which the money is going, and the amount of money. For example the line: 17 6 104 indicates that $104 is being transferred from Account #17 to Account #6. The test data provided includes transfers with the same from and to account numbers, so make
  • 2. sure your program will work correctly for these transfers. For example: 5 5 40 Count these as two transactions for the account (one transaction taking money from the account and one putting money into the account). My goal is to pass each transaction into the queue, the queue will hold the transaction, the worker will take the transaction, complete the deposit/withdraw, and update the balance of the account accordingly. I am required to use BlockingQueue. My problem is that the program is not running correctly. I need to fix the Bank class, how I start up the Bank in main thread, and also work on Worker class. More info: Details I recommend a design with four classes—Bank, Account, Transaction, and Worker. Both the Account and Transactions classes are quite simple. Account needs to store an id number, the current balance for the account, and the number of transactions that have occurred on the account. Remember that multiple worker threads may be accessing an account simultaneously and you must ensure that they cannot corrupt its data. You may also want to override the toString method to handle printing of account information. Transaction is a simple class that stores information on each transaction (see below for more information about each transaction). If you’re careful you can treat the Transaction as immutable. This means that you do not have to worry about multiple threads accessing it. Remember an immutable object’s values never change, therefore its values are not subject to corruption in a concurrent environment. The Bank class maintains a list of accounts and the BlockingQueue used to communicate between the main thread and the worker threads. The Bank is also responsible for starting up the worker threads, reading transactions from the file, and printing out all the account values when everything is done. Note: make sure you start up all the worker threads before reading the transactions from the file. I recommend making the Worker class is an inner class of the Bank class. This way it gets easy access to the list of accounts and the queue used for communication. Workers should check the queue for transactions. If they find a transaction they should process it. If the queue is empty, they will wait for the Bank class to read in another transaction (you’ll get this behavior for free by using a BlockingQueue). Workers terminate when all the transactions have been processed. package Problem1; public class Account { private int id; private int balance;
  • 3. private int numoft; public Account(int id, int balance, int numberOfTransactions) { this.id = id; this.balance = 1000; this.numoft = numberOfTransactions; } public int getBalance() { return balance; } public int getAccountNumber() { return id; } public int getNumOfTransactions() { return numoft; } public void deposite(int amount) { this.balance = this.balance + amount; } public void withdrawl(int amount) { this.balance = this.balance - amount; } public int addNumOfTrans(int amount) { return this.numoft = this.numoft + amount; }
  • 4. public String toString() { return "acct:" + id + " bal:" + balance + " trans:" + numoft + " "; } } _________________________________________ package Problem1; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /* * Transaction class does the calculation job */ public class Transaction { //private static File filename = new File("test2.txt"); // there can be two transactions, <--- goes to worker private int accountTo; private int accountFrom; private int amount; public Transaction(int accountTo, int accountFrom, int amount) { this.accountTo = accountTo; this.accountFrom = accountFrom; this.amount = amount; } public int getAccount1() { return accountTo; } public int getAccount2() {
  • 5. return accountFrom; } public int getTransferAmount() { return amount; } public String toString() { return accountTo + " " + accountFrom + " " + amount; } } ___________________________________ package Problem1; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; /* * Bank would read the file */ public class Bank implements Runnable { private ArrayList accounts; // the arraylist that has all the // account private ArrayList accountid; // the arraylist that hold all the // account id so i can find private ArrayList listOfT; // list of transaction private int worker;
  • 6. private File filename; private BlockingQueue queue; private final Transaction nullTrans = new Transaction(-1, 0, 0); private int numThread; public Bank(int numThread) { this.numThread = numThread; accounts = new ArrayList(); accountid = new ArrayList(); listOfT = new ArrayList(); } public void addAccount(Account a) { accounts.add(a); } // public Account replaceAccount(Account e) // { // return this.accounts.get(index) = e; // } public boolean checkAccounts(int id) { for (Account b : accounts) { if (b.getAccountNumber() == id) { return true; // account exist } } return false; } public Account find(int accountNumber) { for (Account a : accounts) { if (a.getAccountNumber() == accountNumber) // Found a match return a; } // No match in the entire array list throw new IllegalArgumentException("Account number " + accountNumber + " was not found in the bank"); } public String toString() {
  • 7. for (Account a : accounts) { // System.out.println("acct:" + a.getAccountNumber() + " bal:" + // a.getBalance() + " trans:" + a.getNumOfTransactions()); return "acct:" + a.getAccountNumber() + " bal:" + a.getBalance() + " trans:" + a.getNumOfTransactions(); } return null; } public void readTransaction(Scanner in) { int a1; int a2; int balance; while (in.hasNext()) { a1 = in.nextInt(); a2 = in.nextInt(); balance = in.nextInt(); if (!checkAccounts(a1)) { Account newAccount = new Account(a1, 1000, 0); addAccount(newAccount); accountid.add(a1); } if (!checkAccounts(a2)) { Account newAccount = new Account(a2, 1000, 0); addAccount(newAccount); accountid.add(a2); } listOfT.add(new Transaction(a1, a2, balance)); } } @Override public void run() { // TODO Auto-generated method stub try { queue.put(listOfT.get(0)); } catch (InterruptedException e) { // TODO Auto-generated catch block
  • 8. e.printStackTrace(); } } // main thread public static void main(String[] args) throws FileNotFoundException { // start the worker's thread before transaction //int thread = Integer.parseInt(args[1]); // BlockingQueue q = new ArrayBlockingQueue(50); int thread = 4; // numthread Bank myBank = new Bank(thread); myBank.queue = new ArrayBlockingQueue(50); new Thread(myBank).start(); for (int i = 0; i < myBank.queue.size(); i++) { myBank.new Worker().start(); } myBank.filename = new File("test2.txt"); Scanner in = new Scanner(myBank.filename); myBank.readTransaction(in); System.out.println(myBank.accounts); // reading transactions } class Worker extends Thread { // receive the transaction, add the number of transaction amount into // queue // queue will hold the transaction action // find the from account and to account, run() method // get the money public void run() { // TODO Auto-generated method stub while (queue != nullTrans) { try { takeTransaction(queue.take()); } catch (InterruptedException e) {
  • 9. // TODO Auto-generated catch block e.printStackTrace(); } } } public void takeTransaction(Transaction t) { int a1; int a2; int balance; a1 = t.getAccount1(); a2 = t.getAccount2(); balance = t.getTransferAmount(); Account account1 = find(a1); Account account2 = find(a2); account1.withdrawl(balance); account2.deposite(balance); account1.addNumOfTrans(1); account2.addNumOfTrans(1); accounts.remove(a1); addAccount(account1); accounts.remove(a2); addAccount(account2); } } } // you put stuff in queue from bank, // the worker receive it and do the transaction /* * */ Solution public class Account {
  • 10. private int id; private int balance; private int numoft; public Account(int id, int balance, int numberOfTransactions) { this.id = id; this.balance = 1000; this.numoft = numberOfTransactions; } public int getBalance() { return balance; } public int getAccountNumber() { return id; } public int getNumOfTransactions() { return numoft; } public void deposite(int amount) { this.balance = this.balance + amount; } public void withdrawl(int amount) { this.balance = this.balance - amount; } public int addNumOfTrans(int amount) {
  • 11. return this.numoft = this.numoft + amount; } public String toString() { return "acct:" + id + " bal:" + balance + " trans:" + numoft + " "; } } _________________________________________ package Problem1; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /* * Transaction class does the calculation job */ public class Transaction { //private static File filename = new File("test2.txt"); // there can be two transactions, <--- goes to worker private int accountTo; private int accountFrom; private int amount; public Transaction(int accountTo, int accountFrom, int amount) { this.accountTo = accountTo; this.accountFrom = accountFrom; this.amount = amount; } public int getAccount1() { return accountTo; }
  • 12. public int getAccount2() { return accountFrom; } public int getTransferAmount() { return amount; } public String toString() { return accountTo + " " + accountFrom + " " + amount; } } ___________________________________ package Problem1; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; /* * Bank would read the file */ public class Bank implements Runnable { private ArrayList accounts; // the arraylist that has all the // account private ArrayList accountid; // the arraylist that hold all the // account id so i can find
  • 13. private ArrayList listOfT; // list of transaction private int worker; private File filename; private BlockingQueue queue; private final Transaction nullTrans = new Transaction(-1, 0, 0); private int numThread; public Bank(int numThread) { this.numThread = numThread; accounts = new ArrayList(); accountid = new ArrayList(); listOfT = new ArrayList(); } public void addAccount(Account a) { accounts.add(a); } // public Account replaceAccount(Account e) // { // return this.accounts.get(index) = e; // } public boolean checkAccounts(int id) { for (Account b : accounts) { if (b.getAccountNumber() == id) { return true; // account exist } } return false; } public Account find(int accountNumber) { for (Account a : accounts) { if (a.getAccountNumber() == accountNumber) // Found a match return a; } // No match in the entire array list throw new IllegalArgumentException("Account number " + accountNumber + " was not found in the bank");
  • 14. } public String toString() { for (Account a : accounts) { // System.out.println("acct:" + a.getAccountNumber() + " bal:" + // a.getBalance() + " trans:" + a.getNumOfTransactions()); return "acct:" + a.getAccountNumber() + " bal:" + a.getBalance() + " trans:" + a.getNumOfTransactions(); } return null; } public void readTransaction(Scanner in) { int a1; int a2; int balance; while (in.hasNext()) { a1 = in.nextInt(); a2 = in.nextInt(); balance = in.nextInt(); if (!checkAccounts(a1)) { Account newAccount = new Account(a1, 1000, 0); addAccount(newAccount); accountid.add(a1); } if (!checkAccounts(a2)) { Account newAccount = new Account(a2, 1000, 0); addAccount(newAccount); accountid.add(a2); } listOfT.add(new Transaction(a1, a2, balance)); } } @Override public void run() { // TODO Auto-generated method stub try { queue.put(listOfT.get(0));
  • 15. } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // main thread public static void main(String[] args) throws FileNotFoundException { // start the worker's thread before transaction //int thread = Integer.parseInt(args[1]); // BlockingQueue q = new ArrayBlockingQueue(50); int thread = 4; // numthread Bank myBank = new Bank(thread); myBank.queue = new ArrayBlockingQueue(50); new Thread(myBank).start(); for (int i = 0; i < myBank.queue.size(); i++) { myBank.new Worker().start(); } myBank.filename = new File("test2.txt"); Scanner in = new Scanner(myBank.filename); myBank.readTransaction(in); System.out.println(myBank.accounts); // reading transactions } class Worker extends Thread { // receive the transaction, add the number of transaction amount into // queue // queue will hold the transaction action // find the from account and to account, run() method // get the money public void run() { // TODO Auto-generated method stub while (queue != nullTrans) { try {
  • 16. takeTransaction(queue.take()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void takeTransaction(Transaction t) { int a1; int a2; int balance; a1 = t.getAccount1(); a2 = t.getAccount2(); balance = t.getTransferAmount(); Account account1 = find(a1); Account account2 = find(a2); account1.withdrawl(balance); account2.deposite(balance); account1.addNumOfTrans(1); account2.addNumOfTrans(1); accounts.remove(a1); addAccount(account1); accounts.remove(a2); addAccount(account2); } } }