public Investment(Stock stock, int quantity, DateTime timeOfInvestment, Decimal investmentValue) { this.Stock = stock; this.UnitStockValue = investmentValue; this.Quantity = quantity; this.BuyTime = timeOfInvestment; }
public bool importInvestments(string directory, FileType ft) { List<Investment> newInvestments = new List<Investment>(); switch (ft) { case (FileType.JSON): try { string fullPath = directory; if (!directory.EndsWith(".json")) fullPath += ".json"; TextReader stream = new StreamReader(fullPath); string json = stream.ReadToEnd(); JsonConvert.PopulateObject(json, newInvestments); stream.Close(); foreach (Investment inv in newInvestments) listOfInvestments.Add(inv); return true; } catch { return false; } case (FileType.CSV): try { string fullPath = directory; if (!directory.EndsWith(".csv")) fullPath += ".csv"; TextReader stream = new StreamReader(fullPath); string csv = stream.ReadToEnd(); string[] lines = csv.Split('\n'); foreach (string line in lines) { string[] fields = line.Split(','); if (!line.StartsWith("StockName") && !string.IsNullOrEmpty(line)) { Stock s = new Stock(fields[0].Replace('.', ','), fields[1].Replace('.', ',')); int quantity = Convert.ToInt32(fields[3].Replace('.', ',')); DateTime date = Convert.ToDateTime(fields[2].Replace('.', ',')); Decimal value = Convert.ToDecimal(fields[4].Replace('.', ',')); newInvestments.Add(new Investment(s,quantity,date,value)); } } foreach (Investment inv in newInvestments) listOfInvestments.Add(inv); return true; } catch { return false; } default: return false; } }