account.cs

来自「Microsoft?Visual C#?.NET (Core Reference」· CS 代码 · 共 62 行

CS
62
字号
using System;

namespace MSPress.CSharpCoreRef.Bank
{
    public class Account
    {
        public delegate bool DebitPolicy(decimal aWithdrawal);
        public Account(decimal anInitialBalance,
                       DebitPolicy aDebitPolicy)
        {
            _balance = anInitialBalance;
            _debitPolicy = aDebitPolicy;
        }

        public decimal Balance
        {
            get
            {
                return _balance;
            }
        }

        public void Deposit(decimal aDeposit)
        {
            if(aDeposit < 0)
                throw new ArgumentOutOfRangeException();
            _balance += aDeposit;
        }

        public bool Withdrawal(decimal aWithdrawal)
        {
            if(aWithdrawal < 0)
                throw new ArgumentOutOfRangeException();
            if(aWithdrawal < _balance)
            {
                _balance -= aWithdrawal;
                return true;
            }
            else
            {
                // If no debit policy, no overdrafts are permitted.
                if(_debitPolicy == null)
                    return false;

                aWithdrawal -= _balance;
                if(_debitPolicy(aWithdrawal))
                {
                    _balance = 0;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        protected DebitPolicy _debitPolicy;
        protected decimal _balance;
    }
}

⌨️ 快捷键说明

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