我正在尝试创建一个银行帐户,而我的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);
        }


    }

最佳答案

问题在这里:

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


增加兴趣会返回一个值,但是永远不会使用该值。你应该做

 balance = addInterest(balance);


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

关于java - 无法识别addInterest(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32160642/

10-09 05:49