Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("NoteId,Value")] Note note)
        {
            if (id != note.NoteId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(note);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!NoteExists(note.NoteId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(note));
        }
        public async Task <IActionResult> Edit(int id, AutomatedTellerMachine automatedTellerMachine)
        {
            if (id != automatedTellerMachine.AutomatedTellerMachineId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(automatedTellerMachine);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AutomatedTellerMachineExists(automatedTellerMachine.AutomatedTellerMachineId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(automatedTellerMachine));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Edit(int id, [Bind("UserId,Name,Balance")] User user)
        {
            if (id != user.UserId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(user);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserExists(user.UserId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(user));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(int id, [Bind("CoinId,Value,Size")] CoinViewModel coin)
        {
            if (id != coin.CoinId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var model = _context.Coin.Find(coin.CoinId);
                    var size  = _context.CoinSize.Find(coin.Size);

                    model.Value = coin.Value;
                    model.Size  = size;

                    _context.Update(model);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CoinExists(coin.CoinId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                TempData["SuccessMessage"] = "Coin edited sucessfully";
                return(RedirectToAction("Index"));
            }

            return(View(coin));
        }
Exemplo n.º 5
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);
        }