/// <summary> /// Adds the stock. /// </summary> public void AddStock() { try { IList <StockModel5> stockModels = new List <StockModel5>(); Console.WriteLine("Enter customer id"); int id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter stock name"); string name = Console.ReadLine(); Console.WriteLine("Enter price per share"); int price = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter number of share"); int num = Convert.ToInt32(Console.ReadLine()); StockModel5 stock = new StockModel5 { Id = id, Name = name, PricePerShare = price, NumberOfShare = num }; Constants constants = new Constants(); using (StreamReader sr = new StreamReader(constants.StockFile)) { string json = sr.ReadToEnd(); sr.Close(); stockModels = JsonConvert.DeserializeObject <List <StockModel5> >(json); stockModels.Add(stock); var write = JsonConvert.SerializeObject(stockModels); File.WriteAllText(constants.StockFile, write); } } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Buys the stock. /// </summary> /// <exception cref="Exception"> /// Invalid stock id /// or /// Invalid stock id /// or /// Invalid customer id /// or /// No. of shares you mentioned are not available in stock or invalid input /// </exception> public void BuyStock() { int priceOfShare = 0; try { CustomerData customer = new CustomerData(); IList <CustomerModel> customerModels = customer.GetAllCustomers(); Console.WriteLine("id \t name \t valuation"); foreach (var items in customerModels) { Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.Valuation); } Console.WriteLine("Please enter the customer id"); int customerId = Convert.ToInt32(Console.ReadLine()); Stock5 stockData = new Stock5(); IList <StockModel5> stockModels = stockData.GetStock(); Console.WriteLine("id \tname \tnumberofshares \tpricepershare"); foreach (var items in stockModels) { Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.NumberOfShare + "\t" + items.PricePerShare); } Console.WriteLine("Please enter the Stock Id to select stock for the customer"); int stockId = Convert.ToInt32(Console.ReadLine()); StockModel5 stockDataModel = null; bool stockFl = true; foreach (var items in stockModels) { if (items.Id == stockId) { stockDataModel = items; Console.WriteLine(items.Id + "\t" + items.Name + "\t" + items.NumberOfShare + "\t" + items.PricePerShare); stockFl = false; } } if (stockFl == true) { throw new Exception("Invalid stock id"); } Console.WriteLine("Enter the number of shares need to purchase"); int numberOfShares = Convert.ToInt32(Console.ReadLine()); string customerName = string.Empty; string stockName = string.Empty; int amountValuation = 0; if (numberOfShares < stockDataModel.NumberOfShare || numberOfShares <= 0) { priceOfShare = 0; bool stockFlag = true; foreach (var items in stockModels) { if (items.Id == stockId) { items.NumberOfShare = items.NumberOfShare - numberOfShares; priceOfShare = items.PricePerShare; stockName = items.Name; stockFlag = false; } } if (stockFlag == true) { throw new Exception("Invalid stock id"); } bool customerFlag = true; foreach (var item in customerModels) { if (item.Id == customerId) { item.Valuation = item.Valuation - (priceOfShare * numberOfShares); Console.WriteLine("Your cost for buying item is {0} ", priceOfShare * numberOfShares); customerName = item.Name; amountValuation = item.Valuation; customerFlag = false; } } if (customerFlag == true) { throw new Exception("Invalid customer id"); } } else { throw new Exception("No. of shares you mentioned are not available in stock or invalid input"); } TransactionModel transactionModel = new TransactionModel() { CustomerName = customerName, StockName = stockName, NoOfShare = numberOfShares, Amount = priceOfShare * numberOfShares, Time = DateTime.Now.ToString(), TransactionType = TransactionType.Buy }; IList <TransactionModel> transactionModels = Transaction.GetAllTransactions(); transactionModels.Add(transactionModel); Constants constants = new Constants(); Transaction.WriteFile(constants.StockFile, stockModels); Transaction.WriteFile(constants.CustomerData, customerModels); Transaction.WriteFile(constants.TransactionFile, transactionModels); Console.WriteLine("purchase sucessfull"); } catch (Exception e) { Console.WriteLine(e.Message); } }