创建实例/测试代码

时间:2014-03-05 19:44:05

标签: java instance

Arlight所以我拥有所有这些代码,我已经完成了它的相当多...基本上它只是创建一个银行帐户。我为Checking和Savings帐户制作了代码。所有这些代码都是正确的。我只需要帮助我在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");

            }
        }
    }

好的,这是我需要帮助的部分,它可能非常简单,但我无法解决如何写出来,我需要创建一个储蓄和检查的实例,并提取一些钱,这里是什么我做到目前为止。

Testaccount:

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
    }
}

1 个答案:

答案 0 :(得分:0)

您是否要使用您创建的帐户实例中已有的信息?如果没有(即你想创建一个新的支票和储蓄账户,其中包含初始金额)你可以做类似

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

否则,如果您要使用现有的帐户实例并将其设为支票帐户并退出,请执行以下操作:

Checking checking = (Checking) account;
checking.withdraw(50.00);
相关问题