Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

main.cpp #include using std::cout; using std::endl; #include using std::copy; #include using std::ostream_iterator; #include using std::ostringstream; #include using std::boolalpha; using std::fixed; using std::setprecision; #include #include proj08_market.h

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

main.cpp

#include using std::cout; using std::endl; #include using std::copy; #include using std::ostream_iterator; #include using std::ostringstream; #include using std::boolalpha; using std::fixed; using std::setprecision; #include #include "proj08_market.h" #include "proj08_player.h" int next_date(int d){ return d+1; } int main(){ Market mark("dow.txt"); // if you want to dump the stocks map, uncomment the code below // transform(mark.stocks.begin(), mark.stocks.end(), ostream_iterator(cout, ", "), // [](pair> p){ // ostringstream oss; // oss  (25.75 - delta))); assert(p1.stocks["IBM"] == 5); result = p1.sell(mark, "IBM", 20120831, 5); assert(result == true); assert((p1.cash  (1000 - delta))); result = p2.buy(mark, "AA", 20010614, 25); assert(result == true); assert((p2.cash  (219 - delta))); assert(p2.stocks["AA"] == 25); // not enough cash result = p1.buy(mark, "AA", 20120831, 1000); assert(result == false); assert((p1.cash  (1000 - delta))); // bad date result = p1.buy(mark, "AA", 21120831, 10); assert(result == false); assert(p1.cash == 1000); // don't have any of this result = p1.sell(mark, "AA", 20010614, 25); assert(result == false); assert((p1.cash  (1000 - delta))); // don't have that much result = p2.sell(mark, "AA", 20010614, 50); assert(result == false); assert((p2.cash  (219 - delta))); p2.buy(mark, "XOM", 20010614, 1); // p2:184.57,AA:25,XOM:1 cout  pr = mark.high_low_year(2009, "DIS"); cout  

market.h

#ifndef MARKET #define MARKET #include using std::map; #include using std::vector; #include using std::string; #include using std::pair; struct Market{ string file_name; vector symbol_list={"AA","AXP","BA","BAC","CAT","CSCO","CVX","DD", "DIS","GE","HD","HPQ","IBM","INTC","JNJ","JPM", "KFT","KO","MCD","MMM","MRK","MSFT","PFE", "PG","T","TRV","UTX","VZ","WMT","XOM"}; map> stocks; Market()=default; Market(string s); double get_price(string, long) const; pair high_low_year(long year, string symbol); }; #endif

player.h

#ifndef PLAYER #define PLAYER #include using std::string; #include "proj08_market.h" struct Player { double cash; map stocks; Player() = default; Player(double c) : cash(c) {} ; bool buy(Market &, string, long, long); bool sell(Market &, string, long, long); string to_str(); Player combine(Player& p2); }; #endif
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 > 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 o a method double get_price (string stock, long date) o returns the price of the stock on the date if: the date is a valid date the stock svmbol is a valid stock symbol o 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 o if the year or the symbol does not exist, returns the pair {-1.0, -1.0); Plaver You will help create a Player struct. The proj08 player.h provides 2 data members: * double cash How much money the player has . map 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) 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 o 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) o if true, purchase is made and the player info is updated 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 > 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 o a method double get_price (string stock, long date) o returns the price of the stock on the date if: the date is a valid date the stock svmbol is a valid stock symbol o 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 o if the year or the symbol does not exist, returns the pair {-1.0, -1.0); Plaver You will help create a Player struct. The proj08 player.h provides 2 data members: * double cash How much money the player has . map 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) 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 o 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) o if true, purchase is made and the player info is updated

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions