继承/子类帮助

时间:2016-02-15 05:05:43

标签: java inheritance

我正在尝试使用支票和储蓄帐户子类创建我的银行帐户的子类。我创建了帐户/子类,但我很难在支票帐户上实施透支限制,并阻止储蓄帐户被透支。我感谢任何能帮助我展示更多操纵这些子类的方法的人。谢谢!

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

当前代码

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

}

}

1 个答案:

答案 0 :(得分:0)

所以我编辑了储蓄账户,以反映如果账户小于零则无法退出,但不是让我使用这些陈述。

储蓄帐户子类

[webView stringByEvaluatingJavaScriptFromString:
  [NSString stringWithFormat:@"document.getElementById('android').innerHTML = iOS]];