Beispiel #1
0
        static void WithdrawAmount(string cardNumber, string cardPIN, decimal amount)
        {
            var context = new ATMEntities();

            using (var dbTransaction = context.Database.BeginTransaction(IsolationLevel.RepeatableRead))
            {
                try
                {
                    var account = GetValidAccount(context, cardNumber, cardPIN);
                    CheckBallance(account, amount);
                    account.CardCash -= amount;

                    // History Table
                    context.TransactionHistories.Add(new TransactionHistory
                    {
                        CardNumber      = account.CardNumber,
                        TransactionDate = DateTime.Now,
                        Amount          = amount
                    });

                    context.SaveChanges();
                    dbTransaction.Commit();

                    Console.WriteLine("Transaction complete.");
                }
                catch (Exception ex)
                {
                    dbTransaction.Rollback();
                    Console.WriteLine(ex.Message);
                }
            }
        }
Beispiel #2
0
 private static CardAccount GetValidAccount(ATMEntities context, string cardNumber, string cardPIN)
 {
     try
     {
         var account = context.CardAccounts.Single(a => a.CardNumber == cardNumber && a.CardPIN == cardPIN);
         return(account);
     }
     catch (Exception)
     {
         throw new ArgumentException("Invalid Card Number or PIN number!");
     }
 }
        static void WithdrawMoney(decimal money, int accountId)
        {
            using (var context = new ATMEntities())
            {
                using (var dbContextTransaction = context.Database.BeginTransaction())
                {
                    var account = context.CardAccounts.Find(accountId);

                    try
                    {
                        Regex regex = new Regex(@"\d+");
                        Match cardNumberMatch = regex.Match(account.CardNumber);

                        if (!cardNumberMatch.Success)
                        {
                            throw new InvalidOperationException("The card number is invalid.");
                        }

                        Match cardPinMatch = regex.Match(account.CardPIN);

                        if (!cardPinMatch.Success)
                        {
                            throw new InvalidOperationException("The card pin is invalid.");
                        }

                        if (account.CardCash < money)
                        {
                            throw new InvalidOperationException("Not enough money to withdraw.");
                        }

                        context.TransactionHistories.Add(new TransactionHistory()
                        {
                            CardNumber = account.CardNumber,
                            TransactionDate = DateTime.Now,
                            Amount = money
                        });

                        account.CardCash -= money;
                        context.SaveChanges();
                        dbContextTransaction.Commit();
                    }
                    catch (InvalidOperationException)
                    {
                        dbContextTransaction.Rollback();
                    }
                }
            }
        }