2016-02-15 47 views
-1

我正尝试使用检查和储蓄帐户子类创建我的银行帐户的子类。我已经创建了账户/子类,但我很难在支票账户上实现透支限额并防止储蓄账户透支。我很欣赏任何能够帮助我展示如何操纵这些子类的人。谢谢!继承/子类协助

这里是我到目前为止的代码:

目前代码

import java.util.Date; 

public class Accounts 
{ 

public static void main(String[] args) 
{ 
    //Account account = new Account(1122, 20000, .045); 
    Account checkingAccount = new CheckingAccount (1500, 100.00, 0.6); 
    java.util.Date dateCreated = new java.util.Date(); 
    System.out.println("Checking Account ID: " + checkingAccount.id); 
    System.out.println("Balance: $" + checkingAccount.balance); 
    System.out.println("Balance after withdraw of $80: $" + checkingAccount.withdraw(80)); 
    //System.out.println("Overall Account ID: " + account.id); 
    //System.out.println("Date Created: " + dateCreated); 
    //System.out.println("Balance: $" + account.getBalance()); 
    //System.out.println("Interest Rate: " + account.getAnnualInterestRate() + "%"); 
    //System.out.println("Monthly Interest: $" + account.getMonthlyInterestRate()); 
    //System.out.println("Balance after withdraw of $2,500: $" + account.withdraw(2500)); 
    //System.out.println("Balance after deposit of $3,000: $" + account.deposit(3000)); 


} 

//Create Account Class  
public static class Account 
{ 
    //Define Variables 
    public int id; 
    public double balance; // Account Balance 
    public double annualInterestRate;// Store Interest Rate 
    private Date dateCreated; // Stores Account Creation Date 

    // Account Constructor 
    Account() 
    { 
     id = 0; 
     balance = 0.0; 
     annualInterestRate = 0.0;     
    } 

    // Account Constructor with id and initial balance 
    Account(int newId, double newBalance) 
    { 
    id = newId; 
    balance = newBalance; 
    } 

    Account(int newId, double newBalance, double newAnnualInterestRate) 
    { 
    id = newId; 
    balance = newBalance; 
    annualInterestRate = newAnnualInterestRate; 
    } 

    // Methods for id, balance, and annualInterestRate 
    public int getId() 
    { 
    return id; 
    } 

    public double getBalance() 
    { 
    return balance; 
    } 

    public double getAnnualInterestRate() 
    { 
    return annualInterestRate * 100; 
    } 

    public void setId(int newId) 
    { 
    id = newId; 
    } 

    public void setBalance(double newBalance) 
    { 
    balance = newBalance; 
    } 

    public void setAnnualInterestRate(double newAnnualInterestRate) 
    { 
    annualInterestRate = newAnnualInterestRate; 
    } 

    // Accessor method for dateCreated 
    public void setDateCreated(Date newDateCreated) 
    { 
    dateCreated = newDateCreated; 
    } 

    // Define method getMonthlyInterestRate 
    double getMonthlyInterestRate() 
    { 
    return (annualInterestRate/12) * balance; 
    } 

    // Define method withdraw 
    double withdraw(double amount) 
    { 
    return balance -= amount; 
    } 

    // Define method deposit 
    double deposit(double amount) 
    { 
    return balance += amount; 
    } 

} 

//Create Checking Account 
public static class CheckingAccount extends Account 
{   
    CheckingAccount (int id, double balance, double annualInterestRate) 
    { 
     super(id, balance, annualInterestRate); 
    } 



} 



//Create Savings Account 
public class SavingsAccount extends Account 
{ 
    //SavingsAccount constructor 
    public SavingsAccount (int id, double balance, double annualInterestRate) 
    { 
     super(id, balance, annualInterestRate); 
    } 

} 

} 
+0

您可能要重写'在两个子类退出()'方法。 –

+0

而且你需要一个'if'子句来确保只有'balance> amount'时才允许提取。 – Tacocat

+0

你可能有一个重写的例子,我可以看看怎么看覆盖如何工作?我也试图走这条路线,但似乎无法使其工作。 –

回答

0

所以我编辑的储蓄账户,以反映不能够撤销帐户是否小于零,但它不让我使用这些陈述。

储蓄账户子类

public class SavingsAccount extends Account 
{ 
    //SavingsAccount constructor 
    public SavingsAccount (int id, double balance, double annualInterestRate) 
    { 
     super(id, balance, annualInterestRate); 

     public double withdraw(double amount) 
     { 
      if (balance < 0) 
      { 
       System.out.println("Can't withdraw"); 
      } 
      else (balance >= 0) 
      {  
        return balance -= amount; 
      }  
     }