Anophoo

bank

May 15th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.ArrayList;
  7. import java.util.StringTokenizer;
  8. import java.util.concurrent.ArrayBlockingQueue;
  9. import java.util.concurrent.BlockingQueue;
  10. import java.util.concurrent.CountDownLatch;
  11.  
  12. public class Bank {
  13. private static final int NUMBER_OF_ACCOUNTS = 20;
  14. private static final int DEFAULT_BALANCE = 1000;
  15. private static final int TRANSACTION_LIMIT = 30;
  16. private static int numberOfWorkers = 5; // for testing
  17. private static final String FILENAME = "small.txt"; // for testing
  18.  
  19. private static Transaction nullTransaction;
  20. private static BlockingQueue<Transaction> queue;
  21. private static ArrayList<Account> accounts;
  22. private static ArrayList<Worker> workers;
  23. private static CountDownLatch latch;
  24.  
  25. public Bank(String filename, String workerNum) {
  26. nullTransaction = new Transaction(-1, 0, 0);
  27. latch = new CountDownLatch(numberOfWorkers);
  28. numberOfWorkers = Integer.parseInt(workerNum);
  29. workers = new ArrayList<Worker>();
  30. accounts = new ArrayList<Account>();
  31. queue = new ArrayBlockingQueue<Transaction>(TRANSACTION_LIMIT);
  32. workerSetup();
  33. accountSetup();
  34. runThreads();
  35. readTransactions(filename);
  36. }
  37.  
  38. /*
  39. * set up worker information
  40. */
  41. private void workerSetup() {
  42. for (int i = 0; i < numberOfWorkers; i++) {
  43. Worker w = new Worker();
  44. workers.add(w);
  45. }
  46. }
  47.  
  48. /*
  49. * set up account information
  50. */
  51. private void accountSetup() {
  52. for (int i = 0; i < NUMBER_OF_ACCOUNTS; i++) {
  53. Account ac = new Account(i, DEFAULT_BALANCE);
  54. accounts.add(ac);
  55. }
  56. }
  57.  
  58. /*
  59. * after account information is filled out we can run threads
  60. */
  61. private void runThreads() {
  62. for (int i = 0; i < numberOfWorkers; i++) {
  63. workers.get(i).start();
  64. }
  65. }
  66.  
  67. public static String readFile(String filename) {
  68. String content = null;
  69. File file = new File(filename);
  70. FileReader reader = null;
  71. try {
  72. reader = new FileReader(file);
  73. content = new String(Files.readAllBytes(Paths.get(filename)));
  74. reader.close();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. return content;
  79. }
  80.  
  81. private static void readTransactions(String filename) {
  82. String file = readFile(filename);
  83. StringTokenizer st = new StringTokenizer(file);
  84. // it is guaranteed that (tokens % 3) = 0
  85. try {
  86. while (st.hasMoreTokens()) {
  87. int from = Integer.parseInt(st.nextToken());
  88. int where = Integer.parseInt(st.nextToken());
  89. int amount = Integer.parseInt(st.nextToken());
  90. Transaction trans = new Transaction(from, where, amount);
  91. queue.put(trans);
  92. }
  93. for (int i = 0; i < numberOfWorkers; i++) {
  94. queue.put(nullTransaction);
  95. }
  96. } catch (InterruptedException ex) {}
  97. }
  98.  
  99. private void printAccountValues() {
  100. for (int i = 0; i < accounts.size(); i++) {
  101. System.out.println(accounts.get(i).toString());
  102. }
  103. }
  104.  
  105. public static void main(String[] args) {
  106.  
  107. String filename = args[0];
  108. String numberOfWorkers = args[1];
  109. Bank bank = new Bank(filename, numberOfWorkers);
  110.  
  111. try {
  112. latch.await();
  113. } catch (InterruptedException ex) {}
  114. bank.printAccountValues();
  115. }
  116.  
  117. /*
  118. * If they find a transaction they should process it. If the queue is empty,
  119. * they will wait for the Bank class to read in another transaction (you'll
  120. * get this behavior for free by using a BlockingQueue). Workers terminate
  121. * when all the transactions have been processed
  122. */
  123. public class Worker extends Thread {
  124.  
  125. @Override
  126. public void run() {
  127. while (true) {
  128. try {
  129. Transaction tmp = queue.take();
  130. if (tmp == nullTransaction)
  131. break;
  132. else {
  133. // System.out.println("making transaction");
  134. accounts.get(tmp.getFrom()).process(tmp);
  135. accounts.get(tmp.getWhere()).process(new Transaction
  136. (tmp.getWhere(), tmp.getFrom(), -tmp.getAmount()));
  137. }
  138. } catch (Exception ignored) {
  139. ignored.printStackTrace();
  140. }
  141. }
  142. System.out.println(Thread.currentThread().getName() + " has finished");
  143. latch.countDown();
  144. }
  145.  
  146. }
  147. }
Add Comment
Please, Sign In to add comment