Advertisement
spiny94

Untitled

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