Esempio n. 1
0
        /// <summary>
        /// buys the stock the user asks for
        /// </summary>
        /// <param name="account">the account of the user</param>
        static void BuyStock(Account account)
        {
            if (account.portfolios.Count < 1)
            {
                Console.WriteLine("You dont have any portfolios");
                return;
            }
            string answer;

            foreach (Portfolio po in account.portfolios)
            {
                Console.WriteLine(po.Name);
            }
            Console.Write("Please choose a portfolio(Case sensitive): ");
            answer = Console.ReadLine();
            Console.WriteLine();
            Portfolio p = account.GetPortfolio(answer);

            if (p.Name == "error")
            {
                Console.WriteLine("Invalid Portfolio");
                return;
            }
            bool exit = false;

            while (!exit)
            {
                Console.WriteLine("Would you like to: ");
                Console.WriteLine("1. Enter the number of stocks to purchase");
                Console.WriteLine("2. Enter the amount in dollars to purchase stock represented by ticker");
                Console.WriteLine("3. Sell a stock");
                Console.WriteLine("4. Return to Main Menu");
                Console.Write("Please pick a number (1-4): ");
                answer = Console.ReadLine();
                Console.WriteLine();
                if (answer == "1")
                {
                    bool  valid = false;
                    Stock s     = null;
                    while (!valid)
                    {
                        Console.Write("Enter the Ticker: ");
                        string tick = Console.ReadLine();
                        foreach (Stock st in Stocks)
                        {
                            if (st.Ticker == tick)
                            {
                                s     = st;
                                valid = true;
                            }
                        }
                        if (!valid)
                        {
                            Console.WriteLine("Invalid Ticker, please try again");
                        }
                    }
                    valid = false;
                    int amount = 0;
                    while (!valid)
                    {
                        Console.Write("Enter the number of stocks: ");
                        amount = Convert.ToInt32(Console.ReadLine());
                        if (amount * s.Price > account.CashBalance)
                        {
                            valid = false;
                            Console.WriteLine("Insufficent funds");
                        }
                        else if (amount < 1)
                        {
                            valid = false;
                            Console.WriteLine("Invalid number");
                        }
                        else
                        {
                            valid = true;
                        }
                    }
                    p.BuyNumber(amount, s);
                    account.CashBalance -= (amount * s.Price) + TRADEFEE;
                    account.UpdateCash();
                    account.Trades += 1;
                    Console.WriteLine();
                }
                else if (answer == "2")
                {
                    bool  valid = false;
                    Stock s     = null;
                    while (!valid)
                    {
                        Console.Write("Enter the Ticker: ");
                        string tick = Console.ReadLine();
                        foreach (Stock st in Stocks)
                        {
                            if (st.Ticker == tick)
                            {
                                s     = st;
                                valid = true;
                            }
                        }
                        if (!valid)
                        {
                            Console.WriteLine("Invalid Ticker, please try again");
                        }
                    }
                    double amount = 0;
                    double spent  = 0;
                    valid = false;
                    while (!valid)
                    {
                        Console.Write("Enter the amount to spend: ");
                        amount = Convert.ToInt32(Console.ReadLine());
                        if (amount > account.CashBalance)
                        {
                            valid = false;
                            Console.WriteLine("Insufficent funds");
                        }
                        else if (amount < s.Price)
                        {
                            valid = false;
                            Console.WriteLine("Stock price is more expensive");
                        }
                        else
                        {
                            valid = true;
                        }
                    }
                    p.BuyValue(amount, s, out spent);
                    account.CashBalance -= spent;
                    account.UpdateCash();
                    account.Trades += 1;
                }
                else if (answer == "3")
                {
                    if (p.Stocks.Count > 0)
                    {
                        Console.WriteLine("You have: ");
                        Stock prev  = p.Stocks[0];
                        int   count = 0;
                        int   end   = 0;
                        foreach (Stock s in p.Stocks)
                        {
                            if (s.Name == prev.Name)
                            {
                                count++;
                            }
                            else
                            {
                                Console.WriteLine(count + " stocks of " + prev.Name + "(" + prev.Ticker + ")");
                                count = 0;
                                prev  = s;
                            }
                            end++;
                        }
                        Console.WriteLine(count + 1 + " stocks of " + prev.Name + "(" + prev.Ticker + ")");
                        Console.Write("Please enter a ticker to sell: ");
                        string tick = Console.ReadLine();
                        Console.Write("Please enter a number to sell: ");
                        int    num   = Convert.ToInt32(Console.ReadLine());
                        double value = 0;
                        Stock  stock = p.getStockT(tick);
                        if (stock == null)
                        {
                            Console.WriteLine("You don't have any of that stock in this portfolio.");
                        }
                        else if (p.Sell(num, stock, out value))
                        {
                            Console.WriteLine("You sold " + num + " of your " + p.getStockT(tick).Name + " stocks for $" + value.ToString("f"));
                        }
                        else
                        {
                            Console.WriteLine("You sold all of your " + p.getStockT(tick).Name + " stocks for $" + value.ToString("f"));
                        }
                        account.CashBalance += value - 9.99;
                        account.Trades      += 1;
                        return;
                    }
                    else
                    {
                        Console.WriteLine("You don't have any stocks in this portfolio");
                        return;
                    }
                }
                else if (answer == "4")
                {
                    exit = true;
                }
                else
                {
                    Console.WriteLine("Invalid input");
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// asks what the user wants to do with the porfolio balance and does said task
        /// </summary>
        /// <param name="account">user's account</param>
        static void PortfolioBalance(Account account)
        {
            bool exit  = false;
            bool exit2 = false;

            if (account.portfolios.Count < 1)
            {
                Console.WriteLine("You dont have any portfolios");
                return;
            }
            while (!exit2)
            {
                string answer;
                foreach (Portfolio po in account.portfolios)
                {
                    Console.WriteLine(po.Name);
                }
                Console.Write("Please choose a portfolio(Case sensitive): ");
                answer = Console.ReadLine();
                Console.WriteLine();
                Portfolio p = account.GetPortfolio(answer);
                if (p.Name == "error")
                {
                    Console.WriteLine("Invalid Portfolio");
                    return;
                }
                exit = false;
                while (!exit)
                {
                    Console.WriteLine("Would you like to:");
                    Console.WriteLine("1. See the portfolio balance");
                    Console.WriteLine("2. see the cash and positions balance");
                    Console.WriteLine("3. See a Gain/Losses Report (unavailable)");
                    Console.WriteLine("4. Choose another portfolio");
                    Console.WriteLine("5. Return to Main Menu");
                    Console.Write("Please choose a number (1-5): ");
                    answer = Console.ReadLine();
                    Console.WriteLine();
                    if (answer == "1")
                    {
                        p.CheckBal(account.Total);
                    }
                    else if (answer == "2")
                    {
                        p.CheckPosBal();
                    }
                    else if (answer == "3")
                    {
                        p.GenerateReport();
                    }
                    else if (answer == "4")
                    {
                        exit = true;
                    }
                    else if (answer == "5")
                    {
                        exit2 = true;
                        exit  = true;
                    }
                    else
                    {
                        Console.WriteLine("Invalid Answer.");
                    }
                }
            }
        }