Beispiel #1
0
        public static decimal RetriveSomeMoney(string cardNumber, string cardPin, decimal amount)
        {
            ATMEntities dbATM       = new ATMEntities();
            var         transOption = new TransactionOptions();

            transOption.IsolationLevel = IsolationLevel.RepeatableRead;
            var scope = new TransactionScope(TransactionScopeOption.RequiresNew, transOption);

            using (scope)
            {
                foreach (var account in dbATM.CardAccounts)
                {
                    if ((account.CardNumber == cardNumber) && (account.CardPIN == cardPin) && (account.CardCash > amount))
                    {
                        account.CardCash -= amount;

                        var log = new TransactionHistory()
                        {
                            CardNumber      = cardNumber,
                            TransactionDate = DateTime.Now,
                            Amount          = amount
                        };

                        dbATM.TransactionHistories.Add(log);
                        dbATM.SaveChanges();
                        scope.Complete();

                        return(amount);
                    }
                }
            }

            return(-1);
        }
Beispiel #2
0
 public ActionResult Withdraw(TransactionModel model)
 {
     if (ModelState.IsValid && model.debit > 0)
     {
         string dtStamp = DateTime.Now.ToString();
         var    balance = getAccountBalance(model.account);
         if (balance <= model.debit)
         {
             TempData["successW"] = "N";
         }
         else
         {
             var dbATM       = new ATMEntities();
             var transaction = new transaction
             {
                 tStamp  = dtStamp,
                 debit   = model.debit,
                 account = model.account,
                 credit  = 0
             };
             dbATM.transactions.AddObject(transaction);
             dbATM.SaveChanges();
             TempData["successW"] = "Y";
         }
         return(View(model));
     }
     else
     {
         ModelState.AddModelError("", "The withdrawal value provided is incorrect.");
     }
     return(View(model));
 }
        public static void insertTrunsaction(DateTime starttime, DateTime endtime, decimal withdrow)
        {
            db = new ATMEntities();
            int id  = rnd.Next(1, 21);
            var atm = db.ATMs.Where(x => x.id == id).FirstOrDefault();

            if (atm.balance > withdrow)
            {
                atm.balance -= withdrow;
                Models.Trunsaction trunsaction = new Models.Trunsaction()
                {
                    atm_id     = id,
                    start_time = starttime,
                    end_time   = endtime,
                    withdrow   = withdrow
                };
                try
                {
                    db.Trunsactions.Add(trunsaction);
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    db = null;
                }

                //count_trunsaction_day(id);
                DateTime date1 = DateTime.Now;
                DateTime date2 = date1.AddHours(-1);
                trunsaction_Between_date(id, date2, date1);
            }

            db = null;
        }
        public static void DepositMoney(decimal amount, string cardNumber, string cardPin)
        {
            if (cardNumber.Length != 10 || cardPin.Length != 4)
            {
                throw new ArgumentException("Card {0} has invalid length", cardPin.Length == 4 ? "Number" : "Pin");
            }

            using (var db = new ATMEntities())
            {
                using (var tran = db.Database.BeginTransaction(IsolationLevel.RepeatableRead))
                {
                    var card = db.CardAccounts.Where(c => c.Number == cardNumber).FirstOrDefault();

                    if (card == null)
                    {
                        throw new ArgumentException("Card with this number does not exist");
                    }

                    if (card.Pin != cardPin)
                    {
                        throw new ArgumentException("Incorect Pin number");
                    }

                    if (amount < 0 && card.Cash < (-1) * amount)
                    {
                        throw new ArgumentException("Not enough cash");
                    }

                    card.Cash += amount;

                    if (InteractWithMoney(amount))
                    {
                        db.TransactionsHistories.Add(new TransactionsHistory()
                        {
                            Amount          = amount,
                            CardNumber      = cardNumber,
                            TransactionDate = DateTime.Now
                        });

                        try
                        {
                            db.SaveChanges();
                            tran.Commit();
                            Console.WriteLine("Success");
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();
                            throw ex;
                        }
                    }
                    else
                    {
                        tran.Rollback();
                        throw new ArgumentException("Something is wrong with the ATM machine");
                    }
                }
            }
        }
