Beispiel #1
0
        /// <summary>
        /// sells a stock
        /// </summary>
        /// <param name="t">the ticker of the stock</param>
        /// <param name="p">the portfolio to sell from</param>
        /// <param name="amount">the amount to sell</param>
        public void sellStock(string t, string p, int amount)
        {
            t = t.ToUpper();
            Portfolio po = portfolioExists(p);

            if (po != null)
            {
                Stock st = stockExistsSell(t, po);
                if (st != null && amount <= st.Amount)
                {
                    double gain = po.sellStock(st, amount);
                    if (gain != -0.01)
                    {
                        double newPrice = 0;
                        foreach (Stock s in _availableStocks)
                        {
                            if (s.Ticker.Equals(st.Ticker))
                            {
                                newPrice = s.Price * amount;
                            }
                        }
                        double difference = gain - newPrice;
                        _gains      -= difference;
                        _money      += newPrice;
                        _assetMoney -= gain;
                        _stocks     -= amount;
                    }
                    else
                    {
                        Console.WriteLine("error: you do not own that many of " + st.Ticker);
                    }
                }
                else
                {
                    Console.WriteLine("error: could not complete transaction, stock may not exist");
                }
            }
            else
            {
                Console.WriteLine("error: could not find portfolio");
            }
        }
Beispiel #2
0
        public void sellSpecStock(Stock s, string po, int amount)
        {
            Portfolio p = portfolioExists(po);

            if (p != null)
            {
                if (amount <= s.Amount)
                {
                    double gain = p.sellStock(s, amount);
                    if (gain != -0.01)
                    {
                        double newPrice = 0;
                        foreach (Stock st in _availableStocks)
                        {
                            if (st.Ticker.Equals(s.Ticker))
                            {
                                newPrice = st.Price * amount;
                            }
                        }
                        double difference = gain - newPrice;
                        _gains      -= difference;
                        _money      += newPrice;
                        _assetMoney -= gain;
                        _stocks     -= amount;
                    }
                    else
                    {
                        Console.WriteLine("error: you do not own that many of " + s.Ticker);
                    }
                }
            }
            else
            {
                Console.WriteLine("Portfolio does not exist");
            }
        }