Esempio n. 1
0
        /// <summary>
        /// Withdrawals a <paramref name="amount"/> from the <paramref name="bankAccount"/>.
        /// </summary>
        /// <param name="bankAccount">A bank account.</param>
        /// <param name="amount">An amount.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown when <paramref name="bankAccount"/> equal to null.
        /// </exception>
        /// <exception cref="BankAccountNotFoundException">
        /// Thrown when <paramref name="bankAccount"/> not found in the list of bank accounts.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Thrown when <paramref name="amount"/> less than or equal to 0.
        /// </exception>
        public void Withdrawal(BankAccount bankAccount, decimal amount)
        {
            if (ReferenceEquals(bankAccount, null))
            {
                throw new ArgumentNullException(nameof(bankAccount));
            }

            if (!BankAccounts.Contains(bankAccount))
            {
                throw new BankAccountNotFoundException("This bank account not found.");
            }

            if (amount <= 0)
            {
                throw new ArgumentOutOfRangeException("Amount must be greater than 0.", nameof(amount));
            }

            BankAccount     account  = BankAccounts.Find(x => x == bankAccount);
            AccountFeatures features = new AccountFeatures(account.Type);

            account.WithdrawalAmount(amount - features.WithdrawalPrice);
            account.SetBonusFromOperation(_bonusCouter.GetBonuxFromWithdrawal(account, amount));

            _bankAccountListStorage.SaveListOfBankAccounts(StorageFilePath, BankAccounts);
        }
        /// <summary>
        /// Withdrawals a <paramref name="amount"/> from the bank account with the <paramref name="id"/>.
        /// </summary>
        /// <param name="id">The bank account id.</param>
        /// <param name="amount">The amount to refill.</param>
        /// <exception cref="ArgumentException">
        /// Thrown when <paramref name="id"/> or/and <paramref name="amount"/> less than 0.
        /// </exception>
        /// <exception cref="BankServiceException">
        /// Thrown when the bank account with the <paramref name="id"/> not found,
        /// something wrong with updating in the storage or insufficient founds to withdrawal.
        /// </exception>
        public void Withdrawal(int id, decimal amount)
        {
            if (id < 0)
            {
                throw new ArgumentException("The id must be greater than or equal to 0.", nameof(id));
            }

            if (amount < 0)
            {
                throw new ArgumentException("The amount must be greater than or equal to 0.", nameof(amount));
            }

            UpdateList();

            BankAccount bankAccount = _bankAccounts.Find(x => x.ID == id);

            if (ReferenceEquals(bankAccount, null))
            {
                throw new BankServiceException("No such bank account.");
            }

            AccountTypeFeatures features = new AccountTypeFeatures(bankAccount.Type);

            try
            {
                bankAccount.WithdrawalAmount(amount + features.WithdrawalPrice);
                bankAccount.SetBonusFromOperation(_bonusCouter.GetBonuxFromWithdrawal((int)bankAccount.Type, amount));

                _bankAccountStorage.UpdateAccount(bankAccount.ToAccount());
            }
            catch (InsufficientFundsException ex)
            {
                throw new BankServiceException("An error occured while withdrawal amount.", ex);
            }
            catch (AccountNotFoundException ex)
            {
                throw new BankServiceException("An error occured while updating in the storage.", ex);
            }
        }