创建实例/测试代码



Arlight所以我有我做过的所有代码,它相当多。基本上它只是创建一个银行账户。我为支票和储蓄账户制作了代码。所有这些代码都是正确的。我只需要帮助在我的Testaccount类中制作储蓄和支票账户的实例。

主账户代码:

public class Accountdrv {
  public static void main (String[] args) {
    Account account = new Account(1122, 20000, 4.5);
    account.withdraw(2500);
    account.deposit(3000);
    System.out.println("Balance is " + account.getBalance());
    System.out.println("Monthly interest is " +
      account.getMonthlyInterest());
    System.out.println("This account was created at " +
      account.getDateCreated());
  }
}
class Account {
  private int id;
  private double balance;
  private double annualInterestRate;
  private java.util.Date dateCreated;
  public Account() {
    dateCreated = new java.util.Date();
  }
  public Account(int id, double balance, double annualInterestRate) {
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
    dateCreated = new java.util.Date();
  }
  public int getId() {
    return this.id;
  }
  public double getBalance() {
    return balance;
  }
  public double getAnnualInterestRate() {
    return annualInterestRate;
  }
  public void setId(int id) {
    this.id =id;
  }
  public void setBalance(double balance) {
    this.balance = balance;
  }
  public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
  }
  public double getMonthlyInterest() {
    return balance * (annualInterestRate / 1200);
  }
  public java.util.Date getDateCreated() {
    return dateCreated;
  }
  public void withdraw(double amount) {
    balance -= amount;
  }
  public void deposit(double amount) {
    balance += amount;
  }
}

储蓄:

class Savings extends Account{
  public Savings(int id, double balance, double annualInterestRate) {
  super(id, balance, annualInterestRate);
 }
public void withdraw(double amount) {
    if(super.getBalance() < amount)
    {
       System.out.println("Error");
    }
    else
    {
       super.withdraw( amount  );
       System.out.println("Withdraw Completed");
        }
    }
}

检查:

public class Checking extends Account{
   private double overdraft_limit = 100;
    public Checking(int id, double balance, double annualInterestrate){
        //super();
        super(id, balance, annualInterestrate);
    }
        public void withdraw(double amount) {
            if(super.getBalance() >= (amount + overdraft_limit))
            {
                System.out.println(" account Overdrawn");
            }
            else
            {
                super.withdraw( amount );
                System.out.println("Withdraw Completed");
            }
        }
    }
好的,这是我

需要帮助的部分,它可能非常简单,但我无法弄清楚如何写出来,我需要创建一个储蓄和支票实例并提取一些钱,这是我到目前为止所做的。

测试帐户:

public class Testaccount {
    public static void main(String[] args) {
        Account account = new Account(0, 100, 0.6);
        System.out.println(account);
        account.withdraw(10.50);
        System.out.println(account);
        account.deposit(5.0);
        System.out.println(account);
        // Need to add test cases for Savings and Checking
    }
}

是否要使用您创建的帐户实例中已存在的信息?如果没有(即您想创建一个包含初始金额的新支票和储蓄账户),您可以执行以下操作:

Checking checking = new Checking(0,100,0.6);
checking.withdraw(50.00);

否则,如果要使用现有账户实例并将其设置为支票账户并提款,请执行以下操作:

Checking checking = (Checking) account;
checking.withdraw(50.00);