Example #1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            AccountManagement check = new AccountManagement();

            // check if account exists and show the appropriate form
            if (check.AccountExists == 0)
                Application.Run(new frmCreateAccount());
            else
                Application.Run(new frmStockMarket());
        }
 /**
  * Gets the name of the user and put it on a label
  */
 public void fillName()
 {
     AccountManagement account = new AccountManagement();
     string nameOfUser = account.Name;
     lblWelcome.Text = "Welcome: " + nameOfUser;
 }
 /**
  * Gets the amount of money of the user and put it on a label
  */
 public void fillMoney()
 {
     AccountManagement account = new AccountManagement();
     string moneyOfUser = account.Money;
     lblMoney.Text = "Money: " + moneyOfUser;
 }
Example #4
0
        /**
         * Buys a number of stocks, adds the transaction to portfolio and history, and updates the user's money
         */
        public void buyShares(string idOfCompany, string amountOfShares, string costOfShares, string symbolOfCompany, string nameOfCompany, string priceOfShare)
        {
            // check if the user has selected a stock
            int parsedValue;
            if (String.IsNullOrEmpty(nameOfCompany))
            {
                MessageBox.Show("You must specify a stock to buy.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            // check if user has entered an amount of shares
            else if (String.IsNullOrWhiteSpace(amountOfShares))
            {
                MessageBox.Show("You must specify an amount of shares.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            // make sure that user buys whole shares, not fractions
            else if (!int.TryParse(amountOfShares, out parsedValue))
            {
                MessageBox.Show("Invalid number of shares.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                // check how much money user has
                AccountManagement checkMoney = new AccountManagement();
                double totalMoney = Convert.ToDouble(checkMoney.Money);
                double totalCost = Convert.ToDouble(costOfShares);

                // check if the user can afford to buy this stock
                if (totalMoney < totalCost)
                {
                    MessageBox.Show("You can't afford that.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    // convert variables
                    int id = Convert.ToInt32(idOfCompany);
                    int shares = Convert.ToInt32(amountOfShares);
                    NumberFormat format = new NumberFormat();
                    double price = Convert.ToDouble(priceOfShare);
                    string priceString = format.ToUSString(price);
                    double total = Convert.ToDouble(costOfShares);
                    string totalString = format.ToUSString(total);

                    try
                    {
                        // add stock to the portfolio
                        sqlConnection.Open();
                        SqlCommand sqlCommandBuy = new SqlCommand(String.Format("update portfolio set shares = shares + {3}, total = total + {5} where id = {0} if @@rowcount=0 insert into portfolio values ({0}, '{1}', '{2}', {3}, {4}, {5})", id, symbolOfCompany, nameOfCompany, shares, priceString, totalString), sqlConnection);
                        sqlCommandBuy.ExecuteNonQuery();

                        // add buying stock to history
                        SqlCommand sqlCommandHistory = new SqlCommand(String.Format("insert into history values ({0}, 'BUY', CURRENT_TIMESTAMP, '{1}', '{2}', {3}, {4})", id, symbolOfCompany, nameOfCompany, shares, priceString), sqlConnection);
                        sqlCommandHistory.ExecuteNonQuery();

                        // update money
                        SqlCommand sqlCommandMoney = new SqlCommand(String.Format("update account set money = money - {0}", totalString), sqlConnection);
                        sqlCommandMoney.ExecuteNonQuery();

                        // inform the user about the sucessfull transaction
                        MessageBox.Show(String.Format("You successfully bought {0} shares from {1}. The total cost was {2}.", shares, nameOfCompany, total), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    finally
                    {
                        sqlConnection.Close();
                    }
                }
            }
        }