Beispiel #5
0
        public void ATMDatabaseHistoryShoudWithdrawMoney()
        {
            ATMHistory.WithdrawMoney(cardNumber, cardPin, 100M);
            var testContext = new ATMEntities();
            var newCash     = testContext.CardAccounts.First(c => c.CardNumber == cardNumber).CardCach;

            Assert.AreEqual(900M, newCash);
        }
Beispiel #6
0
 public bool UpdatePin(long cardNumber, int oldPin, int newPin)
 {
     using (var dbContext = new ATMEntities())
     {
         var effectedRows = dbContext.UpdateCardDetails(1, cardNumber, oldPin, newPin, null);
         return(effectedRows > 0);
     }
 }
Beispiel #7
0
 public bool WithdrawBalance(long cardNumber, int pin, long newBalance)
 {
     using (var dbContext = new ATMEntities())
     {
         var effectedRows = dbContext.UpdateCardDetails(2, cardNumber, pin, null, newBalance);
         return(effectedRows > 0);
     }
 }
        static void RetrieveMoney(decimal amount, string cardNumber, string cardPin)
        {
            if (cardNumber == null)
            {
                throw new ArgumentNullException("cardNumber", "Provided card number cannot be null!");
            }

            if (cardPin == null)
            {
                throw new ArgumentNullException("cardPIN", "Provided card PIN cannot be null!");
            }

            if (cardNumber.Length != 10)
            {
                throw new ArgumentException(
                          "Provided card number is invalid! Card number must consist of 10 digits!");
            }

            if (cardPin.Length != 4)
            {
                throw new ArgumentException(
                          "Provided card PIN is invalid! Card PIN must consist of 4 digits!");
            }

            using (var scope = new TransactionScope(TransactionScopeOption.Required,
                                                    new TransactionOptions()
            {
                IsolationLevel = IsolationLevel.RepeatableRead
            }))
            {
                using (var atmContext = new ATMEntities())
                {
                    var cardAccount = atmContext.CardAccounts.FirstOrDefault(
                        ca => ca.CardNumber.CompareTo(cardNumber) == 0 &&
                        ca.CardPIN.CompareTo(cardPin) == 0);

                    if (cardAccount == null)
                    {
                        scope.Dispose();
                        throw new TransactionException(
                                  "There is no card account with provided card number and PIN!");
                    }

                    if (cardAccount.CardCash < amount)
                    {
                        scope.Dispose();
                        throw new TransactionException(
                                  "There is no enough money in the account to retrive the requested amount!");
                    }

                    cardAccount.CardCash -= amount;
                    atmContext.SaveChanges();
                }

                scope.Complete();
            }
        }
        public static void insert_into_atm_amount(int atmid, int amount)
        {
            db = new ATMEntities();
            var c = db.ATMs.Where(i => i.id == atmid).FirstOrDefault();

            c.balance += amount;
            db.SaveChanges();
            db = null;
        }
Beispiel #10
0
        public void ATMDatabaseHistoryShoudWithdrawMoneyAndSaveHistory()
        {
            ATMHistory.WithdrawMoney(cardNumber, cardPin, 100M);
            var testContext        = new ATMEntities();
            var newCash            = testContext.CardAccounts.First(c => c.CardNumber == cardNumber).CardCach;
            var newCashTransaction = testContext.TransactionHistories.OrderBy(c => c.TransactionDate).First(c => c.CardNumber == cardNumber).Amount;

            Assert.AreEqual(900M, newCash);
            Assert.AreEqual(900M, newCashTransaction);
        }
