// Jason Miller // CS307 // Project #3 - Stock Market Program with Trader Classes // // This Trader - JAMILLER2 - Buys as many shares as possible with // the allotted money ($100,000). It buys shares from some // random stock. Each think function will check to see if there // was a gain or loss. If loss of more than $10 from the purchase // price, then SELL SOON and sit on what's left. In other words... // Nice try, but I lost money and I can't afford to lose more! //#include "Broker.h" #include "..\market\Trader.h" class jamiller2 : public Trader { // Derive from the Trader class int m_TraderNumber; // Stores the number given to this trader Broker *m_MyBroker; // Define a pointer to the broker class double itsNetWorth; // Define my net worth int itsAOnceInALifetimePurchase; // But lots of stock in some company once StockData itsStockData; // Holds the data of a certain stock AssetData *itsAssetData[1]; // Holds the data of all the assets double itsCurrentPrice; // Keeps track of the current price of the stock double itsPurchasePrice; // Keeps track of the price at which it was purchased int itsBeenPurchased; // Tells the think function when stocks have been purchased int itsTimeToSell; // Tells the Trade function to SELL, SELL, SELL double itsNumberOfShares; // Keeps up with the number of stocks bought. public: // Constructor for jamiller2 class jamiller2(const int); // Think function gives trader a chance to "think" virtual void Think(); // Trade function gives trader a chance to "trade" // return value other than zero means trader is not // done trading....call function again. virtual int Trade(); }; // End of Class Definition //////////////////////////////////////////////////////////////// // Constructor for jamiller2 //////////////////////////////////////////////////////////////// jamiller2::jamiller2(const int aNumber) : Trader(aNumber) { // Set member data for the Trader number m_TraderNumber = aNumber; // Get a pointer to the broker m_MyBroker = GetBroker(); // Initialize variables itsAOnceInALifetimePurchase = 1; itsBeenPurchased = 0; itsTimeToSell = 0; itsCurrentPrice = 0.0; itsPurchasePrice = 0.0; }; // End of the Constructor //////////////////////////////////////////////////////////////// // Think Function //////////////////////////////////////////////////////////////// void jamiller2::Think() { double money = 0; itsNetWorth = m_MyBroker->GetNetWorth(); // Get Info on stock #20 int theReturn = m_MyBroker->GetStockInfo(20,itsStockData); itsCurrentPrice = itsStockData.CurrentPrice; // Get current price of stock if(itsBeenPurchased) { // Get number of shares you have - minus a few for a cushion...in case itsNumberOfShares = (itsNetWorth/itsCurrentPrice) - 10; // Check to see if the stock has lost money. if(itsCurrentPrice < (itsPurchasePrice-5) ) { itsTimeToSell = 1; // Tell trade to dump stock } } }; // End of the Think function //////////////////////////////////////////////////////////////// // Trade Function //////////////////////////////////////////////////////////////// int jamiller2::Trade() { double money = 0; // Holds the money left in my account // theMoneyToBurn needs to leave money for the broker fees double theMoneyToBurn = m_MyBroker->GetBalance() - 60; if(itsAOnceInALifetimePurchase) { // Keep up with the price when the stock was purchased; itsPurchasePrice = itsCurrentPrice; money = m_MyBroker->Buy(20,theMoneyToBurn); itsAOnceInALifetimePurchase = 0; itsBeenPurchased = 1; // Tells think to check the price from now on } if(itsTimeToSell) { itsBeenPurchased = 0; // Reset so think won't do it again itsTimeToSell = 0; // Reset so think won't do it again money = m_MyBroker->Sell(20,(int)itsNumberOfShares); } return(0); // Tells the Broker that I'm done trading for now. }; // End of the Trade Function