Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var atmContext = new ApplicationDbContext();
            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();
        }
Ejemplo n.º 2
0
        public static bool WithdrawMoney(int pin, int cardNumber, decimal moneyToWithdraw, ApplicationDbContext context)
        {
            var transactionScope = new TransactionScope(
               TransactionScopeOption.RequiresNew,
               new TransactionOptions()
               {
                   IsolationLevel = (System.Transactions.IsolationLevel) IsolationLevel.RepeatableRead
               });

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

                var isPinValid = card != null && card.CardPin == pin;
                var isAmountValid = card.CardCash >= moneyToWithdraw;

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

            AddTransactionToHistory(cardNumber, moneyToWithdraw, context);

            context.SaveChanges();
            return true;
        }
Ejemplo n.º 3
0
        private static void AddTransactionToHistory(int cardNumber, decimal moneyToWithdraw, ApplicationDbContext context)
        {
            var transactionScope = new TransactionScope(
               TransactionScopeOption.RequiresNew,
               new TransactionOptions()
               {
                   IsolationLevel = (System.Transactions.IsolationLevel) IsolationLevel.RepeatableRead
               });

            using (transactionScope)
            {
                context.TransactionHistories.Add(new TransactionHistory()
                {
                    TransactionDate = DateTime.Now,
                    Ammount = moneyToWithdraw,
                    CardNumber = cardNumber
                });

                transactionScope.Complete();
            }
        }
Ejemplo n.º 4
0
 private static void PrintAllCards()
 {
     using (var context = new ApplicationDbContext())
     {
         foreach (var card in context.CardAccounts)
         {
             Console.WriteLine("ID:{0} -> Money: {1}", card.Id, card.CardCash);
         }
     }
 }