How do I use div and mod separating the number in bills?

0

I need some formulas in java that when entering a number of "n" digits I separate them into bills of 500 200 100 50 and 20 using div and mod (I am aware that div is "/" and mod is "% ").

    
asked by Victor Carriche 07.04.2016 в 04:52
source

2 answers

4

I will use a simple algorithm to help you understand the problem.

One way to solve this is to start dividing the number between the highest denomination notes and with the remainder continue dividing it successively with those of smaller denomination until there is nothing left. Step by step:

Let's say that the initial number is 2790

How many 500 bills can we use?

2790 div 500: 5

how much is left to distribute?

2790 mod 500: 290

and we continue with the remainder

290 div 200: 1
290 mod 200: 90

90 div 100: 0
90 mod 100: 90

90 div 50: 1
90 mod 50: 40

40 div 20: 2
40 mod 20: 0

Then the final result is

  • 5 of 500
  • 1 of 200
  • 0 of 100
  • 1 of 50
  • 2 of 20

Now try to write the code with this idea.

    
answered by 07.04.2016 / 06:33
source
3

I pass an implementation to you using the algorithm indicated by @Equiso:

public class ContadorDeBilletes {

  private static int[] BILLETES;

  public static final int QUINIENTOS = 500;
  public static final int DOSCIENTOS = 200;
  public static final int CIEN = 100;
  public static final int CINCUENTA = 50;
  public static final int VEINTE = 20;
  public static final int DIEZ = 10;
  public static final int CINCO = 5;

  static {
    BILLETES = new int[] { QUINIENTOS, DOSCIENTOS, CIEN, CINCUENTA, VEINTE, DIEZ, CINCO };
  }

  public static Map<Integer, Integer> procesa(double importe) {
    Map<Integer, Integer> contador = init();
    if (importe <= 0) {
      throw new IllegalArgumentException("Importe no valido....");
    }
    for (int index = 0; index < contador.size(); index++) {
      double resultado = importe / BILLETES[index];
      if (resultado > 0) {
        contador.put(BILLETES[index], (int) resultado);
      }
      importe = importe - ((int) resultado * BILLETES[index]);
    }
    return contador;
  }

  private static Map<Integer, Integer> init() {
    Map<Integer, Integer> contador = new HashMap<Integer, Integer>();
    contador.put(QUINIENTOS, 0);
    contador.put(DOSCIENTOS, 0);
    contador.put(CIEN, 0);
    contador.put(CINCUENTA, 0);
    contador.put(VEINTE, 0);
    contador.put(DIEZ, 0);
    contador.put(CINCO, 0);
    return contador;
  }

}

Your tests:

public class ContadorDeBilletesTest {

  @Test
  public void test() {
    Map<Integer, Integer> contador = ContadorDeBilletes.procesa(5324.234);

    assertThat(contador.get(ContadorDeBilletes.QUINIENTOS), is(10));
    assertThat(contador.get(ContadorDeBilletes.DOSCIENTOS), is(1));
    assertThat(contador.get(ContadorDeBilletes.CIEN), is(1));
    assertThat(contador.get(ContadorDeBilletes.CINCUENTA), is(0));
    assertThat(contador.get(ContadorDeBilletes.VEINTE), is(1));
    assertThat(contador.get(ContadorDeBilletes.DIEZ), is(0));
    assertThat(contador.get(ContadorDeBilletes.CINCO), is(0));
  }

  @Test(expected = IllegalArgumentException.class)
  public void testZero() {
    ContadorDeBilletes.procesa(0);
  }

  @Test
  public void testSinResto() {
    Map<Integer, Integer> contador = ContadorDeBilletes.procesa(500);

    assertThat(contador.get(ContadorDeBilletes.QUINIENTOS), is(1));
    assertThat(contador.get(ContadorDeBilletes.DOSCIENTOS), is(0));
    assertThat(contador.get(ContadorDeBilletes.CIEN), is(0));
    assertThat(contador.get(ContadorDeBilletes.CINCUENTA), is(0));
    assertThat(contador.get(ContadorDeBilletes.VEINTE), is(0));
    assertThat(contador.get(ContadorDeBilletes.DIEZ), is(0));
    assertThat(contador.get(ContadorDeBilletes.CINCO), is(0));
  }

}
    
answered by 07.04.2016 в 09:47