Example #1
0
        static void Main(string[] args)
        {
            int      balance;
            string   accountName;
            Checking checking = null;
            Savings  savings  = null;

            while (true)
            {
                Console.WriteLine("1. Exit program");
                Console.WriteLine("2. Create Checking Account");
                Console.WriteLine("3. Create Savings Account");
                Console.WriteLine("4. Get Checking Balance");
                Console.WriteLine("5. Get Savings Balance");
                Console.WriteLine("6. Make a Deposit to Checking");
                Console.WriteLine("7. Make a Deposit to Savings");
                int menuOption = Utils.GetNumber("Enter a menu option: ");
                if (menuOption == 1)
                {
                    break;
                }
                if (menuOption == 2)
                {
                    float fIntrestRate = 0.02F;
                    accountName = Utils.GetInput("What would you like to name your account: ");
                    balance     = Utils.GetNumber("What is the initial balance of the account: ");
                    checking    = new Checking(accountName, balance, fIntrestRate);
                }
                if (menuOption == 3)
                {
                    float fIntrestRate = 0.03F;
                    accountName = Utils.GetInput("What would you like to name your account: ");
                    balance     = Utils.GetNumber("What is the initial balance of the account: ");
                    savings     = new Savings(accountName, balance, fIntrestRate);
                }
                if (menuOption == 4)
                {
                    Console.WriteLine(checking);
                }
                if (menuOption == 5)
                {
                    Console.WriteLine(savings);
                }
                if (menuOption == 6)
                {
                    int depositAmount = Utils.GetNumber("How much would you like to deposit: ");
                    checking.Deposit(depositAmount);
                }
                if (menuOption == 7)
                {
                    int depositAmount = Utils.GetNumber("How much would you like to deposit: ");
                    savings.Deposit(depositAmount);
                }
            }
        }
        /// <summary>
        /// Withdraws a certain amount of money from the Checking account
        /// </summary>
        /// <param name="amount">AMount to be withdrawn</param>
        /// <param name="checkaccount">Checking account number</param>
        /// <param name="saveaccount">Savings account number</param>
        public static void WithdrawChecking(decimal amount, int checkaccount, int saveaccount)
        {
            string        cs  = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\benke\source\repos\ATM\ATM\ATM Database.mdf;Integrated Security = True; Connect Timeout = 30";
            SqlConnection con = new SqlConnection(cs);

            con.Open();
            SqlCommand command = new SqlCommand("SELECT [Balance] FROM [Account] WHERE [AccountNumber] = " + checkaccount, con);

            decimal balance = Convert.ToDecimal(command.ExecuteScalar());

            balance = System.Math.Abs(balance);
            con.Close();
            Checking takeout = new Checking();
            bool     check   = takeout.checkamount(balance, amount);
            decimal  newbalance;

            if (check == true)
            {
                con.Open();
                newbalance = balance - amount;
                SqlCommand com2 = new SqlCommand("UPDATE [Account] SET [Balance] = " + newbalance + " WHERE [AccountNumber] = " + checkaccount, con);
                com2.ExecuteNonQuery();
            }
            else
            {
                newbalance = amount - balance;
                con.Open();
                SqlCommand command1    = new SqlCommand("SELECT [Balance] FROM [Account] WHERE [AccountNumber] = " + saveaccount, con);
                decimal    savebalance = Convert.ToDecimal(command1.ExecuteScalar());
                savebalance = Math.Abs(savebalance);
                if (savebalance > newbalance)
                {
                    decimal newsavebalance = savebalance - newbalance;
                    newbalance = 0;
                    SQLHelper.Deposit(saveaccount, newsavebalance);
                    SqlCommand com1 = new SqlCommand("UPDATE [Account] SET [Balance] = " + newsavebalance + " WHERE [AccountNumber] = " + saveaccount, con);
                    SqlCommand com2 = new SqlCommand("UPDATE [Account] SET [Balance] = " + newbalance + " WHERE [AccountNumber] = " + checkaccount, con);
                    com1.ExecuteNonQuery();
                    com2.ExecuteNonQuery();
                }
                else
                {
                    MessageBox.Show("You do not have enough funds for this. Get a job loser.");
                }
            }
        }
Example #3
0
        void PopulateAcct()
        {
            int index = -99;

            for (int i = 0; i < myAccounts.Length; i++)
            {
                if (myAccounts[i] == null)
                {
                    index = i;
                    break;
                }
            }
            if (index != -99)
            {
                Console.WriteLine("\nPlease enter a name to be associated with the type of account you want");
                String name = Console.ReadLine();
                for (int i = 0; i < myAccounts.Length; i++)
                {
                    if (myAccounts[i] != null && name.ToLowerInvariant() == myAccounts[i].Name.ToLowerInvariant())
                    {
                        Console.WriteLine("\nThat name has already been used\n");
                        Greeting();
                        return;
                    }

                    if (name == "")
                    {
                        Console.WriteLine("You must enter a name\n");
                        Greeting();
                        return;
                    }
                }
                Console.WriteLine("\nPlease enter a PIN to use with this account");
                String PIN = Console.ReadLine();

                if (PIN == "")
                {
                    Console.WriteLine("You must enter a PIN\n");
                    Greeting();
                    return;
                }

                Console.WriteLine("\nWhat type of account would you like to open, checking or savings?");
                String type = Console.ReadLine();

                if (type.Equals("checking", StringComparison.InvariantCultureIgnoreCase))
                {
                    myAccounts[index] = new Checking(100, index, name, PIN);
                    Console.WriteLine("\nBalances start at $100, and the annual interest rate is 5%");
                }
                else if (type.Equals("savings", StringComparison.InvariantCultureIgnoreCase))
                {
                    myAccounts[index] = new Savings(100, index, name, PIN);
                    Console.WriteLine("\nBalances start at $100, and the annual interest rate is 10%");
                }
                else
                {
                    Console.WriteLine("\nInvalid account type");
                    PopulateAcct();
                }
            }
            else
            {
                Console.WriteLine("\nSorry, all available accounts are full\n");
                Greeting();
            }
        }