checkingaccount.java

来自「用JAVA实现的银行账户系统」· Java 代码 · 共 46 行

JAVA
46
字号
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 + =
减小字号Ctrl + -
显示快捷键?