Advertisement
spiny94

Untitled

Sep 13th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. package BankServices;
  2.  
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9.  
  10. public class Bank {
  11.  
  12. private String n;
  13. private int num = 1;
  14. private double v = 0;
  15. private double prelievo;
  16. private double soldi;
  17.  
  18. Account a;
  19. List<Double> prelievi = new LinkedList<>();
  20. List<Double> s = new LinkedList<>();
  21. HashMap<Integer, Account> accounts = new HashMap<>();
  22.  
  23. public Bank(String n) {
  24. this.n = n;
  25. }
  26.  
  27. public String getName() {
  28. return n;
  29. }
  30.  
  31. public int createAccount(String name, int date, double initial) {
  32. int numd = num++;
  33. a = new Account(numd, name, date, initial);
  34. // int numd = num++;
  35. accounts.put(numd, a);
  36. return numd;
  37. }
  38.  
  39. public Account deleteAccount(int code, int date) throws InvalidCode {
  40. if (!accounts.containsKey(code)) {
  41. throw new InvalidCode();
  42. }
  43. // s.add(accounts.get(code).getValue());
  44. s.add(accounts.get(code).getBalance());
  45. if (date < accounts.get(code).getDate()) {
  46. date = accounts.get(code).getDate();
  47. }
  48. accounts.get(code).setBalance(0);
  49. return accounts.remove(code);
  50. }
  51.  
  52. public Account getAccount(int code) throws InvalidCode {
  53.  
  54. if (!accounts.containsKey(code)) {
  55. throw new InvalidCode();
  56. }
  57. return accounts.get(code);
  58. }
  59.  
  60. public void deposit(int code, int date, double value) throws InvalidCode {
  61. if (!accounts.containsKey(code)) {
  62. throw new InvalidCode();
  63. } else {
  64. // double v = accounts.get(code).getInitial() + value;
  65. // a.addValue(v);
  66. // accounts.get(code).addValue(v);
  67. // accounts.get(code).addToBalance(value);
  68. if (date < accounts.get(code).getDate()) {
  69. date = accounts.get(code).getDate();
  70. accounts.get(code).addToBalance(date, value);
  71. }
  72. }
  73. }
  74.  
  75. public void withdraw(int code, int date, double value) throws InvalidCode,
  76. InvalidValue {
  77. if (!accounts.containsKey(code)) {
  78. throw new InvalidCode();
  79. }
  80. // if (value < accounts.get(code).getValue())
  81. if (value > accounts.get(code).getBalance()) {
  82.  
  83. throw new InvalidValue();
  84. } else {
  85.  
  86. // prelievi.add(accounts.get(code).getValue() - value);
  87. // prelievi.add(accounts.get(code).getBalance() - value);
  88. // double t = accounts.get(code).getBalance() - value;
  89. // accounts.get(code).subtractFromBalance(value);
  90. if (date < accounts.get(code).getDate()) {
  91. date = accounts.get(code).getDate();
  92. accounts.get(code).subtractFromBalance(date, value);
  93. }
  94. }
  95. }
  96.  
  97. public void transfer(int fromCode, int toCode, int date, double value)
  98. throws InvalidCode, InvalidValue {
  99. if (!accounts.containsKey(fromCode) || !accounts.containsKey(toCode)) {
  100. throw new InvalidCode();
  101. }
  102. // double s = accounts.get(fromCode).getValue();
  103. // double s = accounts.get(fromCode).getBalance();
  104. // if (s > accounts.get(toCode).getValue()) {
  105.  
  106. // if (value > s) {
  107. if (value > accounts.get(fromCode).getBalance()) {
  108. throw new InvalidValue();
  109. } else {
  110. // double t = accounts.get(toCode).getValue();
  111. // double t = accounts.get(toCode).getBalance();
  112. // double t;
  113. // t = s- value;
  114. accounts.get(fromCode).subtractFromBalance(date, value);
  115. // accounts.get(toCode).addToBalance( value);
  116. accounts.get(toCode).addToBalance(date, value);
  117. // t += s;
  118.  
  119. }
  120.  
  121. }
  122.  
  123. public double getTotalDeposit() {
  124. double sum = 0;
  125. Collection<Account> at = accounts.values();
  126. Iterator<Account> it = at.iterator();
  127. while (it.hasNext()) {
  128. Account a = it.next();
  129. // sum += a.getValue();
  130. sum += a.getBalance();
  131. }
  132. return sum;
  133. }
  134.  
  135. public List<Account> getAccounts() {
  136. List<Account> p = new LinkedList<Account>(accounts.values());
  137.  
  138. return p;
  139. }
  140.  
  141. public List<Account> getAccountsByBalance(double low, double high) {
  142.  
  143. List<Account> sal = new LinkedList<Account>();
  144. Collection<Account> at = accounts.values();
  145. Iterator<Account> it = at.iterator();
  146. while (it.hasNext()) {
  147. Account a = it.next();
  148. // if (a.getValue() > low && a.getValue() < high)
  149. if (a.getBalance() > low && a.getBalance() < high) {
  150. sal.add(a);
  151. }
  152. }
  153.  
  154. return sal;
  155. }
  156.  
  157. public double getPerCentHigher(double min) {
  158. double per;
  159. double c = 0;
  160. Collection<Account> at = accounts.values();
  161. Iterator<Account> it = at.iterator();
  162. while (it.hasNext()) {
  163. Account a = it.next();
  164. // if (a.getValue() > min)
  165. if (a.getBalance() > min) {
  166. c++;
  167.  
  168. }
  169. }
  170. per = c / at.size() * 100;
  171. return per;
  172.  
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement