⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkingaccount.java

📁 用JAVA实现的银行账户系统
💻 JAVA
字号:
package banking;

public class CheckingAccount extends Account {
  private static final double NO_PROTECTION = -1.0;

  private double overdraftProtection;

  public CheckingAccount(double bal, double protect) {
    super(bal);
    overdraftProtection = protect;
  }
  public CheckingAccount(double bal) {
    this(bal, NO_PROTECTION);
  }

  public boolean withdraw(double amount) {
    boolean result = true;

    if ( balance < amount ) {

      // Not enough balance to cover the amount requested to withdraw
      // Check if there is enough in the overdraft protection (if any)
      double overdraftNeeded = amount - balance;
      if (   (overdraftProtection == NO_PROTECTION)
	  || (overdraftProtection < overdraftNeeded)   ) {

	// No overdraft protection or not enough to cover the amount needed
	result = false;
      } else {

	// Yes, there is overdraft protection and enough to cover the amount
	balance = 0.0;
	overdraftProtection = overdraftProtection - overdraftNeeded;
      }

    } else {

      // Yes, there is enough balance to cover the amount
      // Proceed as usual
      balance = balance - amount;
    }

    return result;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -