示例#1
0
        public void Withdraw(int amount)
        {
            try
            {
                foreach (var item in _atmContext.Inventories
                         .Where(x => x.BillQuantity > 0)
                         .OrderByDescending(x => x.Denomination))
                {
                    while ((amount - item.Denomination) > -1 && item.BillQuantity > 0)
                    {
                        item.BillQuantity -= 1;
                        amount            -= item.Denomination;
                    }
                }

                if (amount > 0)
                {
                    RollBack();
                    throw new ArgumentException("Failure: insufficient funds");
                }
                else
                {
                    _atmContext.SaveChanges();
                }
            }
            catch (Exception)
            {
                RollBack();
                throw;
            }
        }
        public TarjetaEntity Create(TarjetaRequest.Create tarjeta)
        {
            TarjetaEntity tarjetaEntity = new TarjetaEntity();

            tarjetaEntity.Numero = tarjeta.Numero;
            tarjetaEntity.Pin    = tarjeta.Pin;

            EntityEntry <TarjetaEntity> nuevaTarjeta = _tarjetaRepo.Add(tarjetaEntity);

            _context.SaveChanges();

            return(nuevaTarjeta.Entity);
        }
示例#3
0
        public static void UpdateTransactionsHistory(AtmContext atmContext, CardAccount account, decimal oldCash, decimal newCash)
        {
            TransactionScope transaction = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions()
            {
                // Nested transactions must have the same isolation level
                IsolationLevel = IsolationLevel.RepeatableRead
            });

            using (transaction)
            {
                TransactionsHistory tranHistory = new TransactionsHistory()
                {
                    CardAccount = account,
                    OldCash     = oldCash,
                    NewCash     = newCash
                };

                atmContext.TransactionsHistories.Add(tranHistory);
                atmContext.SaveChanges();

                transaction.Complete();
            }
        }
示例#4
0
        public ProfileDetails UpdateProfile(ProfileDetails newProfile)
        {
            _context.Update(newProfile);
            _context.SaveChanges();

            return(newProfile);
        }
示例#5
0
        //Initilize API unused
        public AtmController(AtmContext context)
        {
            _context = context;
            Console.Write("context = {0} ", _context);
            Console.WriteLine("AtmController");

            if (_context.AtmItems.Count() == 0)
            {
                Console.WriteLine("If context AtmController");
                _context.AtmItems.Add(new AtmItem {
                    type = "Item1"
                });
                _context.SaveChanges();
            }
        }
示例#6
0
        public void DenominationBalanceTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <AtmContext>()
                          .UseInMemoryDatabase($"CourseDatabaseForTesting{Guid.NewGuid()}")
                          .Options;

            using (var context = new AtmContext(options))
            {
                var inventories = new Inventory[]
                {
                    new Inventory {
                        Denomination = 100, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 50, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 20, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 10, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 5, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 1, BillQuantity = 10
                    }
                };
                foreach (Inventory i in inventories)
                {
                    context.Inventories.Add(i);
                }
                context.SaveChanges();
            }

            using (var context = new AtmContext(options))
            {
                var repo = new Models.AtmRepository(context);

                //Act
                var obj = repo.DenominationBalance(100);

                //Assert
                Assert.Equal(10, obj.BillQuantity);
            }
        }
示例#7
0
        public void WithdrawBills(List <Bill> resultbills, List <Bill> billsInAtm)
        {
            var correctBillValues = billsInAtm.Select(x => new Bill
            {
                Amount = x.Amount -= resultbills.FirstOrDefault(y => x.Value == y.Value).Amount,
                Value  = x.Value
            });

            var allbills = _context.Bills;

            _context.Bills.RemoveRange(allbills);

            _context.Bills.AddRange(correctBillValues);

            _context.SaveChanges();
        }
