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)