Beispiel #11
0
        private static bool CheckIsHasMoney(ATMEntities context, string cardNumber, decimal money)
        {
            var currentEntity = context.CardAccounts.First(c => c.CardNumber == cardNumber);

            if (currentEntity.CardCach >= money)
            {
                return(true);
            }

            throw new Exception("You do not have enough money!");
        }
Beispiel #12
0
 public bool ValidateCardNumber(long cardNumber)
 {
     using (var dbContext = new ATMEntities())
     {
         var result = dbContext.ValidateCardDetails(1, cardNumber, Pin);
         {
             var effectedRows = dbContext.ValidateCardNumber(cardNumber);
             return(false);
         }
     }
 }
Beispiel #13
0
 public bool ValidatePin(long cardNumber, int pin)
 {
     using (var dbContext = new ATMEntities())
     {
         var result = dbContext.ValidateCardDetails(2, cardNumber, pin).GetEnumerator();
         while (result.MoveNext())
         {
             return(result.Current > 0);
         }
     }
     return(false);
 }
        private static void MoneyWithdraw()
        {
            var context = new ATMEntities();
            using (var dbContextTransaction = context.Database.BeginTransaction(System.Data.IsolationLevel.RepeatableRead))
            {
                try
                {
                    Console.WriteLine("Enter valid PIN");
                    string cardPin = Console.ReadLine();

                    Console.WriteLine("Enter valid Card Number");
                    string cardNumber = Console.ReadLine();

                    Console.WriteLine("Enter amount of money to withdrawal");
                    decimal withdrawalMoney = decimal.Parse(Console.ReadLine());

                    if (cardPin.Length != 4 || cardNumber.Length != 10 || withdrawalMoney < 0)
                    {
                        throw new ArgumentException("The requested arguments are with not valid length or are null");
                    }

                    var allAccounts = context.CardAccounts;

                    if (allAccounts.All(c => c.CardPIN != cardPin))
                    {
                        throw new ArgumentException("Invalid PIN !");
                    }

                    if (allAccounts.All(c => c.CardNumber != cardNumber))
                    {
                        throw new ArgumentException("Invalid Card Number !");
                    }

                    var account = context.CardAccounts
                        .Where(c => c.CardNumber == cardNumber);

                    if (withdrawalMoney > account.First().CardCash)
                    {
                        throw new ArgumentException("Not enough money in account");
                    }

                    account.First().CardCash -= withdrawalMoney;
                    context.SaveChanges();

                    dbContextTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
        }
Beispiel #15
0
        private static bool CheckIsValid(ATMEntities context, string cardNumber, string pinNumber)
        {
            try
            {
                var currentEntity = context.CardAccounts.First(c => c.CardNumber == cardNumber && c.CardPIN == pinNumber);
            }
            catch (Exception)
            {
                throw new Exception("You're entering a wrong card number or PIN!");
            }

            return(true);
        }
        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
                    });
                }
            }
        }
        ////public static int count_trunsaction_day(int atmid)
        //{
        //    db = new ATMEntities();
        //    //List<Trunsaction> z = db.Trunsactions.Where(i => i.atm_id == atmid && i.start_time.Value.ToShortDateString()==DateTime.Today.ToShortDateString()).ToList();
        //    //int y = z.Count;
        //    List<Trunsaction> t = db.Trunsactions.Where(i=>i.atm_id==atmid).ToList();
        //    int x = t.Count;
        //    var atm = db.ATMs.Where(i => i.id == atmid).FirstOrDefault();
        //    atm.T_N = x;
        //    //atm.T_N_last_day = y;
        //    db.SaveChanges();
        //    oldcount = x - oldcount;
        //    db = null;
        //    return oldcount;
        //}

        public static void trunsaction_Between_date(int atmid, DateTime starttime, DateTime endtime)
        {
            db = new ATMEntities();
            List <Trunsaction> q = db.Trunsactions.Where(i => i.atm_id == atmid).ToList();
            int y = q.Count;
            List <Trunsaction> t = q.Where(i => i.start_time >= starttime && i.start_time < endtime).ToList();
            int x   = t.Count;
            var atm = db.ATMs.Where(i => i.id == atmid).FirstOrDefault();

            atm.T_N           = y;
            atm.T_N_last_hour = x;
            db.SaveChanges();
            db = null;
            //return 1;
        }
