0

This is my completed code I was wondering whether it needs a correction

This is my first class

public class Account
{
    private double balance; //STATE
    private double interestRate; //STATE
    private double rate;//STATE

    public Account()
    {
        balance = 0; 
        interestRate = 0;
    }

    public Account(double amount, double interestRate)
    {
        balance = amount;   
        rate = interestRate;

    } 

    public void deposit(double amount)
    {
        balance=balance+amount;
    }

    public void withdraw(double amount)
    {
        balance = balance - amount;
    }

    public void setInterest(double rate)
    {
        balance = balance + balance * rate;
        //this.setInterst = setInterest;  
        //setInterest = InterestRate / 12;
    }

    public double computeInterest(int n)
    {
        balance=Math.pow(balance*(1+rate),n/12); 
        return balance;
    }

    public double getsetInterest()
    {
        return rate;
    }

    public double getBalance()
    {
        return balance;
    }

    public void close()
    {
        balance =0;
    }

}

This is my second class

public class TestAccountInterest
{
    public static void main (String[] args)
    {
        Account acc1 = new Account(500, 0.1);//0.10);
        Account acc2 = new Account(400, 0.2); //0.20);

      /*************************************
       ACC1 ACCOUNT BELOW
       *************************************/
        acc1.deposit(500);
        acc1.withdraw(300);
        acc1.computeInterest(12);
        System.out.println(acc1.computeInterest(12));

        /**************************************
        ACC2 ACCOUNT BELOW
         **************************************/
        acc2.withdraw(200);
        acc2.deposit(800);
        acc2.computeInterest(24);
        System.out.println(acc2.computeInterest(24));

    }

}

Can anyone see whether I would be able to make the code more compact and is the way that I have coded perfectly valid. The code is about an Accounts class with a test account class for the second class it is supposed to calculate the compute interest of 12 months for the first one and for the second one is supposed to be about 24 months.

Omor
  • 23
  • 1
  • 3
  • 5
    If the code works correctly and is just for improvement, it may be better on the [Code Review](https://codereview.stackexchange.com/) website. – AntonH Feb 13 '17 at 19:35
  • well there seems to be a problem when I do run it for the first 12 months the interest looks normal but for the 24 months it gives a very long winded number. – Omor Feb 13 '17 at 19:37
  • Then that is not an improvement, it is a correction. Try editing your title and question to provide more information to receive help. – AntonH Feb 13 '17 at 19:39
  • I'm voting to close this question as off-topic because questions to review working code should to go codereview.stackexchange.com – GhostCat Feb 13 '17 at 19:49

1 Answers1

0
  1. The Account class looks fine, except I don't understand why setIntrest() doesn't set the interest, but instead changes the balance. Maybe that's by design, maybe not, but I found that to be a bit surprising.

  2. If you want to print the 24 month computeIntrest a bit more cleanly, take a look at this question.

  3. These types of questions don't belong on StackOverflow. Here, we mostly (try to) solve problems. Suggesting improvements to working code is this way.

Malt
  • 28,965
  • 9
  • 65
  • 105