Beispiel #1
0
        /// <summary>
        /// Removes a stock from the portfolio based on the ticker value
        /// </summary>
        /// <param name="ticker">String identifier</param>
        /// <returns>The value of the sale in dollars</returns>
        public float sell(string ticker, int quantity)
        {
            float         returnValue   = 0;
            StockQuantity selectedStock = null;

            foreach (StockQuantity s in stocks)//Iterates through every stock, searching for the ticker
            {
                if (s.Stock.Ticker == ticker)
                {
                    selectedStock = s;
                }
            }
            if (selectedStock.Quantity < quantity)//If the quantity to be sold exceeds the owned amount, the user is notified and taken back to the previous screen
            {
                Console.WriteLine("You don't own that many stocks!");
                return(returnValue);
            }
            else
            {
                originPrices -= selectedStock.PriceAtPurchase * quantity;
                returnValue   = selectedStock.Stock.StockPrice * quantity;
                if (selectedStock.Quantity == quantity)
                {
                    stocks.Remove(selectedStock);
                }
                else
                {
                    selectedStock.Quantity -= quantity;
                }
            }
            return(returnValue);
        }
Beispiel #2
0
        /// <summary>
        /// Sells a specified stock from a specified portfolio
        /// </summary>
        /// <param name="portName">Name of the portfolio</param>
        /// <param name="stockName">Name of the stock</param>
        /// <param name="amount">Amount to sell</param>
        /// <returns>Whether or not the sale succeeded or failed</returns>
        public bool Sell(string portName, string stockName, int amount)
        {
            Portfolio     selectedPortfolio = null;
            StockQuantity selectedStock     = null;

            foreach (Portfolio p in portfolios)
            {
                if (p.Name == portName)
                {
                    selectedPortfolio = p;
                }
            }
            if (selectedPortfolio.Name == null)
            {
                Console.WriteLine("Error, portfolio not found");
                return(false);
            }
            foreach (StockQuantity s in selectedPortfolio.Stocks)
            {
                if (s.Stock.Name == stockName)
                {
                    selectedStock = s;
                }
            }
            if (selectedStock == null)
            {
                Console.WriteLine("Error, stock not found");
                return(false);
            }
            cashBalance += selectedPortfolio.sell(selectedStock.Stock.Ticker, amount);
            transactionHistory.Add(new Transaction("SELL", amount, period, selectedStock.Stock, selectedStock.Stock.StockPrice));
            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Adds a stock to the list of stocks contained
        /// </summary>
        /// <param name="stock">The stock to add</param>
        /// <param name="quantity">The number of stocks to buy</param>
        /// <returns>The value of the purchase</returns>
        public float Add(Stock stock, int quantity)
        {
            bool          exists = false;
            StockQuantity sq     = null;

            foreach (StockQuantity s in stocks)
            {
                if (s.Stock.Name == stock.Name)
                {
                    exists = true;
                    sq     = s;
                    break;
                }
            }
            if (!exists)//if the stock does not exist, add a new one to the list
            {
                stocks.Add(new StockQuantity(stock, quantity));
                originPrices += stock.StockPrice * quantity;
            }
            else//If it does exist, just add the quantity
            {
                sq.Quantity  += quantity;
                originPrices += sq.PriceAtPurchase * quantity;
            }

            return(stock.StockPrice * quantity);
        }
Beispiel #4
0
        /// <summary>
        /// Performs operations on the selected portfolio as per user desire
        /// </summary>
        /// <param name="portfolioName">The portfolio to operate on</param>
        private static void PortfolioOperations(string portfolioName)
        {
            if (portfolioName == "X")
            {
                return;
            }
            if (account.PortfolioCount < 1)
            {
                Console.WriteLine("Portfolio not found");
                return;
            }
            Portfolio selected = null;

            foreach (Portfolio p in account.Portfolios)
            {
                if (p.Name == portfolioName)
                {
                    selected = p;
                    break;
                }
            }
            if (selected == null)
            {
                Console.WriteLine("Portfolio not found");
                return;
            }


            while (true)//Loop until user returns (chooses X in the menu)
            {
                float positions = selected.PositionsBalance;
                float gl        = selected.GainLossPercent;
                float glv       = selected.PositionsBalance;//selected.GainLossValue;

                string updown = "+";
                if (glv < 0)
                {
                    updown = "";
                }
                Console.WriteLine("\nPositions Balance: $" + positions + "\nGains/Losses: " + updown + "$" + glv + " (" + updown + gl + "%)" + "\nWhat would you like to do?\nB - Buy stocks\nS - Sell stocks\nV - View report\nSP - Sell this entire portfolio\nX - Return to main menu");
                string input = Console.ReadLine().ToUpper().Trim();
                Console.WriteLine();
                if (input == "B")//Buy a stock
                {
                    string selection     = "";
                    Stock  selectedStock = null;
                    bool   loop          = true;
                    while (loop)
                    {
                        Console.Write("Enter the name or the ticker of the stock to be purchased(To see a list of buyable stocks, enter 'STOCKLIST'), or enter X to cancel: ");
                        selection = Console.ReadLine();
                        Console.WriteLine();
                        if (selection == "X" || selection == "x")
                        {
                            loop = false; break;
                        }
                        if (selection == "STOCKLIST")//If the user wants to see available stocks and prices
                        {
                            foreach (Stock s in stocks)
                            {
                                Console.WriteLine(s.ToString());
                            }
                        }
                        else
                        {
                            foreach (Stock s in stocks)//search through stocks for selected stock
                            {
                                if (selection == s.Name || selection == s.Ticker || selection == s.Ticker.ToLower())
                                {
                                    selectedStock = s;
                                    loop          = false;
                                    break;
                                }
                            }
                            if (selectedStock == null)//if stock was not found, indicate to the user to try again
                            {
                                Console.WriteLine("Stock not found, please try again");
                            }
                        }
                    }
                    if (selection == "X" || selection == "x")
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine(selectedStock.ToString());
                        Console.WriteLine("Available balance: " + account.CashBalance);
                        if (input != "X" && input != "x")
                        {
                            loop = true;
                        }
                        int quant = 0;
                        while (loop)
                        {
                            Console.Write("Enter the quantity to be purchased, or enter the dollar amount of stocks to purchase (Ex: $35.10)\n(A flat rate fee of $9.99 will be applied) or enter X to cancel: ");
                            string read = Console.ReadLine();
                            if (read == "X" || read == "x")
                            {
                                break;
                            }
                            if (read[0] == '$')
                            {
                                try
                                {
                                    string substring = read.Substring(1);
                                    float  dollars   = Convert.ToSingle(substring);
                                    quant = (int)(dollars / selectedStock.StockPrice);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Error, invalid input");
                                }
                            }
                            else
                            {
                                try
                                {
                                    quant = Convert.ToInt32(read);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Error, invalid input");
                                }
                            }
                            int code = account.Buy(selected.Name, selectedStock.Name, quant);
                            if (code == 0)
                            {
                                loop = false;
                            }
                        }
                    }
                }
                else if (input == "S")
                {
                    StockQuantity selectedStock = null;
                    bool          loop          = true;
                    while (loop)
                    {
                        Console.Write("Please enter the ticker or name of the stock to be sold, or enter STOCKLIST to see available stocks or prest X to cancel: ");
                        string selection = Console.ReadLine();
                        if (selection == "X" || selection == "x")
                        {
                            break;
                        }
                        if (selection == "STOCKLIST")//If the user wants to see available stocks and prices
                        {
                            foreach (StockQuantity s in selected.Stocks)
                            {
                                Console.WriteLine(s.ToString());
                            }
                        }
                        else
                        {
                            foreach (StockQuantity s in selected.Stocks)//search through stocks for selected stock
                            {
                                if (selection == s.Stock.Name || selection == s.Stock.Ticker)
                                {
                                    selectedStock = s;
                                    loop          = false;
                                    break;
                                }
                            }
                            if (selectedStock == null)//if stock was not found, indicate to the user to try again
                            {
                                Console.WriteLine("You don't own any stock of that type");
                            }
                        }
                    }
                    loop = true;
                    int quant = 0;
                    while (loop)
                    {
                        Console.Write(selectedStock.ToString() + "\nPlease enter the quantity to sell, or enter X to cancel: ");
                        string read = Console.ReadLine();
                        if (read == "X")
                        {
                            loop = false;
                        }
                        else
                        {
                            try
                            {
                                quant = Convert.ToInt32(read);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Invalid input, please use integer input only");
                            }
                            if (quant > selectedStock.Quantity)
                            {
                                Console.WriteLine("You do not own that many shares");
                            }
                            else
                            {
                                account.Sell(selected.Name, selectedStock.Stock.Name, quant);
                                loop = false;
                            }
                        }
                    }
                }
                else if (input == "V")
                {
                    Console.WriteLine(selected.Report());
                }
                else if (input == "X")
                {
                    return;
                }
                else if (input == "SP")
                {
                    for (int i = selected.Stocks.Count - 1; i >= 0; i--)
                    {
                        account.Sell(selected.Name, selected.Stocks[i].Stock.Name, selected.Stocks[i].Quantity);
                    }
                    account.Portfolios.Remove(selected);
                    return;
                }
                else
                {
                    Console.WriteLine("Error, invalid input");
                }
            }
        }