public void Transfer(BankAccount fromAccount, BankAccount toAccount, decimal transferAmount, Currency transferCurrency) { if (transferAmount <= 0) { throw new ApplicationException("transfer amount must be positive"); } else if (transferAmount == 0) { throw new ApplicationException("invalid transfer amount"); } else if (fromAccount.IsFrozen || toAccount.IsFrozen) { throw new ApplicationException("account is frozen"); } if (fromAccount.Amount < transferAmount) { throw new ApplicationException("insufficient funds"); } if (fromAccount.Currency == toAccount.Currency) { fromAccount.Amount -= transferAmount; toAccount.Amount += transferAmount; } else { var fromAmount = fromAccount.Currency == transferCurrency ? transferAmount : _currencyService .ConvertByAlex(transferCurrency, transferAmount, fromAccount.Currency); var toAmount = toAccount.Currency == transferCurrency ? transferAmount : _currencyService .ConvertByAlex(transferCurrency, transferAmount, toAccount.Currency); fromAccount.Amount -= fromAmount; toAccount.Amount += toAmount; } var transaction = CreateTransaction(transferAmount, transferCurrency, fromAccount, toAccount); fromAccount.OutcomingTransactions.Add(transaction); toAccount.IncomingTransactions.Add(transaction); _transactionBankRepository.Save(transaction); _bankAccountRepository.Save(fromAccount); _bankAccountRepository.Save(toAccount); }
public IActionResult ChangeCurrency(string accountNumber, string amount, string currency) { var account = _accountRepository.Get(accountNumber); var currencyFrom = (Currency)Enum.Parse(typeof(Currency), currency); //var money = _currencyService.ConvertByAlex(currencyFrom, var money = _currencyService.ConvertByAlex((Currency)Enum.Parse(typeof(Currency), currency), Convert.ToDecimal(amount), account.Currency); return(Json(new { money = money.ToString(), currency = AttributeService.GetDisplayValue(account.Currency) })); }
public string GetJsonAsTransferResult(long fromAccountId, string toAccountNumber, decimal transferAmount) { var fromAccount = _bankAccountRepository?.Get(fromAccountId); var toAccount = _bankAccountRepository?.Get(toAccountNumber); if (fromAccount == null || toAccount == null || fromAccount == toAccount) { return(JsonConvert.SerializeObject("wrong account")); } else if (transferAmount > fromAccount.Amount) { return(JsonConvert.SerializeObject("wrong amount")); } _transactionService.Transfer(fromAccount, toAccount, transferAmount); var newAmount = fromAccount.Currency == toAccount.Currency ? transferAmount :_currencyService .ConvertByAlex(fromAccount.Currency, transferAmount, toAccount.Currency); return(JsonConvert.SerializeObject(newAmount)); }