-1

My group and I are working on a somewhat simple program in Java that will take two different prices and calculate the change between it. A problem we are having that we can't seem to find/fix is that the math for the change is completely incorrect.

NOTE: We are using Repl.it

import java.util.Scanner;

class Main {
    public static void main(String[] args) {

        String input, cost, payment, change;
        String bills, coins;
        int dollarsOwed, dollarsPaid;

        Scanner money = new Scanner(System.in);

        System.out.println(" ");
        System.out.println(
                "Please enter the amount of the purcahse followed by the amount of a payment, separated by a comma.");

        System.out.println(" ");
        System.out.print("Amount of Purchase, Amount of Payment: ");
        input = money.nextLine();

        int a = input.indexOf(",");
        payment = input.substring(a + 1);
        cost = input.substring(0, a);
        double x = Double.parseDouble(cost);
        double y = Double.parseDouble(payment);

        dollarsOwed = (int) x;
        double centsOwed1 = (x - dollarsOwed) * 100;
        centsOwed1 = Math.round(centsOwed1);
        int centsOwed = (int) centsOwed1;

        dollarsPaid = (int) y;
        double centsPaid1 = (y - dollarsPaid) * 100;
        centsPaid1 = Math.round(centsPaid1);
        int centsPaid = (int) centsPaid1;

        String billsString = billsChange(dollarsOwed, dollarsPaid);
        String centsString = coinsChange(centsOwed, centsPaid);
        printBills(billsString);
        printCoins(centsString);

    }

    static String billsChange(int dollarsOwed, int dollarsPaid) {
        int dollarsReturned;
        int thisDenomination = 0;
        String retString = "";

        dollarsReturned = dollarsPaid - dollarsOwed;

        // Calculate $100 bills needed
        thisDenomination = dollarsReturned / 100;
        dollarsReturned -= thisDenomination * 100;
        retString += Integer.toString(thisDenomination);

        // Calculate $50 bills needed
        thisDenomination = dollarsReturned / 50;
        dollarsReturned -= thisDenomination * 50;
        retString += Integer.toString(thisDenomination);

        // Calculate $20 bills needed
        thisDenomination = dollarsReturned / 20;
        dollarsReturned -= thisDenomination * 20;
        retString += Integer.toString(thisDenomination);

        // Calculate $10 bills needed
        thisDenomination = dollarsReturned / 10;
        dollarsReturned -= thisDenomination * 10;
        retString += Integer.toString(thisDenomination);

        // Calculate $5 bills needed
        thisDenomination = dollarsReturned / 5;
        dollarsReturned -= thisDenomination * 5;
        retString += Integer.toString(thisDenomination);

        // Calculate $1 bills needed
        retString += Integer.toString(dollarsReturned);

        return (retString);

    }

    static String coinsChange(int centsOwed, int centsPaid) {
        int changeReturned;
        int thisDenomination = 0;
        String retString = "";

        changeReturned = centsPaid - centsOwed;

        // Calculate quarters(25¢) needed
        thisDenomination = changeReturned / 25;
        changeReturned -= thisDenomination * 25;
        retString += Integer.toString(thisDenomination);

        // Calculate dimes(10¢) needed
        thisDenomination = changeReturned / 10;
        changeReturned -= thisDenomination * 10;
        retString += Integer.toString(thisDenomination);

        // Calculate nickels(5¢) needed
        thisDenomination = changeReturned / 5;
        changeReturned -= thisDenomination * 5;
        retString += Integer.toString(thisDenomination);

        // Calculate pennies(1¢) needed
        retString += Integer.toString(changeReturned);

        return (retString);
    }

    static void printBills(String bills) {
        String printString = "";
        String billName = "";
        for (int i = 0; i <= 5; i++) {
            if (bills.charAt(i) != '0') {
                switch (i) {
                case 0:
                    billName = "$100";
                    break;
                case 1:
                    billName = "$50";
                    break;
                case 2:
                    billName = "$20";
                    break;
                case 3:
                    billName = "$10";
                    break;
                case 4:
                    billName = "$5";
                    break;
                case 5:
                    billName = "$1";
                    break;
                }
                printString += (bills.charAt(i) + "-" + billName + " ");
            }
        }
        System.out.print(printString);
    }

    static void printCoins(String coins) {
        String printString = "";
        String coinName = "";
        for (int i = 0; i <= 3; i++) {
            if (coins.charAt(i) != '0') {
                switch (i) {
                case 0:
                    coinName = "$0.25";
                    break;
                case 1:
                    coinName = "$0.10";
                    break;
                case 2:
                    coinName = "$0.05";
                    break;
                case 3:
                    coinName = "$0.01";
                    break;
                }
                printString += (coins.charAt(i) + "-" + coinName + " ");
            }
        }
        System.out.print(printString);
    }
}

Example:

User Input: 11.33,15.00

Expected Output: 3-$1 2-$0.25 1-$0.10 1-$0.05 2-$0.01 (CORRECT)

Actual Output: 4-$1 --$0.25 1-$0.10 --$0.01 (INCORRECT)

Community
  • 1
  • 1
snavelien
  • 1
  • 2
  • 11
    This would be a good time to learn how to *debug* your code. You see: figuring "why is my code not doing what I want it to do" is like half of "learning to program". Don't delegate that part to others, too quickly... – GhostCat Jan 09 '19 at 13:38
  • 2
    What I would do: for all the computations, **print** the intermediate results. And then you run some example, and you do all the computations manually, too. And then, for each and any computation you compare your "manual" result with the one from your code. Yes, tedious boring work. Welcome to debugging. Writing code is nice and easy. Getting it to work is the really interesting part. – GhostCat Jan 09 '19 at 13:40

1 Answers1

0

I think the issue is because you are using int's as your datatype, when u subtract ints datatype then decimal places are dropped therefore if you do 15-11.33 you get 4 because the.33 is dropped in int datatype.

int dollarsOwed, int dollarsPaid   
dollarsReturned = dollarsPaid - dollarsOwed;

change there variables to double, or float and that should fix it.

Tomasz Wida
  • 329
  • 1
  • 6
  • 24
  • 1
    because he is not subtracting 15-11 but he is trying to subtract 15-11.33 but is using int as a data type. – Tomasz Wida Jan 09 '19 at 13:44
  • 1
    Then your answer should say so ;) ... and also note that you quickly run into the typical floating point math issues. Typically, floating point math and money is a NO GO, due to the rounding and precision issues. I would rather suggest to go the other way round, and ensure that **all** numbers are "cent" based, so you have the * 100 all over the place. Then there are no "comma" values any more. – GhostCat Jan 09 '19 at 13:45
  • that is another good solution change everything to cents and the do logic on that :) – Tomasz Wida Jan 09 '19 at 13:56