Beispiel #18
0
        static void Main(string[] args)
        {
            var random = RandomDataGenerator.Instance;
            var db = new ATMEntities();

            var listOfGenerators = new List<IDataGenerator>
            {
                new CardNumberDataGenerator(random, db, 100),
            };

            foreach (var generator in listOfGenerators)
            {
                generator.Generate();
                db.SaveChanges();
            }
        }
Beispiel #19
0
        private double getAccountBalance(string account)
        {
            var balance = 0.0;

            using (ATMEntities db = new ATMEntities())
            {
                var trans = from t in db.transactions
                            where t.account == account
                            select t;
                foreach (transaction tran in trans)
                {
                    balance += (tran.credit - tran.debit);
                }
            }
            return(balance);
        }
Beispiel #20
0
        static void Main(string[] args)
        {
            var random = RandomDataGenerator.Instance;
            var db     = new ATMEntities();

            var listOfGenerators = new List <IDataGenerator>
            {
                new CardNumberDataGenerator(random, db, 100),
            };

            foreach (var generator in listOfGenerators)
            {
                generator.Generate();
                db.SaveChanges();
            }
        }
Beispiel #21
0
        public static void Main()
        {
            // First run "14.Transactions-in-ADO.NET-and-EF.sql"
            // If don't work, check the connection string!!!

            TransactionOptions transactionOptions = new TransactionOptions()
            {
                IsolationLevel = IsolationLevel.RepeatableRead
            };

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOptions))
            {
                ATMEntities db         = new ATMEntities();
                string      pin        = GetPIN();
                string      cardNumber = GetCardNumber();
                decimal     money      = GetMoney();

                var userAccount = db.CardAccounts.Where(x => x.CardPIN == pin && x.CardNumber == cardNumber).FirstOrDefault();

                if (userAccount == null)
                {
                    Console.WriteLine("Incorrect PIN or card number!");
                    return;
                }

                if (userAccount.CardCash > money)
                {
                    userAccount.CardCash = userAccount.CardCash - 200;
                    db.TransactionsHistories.Add(new TransactionsHistory
                    {
                        Ammount         = money,
                        CardNumber      = cardNumber,
                        TransactionDate = DateTime.Now
                    });

                    Console.WriteLine(userAccount.CardCash);
                    db.SaveChanges();
                    Console.WriteLine("Please take {0} money!", money);
                }
                else
                {
                    Console.WriteLine("Not enough money!");
                }

                scope.Complete();
            }
        }
        private static CardAccount InputCardNumber(ATMEntities context)
        {
            Console.Write("--Please, enter your card number: ");
            var candidateCardNumber = Console.ReadLine();

            var cardFound = context.CardAccounts
                .FirstOrDefault(ca => ca.CardNumber.Equals(candidateCardNumber));

            if (cardFound == null)
            {
                throw new InvalidOperationException("--Card number is invalid. Please enter a correct one.");
            }

            Console.WriteLine("--Card number exists.");
            Console.Write("--Insert the PIN of the selected card: ");
            return cardFound;
        }
