我应该设计一个BankAcct
带有抽象方法的类computeIntt()
。Save 和 Time 是 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 将为以下数据实例化两个对象: Save 的帐号为 1234;ABC账户名;余额10,000。时间有账号9876;账户名 DEF;余额20,000。
这应该是输出:正确的一个
帐号为 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
在这种情况下,Save 中的利率为 0.25%,而时间利率为 3.3%。余额取决于是存款还是取款。
请帮助我。谢谢