The code follows the requested format of the output exactly for my assignment. But, for my own personal reference, is there a better way that I could have done anything? Are there any additional tips? Maybe some simple ways that I could gold plate against exceptions or errors? I know how try
, catch
and throw
exceptions work. My instructor doesn't check for it, though, so I wasn't going to spend time on it.
class Money {
public static void main(String[] args){
//Variable and Constant Declaration
int price,amountpaid,dollars,quarters,dimes,nickels,change,remainder = 0;
Scanner sc = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#0.00");
//Input
System.out.println("Please enter the purchase price in cents: ");
price = sc.nextInt();
System.out.println("Please enter the amount tendered in cents: ");
amountpaid = sc.nextInt();
//Calculations
remainder = amountpaid - price;
change = remainder;
dollars = remainder / 100;
remainder = remainder % 100;
quarters = remainder / 25;
remainder = remainder % 25;
dimes = remainder / 10;
remainder = remainder % 10;
nickels = remainder / 5;
remainder = remainder % 5;
pennies = remainder;
//Output
System.out.println("Purchase Price:\t $" + df.format((double)price/100) +
"\nAmount Tendered: $" + df.format((double)amountpaid/100) +
"\n \nYour Change is: $" + df.format((double)change/100) +
"\n \n \t \t " + dollars + " one-dollar bills(s)\n \t \t "
+ quarters + " quarter(s)\n \t \t " + dimes +
" dime(s)\n \t \t " + nickels + " nickle(s)\n \t \t " +
remainder + " penn(y/ies)");
System.out.println("\nThank you for your business. Come back soon.");
} // End Main
} //End Class
System.out.format
. \$\endgroup\$ – Reinstate Monica - M. Schröder Mar 9 '12 at 0:43