Advertisement
irmantas_radavicius

Purse.java

Mar 27th, 2022
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class UnsupportedCoinValueException extends Exception {
  5.     public UnsupportedCoinValueException(String message){
  6.         super(message);
  7.     }
  8. }
  9. class CoinUnavailableException extends Exception {
  10.     public CoinUnavailableException(String message){
  11.         super(message);
  12.     }
  13. }
  14.  
  15. class Purse {
  16.     private double balance = 0;
  17.     private ArrayList<Double> allowedValues = null;
  18.     private ArrayList<Double> coins = new ArrayList<>();
  19.     public Purse(){    
  20.     }
  21.     public Purse(ArrayList<Double> allowedValues){
  22.         this.allowedValues = allowedValues;
  23.     }
  24.    
  25.     public void setCoins(ArrayList<Double> coins){
  26.         this.coins = coins;
  27.     }
  28.    
  29.     public ArrayList<Double> getCoins(){
  30.         return coins;
  31.     }
  32.    
  33.     private void setBalance(double balance){
  34.         if (balance >= 0)
  35.             this.balance = (Math.round(balance*100))/100.0;
  36.         else
  37.             throw new IllegalArgumentException("Balance cannot be negative");
  38.     }
  39.     public ArrayList<Double> getChange() throws Exception {
  40.         ArrayList<Double> change = new ArrayList<>();
  41.         double balance = getBalance();
  42.                
  43.         HashSet<Double> hset = new HashSet<Double>(coins);         
  44.         ArrayList<Double> coinsUnique = new ArrayList<Double>(hset);
  45.         Collections.sort(coinsUnique);     
  46.        
  47.         for(int i = coinsUnique.size()-1; i >= 0; ){                   
  48.             double currentValue = coinsUnique.get(i);
  49.             if((balance >= currentValue) && (coins.contains(currentValue))){
  50.                 balance = balance - currentValue;
  51.                 coins.remove(currentValue);
  52.                 change.add(currentValue);
  53.             } else {
  54.                 --i;
  55.             }
  56.         }
  57.         if (balance > 0){
  58.             int i = 0;         
  59.             while(true){
  60.                 double currentValue = coinsUnique.get(i);
  61.                 if(coins.contains(currentValue)){
  62.                     coins.remove(currentValue);
  63.                     change.add(currentValue);
  64.                     break;
  65.                 } else {                   
  66.                     ++i;
  67.                     if(i == coinsUnique.size()){
  68.                         throw new CoinUnavailableException("Value " + currentValue + " unavailable");
  69.                     }
  70.                 }
  71.             }                  
  72.         }
  73.                
  74.         setBalance(0);
  75.         return change;     
  76.     }
  77.     public double getBalance(){
  78.         return balance;
  79.     }
  80.     public void add(double amount) throws UnsupportedCoinValueException {
  81.         if (allowedValues.contains(amount)){
  82.             this.setBalance(this.getBalance() + amount);
  83.             coins.add(amount);
  84.         } else
  85.             throw new UnsupportedCoinValueException("Value of " + amount + " is not supported");
  86.     }
  87.     public void remove(double amount) throws UnsupportedCoinValueException {
  88.         if (amount >= 0)
  89.             this.setBalance(this.getBalance() - amount);
  90.         else
  91.             this.add(-amount);
  92.     }
  93.    
  94.     public String toString(){
  95.         return "" + balance;
  96.     }
  97.    
  98.     public static void main(String args[]) {       
  99.         try {  
  100.             /*
  101.             double balance = 131.0;
  102.             Double[] data = { 100.0, 10.0, 10.0, 10.0, 20.0, 20.0, 50.0, 10.0 };
  103.             ArrayList<Double> coins = new ArrayList<>();           
  104.             Collections.addAll(coins, data);               
  105.            
  106.             HashSet<Double> hset = new HashSet<Double>(coins);         
  107.             ArrayList<Double> coinsUnique = new ArrayList<Double>(hset);
  108.             Collections.sort(coinsUnique);
  109.             Collections.reverse(coinsUnique);
  110.            
  111.             ArrayList<Double> change = new ArrayList<>();      
  112.             for(int i = 0; i < coinsUnique.size(); ){          
  113.                 System.out.println(balance);
  114.                 for(int j = 0; j < coins.size(); ++j){
  115.                     System.out.print(coins.get(j) + " ");
  116.                 }
  117.                 System.out.println();      
  118.            
  119.                 double currentValue = coinsUnique.get(i);
  120.                 if((balance >= currentValue) && (coins.contains(currentValue))){
  121.                     balance = balance - currentValue;
  122.                     coins.remove(currentValue);
  123.                     change.add(currentValue);
  124.                 } else {
  125.                     ++i;
  126.                 }
  127.             }
  128.             if (balance > 0){
  129.                 int i = coinsUnique.size()-1;
  130.                 double currentValue = coinsUnique.get(i);
  131.                 while(!coins.contains(currentValue)){
  132.                     if(i > 0){
  133.                         --i;
  134.                         currentValue = coinsUnique.get(i);                     
  135.                     } else {
  136.                         throw new CoinUnavailableException("Value " + currentValue + " unavailable");
  137.                     }                  
  138.                 }
  139.                 coins.remove(currentValue);
  140.                 change.add(currentValue);
  141.             }
  142.            
  143.             System.out.println(balance);
  144.             for(int i = 0; i < change.size(); ++i){
  145.                 System.out.print(change.get(i) + " ");
  146.             }
  147.             System.out.println();      
  148.             */
  149.            
  150.             /*
  151.             Purse purse = new Purse();
  152.             purse.setBalance(10);
  153.             purse.add(-10);
  154.             purse.remove(-35);
  155.            
  156.             System.out.println(purse);
  157.             */
  158.            
  159.         } catch(Exception e){
  160.             System.out.println(e);         
  161.             System.out.println("Unexpected error, sorry!");
  162.         }          
  163.     }
  164.    
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement