2014-02-26 30 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 *月的利率。