I am currently writing a program that prints only the coins used when returning change. However, I get 2 assertions errors which are evaluated on the server's back-end. There are no sample test values, you have to make up your own.
expected:[... Change Due $0.01 [1 penny] ]
but was:[... Change Due $0.01 [2 pennies] ]
expected:[... Change Due $0.19 [1 dime 1 nickel 4 pennies ]
but was:[... Change Due $0.19 [2 dimes ]
/**
* Calculate the amount of change based on user input.
* Output coins with an output greater than 0.
* @param args returns an argument array for string objects
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Total amount? ");
double total = input.nextDouble();
System.out.print("Cash payment? ");
double payment = input.nextDouble();
double changeDue = payment - total;
System.out.println();
System.out.printf("Change Due $%.2f\n\n", changeDue);
int cents = (int) Math.ceil(changeDue * 100);
int dollars = Math.round((int) cents / 100);
cents = cents % 100;
int quarters = Math.round((int) cents / 25);
cents = cents % 25;
int dimes = Math.round((int) cents / 10);
cents = cents % 10;
int nickels = Math.round((int) cents / 5);
cents = cents % 5;
int pennies = Math.round((int) cents / 1);
int[] coins = {dollars,quarters,dimes,nickels,pennies};
String[] names = {"dollar", "quarter", "dime", "nickel", "penny"};
for (int i = 0; i < 5; i++) {
if (coins[i] == 1) {
System.out.println(coins[i] + " " + names[i]);
} else if (coins[i] > 1 && i != 4) {
System.out.println(coins[i] + " " + names[i] + "s");
}
}
if (coins[4] > 1) {
System.out.println(coins[4] + " pennies");
}
}