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

📄 account.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// Account.java: The class for describing an account
public class Account
{
  // Two data fields in an account
  private int id;
  private double balance;

  // Construct an account with specified id and balance
  public Account(int id, double balance)
  {
    this.id = id;
    this.balance = balance;
  }

  // Getter method for id
  public int getId()
  {
    return id;
  }

  // Setter method for balance
  public void setBalance(double balance)
  {
    this.balance = balance;
  }

  // Getter method for balance
  public double getBalance()
  {
    return balance;
  }

  // Deposit an amount to this account
  public void deposit(double amount)
    throws NegativeAmountException
  {
    if (amount < 0)
      throw new NegativeAmountException
        (this, amount, "deposit");
    balance = balance + amount;
  }

  // Withdraw an amount from this account
  public void withdraw(double amount)
    throws NegativeAmountException, InsufficientFundException
  {
    if (amount < 0)
      throw new NegativeAmountException
        (this, amount, "withdraw");
    if (balance < amount)
      throw new InsufficientFundException(this, amount);
    balance = balance - amount;
  }
}

⌨️ 快捷键说明

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