0

I took this problem from an experienced programmer to prepare for a programming test.

Problem:

Given the amount of a purchase and the amount given as payment, calculate the number and type of each denomination of currency returned as change. Change is always given with the most of the highest denominations first. The denominations of paper currency used will be $100, $50, $20, $10, $5 and $1. The denominations of the coins will be $0.25, $0.10, $0.05 and $0.01.

INPUT: There will be 5 lines of input. Each line will contain 2 rational numbers representing the amount of a purchase and the amount given as payment.

OUTPUT: For each input line print the change using the number of non-zero denominations used in order of their magnitude from high to low. If no change is due print NONE. Sample Output #1 represents: 1-$5, 2-$1, 1-$.25, 2-$.10 and 2-$.01.

SAMPLE INPUT SAMPLE OUTPUT
1. 12.53, 20.00 1. 121221
2. 11.33, 15.00 2. 32112
3. 35.64, 50.00 3. 14111
4. 72.67, 100.00 4. 112113
5. 106.68, 200.00 5. 123112

I decided to create two programs: a long, easier-to-follow-one and a condensed version.

Here is the long one:

    import java.util.*;

    public class Prac13CorrectChange {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    for (int A = 1; A <= 5; A++) {

        System.out
                .println("What is the amount of your purchase and the amount given as your payment?");
        String input = in.nextLine();

        String[] data = input.split(",");
        double $amount = Double.parseDouble(data[0]);
        double price = Double.parseDouble(data[1]);

        double change = price - $amount;

        int ans1 = ((int) (change / 100));
        change = change - (ans1 * 100);

        int ans2 = ((int) (change / 50));
        change = change - (ans2 * 50);

        int ans3 = ((int) (change / 20));
        change = change - (ans3 * 20);

        int ans4 = ((int) (change / 10));
        change = change - (ans4 * 10);

        int ans5 = ((int) (change / 5));
        change = change - (ans5 * 5);

        int ans6 = ((int) (change / 1));
        change = change - (ans6 * 1);

        int ans7 = ((int) (change / .25));
        change = change - (ans7 * .25);

        int ans8 = ((int) (change / .10));
        change = change - (ans8 * .10);

        int ans9 = ((int) (change / .05));
        change = change - (ans9 * .05);

        int ans10 = ((int) (change / .01));
        change = change - (ans10 * .01);

        String answer = "";

        if (ans1 != 0) {

            answer = answer + Integer.toString(ans1);

        }

        if (ans2 != 0) {

            answer = answer + Integer.toString(ans2);

        }

        if (ans3 != 0) {

            answer = answer + Integer.toString(ans3);

        }

        if (ans4 != 0) {

            answer = answer + Integer.toString(ans4);

        }

        if (ans5 != 0) {

            answer = answer + Integer.toString(ans5);

        }

        if (ans6 != 0) {

            answer = answer + Integer.toString(ans6);

        }

        if (ans7 != 0) {

            answer = answer + Integer.toString(ans7);

        }

        if (ans8 != 0) {

            answer = answer + Integer.toString(ans8);

        }

        if (ans9 != 0) {

            answer = answer + Integer.toString(ans9);

        }

        if (ans10 != 0) {

            answer = answer + Integer.toString(ans10);

        }

        System.out.println(answer);

    }
}

}

And here is the condensed version.

    import java.util.Scanner;

    public class Prac15CorrectChangeShort {

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

    for (int A = 1; A <= 5; A++) {

        System.out
                .println("What is the amount of your purchase and the amount given as your payment?");
        String input = in.nextLine();

        String[] data = input.split(",");
        double $amount = Double.parseDouble(data[0]);
        double price = Double.parseDouble(data[1]);

        double change = price - $amount;
        int ans[] = new int[10];
        double money[] = { 100, 50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01 };

        for (int looptimes = 0; looptimes < 10; looptimes++) {

            ans[looptimes] = ((int) (change / money[looptimes]));
            change = change - (ans[looptimes] * money[looptimes]);

        }

        for (int looptimes = 0; looptimes < 10; looptimes++) {
            if (ans[looptimes] != 0) {
                System.out.print(ans[looptimes]);
            }
        }
        System.out.print('\n');
    }

}

}

The main problem I am having is that, no matter the debugging I do, it is giving me incorrect outputs #2-5. It gives me 1 less for the last digit. Neither the programmer who gave me this problem nor I can find any reason for this occurrence.

Please help.

Amey Gupta
  • 57
  • 1
  • 10

1 Answers1

2

Looks like a floating point error.

Read this

You can use BigDecimal instead of double when calculating the change.

 BigDecimal change = BigDecimal.valueOf(price - $amount);
Ringo
  • 850
  • 9
  • 24
  • That works, but how do I divide with BigDecimal. – Amey Gupta May 17 '15 at 15:39
  • convert the value to double again, since this looks like an excercise, you can store the value of any arithmetic operation into a BigDecimal and convert it to double back and forth as needed. Or have a class that does that. – Ringo May 18 '15 at 02:30