Ejemplo n.º 1
0
        public static void EnsureSeedData(this CoinifyWebContext context)
        {
            var(coinSizes, coins, notes, users, atm, currencyDictionary) = CreateEntitiesArrays();

            if (context.Database.GetPendingMigrations().Count() > 0)
            {
                context.Database.Migrate();
            }

            if (!context.CoinSize.Any())
            {
                context.CoinSize.AddRange(coinSizes);
            }

            if (!context.Coin.Any())
            {
                context.Coin.AddRange(coins);
            }

            if (!context.Note.Any())
            {
                context.Note.AddRange(notes);
            }

            if (!context.AutomatedTellerMachine.Any())
            {
                context.CurrencyDictionary.Add(currencyDictionary);
                context.AutomatedTellerMachine.Add(atm);
            }

            if (!context.User.Any())
            {
                context.User.AddRange(users);
            }

            context.SaveChanges();
        }
Ejemplo n.º 2
0
        public static CurrencyDictionary WithdrawFromAtm(User user, AutomatedTellerMachine atm, int amount, CoinifyWebContext context = null)
        {
            var curDict = atm.CurrencyDictionary;

            var ret = new CurrencyDictionary()
            {
                CoinDictionary = new Dictionary <Coin, int>(),
                NoteDictionary = new Dictionary <Note, int>()
            };

            if (user.Balance - amount < 0)
            {
                throw new InsufficentFundsException($"User {user.UserId} has insuficient funds to withdraw ${amount}");
            }

            user.Balance -= amount;

            var validMoney = GenerateValidMoneyDictionary(curDict);

            foreach (var kvp in validMoney.ToList())
            {
                while (validMoney[kvp.Key] != 0)
                {
                    if ((amount - kvp.Key.Value) < 0)
                    {
                        break;
                    }

                    amount -= kvp.Key.Value;
                    validMoney[kvp.Key]--;

                    if (kvp.Key is Note)
                    {
                        var note = kvp.Key as Note;

                        if (ret.NoteDictionary.ContainsKey(note))
                        {
                            ret.NoteDictionary[note]++;
                        }
                        else
                        {
                            ret.NoteDictionary[note] = 1;
                        }

                        curDict.NoteDictionary[note]--;
                    }
                    else if (kvp.Key is Coin)
                    {
                        var coin = kvp.Key as Coin;

                        if (ret.CoinDictionary.ContainsKey(coin))
                        {
                            ret.CoinDictionary[coin]++;
                        }
                        else
                        {
                            ret.CoinDictionary[coin] = 1;
                        }

                        curDict.CoinDictionary[coin]--;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (amount > 0)
            {
                throw new
                      InsufficientChangeException($"There is no change in ATM {atm.AutomatedTellerMachineId} to withdraw ${amount}");
            }

            if (context != null)
            {
                context.Update(user);
                context.Update(atm);

                context.SaveChanges();
            }

            return(ret);
        }