Advertisement
spiny94

Untitled

Sep 11th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. kage 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.  
  42.         if (date < accounts.get(code).getDate()) {
  43.             date = accounts.get(code).getDate();
  44.         }
  45.        
  46.  
  47.         return accounts.remove(code) ;
  48.     }
  49.  
  50.     public Account getAccount(int code) throws InvalidCode {
  51.  
  52.         if (!accounts.containsKey(code)) {
  53.             throw new InvalidCode();
  54.         }
  55.         return accounts.get(code);
  56.     }
  57.  
  58.     public void deposit(int code, int date, double value) throws InvalidCode {
  59.         if (!accounts.containsKey(code)) {
  60.             throw new InvalidCode();
  61.         } else {
  62.             double v = accounts.get(code).getInitial() + value;
  63.             a.addValue(v);
  64.             if (date < accounts.get(code).getDate()) {
  65.                 date = accounts.get(code).getDate();
  66.  
  67.             }
  68.         }
  69.     }
  70.  
  71.     public void withdraw(int code, int date, double value) throws InvalidCode,
  72.             InvalidValue {
  73.         if (!accounts.containsKey(code)) {
  74.             throw new InvalidCode();
  75.         }
  76.         if (value < accounts.get(code).getValue()) {
  77.             throw new InvalidValue();
  78.         } else {
  79.  
  80.             prelievi.add(accounts.get(code).getValue() - value);
  81.             if (date < accounts.get(code).getDate()) {
  82.                 date = accounts.get(code).getDate();
  83.  
  84.             }
  85.         }
  86.     }
  87.  
  88.     public void transfer(int fromCode, int toCode, int date, double value)
  89.             throws InvalidCode, InvalidValue {
  90.         if (!accounts.containsKey(fromCode) || !accounts.containsKey(toCode)) {
  91.             throw new InvalidCode();
  92.         }
  93.  
  94.         double s = accounts.get(fromCode).getValue();
  95.        
  96.         //if (s > accounts.get(toCode).getValue()) {
  97.         if (value > s){
  98.         throw new InvalidValue();
  99.         } else {
  100.             double t = accounts.get(toCode).getValue();
  101.             t += s;
  102.  
  103.         }
  104.  
  105.     }
  106.  
  107.     public double getTotalDeposit() {
  108.         double sum = 0;
  109.         Collection<Account> at = accounts.values();
  110.         Iterator<Account> it = at.iterator();
  111.         while (it.hasNext()) {
  112.             Account a = it.next();
  113.             sum += a.getValue();
  114.  
  115.         }
  116.         return sum;
  117.     }
  118.  
  119.     public List<Account> getAccounts() {
  120.         List<Account> p = new LinkedList<Account>(accounts.values());
  121.  
  122.         return p;
  123.     }
  124.  
  125.     public List<Account> getAccountsByBalance(double low, double high) {
  126.  
  127.         List<Account> sal = new LinkedList<Account>();
  128.         Collection<Account> at = accounts.values();
  129.         Iterator<Account> it = at.iterator();
  130.         while (it.hasNext()) {
  131.             Account a = it.next();
  132.             if (a.getValue() > low && a.getValue() < high) {
  133.                 sal.add(a);
  134.             }
  135.         }
  136.         return sal;
  137.     }
  138.  
  139.     public double getPerCentHigher(double min) {
  140.         double per;
  141.         double c = 0;
  142.         Collection<Account> at = accounts.values();
  143.         Iterator<Account> it = at.iterator();
  144.         while (it.hasNext()) {
  145.             Account a = it.next();
  146.             if (a.getValue() > min) {
  147.                 c++;
  148.  
  149.             }
  150.         }
  151.         per = c / at.size() * 100;
  152.         return per;
  153.  
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement