# Savings Account - Need to list months

• 04-20-2011, 06:11 PM
Moka
Savings Account - Need to list months
Hello,
I am working on an assignment for a class. It involves creating a custom class to create a savings account, modify the annual interest rate for all savings accounts, and calculating the monthly interest. Then I created a test with two new accounts (saver1 and saver2). I was to set the interest rate to 4% then "calculate the monthly interest for each of 12 months and print the new balances for both savers". To me, this means I need to print something like:

January - (balance for that month)
February - (balance for that month)
...etc.

I know how to calculate the balance after 1 month, and I think I could figure out using loops to print out the balance for each month, but I have a feeling that my professor wants me to use arrays and loops to print it out like above.

The exact instructions for this assignment are:

Create class SavingsAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12-this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a program to test class SavingsAccount. Instantiate two savingsAccount objects, saver1 and saver2, with balances of \$2000.00 and \$3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest for each of 12 months and print the new balances for both savers. Next, set the annualInterestRate to 5%, calculate the next month's interest and print the new balances for both savers.

Here is the code I have right now:

Code:

```class SavingsAccount {         private static double annualInterestRate;         private double savingsBalance;                 public SavingsAccount(double savingsBalance)         {                 setBalance( savingsBalance );         }                 public static void modifyInterestRate(double i)         {                 annualInterestRate = i;         }                 public void setBalance(double b)         {                 savingsBalance = b;         }                           public double getBalance()         {                 return savingsBalance;         }                 public double calculateMonthlyInterest()         {                 savingsBalance += savingsBalance * ((annualInterestRate/100)/12);                 return savingsBalance;         }                         }```

And my test (a little haphazard right now):

Code:

```public class SavingsAccountTest {                 public static void main (String [] args)         {                 SavingsAccount saver1 = new SavingsAccount(2000.00);                                 System.out.println("Balance for account 'saver1': " + saver1.getBalance());                                 saver1.modifyInterestRate(4);                                 saver1.calculateMonthlyInterest();                                 System.out.println("New balance for account 'saver1': " + saver1.getBalance());                                 SavingsAccount saver2 = new SavingsAccount(3000.00);                                 System.out.println("Balance for account 'saver2': " + saver2.getBalance());                                 saver2.modifyInterestRate(4);                                 saver2.calculateMonthlyInterest();                                 System.out.println("New balance for account 'saver2': " + saver2.getBalance());                         }         }```
Any help is greatly appreciated. Thank you!
• 04-20-2011, 06:14 PM
Moka
Also, is there a way to suppress the output so there is only 2 digits after the decimal?
Thanks again
• 04-20-2011, 06:29 PM
Moka
I was able to use a for loop to have the program output 12 values (the balance for each month). I'm still having the issue of rounding though.

Here is the loop I used:

Code:

```for (int m = 1; m <= 12; m++)         {                                                 saver1.calculateMonthlyInterest();                                 System.out.println("Balance for 'saver1': " + saver1.getBalance());                         }```
• 04-20-2011, 06:56 PM
Moka
:P
Guess I figured it out on my own...I didn't use any arrays, so hopefully this will still be ok.
• 04-21-2011, 12:47 AM
delgeezee
How did you solve the rounding issue? I am working on this same assignment.
• 04-21-2011, 12:58 AM
Junky
You shouldn't use double or float to represent money due to the inherent inaccuracies. You can use BigDecimal or use an int multiplied by a factor of 100. That is \$5.67 becomes 567.

If you do/must use double then you can use DecimalFormat, the printf method or the String.format method. NOTE: you only need to round to 2 decimals when displaying the amount. Do not change the value stored in your double variable.
• 04-21-2011, 04:28 AM
Moka
@Junky : Yes, I'm aware of BigDecimal and how it should be used with monitary values, but I'm still not 100% clear on how it works and I needed to get this assignment done quick.

@delgeezee : I landed out using DecimalFormat. It actually worked nicely.