Ejemplo n.º 1
0
        private IEnumerable <Coin> Withdraw(int sum, WithdrawalMode mode)
        {
            var result = new List <Coin>();

            while (sum > 0)
            {
                Coin coin;

                switch (mode)
                {
                case WithdrawalMode.Buy:
                    coin        = GetCoinForBuy(sum);
                    coin.IsUser = false;
                    break;

                case WithdrawalMode.Change:
                    coin        = GetCoinForChange(sum);
                    coin.IsUser = true;
                    break;

                case WithdrawalMode.Flush:
                    coin = GetCoinForFlush(sum);
                    _coins.Remove(coin);
                    break;

                default:
                    throw new NotImplementedException();
                }

                result.Add(coin);
                sum -= coin.Value;
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to transfer funds from one BankAccount to another.
        /// </summary>
        /// <param name="other">Destination BankAccount.</param>
        /// <param name="amount">Amount to transfer.</param>
        /// <param name="allowOverdraw">True if the transaction can leave the source account negative.</param>
        /// <returns>True if the transaction succeeded.</returns>
        public bool TryTransferTo(BankAccount other, decimal amount, WithdrawalMode withdrawalMode = WithdrawalMode.RequireFullBalance)
        {
            if (other == null)
            {
                return(false);
            }

            if (TryWithdraw(amount, withdrawalMode))
            {
                other.Deposit(amount);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Tries to withdraw a specified amount from the BankAccount's Balance.
        /// </summary>
        /// <param name="amount">Amount to withdraw.</param>
        /// <param name="allowOverdraw">True if the account can go negative.</param>
        /// <returns>True if transaction succeeded.</returns>
        public bool TryWithdraw(decimal amount, WithdrawalMode withdrawalMode = WithdrawalMode.RequireFullBalance)        // bool allowOverdraw = false)
        {
            var     bank = BankingPlugin.Instance.Bank;
            decimal newBalance, previousBalance;

            if (amount < 0)
            {
                amount = Math.Abs(amount);
            }

            lock (locker)
            {
                if ((Balance - amount) < 0)
                {
                    if (withdrawalMode == WithdrawalMode.RequireFullBalance)
                    {
                        return(false);
                    }

                    if (withdrawalMode == WithdrawalMode.StopAtZero)
                    {
                        amount = Balance;
                    }
                }

                previousBalance = Balance;
                Balance        -= amount;
                newBalance      = Balance;
                //bank.Database.Update(this);
                MarkAsDirty();
            }

            //bank.InvokeBalanceChanged(this, ref newBalance, ref previousBalance);
            bank.InvokeAccountWithdraw(this, ref newBalance, ref previousBalance);

            return(true);
        }