Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class UnsupportedCoinValueException extends Exception {
- public UnsupportedCoinValueException(String message){
- super(message);
- }
- }
- class CoinUnavailableException extends Exception {
- public CoinUnavailableException(String message){
- super(message);
- }
- }
- class Purse {
- private double balance = 0;
- private ArrayList<Double> allowedValues = null;
- private ArrayList<Double> coins = new ArrayList<>();
- public Purse(){
- }
- public Purse(ArrayList<Double> allowedValues){
- this.allowedValues = allowedValues;
- }
- public void setCoins(ArrayList<Double> coins){
- this.coins = coins;
- }
- public ArrayList<Double> getCoins(){
- return coins;
- }
- private void setBalance(double balance){
- if (balance >= 0)
- this.balance = (Math.round(balance*100))/100.0;
- else
- throw new IllegalArgumentException("Balance cannot be negative");
- }
- public ArrayList<Double> getChange() throws Exception {
- ArrayList<Double> change = new ArrayList<>();
- double balance = getBalance();
- HashSet<Double> hset = new HashSet<Double>(coins);
- ArrayList<Double> coinsUnique = new ArrayList<Double>(hset);
- Collections.sort(coinsUnique);
- for(int i = coinsUnique.size()-1; i >= 0; ){
- double currentValue = coinsUnique.get(i);
- if((balance >= currentValue) && (coins.contains(currentValue))){
- balance = balance - currentValue;
- coins.remove(currentValue);
- change.add(currentValue);
- } else {
- --i;
- }
- }
- if (balance > 0){
- int i = 0;
- while(true){
- double currentValue = coinsUnique.get(i);
- if(coins.contains(currentValue)){
- coins.remove(currentValue);
- change.add(currentValue);
- break;
- } else {
- ++i;
- if(i == coinsUnique.size()){
- throw new CoinUnavailableException("Value " + currentValue + " unavailable");
- }
- }
- }
- }
- setBalance(0);
- return change;
- }
- public double getBalance(){
- return balance;
- }
- public void add(double amount) throws UnsupportedCoinValueException {
- if (allowedValues.contains(amount)){
- this.setBalance(this.getBalance() + amount);
- coins.add(amount);
- } else
- throw new UnsupportedCoinValueException("Value of " + amount + " is not supported");
- }
- public void remove(double amount) throws UnsupportedCoinValueException {
- if (amount >= 0)
- this.setBalance(this.getBalance() - amount);
- else
- this.add(-amount);
- }
- public String toString(){
- return "" + balance;
- }
- public static void main(String args[]) {
- try {
- /*
- double balance = 131.0;
- Double[] data = { 100.0, 10.0, 10.0, 10.0, 20.0, 20.0, 50.0, 10.0 };
- ArrayList<Double> coins = new ArrayList<>();
- Collections.addAll(coins, data);
- HashSet<Double> hset = new HashSet<Double>(coins);
- ArrayList<Double> coinsUnique = new ArrayList<Double>(hset);
- Collections.sort(coinsUnique);
- Collections.reverse(coinsUnique);
- ArrayList<Double> change = new ArrayList<>();
- for(int i = 0; i < coinsUnique.size(); ){
- System.out.println(balance);
- for(int j = 0; j < coins.size(); ++j){
- System.out.print(coins.get(j) + " ");
- }
- System.out.println();
- double currentValue = coinsUnique.get(i);
- if((balance >= currentValue) && (coins.contains(currentValue))){
- balance = balance - currentValue;
- coins.remove(currentValue);
- change.add(currentValue);
- } else {
- ++i;
- }
- }
- if (balance > 0){
- int i = coinsUnique.size()-1;
- double currentValue = coinsUnique.get(i);
- while(!coins.contains(currentValue)){
- if(i > 0){
- --i;
- currentValue = coinsUnique.get(i);
- } else {
- throw new CoinUnavailableException("Value " + currentValue + " unavailable");
- }
- }
- coins.remove(currentValue);
- change.add(currentValue);
- }
- System.out.println(balance);
- for(int i = 0; i < change.size(); ++i){
- System.out.print(change.get(i) + " ");
- }
- System.out.println();
- */
- /*
- Purse purse = new Purse();
- purse.setBalance(10);
- purse.add(-10);
- purse.remove(-35);
- System.out.println(purse);
- */
- } catch(Exception e){
- System.out.println(e);
- System.out.println("Unexpected error, sorry!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement