static void Main() { var atmContext = new AtmContext(); atmContext.Database.Initialize(true); PrintAllCards(); using (atmContext) { // for invalid data try with different pin, cardNumber and moneyToWithdraw to use that nothing happens int pin = 1111; int cardNumber = 1234567890; decimal moneyToWithdraw = 200M; if (WithdrawMoney(pin, cardNumber, moneyToWithdraw, atmContext)) { Console.WriteLine("\nMoney withdrawn\n"); } else { Console.WriteLine("\nCan not withdraw money.\n"); } } PrintAllCards(); }
private IKernel RegisterApplicationComponents(IApplicationBuilder app) { // IKernelConfiguration config = new KernelConfiguration(); var kernel = new StandardKernel(); // Register application services foreach (var ctrlType in app.GetControllerTypes()) { kernel.Bind(ctrlType).ToSelf().InScope(RequestScope); } var options = new DbContextOptionsBuilder <AtmContext>() .UseInMemoryDatabase(databaseName: "AtmContext") .Options; var context = new AtmContext(options); // This is where our bindings are configurated kernel.Bind <IService <Operation> >().To <OperationService>().InScope(RequestScope); kernel.Bind <IRepository <Operation> >().To <OperationRepository>().InScope(RequestScope); kernel.Bind <IService <CreditCard> >().To <CreditCardService>().InScope(RequestScope); kernel.Bind <IRepository <CreditCard> >().To <CreditCardRepository>().InScope(RequestScope); kernel.Bind <AtmContext>().ToConstant(context); // Cross-wire required framework services kernel.BindToMethod(app.GetRequestService <IViewBufferScope>); return(kernel); }
public LKPageViewModel() { AtmContext context = new AtmContext(); Transactions.AddRange(from t in context.Transactions where t.Account.AccountId == PublcElements.AccountId select t); CurrentAccount = (from a in context.Accounts where a.AccountId == PublcElements.AccountId select a).First(); }
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(); } }
private static void PrintAllCards() { using (var context = new AtmContext()) { foreach (var card in context.CardAccounts) { Console.WriteLine("ID:{0} -> Money: {1}", card.CardAccountId, card.CardCash); } } }
//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(); } }
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); } }
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"); } } } }
private static void AddTransactionToHistory(int cardNumber, decimal ammount, AtmContext context) { var transactionScope = new TransactionScope( TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.RepeatableRead }); using (transactionScope) { context.TransactionHistories.Add(new TransactionHistory() { TransactionDate = DateTime.Now, Ammount = ammount, CardNumber = cardNumber }); transactionScope.Complete(); } }
public AtmContext Get() { return(dataContext ?? (dataContext = new AtmContext())); }
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); }
public AtmRepository(AtmContext atmContext) { _atmContext = atmContext; }
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; } } } }
public TarjetaService(AtmContext context) { _tarjetaRepo = context.tarjetaRepository; _context = context; }
public BillRepository(AtmContext context) { _context = context; }
public AtmMachineRepository(AtmContext context) { _context = context; }
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; } } } }
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; } } } }