Beispiel #23
0
        // **************************************
        // URL: /Transaction/Balance
        // **************************************
        public ActionResult Balance(string userName = "")
        {
            if (userName != "")
            {
                var accountNumber             = "";
                var oldUser                   = MembershipService.GetUser(userName, false);
                var userID                    = oldUser.ProviderUserKey.ToString();
                List <TransactionModel> trans = new List <TransactionModel> {
                };
                using (ATMEntities db = new ATMEntities())
                {
                    var customer = from c in db.customers
                                   where c.idCustomer == userID
                                   select c;
                    accountNumber = customer.Single().account;

                    var dbtrans = from t in db.transactions
                                  where t.account == accountNumber
                                  select t;
                    var balance = getAccountBalance(accountNumber);

                    foreach (transaction dbtran in dbtrans)
                    {
                        TransactionModel tempInstance = new TransactionModel();
                        tempInstance.account = dbtran.account;
                        tempInstance.debit   = Convert.ToSingle(dbtran.debit);
                        tempInstance.credit  = Convert.ToSingle(dbtran.credit);
                        tempInstance.tStamp  = dbtran.tStamp;
                        trans.Add(tempInstance);
                    }

                    Session.Add("accountNumber", accountNumber);
                    TempData["balance"] = balance;

                    return(View(trans));
                }
            }
            else
            {
                return(View());
            }
        }
Beispiel #24
0
        public void AddNewAccount(long cardNumber, string name, int pin, long balance)
        {
            ATM atmObject = null;

            using (var dbContext = new ATMEntities())
            {
                var results = dbContext.spNewAccount(cardNumber, name, pin, balance).GetEnumerator();
                while (results.MoveNext())
                {
                    var result = results.Current;
                    atmObject = new ATM();

                    atmObject.CardNumber = result.CardNumber;
                    atmObject.Name       = result.Name;
                    atmObject.Pin        = result.Pin;
                    //atmObject.Cvv = result.Cvv;
                    atmObject.CurrentBalance = result.CurrentBalance.Value;
                }
            }
        }
Beispiel #25
0
        public ATM GetAtmDetails(long cardNumber, int pin)
        {
            ATM atmObject = null;

            using (var dbContext = new ATMEntities())
            {
                var res = dbContext.GetCardDetails(cardNumber, pin).GetEnumerator();
                while (res.MoveNext())
                {
                    var result = res.Current;
                    atmObject                = new ATM();
                    atmObject.CardNumber     = result.CardNumber;
                    atmObject.Name           = result.Name;
                    atmObject.Pin            = result.Pin;
                    atmObject.Cvv            = result.Cvv;
                    atmObject.CurrentBalance = result.CurrentBalance.Value;
                }
            }
            return(atmObject);
        }
Beispiel #26
0
        static void Main()
        {
            Console.Write("Enter Card Number: ");
            string cardNum = Console.ReadLine();

            Console.Write("PIN: ");
            string pin = Console.ReadLine();

            Console.Write("Amount of money to withdrawal: ");
            decimal withdrawaledMoney = decimal.Parse(Console.ReadLine());

            var ctx = new ATMEntities();

            using (var dbContextTransaction = ctx.Database.BeginTransaction(IsolationLevel.RepeatableRead))
            {
                var card = ctx.CardAccounts.FirstOrDefault(a => a.CardNumber == cardNum);
                if (card == null || card.CardPIN != pin)
                {
                    dbContextTransaction.Rollback();
                    throw new Exception("Invalid input data.");
                }

                decimal moneyAfterWithdrawal = card.CardCash - withdrawaledMoney;
                if (moneyAfterWithdrawal < 0)
                {
                    dbContextTransaction.Rollback();
                    throw new Exception("Insufficient amount of money.");
                }

                card.CardCash = moneyAfterWithdrawal;
                ctx.TransactionHistories.Add(new TransactionHistory()
                {
                    CardNumber      = card.CardNumber,
                    TransactionDate = DateTime.Now,
                    Amount          = withdrawaledMoney
                });
                ctx.SaveChanges();

                dbContextTransaction.Commit();
            }
        }
        public static void SeedData()
        {
            using (var db = new ATMEntities())
            {
                if (db.CardAccounts.FirstOrDefault() == null)
                {
                    return;
                }

                for (int i = 0; i < 25; i++)
                {
                    db.CardAccounts.Add(new CardAccount()
                    {
                        Cash   = 10000,
                        Number = new string(i.ToString()[0], 10),
                        Pin    = new string(i.ToString()[0], 4)
                    });
                }

                db.SaveChanges();
            }
        }
        static void Main()
        {
            Console.Write("Enter Card Number: ");
            string cardNum  = Console.ReadLine();
            Console.Write("PIN: ");
            string pin  = Console.ReadLine();
            Console.Write("Amount of money to withdrawal: ");
            decimal withdrawaledMoney = decimal.Parse(Console.ReadLine());

            var ctx = new ATMEntities();
            using (var dbContextTransaction = ctx.Database.BeginTransaction(IsolationLevel.RepeatableRead))
            {
                var card = ctx.CardAccounts.FirstOrDefault(a => a.CardNumber == cardNum);
                if (card == null || card.CardPIN != pin)
                {
                    dbContextTransaction.Rollback();
                    throw new Exception("Invalid input data.");
                }

                decimal moneyAfterWithdrawal = card.CardCash - withdrawaledMoney;
                if(moneyAfterWithdrawal < 0)
                {
                    dbContextTransaction.Rollback();
                    throw new Exception("Insufficient amount of money.");
                }

                card.CardCash = moneyAfterWithdrawal;
                ctx.TransactionHistories.Add(new TransactionHistory()
                {
                    CardNumber = card.CardNumber,
                    TransactionDate = DateTime.Now,
                    Amount = withdrawaledMoney
                });
                ctx.SaveChanges();

                dbContextTransaction.Commit();
            }
        }
