In this assignment you will practice creating our own user-defined data structures to be used in a simple stock market simulation. You will utilize what you have learned to date as well as using structs.
Background
We will use some real data from the Dow Jones Industrial Average (http://en.wikipedia.org/wiki/Dow_Jones_Industrial_Average ), a set of 30 stocks that are used as an indication of the U.S. stock market. This sample is from 8/31/2012 to 6/14/2001.
Market
You will help create a Market struct. The proj08_market.h file provides three data members:
string file_name , the name of the file containing the data
vector< symbol_list. A list of the short names of each of the 30 stocks in the Dowstring>
map< long, vector< double> > stocks. The prices of the 30 stocks on the date (the key). The prices are in order of the elements in symbol_list (that is, alphabetically by short name)
You must write the following methods:
a constructor that takes a single string argument, a string file name
That file contains closing stock prices for 30 stocks of the Dow Jones for a particular date (see format below). It populates the data member stocks .
a method double get_price(string stock, long date).
returns the price of the stock on the date if: the date is a valid date, the stock symbol is a valid stock symbol
returns a -1.0 otherwise
a method high_low_year(long year, string symbol). Returns as a pair the high and low values (in that order) for that stock for the provided year.
if the year or the symbol does not exist, returns the pair {-1.0, -1.0};
Player
You will help create a Player struct. The proj08_player.h provides 2 data members:
double cash How much money the player has
map< tring, long> stocks. The key is the stock symbol and the long is the quantity of that stock that the player owns (an integer value of stocks)s
You must write the following methods:
a constructor that takes a single parameter, the double cash the player starts with.
a method bool buy(Market &m, string stock, long date, long quantity). An attempt to buy a stock by the player from the Market on the specified date.
returns true if the player: has enough cash to make the purchase, the stock symbol is one of the valid 30 symbols, the date is valid (within the range of dates stored in Market)
if true, purchase is made and the player info is updated: cash reduced, stocks updated
if false return, no action taken
a method bool sell (Market &m, string stock, long date, long quantity)
returns true if the player: has the stock to sell (can't sell what you don't have), has at least the quantity indicated (can't sell more than you have)
if true, player info is updated: cash is increased, stocks updated
if false, no action taken
a method string to_str()
returns a string representation of the player
format is: cash,symbol:quantity,symbol:quantity (see Mimir format)
numeric output is fixed, setprecision(2)
always prints the cash value (even if 0.00), but only prints stocks (symbol:quant) if there are indeed any key:value pairs in stocks.
a method Player combine(Player &b)
returns a new Player that has, as a combination, all the cash and stocks of the two players: the caller and the argument.
the caller and the argument Players have their cash set to 0 and their stocks cleared
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.