public List<StockTransaction> combineSameStockTransactions(List<StockTransaction> trans) { List<StockTransaction> result = new List<StockTransaction>(); HashSet<StockTransaction> allSymbols = new HashSet<StockTransaction>(); foreach (StockTransaction s in trans) { allSymbols.Add(s); } foreach (StockTransaction s in allSymbols) { StockTransaction combined = new StockTransaction(); combined.Stock_Name = s.Stock_Name; combined.Ticker_Symbol = s.Ticker_Symbol; int combinedQuantity = 0; int count = 0; decimal sum = 0; foreach (StockTransaction t in trans) { if (s.Ticker_Symbol == t.Ticker_Symbol) { if (t.Rate.ToString().Contains('-')) { combinedQuantity = combinedQuantity - t.Quantity; } else { combinedQuantity = combinedQuantity + t.Quantity; count++; sum = sum + t.Rate; } } } combined.Quantity = combinedQuantity; combined.Rate = sum / count; Boolean inList = false; foreach (StockTransaction sTrans in result) { if (sTrans.Stock_Name == combined.Stock_Name) { inList = true; } } if (inList == false && combined.Quantity > 0) { result.Add(combined); } } return result; }
public bool BuyStock(StockTransaction trans) { try { using (var context = new EntityContext()) { context.StockTransaction.Add(trans); context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public ActionResult SaveBuyTransaction(String stockName, String stockTicker, int quantity, double rate) { StockTransaction trans = new StockTransaction(); trans.Stock_Name = stockName; trans.Ticker_Symbol = stockTicker; trans.Quantity = quantity; trans.Rate = (Decimal)rate; trans.Timestamp = DateTime.Now; // trans.transID = ; // need to figure out how to get userid HttpCookie cookie = Request.Cookies["SESSION_ID"]; trans.User_ID = accountService.getUserID(new Guid(cookie.Value)); stockService.BuyStock(trans); return new EmptyResult(); }