银行账户的透支方法

时间:2015-06-16 13:42:34

标签: java user-accounts banking

我尝试应用一种方法来检查帐户是否已超过OVERDRAFT_LIMIT。我尝试了几件事,但没有成功。

这是我所做的,但不知道如何应用它:

public void testForOverdraft(double balancE)
{
    if(balancE <= 0 && balancE >= OVERDRAFT_LIMIT)
    {
        withdraw(10);
        System.out.println("The account is overdrawn.");
    }
    else if(balancE <= 0 && balancE <= OVERDRAFT_LIMIT)
    {
        withdraw(20);
        System.out.println("The account is locked until a deposit is made to bring the account up to a positive value."); 
    }
}

帐户:

import java.util.Date;

public class Account
{
    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;
    private double monthlyInterestRate;

    public Account()
    {
        id = 0;
        balance = 0;
        annualInterestRate = 0;
    }
    public Account(int iD, double balancE)
    {
        id = iD;
        balance = balancE;
    }
    public void setID(int iD)
    {
        id = iD;
    }
    public int getID()
    {
        return(id);
    }
    public void setBalance(double balancE)
    {
        balance = balancE;
    }
    public double getBalance()
    {
        return(balance);
    }
    public void setAnnualInterestRate(double AIR)
    {
        annualInterestRate = AIR;
    }
    public double getAnnualInterestRate()
    {
        return(annualInterestRate);
    }
    public void setDateCreated(Date dateCreated)
    {
        this.dateCreated = dateCreated;
    }
    public double getMonthlyInterestRate()
    {
        return((annualInterestRate / 100) / 12);
    }
    public double getMonthlyInterest()
    {
        return(balance * monthlyInterestRate);
    }
    public void withdraw(double ammount)
    {
        balance = balance - ammount;
        setBalance(balance);
    }
    public void deposit(double ammount) {
        balance = balance + ammount;
        setBalance(balance);
    }
}

的CheckingAccount:

public class CheckingAccount extends Account
{
    final static double OVERDRAFT_LIMIT = -50.00;
    private double annualInterest;

    public CheckingAccount()
    {
        super(); 
    }
    public CheckingAccount(int iD, double balancE)
    {
        super(iD, balancE);
    }
    public double getAnnualInterest()
    {
        return((getBalance() * getAnnualInterestRate()) / 100);
    }
}

测试

public class Test extends CheckingAccount
{
    public static void main(String [] args)
    {
        CheckingAccount a1 = new CheckingAccount(1122, 15.00);
        a1.withdraw(5.00);
        a1.deposit(00.00);
        a1.setAnnualInterestRate(4.5);
        Date dat = new Date();

        System.out.println("Balance: " + 
                "\nMonthly Interest: " + a1.getMonthlyInterest() + 
                "\nDate Created: " + dat);

    }
}

1 个答案:

答案 0 :(得分:0)

在撤销方法中确认之前检查新余额

如果成功完成,则撤销方法将返回true

public boolean withdraw(double ammount)
{
    boolean success ;
    double aux ;

    aux = balance - ammount ;

    if(aux < OVERDRAFT_LIMIT)
    {
        success = false ;
    }
    else 
    {
        setBalance(aux);
        success = true ;
    }

    return success ;
}

Then to use it just go like this 

if(!withdraw(60))
{
    System.out.println("The account is locked until a deposit is made to bring the account up to a positive value."); 
}