我试图获得利息和总余额,但我不明白我的代码有什么问题
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();
}
}
慕桂英4014372
相关分类