示例#8
0
        public static void WithdrawMoney(decimal amount, string cardNumber, string cardPIN)
        {
            AtmContext atmContext = new AtmContext();

            using (atmContext)
            {
                TransactionScope transaction = new TransactionScope(
                    TransactionScopeOption.Required,
                    new TransactionOptions()
                {
                    // This isolation level locks only the current account.
                    // The account can be read but can't be modified.
                    // The other accounts are not locked
                    IsolationLevel = IsolationLevel.RepeatableRead
                });

                using (transaction)
                {
                    try
                    {
                        CardAccount account = atmContext.CardAccounts
                                              .FirstOrDefault(
                            c => c.CardNumber == cardNumber &&
                            c.CardPIN == cardPIN &&
                            c.CardCash >= amount);

                        decimal oldCash = account.CardCash;
                        decimal newCash = account.CardCash - amount;

                        account.CardCash = newCash;
                        atmContext.SaveChanges();

                        UpdateTransactionsHistory(atmContext, account, oldCash, newCash);

                        transaction.Complete();
                    }
                    catch (NullReferenceException)
                    {
                        Console.WriteLine("No such card exists or the cash in the card is not enough");
                    }
                }
            }
        }
示例#9
0
        private static bool WithdrawMoney(int pin, int cardNumber, decimal moneyToWithdraw, AtmContext context)
        {
            var transactionScope = new TransactionScope(
                TransactionScopeOption.RequiresNew,
                new TransactionOptions()
            {
                IsolationLevel = IsolationLevel.RepeatableRead
            });

            using (transactionScope)
            {
                var card = context.CardAccounts.Where(c => c.CardNumber == cardNumber).FirstOrDefault();

                if (card == null)
                {
                    return(false);
                }

                var isCardPinValid         = card.CardPin == pin;
                var isMoneyToWithdrawValid = card.CardCash >= moneyToWithdraw;

                if (isCardPinValid && isMoneyToWithdrawValid)
                {
                    card.CardCash -= moneyToWithdraw;
                    transactionScope.Complete();
                }
                else
                {
                    return(false);
                }
            }

            AddTransactionToHistory(cardNumber, moneyToWithdraw, context);
            context.SaveChanges();
            return(true);
        }
示例#10
0
        public void SimpleTest()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <AtmContext>()
                          .UseInMemoryDatabase($"CourseDatabaseForTesting{Guid.NewGuid()}")
                          .Options;

            using (var context = new AtmContext(options))
            {
                var inventories = new Inventory[]
                {
                    new Inventory {
                        Denomination = 100, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 50, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 20, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 10, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 5, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 1, BillQuantity = 10
                    }
                };
                foreach (Inventory i in inventories)
                {
                    context.Inventories.Add(i);
                }
                context.SaveChanges();
            }

            using (var context = new AtmContext(options))
            {
                var repo = new Models.AtmRepository(context);

                //Withdraw $208
                repo.Withdraw(208);
                foreach (var item in repo.Balance)
                {
                    switch (item.Denomination)
                    {
                    case 100:
                        Assert.Equal(8, item.BillQuantity);
                        break;

                    case 50:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 20:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 10:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 5:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 1:
                        Assert.Equal(7, item.BillQuantity);
                        break;
                    }
                }

                //Withdraw $9
                repo.Withdraw(9);
                foreach (var item in repo.Balance)
                {
                    switch (item.Denomination)
                    {
                    case 100:
                        Assert.Equal(8, item.BillQuantity);
                        break;

                    case 50:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 20:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 10:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 5:
                        Assert.Equal(8, item.BillQuantity);
                        break;

                    case 1:
                        Assert.Equal(3, item.BillQuantity);
                        break;
                    }
                }

                //Withdraw $9
                ArgumentException exception = Assert.Throws <ArgumentException>(() => repo.Withdraw(9));
                Assert.Equal("Failure: insufficient funds", exception.Message);

                //Get Denomination for $20
                var obj = repo.DenominationBalance(20);
                Assert.Equal(10, obj.BillQuantity);

                //Get Denomination for $1
                obj = repo.DenominationBalance(1);
                Assert.Equal(3, obj.BillQuantity);

                //Get Denomination for $100
                obj = repo.DenominationBalance(100);
                Assert.Equal(8, obj.BillQuantity);

                //Restock
                repo.Restock();
                foreach (var item in repo.Balance)
                {
                    switch (item.Denomination)
                    {
                    case 100:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 50:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 20:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 10:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 5:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 1:
                        Assert.Equal(10, item.BillQuantity);
                        break;
                    }
                }
            }
        }
