Advertisement
spiny94

Untitled

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