Java程序演示抽象的BankAccount类和SavingsAccount子类

时间:2014-03-04 17:34:08

标签: java

本周我的任务是编写一个抽象的BankAccount类和一个扩展BankAccount的SavingsAccount类。我已经写出了代码作为赋值请求它似乎完美编译。我现在必须写一个驱动程序来测试这两个类,这就是我被卡住的地方。为了清楚我不是要求任何人为我编写它,我希望最终能够自己完成这一切。我只想要一点指导。 (我和我的导师一次安排了一次,他已经取消了两次)

我基本上想知道如何为这两个类编写驱动程序类。 我的课程在字段或方法方面是否遗漏了什么?你如何经验丰富的程序员计划出这种东西?

如果您有任何建议,我们将不胜感激!

public abstract class BankAccount
{
  double balance;
  int numOfDeposits;
  int numOfWithdraws;
  double interestRate;
  double annualInterest;
  double monSCharges;
  double amount;
  double monInterest;

  //constructor accepts arguments for balance and annual interest rate
  public BankAccount(double bal, double intrRate)
  {
    balance = bal;
    annualInterest = intrRate;
  }

  //sets amount
   public void setAmount(double myAmount)
    {
        amount = myAmount;
    }

  //method to add to balance and increment number of deposits
  public void deposit(double amountIn)
  {
    balance = balance + amountIn;
    numOfDeposits++;
  }

  //method to negate from balance and increment number of withdrawals
  public void withdraw(double amount)
  {
    balance = balance - amount;
    numOfWithdraws++;
  }

  //updates balance by calculating monthly interest earned
  public double calcInterest()
  {
    double monRate;

    monRate= interestRate / 12;
    monInterest = balance * monRate;
    balance = balance + monInterest;
    return balance;
  }

  //subtracts services charges calls calcInterest method sets number of withdrawals and deposits
  //and service charges to 0
  public void monthlyProcess()
  {
    calcInterest();
    numOfWithdraws = 0;
    numOfDeposits = 0;
    monSCharges = 0;
  }

  //returns balance
  public double getBalance()
  {
    return balance;
  }

  //returns deposits
  public double getDeposits()
  {
    return numOfDeposits;
  }

  //returns withdrawals
  public double getWithdraws()
  {
    return numOfWithdraws;
  }
}

和子类

public class SavingsAccount extends BankAccount
{
  //sends balance and interest rate to BankAccount constructor
  public SavingsAccount(double b, double i)
  {
    super(b, i);
  }

  //determines if account is active or inactive based on a min acount balance of $25
  public boolean isActive()
  {
    if (balance >= 25)
      return true;
    return false;
  }

  //checks if account is active, if it is it uses the superclass version of the method
  public void withdraw()
  {
    if(isActive() == true)
    {
      super.withdraw(amount);
    }
  }

  //checks if account is active, if it is it uses the superclass version of deposit method
  public void deposit()
  {
    if(isActive() == true)
    {
      super.deposit(amount);
    }
  }

  //checks number of withdrawals adds service charge
  public void monthlyProcess()
  {
    if(numOfWithdraws > 4)
      monSCharges++;
  }
}

1 个答案:

答案 0 :(得分:2)

驱动程序或运行器类通常是一个具有main方法的类,您可以在其中运行代码。基本上...

public class TestDriver {
    public static void main(String[] args) {
        // Run your code here...
    }
}

您可能需要做的是在其中创建一些SavingsAccount对象,并显示它实现的方法有效。

如果这是学校作业,如果您不了解要求,可能需要从教师那里获得更具体的详细信息。