2014-03-05 69 views
0

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"); 

      } 
     } 
    } 

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

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

您应该考虑使用'JUnit'或测试代码 –

+1

另一个框架你注意到类的Accountdrv“在同一个文件作为“帐户”类。这几乎是你想要做的。现在只需考虑相关案例进行测试。 – AnxGotta

回答

0

你想对已经使用该信息存在于您所创建的帐户的实例?如果不是(即您要创建一个新的支票和储蓄账户,在他们最初的金额),你可以做这样的事情

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

否则,如果要使用现有的帐户实例,并使它成为一个支票帐户和退出,请执行以下操作:

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