Пример #1
0
        public void StoreMoney(UserMoney money)
        {
            var userMoney = this.Money.Where(s => s.Currency.Name.Equals(money.Currency.Name)).FirstOrDefault();

            if (userMoney == null)
            {
                userMoney = money;
                this.Money.Add(userMoney);
            }
            else
            {
                userMoney.CashIn(money.Amount);
            }
        }
Пример #2
0
        public void Convert(Currency fromCurrency, Currency toCurreny, decimal amount)
        {
            var moneyFrom = this.Money.Where(s => s.Currency.Name.Equals(fromCurrency.Name)).FirstOrDefault();

            if (moneyFrom == null)
            {
                throw new Exception($"The current user dont have money stored with the {fromCurrency.Name} currency.");
            }

            var moneyTo = this.Money.Where(s => s.Currency.Name.Equals(toCurreny.Name)).FirstOrDefault();

            if (moneyTo == null)
            {
                moneyTo = new UserMoney
                {
                    Currency = toCurreny
                };
            }

            decimal cashInAmount  = amount,
                    cashOutAmount = 0m;

            if (moneyFrom.CanConvert(toCurreny, amount))
            {
                decimal mfConvertedAmount = moneyFrom.ConvertToCurrency(toCurreny);
                cashOutAmount = mfConvertedAmount - (mfConvertedAmount - cashInAmount);

                moneyTo.CashIn(cashInAmount);
                moneyFrom.CashOut(cashOutAmount);
            }
            else
            {
                throw new Exception($"The {moneyFrom.Currency.Name} money balance is not enough to convert to {cashInAmount} {toCurreny.Name}");
            }

            if (moneyTo == null)
            {
                this.Money.Add(moneyTo);
            }
        }