public static void WithdrawMoney(AtmDataDbContext context, CardAccount card, decimal amount)
        {
            var transaction = context.Database.BeginTransaction();
            try
            {
                CardAccount targetCard;
                var found = context.CardAccounts.Where(c => c.CardNumber == card.CardNumber && c.CardPIN == card.CardPIN && c.CardCash >= amount);
                if (found.Count() == 0)
                {
                    throw new ApplicationException("Invalid card operation attempt.");
                }

                targetCard = found.First();
                Console.WriteLine("Initial balance: {0}", targetCard.CardCash);
                targetCard.CardCash -= amount;
                context.SaveChanges();
                context.TransactionsHistories.Add(new TransactionsHistory()
                {
                    CardNumber = card.CardNumber,
                    TransactionDate = DateTime.Now,
                    Amount = amount
                });
                context.SaveChanges();
                transaction.Commit();
                Console.WriteLine("Transaction completed successfully.");
                Console.WriteLine("Balance after the transaction: {0}", targetCard.CardCash);
            }
            catch (Exception e)
            {
                Console.WriteLine("Transaction not completed. Changes rolled back.");
                Console.WriteLine(e.Message);
                transaction.Rollback();
            }
        }
 public static void Main()
 {
     var context = new AtmDataDbContext();
     using (context)
     {
         Console.WriteLine("All cards:");
         Console.WriteLine(string.Join(Environment.NewLine, context.CardAccounts.Select(c => new { Card = c.CardNumber, Balance = c.CardCash })));
         try
         {
             var targetCard = new CardAccount() { CardNumber = "1234567892", CardPIN = 3434 };
             WithdrawMoney(context, targetCard, 200m);
             WithdrawMoney(context, targetCard, 2000m);
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
 }
Ejemplo n.º 3
0
        private static CardAccount GenerateCardAccount(HashSet<long> registeredCardNumbers, IList<int> cardHolderIds, IRandomNumberProvider numberProvider, IRandomDataProvider randomDataProvider)
        {
            string cardNumber = string.Empty;
            long number = new long();
            while (number == 0 || registeredCardNumbers.Contains(number))
            {
                number = (long)numberProvider.GetIntInRange(0, 1999999999) * (long)numberProvider.GetIntInRange(1, 5);
            }

            registeredCardNumbers.Add(number);
            cardNumber = number.ToString().PadLeft(10, '0');
            var holderId = cardHolderIds[numberProvider.GetIntUpTo(cardHolderIds.Count - 1)];
            var pin = randomDataProvider.GetStringExact(4, RandomDataType.Numerics);
            var cash = (decimal)numberProvider.GetDoubleInRange(100.00D, 10000.00D);

            CardAccount currentCardAccount = new CardAccount()
            {
                CardNumber = cardNumber,
                CardHolderId = holderId,
                CardPin = pin,
                CardCash = cash
            };
            return currentCardAccount;
        }