import java.io.*;
//-----------------------------------------------------------------
// CS100 P1 Q5
//
// This program prompts the user for an amount between 0 and 100 in
// cents, checks the validity of input, and determines the change
// that would be received from one dollar, maximizing the amount of
// coins with higher value. For example,
//
// Enter the purchase amount [0 - 100] : 36
// Your change of 64 cents is given as :
// 2 Quarters
// 1 Dime
// 4 Pennies
//
// Note : This is exactly the same as Q4, except that the output
// is grammartically correct, and is as minimum as possible. (0s
// are not printed).
//
// Author : Wei Tsang Ooi (weitsang@cs.cornell.edu)
// Date : 5 July 1999
//-----------------------------------------------------------------
class BetterCoinsChanger {
//-------------------------------------------------------------
// intro
//
// This prints an introduction message.
//-------------------------------------------------------------
private static void intro()
{
String msg = "I will ask you to enter a purchase amount\n" +
"under a dollar. This program will find out\n" +
"how the change should be given if a one dollar is\n" +
"received.\n";
System.out.println(msg);
}
//-------------------------------------------------------------
// getInput
//
// input : none
// return : an integer value input by user
//
// This method prompts the user for input, and returns the value
// input by the user if the input is between 0 and 100. Returns
// -1 otherwise.
//-------------------------------------------------------------
private static int getInput() throws IOException
{
// Prompt the user for an interger
BufferedReader stdin;
stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the purchase amount : ");
int i = Integer.parseInt(stdin.readLine());
// Here we check if the input is valid. We return -1 if it
// is not valid, otherwise we return the value.
if (i > 100 || i < 0) {
System.out.println("ERROR : input must be between 0 to 100" +
"(inclusive).");
return -1;
} else {
return i;
}
}
//-------------------------------------------------------------
// printAmount
//
// input : an amount - number of coins for a type
// singular - the singular form of the type
// prural - the prural form of the type
// return : none
//
// If the amount is zero, do nothing. If the amount is one
// print out the amount plus the singular string, otherwise
// print the amount plus the prural string.
//-------------------------------------------------------------
private static void printAmount (int amount, String singular, String prural)
{
if (amount == 0)
return;
else if (amount == 1)
System.out.println(amount + " " + singular);
else
System.out.println(amount + " " + prural);
}
//-------------------------------------------------------------
// changeCoins
//
// input : an amount.
// return : none
//
// Output the number of coins returned that would be received
// from one dollar.
//-------------------------------------------------------------
private static void changeCoins (int amount)
{
// We first check if the purchase amount is a dollar,
// if so there is no need to continue since no change
// will be given.
if (amount == 100) {
System.out.println("No change for you.");
return;
}
// Now we calculate the number of coins of each type
// that should be returned.
int totalChange = 100 - amount;
int left = totalChange;
int numOfQuaters = left / 25;
left %= 25;
int numOfDimes = left / 10;
left %= 10;
int numOfNickels = left / 5;
int numOfPennies = left % 5;
System.out.println("Your change of " + totalChange
+ " cents is given as:");
// Finally we print out the amounts of coins for each
// type
printAmount(numOfQuaters, "Quater", "Quaters");
printAmount(numOfDimes, "Dime", "Dimes");
printAmount(numOfNickels, "Nickel", "Nickels");
printAmount(numOfPennies, "Penny", "Pennies");
}
public static void main(String[] args) throws IOException
{
intro();
int amount = getInput();
if (amount == -1) {
return;
} else {
changeCoins(amount);
}
}
}