Ejemplo n.º 1
0
        /// <summary>
        /// Refills a <paramref name="bankAccount"/> by <paramref name="amount"/>.
        /// </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 Refill(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.RefillAmount(amount + features.RefillPrice);
            account.SetBonusFromOperation(_bonusCouter.GetBonusFromRefill(account, amount));

            _bankAccountListStorage.SaveListOfBankAccounts(StorageFilePath, BankAccounts);
        }
        /// <summary>
        /// Withdrawals the amount.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="viewModel"/>
        /// </exception>
        public void WithdrawalAmount(BillViewModel viewModel)
        {
            if (ReferenceEquals(viewModel, null))
            {
                throw new ArgumentNullException(nameof(viewModel));
            }

            if (ReferenceEquals(viewModel.ClientId, null))
            {
                return;
            }

            Account account = Find(viewModel.ClientId);

            if (ReferenceEquals(account, null) == false)
            {
                account.Bonus  = bonusCounter.GetBonusFromRefill(accountTypeFeatures, viewModel.Currency);
                account.Amount = account.Amount + account.Bonus - viewModel.Currency;
            }
        }
        /// <summary>
        /// Refills a bank account with the <paramref name="id"/> by the <paramref name="amount"/>.
        /// </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 refill.
        /// </exception>
        public void Refill(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.RefillAmount(amount - features.RefillPrice);
                bankAccount.SetBonusFromOperation(_bonusCouter.GetBonusFromRefill((int)bankAccount.Type, amount));

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