import java.io.*; //----------------------------------------------------------------- // CS100 P1 Q4 // // 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 Dimes // 0 Nickels // 4 Pennies // // Author : Wei Tsang Ooi (weitsang@cs.cornell.edu) // Date : 5 July 1999 //----------------------------------------------------------------- class CoinsChanger { //------------------------------------------------------------- // 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). Bye bye"); return -1; } else { return i; } } //------------------------------------------------------------- // changeCoins // // input : an amount. This is guranteed to be between 0 - 100. // return : none // // Output the number of coins returned that would be received // from one dollar. //------------------------------------------------------------- private static void changeCoins (int amount) { // find out what the change should be. int totalChange = 100 - amount; // find out how many coins of each type should be // returned. int left = totalChange; int numOfQuaters = left / 25; left %= 25; int numOfDimes = left / 10; left %= 10; int numOfNickels = left / 5; int numOfPennies = left % 5; // finally, we print the result. System.out.println("Your change of " + totalChange + " cents is given as:"); System.out.println(numOfQuaters + " Quaters"); System.out.println(numOfDimes + " Dimes"); System.out.println(numOfNickels + " Nickels"); System.out.println(numOfPennies + " Pennies"); } public static void main(String[] args) throws IOException { intro(); int amount = getInput(); if (amount == -1) { return; } else { changeCoins(amount); } } }