/// <summary> /// Adds purchase to stock history. /// </summary> /// <param name="tick"> The ticker of the stock purchase. </param> /// <param name="quantity"> The quantity of the stock to buy. </param> /// <param name="prices"> The price of the stock. </param> public void BuyStock(Ticker tick, int quantity, Dictionary <Ticker, decimal> prices) { StockCollection purchase = new StockCollection(tick, quantity, prices[tick]); stocks.Add(purchase); }
public StockCollection(Ticker stockTicker, int quantity, decimal priceAtPurchase) { this.stockTicker = stockTicker; this.quantity = quantity; this.priceAtPurchase = priceAtPurchase; }
/// <summary> /// PortBuy /// Buys stocks and stores them in the portfolio /// </summary> /// <param name="port"></param> static void PortBuy(Portfolio port) { bool temp = false; while (temp == false) { Console.WriteLine("Stocks Available: "); foreach (Ticker t in tickers) { Console.WriteLine("\t" + t.ToString()); } bool found = false; Ticker tempTick = new Ticker(); while (found == false) { Console.Write("Enter ticker of stock to buy: "); string tick = Console.ReadLine(); if (tick == "exit") { return; } foreach (Ticker t in tickers) { if (t.Tag == tick.ToUpper()) { if (port.stocks.Count == 0) { found = true; tempTick = t; } foreach (Stock s in port.stocks) { if (s.ticker.Tag == t.Tag) { Console.WriteLine("You cannot buy stock from a company you already have stock in: (" + t.Tag + ")"); } else { tempTick = t; found = true; } } } } if (found == false) { Console.WriteLine("Ticker not found!"); } } int amount = 0; bool valid = false; while (!valid) { Console.Write("Enter amount of stock to buy: "); try { string input = Console.ReadLine(); if (input == "exit") { return; } amount = Convert.ToInt32(input); if (amount >= 1) { valid = true; } } catch (Exception) { } if (!valid) { Console.WriteLine("Entry Invalid! Only enter positive integers."); } else { double price = 9.99; price += tempTick.Price * amount; if (price > funds) { Console.WriteLine("You have insufficient funds for this transaction."); temp = true; valid = false; } else { port.AmountStocks += amount; Stock stock = new Stock(tempTick, amount); port.stocks.Add(stock); funds -= price; Console.WriteLine("A transfer fee of $9.99 has been added to your purchase."); Console.WriteLine("You have bought " + amount + " stocks in " + tempTick.Name + " for a price of " + price.ToString("C")); Console.WriteLine("Your current funds: " + funds.ToString("C")); temp = true; } } } } }