2014-02-26 94 views
0

我不得不創建一個被定義爲一個銀行賬戶建模使用類賬戶。 (該帳戶擁有的屬性賬號,餘額,年利率,創建日期和方法存入/提取資金。)創建子類

我怎麼會創建兩個子類進行檢查和儲蓄賬戶?支票賬戶必須具有透支限額,但儲蓄不能超支。

任何幫助或建議將是真棒,謝謝(:

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; 
    } 
} 
+0

你可以在Accountdrv類中創建單獨的操作方法 – Kick

回答

0

考慮定義一個或多個接口 例如:

public interface Account 
{ 
    public double getAnnualInterestRate(); 
    public Date getDateCreated(); 
    public int getId(); 
    public double getMonthlyInterest(); 

    public void deposit(double amount); 
    public void withdraw(double amount); 
} 

public interface CheckingAccount 
extends Account 
{ 
    public long getOverdraftLimit(); 
} 

這可能是合理補充制定者,但我寧願要麼設置在實現類的構造函數的vlues或將制定者在實現類,但不是在接口。合理的例外可能setOverDraftLimit()中的CheckingAccount接口。

您正在一些常見的錯誤賺錢:

  • 錢不是浮點值。這是一個固定點值。 double永遠不會是正確的金錢。使用long代替和存儲作爲一個單元(例如10000 = 1美元)的級分。
  • 瞭解如何執行利息計算。年利率絕不是12 *月的利率。