Home > Net >  Cash to Change in Java
Cash to Change in Java

Time:11-15

I am new to Java and was trying to write code that would tell you how much change you would receive for a given amount of money (in minimum number of coins). For example $0.31 would give 1 quarters, 0 dimes, 1 nickels, 1 pennies.

I got to a point where my code seemed to work. However, while most values work, specifically any multiple of 0.41 doesn't work (ex. 0.41, 0.82 ...). For example $0.41 results in 1 quarters, 1 dimes, 1 nickels, 0 pennies, which only adds up to $0.40.

Here is my code:

import java.util.Scanner;

public class Cash {
    public static void main(String[] args){
        Scanner scanner_obj = new Scanner(System.in);

        System.out.print("Enter Amount: $");
        double amount = Double.parseDouble(scanner_obj.nextLine());
        double[] change = calc_Change(amount);

        System.out.println(change[0]   " Quarters");
        System.out.println(change[1]   " Dimes");
        System.out.println(change[2]   " Nickels");
        System.out.println(change[3]   " Pennies");

        System.out.println(check_Change(amount, change));
    }

    public static double[] calc_Change(double amount){
        double[] change = {0, 0, 0, 0};

        while (amount >= 0.25){amount -= 0.25; change[0]  = 1;}
        while (amount >= 0.10){amount -= 0.10; change[1]  = 1;}
        while (amount >= 0.05){amount -= 0.05; change[2]  = 1;}
        while (amount >= 0.01){amount -= 0.01; change[3]  = 1;}

        return change;
    }

    public static boolean check_Change(double amount, double[] change){
        double total = change[0]*0.25   change[1]*0.10   change[2]*0.05   change[3]*0.01;
        System.out.println(amount   " vs "   total);

        return (amount == total);
    }
}

I was using the check_Change function to check my results.

I don't know why this doesn't work and why it is only certain numbers.

CodePudding user response:

When you are subtracting from a double - the internal representation is not exact - running your code and printing out the amount every time you subtract yields the following:

0.41
0.15999999999999998
0.05999999999999997
0.009999999999999967

As you can see the binary representation of the double is not exact...

try convert to an int (i.e. multiply by 100) then do the calculations

public class CashMain {

    public static void main(String[] args){
        Scanner scanner_obj = new Scanner(System.in);

        System.out.print("Enter Amount: $");
        double amount = Double.parseDouble(scanner_obj.nextLine());
        System.out.println(amount);
        int numCents = (int) (amount*100);
        System.out.println(numCents);
        
        int[] change = calc_Change(numCents);

        System.out.println(change[0]   " Quarters");
        System.out.println(change[1]   " Dimes");
        System.out.println(change[2]   " Nickels");
        System.out.println(change[3]   " Pennies");

        System.out.println(check_Change(numCents, change));
    }

    public static int[] calc_Change(int amount){
        int[] change = {0, 0, 0, 0};

        while (amount >= 25){
            amount -= 25; 
            change[0]  = 1;
        }
        while (amount >= 10){
            amount -= 10; 
            change[1]  = 1;
        }
        while (amount >= 5){
            amount -= 5; 
            change[2]  = 1;
        }
        while (amount >= 1){
            amount -= 1; 
            change[3]  = 1;
        }

        return change;
    }

    public static boolean check_Change(int amount, int[] change){
        int total = change[0]*25   change[1]*10   change[2]*5   change[3]*1;
        System.out.println(amount   " vs "   total);

        return (amount == total);
    }
}

output

Enter Amount: $0.41
0.41
41
1 Quarters
1 Dimes
1 Nickels
1 Pennies
41 vs 41
true

CodePudding user response:

You can use BigDecimal for precision in your arithmetic operations.

public class Cash {
    public static void main(String[] args){
        Scanner scanner_obj = new Scanner(System.in);

        System.out.print("Enter Amount: $");
        BigDecimal amount = new BigDecimal(scanner_obj.nextLine());
        double[] change = calc_Change(amount);

        System.out.println(change[0]   " Quarters");
        System.out.println(change[1]   " Dimes");
        System.out.println(change[2]   " Nickels");
        System.out.println(change[3]   " Pennies");

        System.out.println(check_Change(amount, change));
    }

    public static double[] calc_Change(BigDecimal amount){
        double[] change = {0, 0, 0, 0};

        while (amount.doubleValue() >= 0.25){
            amount = amount.subtract(new BigDecimal("0.25")); change[0] =1;
        }
        while (amount.doubleValue() >= 0.10){
            amount = amount.subtract(new BigDecimal("0.10")); change[1]  = 1;
        }
        while (amount.doubleValue() >= 0.05){
            amount = amount.subtract(new BigDecimal("0.05")); change[2]  = 1;
        }
        while (amount.doubleValue() >= 0.01){
            amount = amount.subtract(new BigDecimal("0.01")); change[3]  = 1;
        }

        return change;
    }

    public static boolean check_Change(BigDecimal amount, double[] change){
        double v1 = change[0] * 0.25;
        double v2 = change[1] * 0.10;
        double v3 = change[2] * 0.05;
        double v4 = change[3] * 0.01;
        double total = v1   v2   v3   v4;
        System.out.println(amount   " vs "   total);

        return (amount.doubleValue() == total);
    }
}
  •  Tags:  
  • java
  • Related