Search code examples
javacoin-change

Java Euro Coin Change Denomination


I believe I am having a logical issue with how to develop the section of code responsible for taking the remainder and checking if I can extract change from displayed change categories. It's designed to take a value of how much change you owe back to someone and make the most efficient set of change to give.

My current output:

The change for 328.0 Euro cents is:
1.64   € 2
1.28   € 1
0.56   € 0.50
1.4   € 0.20
0.8   € 0.10
1.6   € 0.05
1.5   € 0.02
1.0   € 0.01

Correct output:

The change for 328 Euro cents is:
1 €2
1 €1
0 €0.50
1 €0.20
0 €0.10
1 €0.05
1 €0.02
1 €0.01

I'm pretty stumped, I'd appreciate the help, thank you.

import java.util.Scanner; // Enables user input

    public class Change {
       private static Scanner scnr;

    public static void main(String [] args) {
           scnr = new Scanner(System.in);
          double changeR      = 0; // User input
          double oneCent      = 0; // Number of one cent coins
          double twoCent      = 0; // Number of two cent coins
          double fiveCent     = 0; // Number of five cent coins
          double tenCent      = 0; // Number of ten cent coins
          double twentyCent   = 0; // Number of twenty cent coins
          double fiftyCent    = 0; // Number of fifty cent coins
          double oneEUC       = 0; // Number of one Euro cent coins
          double twoEUC       = 0; // Number of two Euro cent coins

          System.out.println("Please enter the amount of change in Euro cents to be returned (a number between 0 & 499): ");
          changeR = scnr.nextInt(); // Gathers user input

          System.out.println("The change for " + changeR + " Euro cents is: "); // Outputs users inputted value

          twoEUC = changeR / 200;
          changeR = changeR % 200;

          oneEUC = changeR / 100;
          changeR = changeR % 100;

          fiftyCent = changeR / 50;
          changeR = changeR % 50;

          twentyCent = changeR / 20;
          changeR = changeR % 20;

          tenCent = changeR / 10;
          changeR = changeR % 10;

          fiveCent = changeR / 5;
          changeR = changeR % 5;

          twoCent = changeR / 2;
          changeR = changeR % 2;

          oneCent = changeR / 1;
          changeR = changeR % 1;

          System.out.println( twoEUC + "   \u20ac" + " 2");
          System.out.println( oneEUC + "   \u20ac" + " 1");
          System.out.println( fiftyCent + "   \u20ac" + " 0.50");
          System.out.println( twentyCent + "   \u20ac" +" 0.20");
          System.out.println( tenCent + "   \u20ac" +" 0.10");
          System.out.println( fiveCent + "   \u20ac" +" 0.05");
          System.out.println( twoCent + "   \u20ac" +" 0.02");
          System.out.println( oneCent + "   \u20ac" +" 0.01");

          return;
       }
    }

Solution

  • As is common when dealing with money¹, you need to deal with integers only.

    Changing all your variables to int will give you the proper values, as integer division will make sure that 238 / 200 = 1. Instead of double division which will result in 238.0 / 200.0 = 1.19.

    ¹ Handling money in code (as well as real life) is a difficult problem. You need to present the output in a user readable form, so you don't want to show prices as 5.0001 € due to the problem of double imprecision, yet you don't want calculations to do "eager" rounding so the end result is off. One of the simplest ways is to take the smallest "atom", i.e. cents and deal in them only.

    Why not use double or float to represent currency