Calculate compounded interest My job for now is to teach a trainee how to program. I told him to solve the following task, given by a book (I try to translate it):




  Write a program for compound interest calculation. The user has to
  enter the amount of money, the years, and the interest rate. The
  program should look like this:

Money in Euro: 100
Annual percentage rate: 6
Term in years: 4
Money after 1 years: 106.0
Money after 2 years: 112.36
Money after 3 years: 119.1016
Money after 4 years: 126.247696



Important Note

He hasn't learned anything about OOP, exception handling or using objects and methods yet. He now learns how to design simple algorithms. But he has gotten a hint how to use the scanner class to get the user input.

I want to show him my own example solution, so that he can learn. I want to ask you, if my example solution is okay for showing it to a beginner. Is it well written or have I ignored any good practices?



// use "int variable = scanner.nextInt()" for getting int values
// use "double variable = scanner.nextDouble()" for getting double values

import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String args[]) {
        // write your solution here:    
        double money;
        int years;
        double rate;

        System.out.print("Money in Euro: ");
        money = scanner.nextDouble();
        System.out.print("Annual percentage rate: ");
        rate = scanner.nextDouble();
        System.out.print("Term in years: ");
        years = scanner.nextInt();

        for (int i = 1; i &lt;= years; i++) {
            money += money * (rate / 100);
            System.out.println("Money after " + i + ". years: " + money);
        }
    }
}
I have some suggestion for you.

1) Constant name should always be uppercase.

private static final Scanner SCANNER = new Scanner(System.in);


2) When creating arrays, I suggest that you use the java style instead of the c style, since it's the more used and less error-prone, in my opinion.

Before

public static void main(String args[]) {
   //[...]
}


After

public static void main(String[] args) {
   //[...]
}


3) For the variables, I suggest that you create them on the same line as the initialization, you will save 3 lines in the method and make the code more readable.

        System.out.print("Money in Euro: ");
        double money = SCANNER.nextDouble();
        System.out.print("Annual percentage rate: ");
        double rate = SCANNER.nextDouble();
        System.out.print("Term in years: ");
        int years = SCANNER.nextInt();


4) You can extract the questions in methods, to separate the logic and it will be easier to read, if you want; but will have to explain to him :)

public static void main(String args[]) {
    // write your solution here:

    double money = askForAmountInEuro();
    double rate = askForAnnualPercRate();
    int years = askForTerm();

    for (int i = 1; i &lt;= years; i++) {
        money += money * (rate / 100);
        System.out.println("Money after " + i + ". years: " + money);
    }
}

private static int askForTerm() {
    System.out.print("Term in years: ");
    return SCANNER.nextInt();
}

private static double askForAnnualPercRate() {
    System.out.print("Annual percentage rate: ");
    return SCANNER.nextDouble();
}

private static double askForAmountInEuro() {
    System.out.print("Money in Euro: ");
    return SCANNER.nextDouble();
}
```