/* Lesley Hogan * July 14th 2003 * GetChange.java */ package net.bunnie.dos; import net.bunnie.*; public class GetChange { // input number in xx.xx format, get change // for those mcdonalds workers out there public static void main (String[] args) { // TerminalIO double change = 0.0; KeyboardReader r = new KeyboardReader(); System.out.print("Please enter the amount of money\nto be converted in dollars:\n$"); change = r.readDouble(); System.out.print("\n\n"); // convert to cents, output arrays int changeInt = (int) (change * 100); int[] moneyArray = {2000, 1000, 500, 100, 25, 10, 5, 1}; int[] output = new int[8]; String[] outputNames = {"$20 bills", "$10 bills", "$5 bills", "$1 bills", "Quarters", "Dimes", "Nickels", "Pennies"}; /* format number... * if number is .80 (80 cents) * then it will only print "$0.8" without this * this makes it print "$0.80" */ String centsNowDollars; String centsInDollars; String totalCentsLengthString; String totalCentsString; int centsGivenLength; int centsGiven; int totalCents; int centsOnlyDollars; int totalCentsLength; centsInDollars = "" + changeInt; centsGivenLength = centsInDollars.length(); switch (centsGivenLength) { case 1: centsNowDollars = "0.0" + changeInt; break; case 2: centsNowDollars = "0." + changeInt; break; default: centsOnlyDollars = (int) (changeInt / 100); totalCents = changeInt % 100; totalCentsLengthString = "" + totalCents; totalCentsLength = totalCentsLengthString.length(); if (totalCentsLength == 1) { totalCentsString = "0" + totalCents; } else { totalCentsString = "" + totalCents; } centsNowDollars = "" + centsOnlyDollars + "." + totalCentsString; break; } // print out properly formatted number System.out.print("The change for $" + centsNowDollars + " is...\n\n"); // do loop to go through arrays int i = 0; do { output[i] = changeInt / moneyArray[i]; changeInt = changeInt % moneyArray[i]; System.out.println(output[i] + " " + outputNames[i]); i++; } while (i < moneyArray.length); System.out.println(); } }