patternjavaMinor
Converting money into change
Viewed 0 times
moneyconvertingintochange
Problem
I am trying to figure out a more efficient way of converting money into change. The code below is what i have written so far. It works perfectly but i know the code can be improved. Is there a more efficient way of writing this code?
public class Problem2 {
public static int quarters(int total)
{
return total/25;
}
public static int dimes(int total)
{
return (total - quarters(total)*25) /10;
}
public static int nickels(int total)
{
return (total - (quarters(total)*25) - (dimes(total)*10)) / 5;
}
public static int pennies(int total)
{
return total - (quarters(total)*25) - (dimes(total)*10) - (nickels(total)*5);
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String total;
int intTotal;
System.out.println("Hi, this application will take your number and will display the minimum amount of"
+ "coins necessary to represent the dollar amount");
System.out.println("Please enter in the total amount of dollars and cents:");
total = keyboard.next();
total = total.replace(".","");
intTotal = Integer.parseInt(total);
System.out.println(quarters(intTotal) + " Quarters,"
+dimes(intTotal) + " Dimes,"
+nickels(intTotal) + " Nickels,"
+pennies(intTotal) + " Pennies");
}
}Solution
Minor tweak
A shorter version of this would be
Of course, it would be reasonable to argue that the consistency of the original outweighs the terseness of this version.
Don't forget and repeat work
I would also consider making this an object rather than a static class. Then you wouldn't have to recalculate these every time that you wanted one.
And this may be a little too much single responsibility principle. Consider
This is DRYer, as it puts the change breakdown in just one place rather than doing
A good compiler should generally be able to find the quotient and remainder in one operation, so this should simplify to three calculations even though there are seven assignments. Your original version takes nineteen just to count pennies. Thirty-three to calculate all four. Even if subtractions and multiplications are lower weight than divisions, seven of those were divisions.
I added the constants, as they make it less likely that you will divide by one value and take the remainder of a different value.
I won't write out the getters or the object field declarations.
Just-in-time declarations
In Java, it's more customary to do variable declarations just in time to be used. So
public static int pennies(int total)
{
return total - (quarters(total)*25) - (dimes(total)*10) - (nickels(total)*5);
}A shorter version of this would be
public static int pennies(int total)
{
return total % 5;
}Of course, it would be reasonable to argue that the consistency of the original outweighs the terseness of this version.
Don't forget and repeat work
I would also consider making this an object rather than a static class. Then you wouldn't have to recalculate these every time that you wanted one.
And this may be a little too much single responsibility principle. Consider
public void initialize(int total) {
quartersCount = total / QUARTER_VALUE;
total %= QUARTER_VALUE;
dimesCount = total / DIME_VALUE;
total %= DIME_VALUE;
nickelsCount = total / NICKEL_VALUE;
total %= NICKEL_VALUE;
penniesCount = total;
}This is DRYer, as it puts the change breakdown in just one place rather than doing
total - (quarters(total)*25) in three different methods. A good compiler should generally be able to find the quotient and remainder in one operation, so this should simplify to three calculations even though there are seven assignments. Your original version takes nineteen just to count pennies. Thirty-three to calculate all four. Even if subtractions and multiplications are lower weight than divisions, seven of those were divisions.
I added the constants, as they make it less likely that you will divide by one value and take the remainder of a different value.
I won't write out the getters or the object field declarations.
Just-in-time declarations
String total;
int intTotal;
System.out.println("Hi, this application will take your number and will display the minimum amount of"
+ "coins necessary to represent the dollar amount");
System.out.println("Please enter in the total amount of dollars and cents:");
total = keyboard.next();
total = total.replace(".","");
intTotal = Integer.parseInt(total);In Java, it's more customary to do variable declarations just in time to be used. So
System.out.println("Hi, this application will take your number and will display the minimum amount of"
+ "coins necessary to represent the dollar amount");
System.out.println("Please enter in the total amount of dollars and cents:");
String total = keyboard.next();
total = total.replace(".","");
int intTotal = Integer.parseInt(total);Code Snippets
public static int pennies(int total)
{
return total - (quarters(total)*25) - (dimes(total)*10) - (nickels(total)*5);
}public static int pennies(int total)
{
return total % 5;
}public void initialize(int total) {
quartersCount = total / QUARTER_VALUE;
total %= QUARTER_VALUE;
dimesCount = total / DIME_VALUE;
total %= DIME_VALUE;
nickelsCount = total / NICKEL_VALUE;
total %= NICKEL_VALUE;
penniesCount = total;
}String total;
int intTotal;
System.out.println("Hi, this application will take your number and will display the minimum amount of"
+ "coins necessary to represent the dollar amount");
System.out.println("Please enter in the total amount of dollars and cents:");
total = keyboard.next();
total = total.replace(".","");
intTotal = Integer.parseInt(total);System.out.println("Hi, this application will take your number and will display the minimum amount of"
+ "coins necessary to represent the dollar amount");
System.out.println("Please enter in the total amount of dollars and cents:");
String total = keyboard.next();
total = total.replace(".","");
int intTotal = Integer.parseInt(total);Context
StackExchange Code Review Q#118006, answer score: 3
Revisions (0)
No revisions yet.