我应该用一个抽象方法BankAcct设计一个computeIntt()类。保存和时间属于BankAcct。问题是我无法实现兴趣和新余额的显示

这是我当前的代码:

public abstract class BankAcct
{
    private int accountNumber;
    private String accountName;
    private double bal;

    public BankAcct(int act, String name, double m)
    {
        accountNumber = act;
        accountName = name;
        bal = m;
    }

    public double getBalance()
    {
        return bal;
    }

    public String getAccountName()
    {
        return accountName;
    }

    public int getAccountNumber()
    {
        return accountNumber;
    }

    public void deposit(double m)
    {
            bal+=m;
    }

    public void withdraw(double m)
    {
        bal-=m;
    }

    public abstract double computeInt();
}




public class Save extends BankAcct
{
    public Save(int act, String name, double m)
    {
        super(act, name, m);
    }

    int act;
    String name="";
    double m;

    public double computeInt()
    {
        return m * 0.0025;
    }

}




public class Time extends BankAcct
{
    public Time(int act, String name, double m)
    {
        super(act, name, m);
    }

    int act;
    String name="";
    double m;

    public double computeInt()
    {
        return m * 0.033;
    }

}




public class MainPr
{
    public static void main(String[] args)
    {
        BankAcct sav = new Savings(1234, "ABC", 10000);
        BankAcct td = new TimeDeposit(9876, "DEF", 20000);

        // display the current balance

        System.out.println(sav.getAccountName() + " with account number " +
                    sav.getAccountNumber() + " has a balance of " +
                    sav.getBalance());

        System.out.println(td.getAccountName() + " with account number " +
            td.getAccountNumber() + " has a balance of " +
            td.getBalance());

        // deposit money

        sav.withdraw(5000.0);

        // display current value with its interest

        System.out.println("\n" + sav.getAccountName() + " with account number " +
            sav.getAccountNumber() + " earned an interest of " +
            sav.computeInt());

        System.out.println(sav.getAccountName() + " with account number " +
            sav.getAccountNumber() + " has a NEW BALANCE of " +
            sav.getBalance());

        td.deposit(2000.0);

        System.out.println("\n" + td.getAccountName() + " with account number " +
            td.getAccountNumber() + " earned an interest of " +
            td.computeInt());

        System.out.println(td.getAccountName() + " with account number " +
            td.getAccountNumber() + " has a NEW BALANCE of " +
            td.getBalance());
    }
}


MainPr将实例化以下数据的两个对象:
保存的帐号为1234; ABC的帐户名称;余额为10,000。
时间账户号为9876;帐户名称DEF;余额为20,000。

这应该是输出:CORRECT ONE


帐号为1234的ABC余额为10000.0

账号9876的DEF余额为20000.0

帐号为1234的ABC赚取了利息25.0

帐号为1234的ABC的新余额为5025.0

账号9876的DEF获得利息660.0

帐号为9876的DEF的新余额为22660.0


但是我的程序产生了错误的输出:


帐号为1234的ABC余额为10000.0

账号9876的DEF余额为20000.0

帐号为1234的ABC赚取利息0.0

帐号为1234的ABC的新余额为5000.0

拥有9876帐号的DEF获得了利息0.0

帐号为9876的DEF的新余额为22000.0


在这种情况下,在“保存”中,利率为0.25%,而时间利率为3.3%。余额取决于存入还是取出。

请帮助我。谢谢

最佳答案

您的实例变量m在子类中始终为0(从未初始化),但是看起来好像它们在您的bal类中设置为BankAcct一样。这不适用于您在computerInterest中的TimeDeposit方法:

public double computeInterest() {
    return getBalance() * 0.033;
}


其他子类中应提供类似的修复程序,或至少初始化您的m变量。与此相关的是,您的子类不需要定义actname,因为您的超类BankAcct已经定义并存储了它。

另外,您的computerInterest并没有修改您的程序似乎期望的余额。如果是这样,这可能更合适:

public double computeInterest() {
    double interest = getBalance() * 0.033;
    deposit(interest);
    return interest;
}

09-08 11:28