Exemple #1
0
/*
 * buy function
 * takes Market reference, stock, date, and quantity
 * returns true or false depending if conditions meet to buy 
 */
bool Player::buy(Market &m, string stock, long date, long quantity) {
  bool result=true;
  double stockprice = m.get_price(stock,date)*quantity;
  if (stockprice > 0 && stockprice <= cash) {
    stocks[stock]+=quantity;
    cash -= stockprice;
  }
  else 
    result=false;
  return result;
}
Exemple #2
0
/*
 * sell function
 * takes Market reference, stock, date, and quantity
 * returns true or false depending if conditions meet to sell 
 */
bool Player::sell(Market &m, string stock, long date, long quantity) {
  bool result = true;
  double stockprice = m.get_price(stock,date)*quantity;
  auto mark = stocks.find(stock); 
  if (mark != stocks.end()) {
    if (stocks[stock] >= quantity && stockprice > 0) {
      cash +=stockprice;
      stocks[stock]-=quantity;
    }
    else 
      result=false;
  } else 
    result=false;
  return result;
}