Compound interest with switch and [preferably] without break in each case

0

Hello. I have the table below, which calculates interest on interest [0.5% per month, cumulative] and would like to do it with switch and [preferably] without break in each case. I also wanted the case to start with the last installment ( case 12: , in this case).

par-    valor 
cela   a pagar
1    R$ 100,00 
2    R$ 105,00 
3    R$ 110,25 
4    R$ 115,76 
5    R$ 121,55 
6    R$ 127,63 
7    R$ 134,01 
8    R$ 140,71 
9    R$ 147,75 
10   R$ 155,13 
11   R$ 162,89 
12   R$ 171,03 

I came up with a very "rustic" solution, which solved the problem, but that was too pedreirona:

import java.util.Scanner;

public class SwitchExercicio003 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner leitor = new Scanner(System.in);
    System.out.println("Qual parcela você está pagando? De 1 a 12");
    int parcela = leitor.nextInt();
    double valor = 100.00;
    double taxa = 1.05;
    double case2 = valor * taxa; 
    double case3 = case2 * taxa;
    double case4 = case3 * taxa;
    double case5 = case4 * taxa;
    double case6 = case5 * taxa;
    double case7 = case6 * taxa;
    double case8 = case7 * taxa;
    double case9 = case8 * taxa;
    double case10 = case9 * taxa;
    double case11 = case10 * taxa;
    double case12 = case11 * taxa;

    switch (parcela) {
    case 1:
        System.out.println("Você deve pagar R$ " + valor);
        break;
    case 2:
        System.out.println("Você deve pagar R$ " + case2);
        break;
    case 3:
        System.out.println("Você deve pagar R$ " + case3);
        break;
    case 4:
        System.out.println("Você deve pagar R$ " + (case4));
        break;
    case 5:
        System.out.println("Você deve pagar R$ " + (case5));
        break;
    case 6:
        System.out.println("Você deve pagar R$ " + (case6));
        break;
    case 7:
        System.out.println("Você deve pagar R$ " + (case7));
        break;
    case 8:
        System.out.println("Você deve pagar R$ " + (case8));
        break;
    case 9:
        System.out.println("Você deve pagar R$ " + (case9));
        break;
    case 10:
        System.out.println("Você deve pagar R$ " + (case10));
        break;
    case 11:
        System.out.println("Você deve pagar R$ " + (case11));
        break;
    case 12:
        System.out.println("Você deve pagar R$ " + (case12));
        break;
    default:
        System.out.println("Oops, digite um número entre 1 e 12!");
    }

}

}

The teacher said that I could do exactly as I mentioned in the title of the post, but I could not get anywhere near that. Thank you in advance for your help. Thanks.

    
asked by anonymous 04.04.2016 / 15:32

2 answers

1

It would be easier to create a method that calculates the interest , follows sample code:

public class TesteJuros {
    public static void main(String[] args) {
        for (int i = 0; i < 12; i++) {
            System.out.println(String.format("%.2f", calcularJuros(5, i, 100)));
        }
    }

    public static double calcularJuros(double taxa, int mesesDecorridos, double valor) {
        // formula para calculo de juros compostos
        double multiplicador = Math.pow(1.0 + taxa / 100.0, mesesDecorridos) - 1.0;
        return valor + multiplicador * valor;
    }
}

Abcs!

    
04.04.2016 / 16:28
0
switch (parcela) {
    default:
        System.out.println("Oops, digite um número entre 1 e 12!");
        break
    case 12:
        valor *= taxa;
    case 11:
        valor *= taxa;
    case 10:
        valor *= taxa;
    case 9:
        valor *= taxa;
    case 8:
        valor *= taxa;
    case 7:
        valor *= taxa;
    case 6:
        valor *= taxa;
    case 5:
        valor *= taxa;
    case 4:
        valor *= taxa;
    case 3:
        valor *= taxa;
    case 2:
        valor *= taxa;
    case 1:
        System.out.println("Você deve pagar R$ " + valor);
}

And if you really need to fix this without break:

switch (parcela) {
    default:
        valor = 0.0
    case 12:
        valor *= taxa;
    // ...
    case 2:
        valor *= taxa;
    case 1:
        if (valor > 0.0) {
            System.out.println("Você deve pagar R$ " + valor);
        } else {
            System.out.println("Oops, digite um número entre 1 e 12!");
        }
}
    
04.04.2016 / 16:25