Beispiel #29
0
        protected override void Seed(ATM.Data.ATMEntities context)
        {
            var ctx    = new ATMEntities();
            var random = new Random();

            if (ctx.CardAccounts.Count() == 0)
            {
                for (var i = 0; i < 10; i++)
                {
                    var randomCardNumber = random.Next(1000000000).ToString().PadLeft(10, '0');
                    var randomCardPIN    = random.Next(10000).ToString().PadLeft(4, '0');
                    var randomBalance    = (decimal)(random.Next(100000) + 100);

                    ctx.CardAccounts.Add(new CardAccount()
                    {
                        CardNumber = randomCardNumber,
                        CardPIN    = randomCardPIN,
                        CardCash   = randomBalance
                    });
                }
            }
            ctx.SaveChanges();
        }
Beispiel #30
0
 public ActionResult Deposit(TransactionModel model)
 {
     if (ModelState.IsValid && model.credit > 0)
     {
         string dtStamp     = DateTime.Now.ToString();
         var    dbATM       = new ATMEntities();
         var    transaction = new transaction
         {
             tStamp  = dtStamp,
             credit  = model.credit,
             account = model.account,
             debit   = 0
         };
         dbATM.transactions.AddObject(transaction);
         dbATM.SaveChanges();
         TempData["successD"] = "Y";
     }
     else
     {
         ModelState.AddModelError("", "The deposit value provided is incorrect.");
     }
     return(View(model));
 }
Beispiel #31
0
 public void InitializeContext()
 {
     context     = new ATMEntities();
     cardNumber  = "3333333333";
     cardPin     = "3333";
     money       = 1000M;
     countEntity = context.CardAccounts.Count(c => c.CardNumber == cardNumber);
     if (countEntity == 0)
     {
         context.CardAccounts.Add(new CardAccount()
         {
             CardNumber = cardNumber,
             CardPIN    = cardPin,
             CardCach   = money
         });
         context.SaveChanges();
     }
     else
     {
         context.CardAccounts.First(c => c.CardNumber == cardNumber).CardCach = money;
         context.SaveChanges();
     }
 }
