我试图获取利息和总余额,但是我不明白我的代码有什么问题
public class Account {
private double bal; //The current balance
private int accnum; //The account number
public String owner;
public int balance;
public Account() {
}
public Account(int a)
{
bal=0.0;
accnum=a;
}
public void deposit(double sum)
{
if (sum>0)
bal+=sum;
else
System.err.println("Account.deposit(...): "
+"cannot deposit negative amount.");
}
public void withdraw(double sum)
{
if (sum>0)
bal-=sum;
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
}
public double getBalance()
{
return bal;
}
public double getAccountNumber()
{
return accnum;
}
public String toString()
{
return "Acc: " + accnum + ": " + "Balance: " + bal;
}
public final void print()
{
//Don't override this,
//override the toString method
System.out.println( toString() );
}
}
SavingsAccount
类
public class SavingsAccount extends Account{
private double monthlyInterestRate;
public SavingsAccount( int a, double mI)
{
super(a);
monthlyInterestRate = mI;
}
public double getInterest()
{
return monthlyInterestRate*super.getBalance();
}
public double totalBalance(){
return super.getBalance() + getInterest();
}
public String toString()
{
return "Interest: " + getInterest() + "\n" +
"Total balance: " + totalBalance();
}
}
CurrentAccount
类
public class CurrentAccount extends Account {
private double overdraftLimit;
public CurrentAccount() {}
public CurrentAccount(int a, double overdraftLimit)
{
super(a);
setOverdraftLimit(overdraftLimit);
}
public void setOverdraftLimit(double overdrafLimit)
{
this.overdraftLimit = overdrafLimit ;
}
public double getOverdraftLimit()
{
return overdraftLimit;
}
public String toString()
{
return "Overdraft limit: " + overdraftLimit;
}
}
AccountMain
类
public class AccountMain {
public static void main(String[] args) {
Account ac = new Account(100555);
ac.deposit(2000);
SavingsAccount sa = new SavingsAccount(100555, 0.2);
CurrentAccount ca = new CurrentAccount(100555, 800);
System.out.println(ac.toString());
System.out.println(sa.toString());
System.out.println(ca.toString());
输出为
Acc:100555:余额:2000.0
兴趣:0.0
总余额:0.0
透支额度:800.0
答案 0 :(得分:0)
在您的代码中
public SavingsAccount(int a,double mI){ 超级(a); MonthlyInterestRate = mI; }
您正在呼叫
super(a);
您拥有的超级帐户中的
公共帐户(int a){
bal = 0.0; accnum = a; }
因此您的余额为0,因此public double getInterest()返回0,因为
monthlyInterestRate * super.getBalance();
等于 monthlyInterestRate * 0