/*
Reads two double values between 0 and 100 (inclusive) representing:
1. a purchase amount in dollars and cents
2. a payment by the user
Produces an error message if either input value is out of range OR if the user's payment is less than
the purchase amount. If the input is valid, program determines the amount of change that should be
returned and prints the numbers of fifty-, twenty-, ten-, five-, and one-dollar bills as well as the numbers
of quarters, dimes, nickels and pennies. Maximizes bills and coins with higher values.
*/
import java.io.*;
public class P1_5A {
public static void main (String args[]) {
double price; // purchase amount of item
double payment; // money paid by user
double change; // change owed to user
int dollarChange; // total change in bills
int coinChange; // total change in coins
int hundreds,fifties, twenties, tens, fives, ones;
int q, d, n, p; // number of quarters, dimes, nickels, pennies
// initialize Text object in to read from standard input.
TokenReader in = new TokenReader(System.in);
// prompt user to enter the purchase amount
System.out.println("Enter the purchase amount in dollars and cents [eg. 25.92]");
System.out.print("Be sure to make it < 100.00: ");
System.out.flush();
price = in.readDouble();
// print error message if purchase amount is out of range
if((price<0)||(price>100))
System.out.println("That purchase amount is invalid.");
else {
// prompt user for payment
System.out.println("\nEnter your payment in dollars and cents.");
System.out.print("Remember, this store does not accept payments > 100 : ");
System.out.flush();
payment = in.readDouble();
// print error message if payment amount is invalid
if (payment > 100)
System.out.println("We can't accept that much money.");
else if ( payment < price)
System.out.println("\nWe'll hold the item until you can pay for it.");
else {
// separately determine change required in bills and change in coins
change = payment - price;
dollarChange = (int) Math.floor(change);
coinChange = (int) (Math.floor(change*100 + 0.5) - 100*Math.floor(change));
// calculate numbers of fifties, twenties, tens, fives, ones.
fifties = dollarChange/50;
twenties = (dollarChange%50)/20;
tens = ((dollarChange%50)%20)/10;
fives = (((dollarChange%50)%20)%10)/5;
ones = ((((dollarChange%50)%20)%10)%5);
// calculate numbers of quarters, dimes, nickels, pennies.
q = coinChange/25;
d = (coinChange%25)/10;
n = ((coinChange%25)%10)/5;
p = (((coinChange%25)%10)%5);
// print numbers of each denomination. Exclude 0 values
System.out.print("\nYour change of " + dollarChange + " dollar" + (dollarChange != 1 ? "s" : "") + " and " + coinChange + " cents is given as: ");
System.out.print( fifties != 0 ? "\n" + fifties + " fift" + (fifties != 1 ? "ies" : "y") : "");
System.out.print(twenties != 0 ? "\n" + twenties + " twent" + (twenties != 1 ? "ies" : "y") : "");
System.out.print(tens != 0 ? "\n" + tens + " ten" + (tens != 1 ? "s" : "") : "");
System.out.print(ones != 0 ? "\n" + ones + " one" + (ones != 1 ? "s" : "") : "");
System.out.print(q != 0 ? "\n" + q + " quarter" + (q != 1 ? "s" : "") : "");
System.out.print(d != 0 ? "\n" + d + " dime" + (d != 1 ? "s" : "") : "");
System.out.print(n != 0 ? "\n" + n + " nickel" + (n != 1 ? "s" : "") : "");
System.out.print(p != 0 ? "\n" + p + " penn" + (p != 1 ? "ies.\n" : "y.\n") : "");
System.out.flush();
}
}
// wait for user to enter input to ensure console window remains visible
in.waitUntilEnter();
} // end main
} // end P1_5A
/*
Sample output:
---------------------------------------------------------------------------
Enter the purchase amount in dollars and cents [eg. 25.92]
Be sure to make it < 100.00: 25.92
Enter your payment in dollars and cents.
Remember, this store does not accept payments > 100 : 50.00
Your change of 24 dollars and 8 cents is given as:
1 twenty
4 ones
1 nickel
3 pennies.
*/