Beispiel #32
0
        public static void WithdrawMoney(string cardNumber, string pinNumber, decimal money)
        {
            using (var context = new ATMEntities())
            {
                using (var dbContextTransaction = context.Database.BeginTransaction(IsolationLevel.RepeatableRead))
                {
                    try
                    {
                        if (CheckIsValid(context, cardNumber, pinNumber))
                        {
                            if (CheckIsHasMoney(context, cardNumber, money))
                            {
                                var currentEntity = context.CardAccounts.First(c => c.CardNumber == cardNumber && c.CardPIN == pinNumber);
                                var historyEntity = context.TransactionHistories;
                                currentEntity.CardCach = currentEntity.CardCach - money;
                                historyEntity.Add(new TransactionHistory()
                                {
                                    CardNumber      = currentEntity.CardNumber,
                                    TransactionDate = DateTime.Now,
                                    Amount          = currentEntity.CardCach
                                });

                                context.SaveChanges();
                                Console.WriteLine("You have got your money!Amount after withdraw {0}!", currentEntity.CardCach);
                            }
                        }

                        dbContextTransaction.Commit();
                    }
                    catch (Exception e)
                    {
                        dbContextTransaction.Rollback();
                        Console.WriteLine("Transaction cancled!\nError Occured: {0}", e.Message);
                    }
                }
            }
        }
        public static void Main()
        {
            var context = new ATMEntities();

            while (true)
            {
                try
                {
                    // Card number: 0963596714
                    // Card PIN: 7206

                    // Read and validate Card Number and Card PIN
                    var cardFound = InputCardNumber(context);
                    InputCardPIN(cardFound);

                    // Read the amount of money and withdraw them
                    Console.Write("--Insert the amount you want to withdraw: ");
                    var amountToWithdraw = decimal.Parse(Console.ReadLine());
                    Withdraw(amountToWithdraw, cardFound);

                    // Adding transaction history entry.
                    context.TransactionHistoryEntries.Add(new TransactionHistoryEntry
                    {
                        CardNumber = cardFound.CardNumber,
                        TransactionDate = DateTime.Now,
                        Amount = amountToWithdraw
                    });
                    Console.Write("--Created transaction history log.");

                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, false);

                    var accountNumber = "";
                    var oldUser       = MembershipService.GetUser(model.UserName, false);
                    var userID        = oldUser.ProviderUserKey.ToString();
                    using (ATMEntities db = new ATMEntities())
                    {
                        var customer = from c in db.customers
                                       where c.idCustomer == userID
                                       select c;
                        accountNumber = customer.Single().account;
                    }
                    Session.Add("accountNumber", accountNumber);

                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            return(View(model));
        }
 public ActionResult Register(RegisterModel model)
 {
     if (ModelState.IsValid)
     {
         // Attempt to register the user
         MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
         if (createStatus == MembershipCreateStatus.Success)
         { // On success create a new customer data
             MembershipUser newUser   = MembershipService.GetUser(model.UserName, false);
             string         newUserID = newUser.ProviderUserKey.ToString();
             string         dtStamp   = DateTime.Now.ToString();
             string         accountNo = Guid.NewGuid().ToString();
             var            dbATM     = new ATMEntities();
             var            customer  = new customer
             {
                 idCustomer = newUserID,
                 firstName  = model.FirstName,
                 lastName   = model.LastName,
                 account    = accountNo,
                 cDate      = dtStamp
             };
             dbATM.customers.AddObject(customer);
             dbATM.SaveChanges();
             FormsService.SignIn(model.UserName, false);
             Session.Add("accountNumber", accountNo);
             TempData["justRegistered"] = "Y";
             return(RedirectToAction("Index", "Home"));
         }
         else
         {
             ModelState.AddModelError("", CustomerValidation.ErrorCodeToString(createStatus));
         }
     }
     ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
     return(View(model));
 }
Beispiel #36
0
 public DataGenerator(IRandomDataGenerator randomDataGenerator, ATMEntities atmEntities, int countGeneratedObjects)
 {
     this.random = randomDataGenerator;
     this.db = atmEntities;
     this.count = countGeneratedObjects;
 }
 public CardNumberDataGenerator(IRandomDataGenerator randomDataGenerator, ATMEntities atmEntities, int countGeneratedObjects)
     : base(randomDataGenerator, atmEntities, countGeneratedObjects)
 {
 }