0

I am trying to create a bank account and my addInterest() function is not working. It seems like it doesn't recognize my function. It doesn't add the interest onto balance like it should. I think the calculation doesn't happen at all. I have tried to modify the addInterest() but to no avail. I cannot understand what's wrong. Any help would be appreciated.

package lab2;
import java.util.*;
import java.io.*;

public class Account {

    protected String accName;
    protected Double balance;

    public Account(String name, double initDeposit) {
        accName = name;
        balance = initDeposit;
    }

    public String getAccountName() {
    return accName;
    }

    public double getBalance() {
    return balance;
    }

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

    public void withdraw(double amount) {
        int fee = 6;
        if (balance < 100) {
            amount += fee;
            balance -= amount;
        }   
        else if (amount > balance) {
            double limit = -50;
            if (balance > limit)
                balance = limit;

        }
        else
            balance -= amount;

    }

    public String toString() {
        return accName + ' ' + balance;
    }

    public static class CurrentAccount extends Account {

        final static double DEFAULT_LIMIT = -50;
        double limit = DEFAULT_LIMIT;

        public CurrentAccount(String name, double i) {
            super(name, i);
        }

        public void withdraw(double amount) {
            double fee = 6;

            if (balance < 100) {
                amount += fee;
                balance -= amount;
            }
            if (amount > balance) {
                double res;
                res = balance - amount;

                if (res > -50) 
                    res = limit;
            }

            balance -= amount;

        }

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

        public String toString() {
            return accName + ' ' + balance;
        }

    }

    public static class DepositAccount extends Account {

        final static double DEFAULT_INTEREST = 0.04;
        double interestRate = DEFAULT_INTEREST;

        public DepositAccount(String name, double i) {
            super(name, i);
        }

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

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

        public double addInterest(double bal) {
            return (bal*interestRate)+bal;
        }

        public String toString() {
            return accName + ' ' + balance;
        }

    }

    public static class HigherRateAccount extends Account {

        final static double DEFAULT_INTEREST = 0.07;
        double interestRate = DEFAULT_INTEREST;

        public HigherRateAccount(String name, double i) {
            super(name, i);
        }

        public void withdraw(double amount) {
            double fee;
            fee = 10;

            double feeFixed = amount + fee;
            balance += feeFixed;
        }

        public double addInterest(double bal) {
            return (bal*interestRate)+bal;
        }

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

        public String toString() {
            return accName + ' ' + balance;
        }
    }


     public static void main(String[] args) {
        Account arthur = new CurrentAccount("Arthur", 200);
        Account brenda = new DepositAccount("Brenda", 70);
        Account charlie = new HigherRateAccount("Charlie", 1000);
        System.out.println(arthur);
        System.out.println(brenda);
        System.out.println(charlie);
        arthur.withdraw(50);
        brenda.withdraw(50);
        charlie.deposit(1000);
        System.out.println(arthur);
        System.out.println(brenda);
        System.out.println(charlie);
        arthur.withdraw(175);
        brenda.deposit(90);
        charlie.withdraw(3000);
        System.out.println(arthur);
        System.out.println(brenda);
        System.out.println(charlie);
        }


    }
Leo
  • 11
  • 1
  • 2
  • 5
  • 2
    It is easier to understand your question if you say 1. Here is my input. 2. Here is what I expect for output 3. Here is what is actually happening – fdsa Aug 22 '15 at 20:55

2 Answers2

1

The problem is here:

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

Add interest returns a value, but this value is never used. You should do

 balance = addInterest(balance);

instead (probably, I think you already add the balance in the addInterest function).

eric.m
  • 1,577
  • 10
  • 20
1

You are returning a value from addInterest, but in the method that calls it, you are not doing anything with the number that was returned. You just call.

So addInterest returns the new balance, but that value is not assigned to the balance anywhere.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79