Esempio n. 1
0
        static void Main(string[] args)
        {
            Database.SetInitializer(
                new MigrateDatabaseToLatestVersion<ATMContext, Configuration>());

            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 not withdrawn");
                }
            }

            Console.WriteLine("\nNew cards: ");
            ATMActions.ShowCards();
        }
Esempio n. 2
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);
         }
     }
 }
Esempio n. 3
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();
     }
 }
Esempio n. 4
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;
            }
        }