0

我正在尝试创建一个银行帐户,但我的 addInterest() 函数不起作用。它似乎无法识别我的功能。它不会像应有的那样将利息添加到余额中。我认为计算根本不会发生。我试图修改 addInterest() 但无济于事。我无法理解出了什么问题。任何帮助,将不胜感激。

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);
        }


    }
4

2 回答 2

1

问题在这里:

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

添加兴趣返回一个值,但从不使用此值。你应该做

 balance = addInterest(balance);

相反(可能,我认为您已经在 addInterest 函数中添加了余额)。

于 2015-08-22T20:55:51.443 回答
1

您正在从 中返回一个值addInterest,但是在调用它的方法中,您没有对返回的数字做任何事情。你只要打电话。

所以addInterest返回新余额,但该值未分配给任何地方的余额。

于 2015-08-22T20:55:59.187 回答