/**
         * Enables the user to save the edits made to their account
         */
        public void editAccount(string Id, string newUsername, string newName, string newSurname, double newMoney)
        {
            // check if the user has entered all fields
            double parsedValue;
            string money = newMoney.ToString();
            if (String.IsNullOrWhiteSpace(newUsername) || String.IsNullOrWhiteSpace(newName) || String.IsNullOrWhiteSpace(newSurname) || String.IsNullOrWhiteSpace(money))
            {
                MessageBox.Show("You can't leave an empty field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            // make sure that money is a number
            else if (!double.TryParse(money, out parsedValue))
            {
                MessageBox.Show("Invalid amount of money.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                try
                {
                    // convert variables
                    NumberFormat format = new NumberFormat();
                    string newMoneyString = format.ToUSString(newMoney);

                    // insert new data
                    sqlConnection.Open();
                    SqlCommand sqlCommand = new SqlCommand(String.Format("update account set username = '******', name = '{1}', surname = '{2}', money = {3} where Id = {4}", newUsername, newName, newSurname, newMoneyString, Id), sqlConnection);
                    sqlCommand.ExecuteNonQuery();

                    // inform the user about the sucessfull transaction
                    MessageBox.Show("You successfully edited your account.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
        }
Пример #2
0
        /**
         * Sells a number of stocks, adds the transaction to portfolio and history, and updates the user's money
         */
        public void sellShares(ComboBox cmbSell, 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(cmbSell.Text))
            {
                MessageBox.Show("You must specify a stock to sell.", "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 shares user has
                UserPortfolio checkShares = new UserPortfolio();
                double userShares = Convert.ToDouble(checkShares.getShares(nameOfCompany));

                // 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);

                // check if the user wants to sell more than he has
                if (userShares < shares)
                {
                    MessageBox.Show("You have less than the amount of shares you want to sell.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    try
                    {
                        // remove stock from the portfolio
                        sqlConnection.Open();
                        SqlCommand sqlCommandSell = new SqlCommand(String.Format("update portfolio set shares = shares - {3}, total = total - {5} where id = {0} if ((select shares from portfolio where Id = {0}) <= {3}) delete from portfolio where id = {0}", id, symbolOfCompany, nameOfCompany, shares, priceString, totalString), sqlConnection);
                        sqlCommandSell.ExecuteNonQuery();

                        // add selling stock to history
                        SqlCommand sqlCommandHistory = new SqlCommand(String.Format("insert into history values ({0}, 'SELL', 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 sold {0} shares from {1}. The total cost was {2}.", shares, nameOfCompany, total), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    finally
                    {
                        sqlConnection.Close();
                    }
                }
            }
        }
Пример #3
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();
                    }
                }
            }
        }