2014-03-05 121 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);