/// <summary> /// Exports the Investment Object, to be used by other programs /// </summary> /// <param name="inv">Investment Object to be Exported</param> /// <param name="directory">Path and filename to where the file will be saved</param> /// <param name="ft">File type for the file</param> /// <returns></returns> public static Boolean exportInvestment(Investment inv, string directory, FileType ft) { switch (ft) { case (FileType.JSON): try { string fullPath = directory; if (!directory.EndsWith(".json")) fullPath += ".json"; TextWriter WriteFileStream = new StreamWriter(fullPath + ".json", false); WriteFileStream.Write(JsonConvert.SerializeObject(inv, Formatting.Indented)); WriteFileStream.Close(); return true; } catch { return false; } case (FileType.CSV): try { string fullPath = directory; if (!directory.EndsWith(".csv")) fullPath += ".csv"; TextWriter WriteFileStream = new StreamWriter(fullPath + ".csv", false); WriteFileStream.WriteLine(csvHead()); WriteFileStream.WriteLine(inv.toCSVLine()); WriteFileStream.Close(); return true; } catch { return false; } default: return false; } }
/// <summary> /// Remove an investment from the portfolio /// </summary> /// <param name="inv">The Investment object to be removed</param> public Boolean removeInvestment(Investment inv) { return listOfInvestments.Remove(inv); }
/// <summary> /// Add an investment to the portfolio /// </summary> /// <param name="inv">The Investment object to be added</param> public void addInvestment(Investment inv) { if (listOfInvestments == null) listOfInvestments = new List<Investment>(); listOfInvestments.Add(inv); }
public List<ProfitDay> calculateProfits(CurrencyExchanger ce, Investment i, string data) { float stockProfit = 0; List<ProfitDay> thisCompanyProfits = new List<ProfitDay>(); DateTime calculateDate = i.BuyTime; QueryResult qr = new QueryResult(data, i.Stock.StockName); foreach (StockDay sd in qr.StockDays) { if (sd.Date >= i.BuyTime) { stockProfit = (float)(sd.ClosingValue - i.UnitStockValue) * i.Quantity; if (i.Stock.Currency != preferedCurrency) { stockProfit = ce.ExchangeValue(i.Stock.Currency, preferedCurrency, stockProfit); } thisCompanyProfits.Add(new ProfitDay(new DateTime(calculateDate.Year, calculateDate.Month, calculateDate.Day), stockProfit)); } } return thisCompanyProfits; }
private void QueryFinished(object sender, RunWorkerCompletedEventArgs e) { DownloaderResult dr = e.Result as DownloaderResult; if (dr.Error != null) { errorLabel.Text = dr.Error; bAdd.Enabled = true; } else { try { Investment inv = new Investment(new Stock(tbCompanyName.Text), Convert.ToInt32(tbQuantityBought.Text), DateTime.Parse(dateTimePicker1.Value.ToShortDateString()), Convert.ToDecimal(tbStockValue.Text.Replace(".", ","))); thisPortfolio.addInvestment(inv); errorLabel.Text = ""; this.Close(); } catch(CurrencyParseFailedException) { errorLabel.Text = "Failed to get original currency of company stocks."; } catch(CurrencyNotFoundException) { errorLabel.Text = "Failed to perform currency exchange."; } finally { bAdd.Enabled = true; } } }