Guest User

Untitled

a guest
Apr 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. import java.io.*;
  2.  
  3.  
  4. public class CheckAccount {
  5. //Constructor
  6. CheckAccount(String info){
  7. String[] passedInfo = info.split(" ");
  8.  
  9.  
  10.  
  11. this.fileLoc = passedInfo[1];
  12. this.accNum = passedInfo[2];
  13. this.fName = passedInfo[3];
  14. this.lName = passedInfo[4];
  15. this.bal = Double.parseDouble(passedInfo[5]);
  16.  
  17.  
  18.  
  19. File file = new File(fileLoc);
  20. try {
  21. int counter = 0;
  22. BufferedReader in = new BufferedReader(new FileReader(file));
  23. try {
  24. String line = null;
  25. while ((line = in.readLine()) != null && counter <= 30)
  26. {
  27. transactions[counter] = line;
  28. counter++;
  29. }
  30. }
  31. finally {
  32. in.close();
  33. }
  34. }
  35. catch (IOException ex){
  36. System.out.println("File Read Error for: " + fileLoc);
  37. }
  38.  
  39. /*for (int c = 0; c < transactions.length; c++){
  40. System.out.println(transactions[c]);
  41. } */
  42.  
  43. run();
  44. }
  45.  
  46.  
  47.  
  48. private void run(){
  49. System.out.println("Account: " + accNum + " Name: " + fName + " " + lName);
  50. System.out.println(padLeft("Date", 13) + padLeft("Description", 40) + padRight("Amount", 8) + " Balance");
  51. System.out.println(padLeft("", 13) + padLeft("Your balance at beginning of period", 40) + padLeft("", 8) + padRight("" + bal, 10));
  52.  
  53.  
  54.  
  55. double[][] checksCleared = new double[30][2];
  56. int cc = 0;
  57. double checkTotal = 0, depositTotal = 0, servTotal = 0;
  58.  
  59.  
  60.  
  61. for (int trans = 0; transactions[trans] != null; trans++){
  62. String[] oneTrans = transactions[trans].split(" ");
  63. boolean isDeposit = oneTrans[0].equals("DPT");
  64. String date = oneTrans[1] + "-" + oneTrans[2] + "-" + oneTrans[3];
  65. double amt = 0;
  66. if(isDeposit){
  67. amt = Double.parseDouble(oneTrans[4]);
  68. bal += amt;
  69. depositTotal += amt;
  70. System.out.println(padLeft(date, 13) + padLeft("Deposit", 40) + padRight("" + Math.round(amt*100)/100 + fixdp("." + Math.abs(Math.round(amt*100)%100)), 8) + padRight("" + Math.round(bal*100)/100 + fixdp("." + Math.abs(Math.round(bal*100)%100)), 10));
  71. }
  72. else{
  73. amt = Double.parseDouble(oneTrans[5]);
  74. int checkNum = Integer.parseInt(oneTrans[4]);
  75. String desc = oneTrans[6];
  76. checksCleared[cc][0] = checkNum;
  77. checksCleared[cc][1] = amt;
  78. cc++;
  79. bal -= amt;
  80. checkTotal += amt;
  81. System.out.println(padLeft(date, 13) + padLeft(desc.trim() + ", #" + checkNum, 40) + padRight("" + Math.round(amt*100)/100 + fixdp("." + Math.abs(Math.round(amt*100)%100)), 8) + padRight("" + Math.round(bal*100)/100 + fixdp("." + Math.abs(Math.round(bal*100)%100)), 10));
  82. if(bal < 0){
  83. bal -= 20;
  84. servTotal += 20;
  85. System.out.println(padLeft(date, 13) + padLeft("Service Fee", 40) + padRight("20.00", 8) + padRight("" + Math.round(bal*100)/100 + "." + Math.abs(Math.round(bal*100)%100), 10));
  86. }
  87. }
  88. }
  89.  
  90.  
  91.  
  92. System.out.println("\n" + padLeft("Total checks:", 20) + padRight("" + Math.round(checkTotal*100)/100 + fixdp("." + Math.abs(Math.round(checkTotal*100)%100)), 10));
  93. System.out.println(padLeft("Total deposits:", 20) + padRight("" + Math.round(depositTotal*100)/100 + fixdp("." + Math.abs(Math.round(depositTotal*100)%100)), 10));
  94. System.out.println(padLeft("Total service fees:", 20) + padRight("" + Math.round(servTotal*100)/100 + fixdp("." + Math.abs(Math.round(servTotal*100)%100)), 10) + "\n");
  95.  
  96.  
  97.  
  98. quickSort(checksCleared);
  99.  
  100.  
  101.  
  102. System.out.println("\t\tChecks Cleared");
  103.  
  104.  
  105.  
  106. for (int c = 0; c < checksCleared.length; c++){
  107. if ((int) checksCleared[c][0] != 0){
  108. String ccnum = "#" + (int) checksCleared[c][0];;
  109. if (c != 0 && checksCleared[c-1][0] != checksCleared[c][0] - 1 && checksCleared[c-1][0] != 0)
  110. ccnum += "*";
  111. System.out.println("\t\t" + padLeft(ccnum, 5) + padRight("" + Math.round(checksCleared[c][1]*100)/100 + fixdp("." + Math.abs(Math.round(checksCleared[c][1]*100)%100)), 6));
  112. }
  113. }
  114. }
  115.  
  116.  
  117.  
  118. private void quickSort(double ccarray[][]){
  119. quickSort(ccarray, 0, ccarray.length - 1);
  120. }
  121.  
  122.  
  123. public void quickSort(double ccarray[][], int start, int end){
  124. int i = start;
  125. int j = end;
  126.  
  127.  
  128. if (end - start >= 1){
  129. double pivot = ccarray[start][0];
  130.  
  131.  
  132. while (j > i){
  133. while (ccarray[i][0] <= pivot && i <= end && j > i)
  134. i++;
  135. while (ccarray[j][0] > pivot && j >= start && j >= i)
  136. j--;
  137. if (j > i)
  138. swap(ccarray, i, j);
  139. }
  140. swap(ccarray, start, j);
  141. quickSort(ccarray, start, j - 1);
  142. quickSort(ccarray, j + 1, end);
  143. }
  144. else{
  145. return;
  146. }
  147. }
  148.  
  149.  
  150. public void swap(double ccarray[][], int loc1, int loc2){
  151. double[] temp = ccarray[loc1];
  152. ccarray[loc1] = ccarray[loc2];
  153. ccarray[loc2] = temp;
  154. }
  155.  
  156.  
  157.  
  158. private String padLeft(String str, int length){
  159. int desL = length - str.length();
  160. for (int c = 0; c < desL; c++)
  161. str = str + " ";
  162. return str;
  163. }
  164.  
  165.  
  166.  
  167. private String padRight(String str, int length){
  168. int desL = length - str.length();
  169. for (int c = 0; c < desL; c++)
  170. str = " " + str;
  171. return str;
  172. }
  173.  
  174.  
  175.  
  176. private String fixdp(String toFix){
  177. if (toFix.endsWith(".0"))
  178. toFix += "0";
  179. if (toFix.length() == 2)
  180. toFix = ".0" + toFix.substring(1);
  181. return toFix;
  182. }
  183.  
  184.  
  185.  
  186. //Instance variables
  187. String fileLoc = null, accNum = null, fName = null, lName = null;
  188. double bal = 0;
  189. String transactions[] = new String[30];
  190. }
Add Comment
Please, Sign In to add comment