银行账户透支方式

Posted

技术标签:

【中文标题】银行账户透支方式【英文标题】:Overdraft method for bank account 【发布时间】:2015-09-01 09:04:01 【问题描述】:

我正在尝试应用一种方法来检查帐户是否已超过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);

    

【问题讨论】:

balancE &lt;= 0 &amp;&amp; balancE &gt;= OVERDRAFT_LIMIT - 这有可能吗?考虑一下,您应该可以解决问题。 @sparc_spread 如果他将 OVERDRAFT_LIMIT 存储为负数,这将是有意义的。 现在我想到透支限额的意思,你是对的。 更新了标签和格式。初次使用的用户 +1 的好问题 标题没有提供有关您所问内容的任何线索。许多不必要的代码与您的问题无关。你甚至没有提到这段代码的目的:如果它是用于现实生活中的系统,那么一切都做错了。你至少应该提到它只是为了你的学习等 【参考方案1】:

在提款方法中分配之前检查新余额

如果完全成功,withdraw方法将返回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."); 

【讨论】:

以上是关于银行账户透支方式的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中向银行账户添加利率

编写程序,模拟银行存取款业务。 (1)建银行账户类bank,有成员:储户现有款项account(int型)和

LeetCode:Database 113.银行账户概要

LeetCode:Database 113.银行账户概要

LeetCode:Database 113.银行账户概要

LeetCode:Database 113.银行账户概要