internal static CardAccount TakeCardAccountByCardNumber(string  cardNumber,ATMdbEntities db)
 {
     var account = db.CardAccounts.Where(a => a.CardNumber == cardNumber).First();
     if (account == null)
     {
         throw  new ArgumentException("Invalid account !");
     }
     return account;
 }
        internal static CardAccount TakeCardAccountByCardNumber(string cardNumber, ATMdbEntities db)
        {
            var account = db.CardAccounts.Where(a => a.CardNumber == cardNumber).First();

            if (account == null)
            {
                throw  new ArgumentException("Invalid account !");
            }
            return(account);
        }
        static void Main(string[] args)
        {
            var db = new ATMdbEntities();

            using (var tran = db.Database.BeginTransaction(IsolationLevel.RepeatableRead))
            {
                try
                {
                    Console.WriteLine("Please enter a card number");
                    string number = "1234567890";
                    Console.WriteLine("Please enter a card pin");
                    string pin = "2222";
                    Console.WriteLine("Please enter ammount of money to withdraw");
                    decimal withdrawMoney = decimal.Parse(Console.ReadLine());

                    var account = Validation.TakeCardAccountByCardNumber(number, db);

                    Validation.ValidatePin(account, pin);

                    Validation.ValidateWithdrawMoney(account, withdrawMoney);

                    db.TransactionHistories.Add(new TransactionHistory()
                    {
                        CardNumber      = account.CardNumber,
                        Amount          = account.CardCash,
                        TransactionDate = DateTime.Now
                    });
                    account.CardCash -= withdrawMoney;
                    db.SaveChanges();

                    Console.WriteLine(account.CardCash);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    tran.Rollback();
                }
                tran.Commit();
            }
        }