HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Change-Making Problem Solution

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
problemmakingchangesolution

Problem

Please take a moment and review my code and tell me how I did. This is the first time in my class (CSC 111) I've been given an assignment in which I had to use a lot of my own resources and develop most of it on my own.


Write an application that prompts for and reads a double value
representing a monetary amount. Then determine the least
number of each bill and coin needed to represent that amount,
starting with the highest. Assume that ten dollars is the
maximum size needed.
For example, if the value 47.63 is entered the program should
display



  • 4 ten dollar bills



  • 1 five dollar bills



  • 2 one dollar bills



  • 2 quarters



  • 1 dimes



  • 0 nickels



  • 3 pennies




My code works mostly flawlessly (I believe). I would like to hear other people's opinion on my solution. I understand it probably isn't the most efficient out there. How can it be improved? Are there things to watch out for?

```
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Scanner;
import java.util.Locale;

public class Assignment01
{
public static void main(String[] args)
{

/ Define Constants /

BigDecimal tenDollar = BigDecimal.valueOf(10);
BigDecimal fiveDollar = BigDecimal.valueOf(5);
BigDecimal oneDollar = BigDecimal.valueOf(1);
BigDecimal quarter = BigDecimal.valueOf(0.25);
BigDecimal dime = BigDecimal.valueOf(0.10);
BigDecimal nickel = BigDecimal.valueOf(0.05);
BigDecimal penny = BigDecimal.valueOf(0.01);

/ Get Input From User /

System.out.print("Please enter the amount to be converted: ");
Scanner scan = new Scanner(System.in);
BigDecimal money = scan.nextBigDecimal();

NumberFormat usdFormat = NumberFormat.getCurrencyInstance(Locale.US);
usdFormat.setMinimumFractionDigits(2);
usdFormat.setMaximumFractionDigits(2);

System.out.println("Amount you entered: " + usdFormat.format(money));

/* Begin Processing and Displaying Information

Solution

BigDecimal money = scan.nextBigDecimal();


The specification said


reads a double value representing a monetary amount.

However, BigDecimal is the correct type to represent a monetary amount. There's a case to be made for using scan.nextDouble() to comply with the spec and then converting it to BigDecimal. There's also a case to be made that you should talk to the person who set the task and suggest improving the spec.

BigDecimal tenDollarNext = money.divideToIntegralValue(tenDollar);
    System.out.println(tenDollarNext.intValue() + " Ten Dollar Bills");

    BigDecimal fiveDollarNext = money.subtract(tenDollar.multiply(tenDollarNext));
    BigDecimal i = fiveDollarNext.divide(fiveDollar);


Why the inconsistencies? tenDollarNext and fiveDollarNext aren't analogues of each other, and one uses divideToIntegralValue(...) where the other uses divide(...).intValue().

There are seven chunks of code dealing with seven values of token which are virtually identical. DRY: use a loop.

Code Snippets

BigDecimal money = scan.nextBigDecimal();
BigDecimal tenDollarNext = money.divideToIntegralValue(tenDollar);
    System.out.println(tenDollarNext.intValue() + " Ten Dollar Bills");

    BigDecimal fiveDollarNext = money.subtract(tenDollar.multiply(tenDollarNext));
    BigDecimal i = fiveDollarNext.divide(fiveDollar);

Context

StackExchange Code Review Q#154885, answer score: 4

Revisions (0)

No revisions yet.