Пример #1
0
        public static void Welcome()
        {
            using (var db = new CodeFirstATMContext())
            {
                Console.WriteLine("FIRST BANK OF DAD");
                Console.WriteLine("1) New User");
                Console.WriteLine("2) Login");
                int choice = int.Parse(Read(">  "));

                if (choice == 1) //New user
                {
                    var username = Read("Enter a UserName...");
                    if (db.Users.Any(u => u.Username == username))
                    {
                        Console.WriteLine("This username is already taken. Press any key to try again");
                        Console.ReadKey();
                        Console.Clear();
                        Welcome();
                    }
                    else
                    {
                        var  password = Read("Enter a Password...");
                        User newUser  = new User
                        {
                            Username = username,
                            Password = password,
                        };

                        db.Users.Add(newUser);
                        db.SaveChanges();

                        var tmpUserId = db.Users.Where(u => u.Username == username).First();
                        int UserId    = tmpUserId.UserId;

                        Transaction newTransaction = new Transaction //added to avoid null error at balance method
                        {
                            UserId = UserId,
                            Amount = 0,
                        };
                        db.Transactions.Add(newTransaction);
                        db.SaveChanges();


                        Console.WriteLine("Congratulations, your account is now active.");
                        Console.WriteLine("Press any key to login");
                        Console.ReadKey();
                        Console.Clear();
                        Login(db);
                    }
                }
                else //Login
                {
                    Login(db);
                }
            }
        }
Пример #2
0
        public static void Deposit(CodeFirstATMContext db, int _userId)
        {
            double amount = double.Parse(Read("Enter the amount that you would like to deposit"));

            Transaction newTransaction = new Transaction
            {
                UserId = _userId,
                Amount = amount,
            };

            db.Transactions.Add(newTransaction);
            db.SaveChanges();
            Console.Clear();
            TransactionScreen(db, 1);
        }
Пример #3
0
        public static void Withdraw(CodeFirstATMContext db, int _userId)
        {
            double balance = db.Transactions.Where(t => t.UserId == _userId).Sum(t => t.Amount);
            double amount  = double.Parse(Read("Enter the amount that you would like to withdraw"));

            if ((balance - amount) >= 0)
            {
                Transaction newTransaction = new Transaction
                {
                    UserId = _userId,
                    Amount = -(amount),
                };
                db.Transactions.Add(newTransaction);
                db.SaveChanges();
                Console.Clear();
                TransactionScreen(db, _userId);
            }
            Console.Clear();
            Console.WriteLine("You have insufficient funds for this transaction");
            Console.WriteLine("Press any key to return to the menu");
            Console.ReadKey();
            TransactionScreen(db, _userId);
        }