コード例 #1
0
ファイル: ATM_DAO.cs プロジェクト: StanisInt/SoftUniHomeworks
        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.");
                }
            }
        }
コード例 #2
0
ファイル: ATM_DAO.cs プロジェクト: StanisInt/SoftUniHomeworks
        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();
            }
        }
コード例 #3
0
        static void Main()
        {
            var context = new ATMEntities();
            string cardNumber = String.Empty;
            decimal amountToWithdraw = 0;

            var accountToWithdraw = context.CardAccounts.Find(1);
            var threwException = false;

            // Unfinished!

            while (true)
            {
                try
                {
                    Console.WriteLine("Please enter the card number: ");
                    cardNumber = Console.ReadLine();
                    Console.WriteLine("Please enter the card PIN: ");
                    string cardPin = Console.ReadLine();
                    Console.WriteLine("Please enter the amount to withdraw: ");
                    amountToWithdraw = decimal.Parse(Console.ReadLine());

                    if (amountToWithdraw > accountToWithdraw.CardCash || accountToWithdraw.CardCash == null)
                    {
                        throw new ArgumentOutOfRangeException(
                            "You cannot withdraw more cash than what you currently have: " + accountToWithdraw.CardCash);
                    }

                    using (var transaction = context.Database.BeginTransaction())
                    {

                    }
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    Console.WriteLine(ex.Message);
                    threwException = true;
                }

                if (!threwException)
                {
                    context.TransactionHistories.Add(new TransactionHistory()
                    {
                        CardNumber = cardNumber,
                        Amount = amountToWithdraw,
                        TransactioDate = DateTime.Now
                    });
                }
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: aleks-todorov/HomeWorks
        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());
                }
            }
        }