Click here to Skip to main content
16,018,092 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi I'm trying to create a compound interest calculator using a switch statement which offers 4 different accounts which each hold a different interest rate the accounts are S, D, C or L. The user enters the amount they wish to place in the account then they choose which account they'd like to use and then then it should output the value after of their money after being in the account for 1 year but it's only outputting their initial amount, below is my code I appreciate any help!

Java
import java.util.*;

public class CompoundInterest {
    public static void main(String[] args) {
    double amount;
    double rate = 0;
    double time = 1;
    char group;
    Scanner keyboard = new Scanner(System.in);
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter an amount");
    
    int p = sc.nextInt();
    
    System.out.println("Please enter an account type (S, D, C or L");
    group = keyboard.next() .charAt(0);
   
    switch(group)
    {
        case 'S' : rate = 3.0;
        break;
        case 'D' : rate = 0.5;
        break;
        case 'C' : rate = 1.5;
        break;
        case 'L' : rate = 4.0;
    }
    amount = p * Math.pow(1+rate, time);
    System.out.println("Value after one year is: " + amount +"" );
    }

}
Posted
Updated 28-Nov-14 5:26am
v2
Comments
Tomas Takac 28-Nov-14 10:55am    
The only thing that catches my eye is p being an int. Try to change that to double.
Member 10686581 28-Nov-14 11:04am    
Still the same problem
Tomas Takac 28-Nov-14 11:18am    
No idea. Try to print values of p, rate & time just before the calculation. Maybe that will tell you something.

1 solution

Your formula is not correct, see http://www.icsejavatutorial.com/programs/compound-interest[^]. Also your rate values are 100 times too high, they should be 0.03, 0.005, 0.015, 0.04.
 
Share this answer
 
v2
Comments
CPallini 28-Nov-14 13:05pm    
Sorry, I intended 5 but clicked on 4. Sorry again.
OK, looks fixed now.
Richard MacCutchan 28-Nov-14 13:30pm    
No votes at all at the moment. Did you really mean this comment for me? And for the record you can revote any questions or answers if you change your mind (or miss, when drunk). :)
CPallini 28-Nov-14 13:37pm    
I keep voting 5, but nothing happens (I'm not drunk, yet :-) ).
Richard MacCutchan 29-Nov-14 4:22am    
Looks like the grappa worked.
CPallini 29-Nov-14 5:51am    
It always does.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900