コード例 #1
0
ファイル: Program.cs プロジェクト: Dyno1990/TelerikAcademy-1
        public static void Main()
        {
            Database.SetInitializer(
                new MigrateDatabaseToLatestVersion<ATMContext, Configuration>());

            // Uncomment the seed method in Configuration to populate.
            ATMActions.ShowCards();

            int pin = 2222;
            int cardNumber = 1222222222;
            decimal moneyToWithdraw = 300m;

            using (ATMContext db = new ATMContext())
            {
                if (ATMActions.WithdrawMoney(pin, cardNumber, moneyToWithdraw, db))
                {
                    Console.WriteLine("Money withdrawn");
                }
                else
                {
                    Console.WriteLine("Money withdrawn");
                }
            }

            Console.WriteLine("\nNew cards: ");
            ATMActions.ShowCards();
        }
コード例 #2
0
        public void CheckRecordNumber()
        {
            Database.SetInitializer(
                new DropCreateDatabaseAlways<ATMContext>());

            decimal money = 2000;
            decimal toWithdraw = 1000;
            int number = 1111111111;
            int cardPin = 1111;

            using (var db = new ATMContext())
            {
                db.CardAccounts.Add(new CardAccount()
                { CardCash = money, CardNumber = number, CardPIN = cardPin });

                db.SaveChanges();

                ATMActions.WithdrawMoney(cardPin, number, toWithdraw, db);
                db.SaveChanges();

                var actual = (from c in db.TransactionsHistory
                              select c).First();

                Assert.AreEqual(number, actual.CardNumber);
                db.Dispose();
            }
        }
コード例 #3
0
 public static void ShowCards()
 {
     using (ATMContext db = new ATMContext())
     {
         foreach (var item in db.CardAccounts)
         {
             Console.WriteLine("ID:{0}, Monies: {1}", item.Id, item.CardCash);
         }
     }
 }
コード例 #4
0
 public static void RecordWithdrawal(int cardNumber, decimal ammount, ATMContext db)
 {
     using (var scope = new TransactionScope(
                 TransactionScopeOption.RequiresNew,
                 new TransactionOptions()
                 {
                     IsolationLevel = IsolationLevel.RepeatableRead
                 }))
     {
         db.TransactionsHistory.Add(new TransactionHistory()
         {
             TransactionDate = DateTime.Now,
             Ammount = ammount,
             CardNumber = cardNumber
         });
         scope.Complete();
     }
 }
コード例 #5
0
        public static bool WithdrawMoney(int PIN, int cardNumber, decimal moneyToWithdraw, ATMContext db)
        {
            bool success = true;
            int cardNumberToRecord = 0;


            using (var scope = new TransactionScope(
                        TransactionScopeOption.RequiresNew,
                        new TransactionOptions()
                        {
                            IsolationLevel = IsolationLevel.RepeatableRead
                        }
                    ))
            {
                var card = (from c in db.CardAccounts
                            where c.CardNumber == cardNumber
                            select c).First();


                if (card == null || card.CardPIN != PIN || card.CardCash < moneyToWithdraw)
                {
                    success = false;
                }
                else
                {
                    card.CardCash -= moneyToWithdraw;
                    cardNumberToRecord = card.CardNumber;
                    scope.Complete();
                }
            }

            if (success)
            {
                RecordWithdrawal(cardNumberToRecord, moneyToWithdraw, db);
                db.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }

        }