Beispiel #1
0
        public void WithdrawToLessThan0()
        {
            decimal money = 100;
            string number = "46346";
            string cardPin = "3456";

            using (ATMEntities context = new ATMEntities())
            {
                context.CardAccounts.Add(new CardAccount() { CardCash = money, CardNumber = number, CardPIN = cardPin });
                context.SaveChanges();

                TransactionScope tran = new TransactionScope(TransactionScopeOption.Required,
                new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead });

                using (tran)
                {
                    var account = context.CardAccounts.Where(x => x.CardNumber == number
                        && x.CardPIN == cardPin && x.CardCash == money).First();
                    if (account.CardCash >= 2 * money)
                    {
                        account.CardCash -= 2 * money;

                        context.SaveChanges();
                        tran.Complete();
                    }

                    context.SaveChanges();
                }

                var acc = context.CardAccounts.Where(x => x.CardNumber == number && x.CardPIN == cardPin).First();
                Assert.AreEqual(money, acc.CardCash);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            decimal cash = 200;
            ATMEntities context = new ATMEntities();
            TransactionScope tran = new TransactionScope(TransactionScopeOption.Required,
                new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead });

            using (tran)
            {
                var account = context.CardAccounts.Where(x => x.Id == 1).First();
                if (CheckCardPIN(account.CardPIN) && CheckCardNumber(account.CardNumber) && account.CardCash >= cash)
                {
                    account.CardCash -= cash;
                    context.TranscationsHistories.Add(
                        new TranscationsHistory()
                    {
                        CardNumber = account.CardNumber,
                        TranscationDate = DateTime.Now,
                        Amount = account.CardCash
                    });

                    context.SaveChanges();
                    tran.Complete();
                }
            }
        }