Beispiel #1
0
        public static void WithdrawMoney(decimal amount, string cardPin, string cardNumber)
        {
            using (var atmEntities = new ATMEntities())
            {
                using (var transaction = atmEntities.Database.BeginTransaction())
                {
                    var currentCard = atmEntities.CardAccounts.FirstOrDefault(c => c.CardNumber == cardNumber && c.CardPIN == cardPin);

                    if (currentCard == null)
                    {
                        transaction.Rollback();
                        throw new ArgumentNullException("There are no Card Account in database with this Pin or card number.");
                    }

                    if (currentCard.CardCash < amount)
                    {
                        transaction.Rollback();
                        throw new InvalidOperationException("You don't have enough money.");
                    }

                    currentCard.CardCash -= amount;

                    LogWithdrawalTransaction(cardNumber, amount);

                    atmEntities.SaveChanges();
                    transaction.Commit();

                    Console.WriteLine("Transaction completed.");
                }
            }
        }
Beispiel #2
0
        private static void LogWithdrawalTransaction(string cardNumber, decimal amount)
        {
            using (var atmEntities = new ATMEntities())
            {
                atmEntities.TransactionHistories.Add(
                    new TransactionHistory { Amount = amount, CardNumber = cardNumber, TransactionDate = DateTime.Now });

                atmEntities.SaveChanges();
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var account = new CardAccount();
            account.CardNumber = "9876543";
            account.CardPin = "22222";
            decimal transactionMoney = 500;

            var tran = new TransactionScope();
            using (tran)
            {
                try
                {
                    var dbCon = new ATMEntities();

                    var card = (from c in dbCon.CardAccounts
                                where c.CardNumber == account.CardNumber
                                select c).FirstOrDefault();

                    if (card == null)
                    {
                        throw new InvalidOperationException(" Current Card does not exist! ");
                    }
                    else
                    {
                        if (account.CardPin == card.CardPin)
                        {
                            if (account.CardCash > 200 && account.CardCash - transactionMoney > 0)
                            {
                                account.CardCash -= transactionMoney;
                                dbCon.CardAccounts.Attach(account);
                                dbCon.SaveChanges();
                            }
                            else
                            {
                                throw new ArgumentException("There are not enough money in your card!");
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Wrong Pin! Try again!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Transaction was not completed successfully! " + ex.Message.ToString());
                }
            }
        }