示例#1
0
文件: Program.cs 项目: krumov/telerik
        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();
        }
示例#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);
         }
     }
 }
示例#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();
     }
 }
示例#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;
            }
        }