示例#11
0
        public void WithdrawTest_Success_Many()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <AtmContext>()
                          .UseInMemoryDatabase($"CourseDatabaseForTesting{Guid.NewGuid()}")
                          .Options;

            using (var context = new AtmContext(options))
            {
                var inventories = new Inventory[]
                {
                    new Inventory {
                        Denomination = 100, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 50, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 20, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 10, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 5, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 1, BillQuantity = 10
                    }
                };
                foreach (Inventory i in inventories)
                {
                    context.Inventories.Add(i);
                }
                context.SaveChanges();
            }

            using (var context = new AtmContext(options))
            {
                var repo = new Models.AtmRepository(context);

                //Withdraw $208
                repo.Withdraw(208);
                foreach (var item in repo.Balance)
                {
                    switch (item.Denomination)
                    {
                    case 100: Assert.Equal(8, item.BillQuantity);
                        break;

                    case 50:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 20:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 10:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 5:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 1:
                        Assert.Equal(7, item.BillQuantity);
                        break;
                    }
                }

                //Withdraw $1,398
                repo.Withdraw(1398);
                foreach (var item in repo.Balance)
                {
                    switch (item.Denomination)
                    {
                    case 100:
                        Assert.Equal(0, item.BillQuantity);
                        break;

                    case 50:
                        Assert.Equal(0, item.BillQuantity);
                        break;

                    case 20:
                        Assert.Equal(6, item.BillQuantity);
                        break;

                    case 10:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 5:
                        Assert.Equal(8, item.BillQuantity);
                        break;

                    case 1:
                        Assert.Equal(4, item.BillQuantity);
                        break;
                    }
                }

                //Withdraw $1,398
                repo.Restock();
                repo.Withdraw(1398);
                foreach (var item in repo.Balance)
                {
                    switch (item.Denomination)
                    {
                    case 100:
                        Assert.Equal(0, item.BillQuantity);
                        break;

                    case 50:
                        Assert.Equal(3, item.BillQuantity);
                        break;

                    case 20:
                        Assert.Equal(8, item.BillQuantity);
                        break;

                    case 10:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 5:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 1:
                        Assert.Equal(7, item.BillQuantity);
                        break;
                    }
                }

                //Withdraw $189
                repo.Restock();
                repo.Withdraw(189);
                foreach (var item in repo.Balance)
                {
                    switch (item.Denomination)
                    {
                    case 100:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 50:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 20:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 10:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 5:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 1:
                        Assert.Equal(6, item.BillQuantity);
                        break;
                    }
                }
            }
        }
示例#12
0
        public void WithdrawTest_Failure_Single()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <AtmContext>()
                          .UseInMemoryDatabase($"CourseDatabaseForTesting{Guid.NewGuid()}")
                          .Options;

            using (var context = new AtmContext(options))
            {
                var inventories = new Inventory[]
                {
                    new Inventory {
                        Denomination = 100, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 50, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 20, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 10, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 5, BillQuantity = 10
                    },
                    new Inventory {
                        Denomination = 1, BillQuantity = 10
                    }
                };
                foreach (Inventory i in inventories)
                {
                    context.Inventories.Add(i);
                }
                context.SaveChanges();
            }

            using (var context = new AtmContext(options))
            {
                var repo = new Models.AtmRepository(context);

                //Withdraw $2,000,000
                ArgumentException exception = Assert.Throws <ArgumentException>(() => repo.Withdraw(2000000));
                Assert.Equal("Failure: insufficient funds", exception.Message);

                //Withdraw $208
                //This tests that when a failure occurs the changes
                //  are rolled back to their original amounts.
                repo.Withdraw(208);
                foreach (var item in repo.Balance)
                {
                    switch (item.Denomination)
                    {
                    case 100:
                        Assert.Equal(8, item.BillQuantity);
                        break;

                    case 50:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 20:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 10:
                        Assert.Equal(10, item.BillQuantity);
                        break;

                    case 5:
                        Assert.Equal(9, item.BillQuantity);
                        break;

                    case 1:
                        Assert.Equal(7, item.BillQuantity);
                        break;
                    }
                }
            }
        }