On your own, create a modular C++ program to calculate the gain or loss status of a stock. The data maintained for a stock object consists of the following (validation not required for this project, assume input data in valid ranges):

Trading Symbol C++ string object containing the abbreviation for the trading symbol

Company Name C++ string object containing the full name of the company

Number of Shares integral number (greater than 0 and less than or equal to 10,000 shares)

Purchase Price floating point number representing the purchase price of single stock (greater than 0 and less than or equal to 100.00)

Current Price floating point number representing the current price of single stock (greater than or equal to 0 and less than or equal to 100.00)

Define a stock class data type to contain all the private data for a single stock. Include public constructor, destructor, set, and get methods for the private data in your definition. In addition, include the following public methods in your definition:

  • CurrValue Returns the current value of the stock (Number of Shares * Current Price).
  • Gain Returns boolean true if stock has gain (current price is greater than purchase price), false otherwise.
  • GainAmt If Gain method returns true, calculate and return amount of gain, otherwise return -1
  • TaxGainAmt If GainAmt method returns non-negative number, calculate and return tax amount on gain using long term capital gain tax rate, otherwise return -1. Use a named constant for the long term capital gain tax rate which is 15%
  • DisplayData Display (to standard output) the objects data according to the following format. If the stock has a loss or breakeven status (breakeven is defined as net difference of less than .05 per share between purchase and current prices), only display the stock status. If the stock has a gain, calculate and display the amount of the gain and the tax payable for that gain.
Company Name: ABC Enterprises
Number of Shares: 563
Purchase Price (per share): 7.75
Current Price (per share): 20.25
Stock Status: GAIN Amount of Gain: 7037.50
Tax Owed: 1055.63

Create an array of these stock class objects to store the data for all stocks within a portfolio (use a named constant for the maximum number of array elements as 15). Also create an array of pointers to the stock class objects. Make each element in the array of pointers contain the address of the corresponding element in the array of stock class objects. Thus, you will have two arrays: an array of stock class objects and an array of pointers to those stock class objects.

Prompt the user for an input data file name. This data file contains the portfolios stock data where each line has one stocks data (there can be any number of lines in the fileonly read until the end of file is reached or the maximum number of array elements are read, whichever comes first):

Field Data Type Digits After Decimal Delimiter
Trading Symbol Character #
Company Name Character #
Number of Shares Numerical 0 space
Purchase Price Numerical 2 space
Current Price Numerical 2 newline

Store each line of data into an element of the array of stock class objects. After all data has been read from the file, sort the array of pointers according to increasing trading symbol. Only elements in the array of pointers should change position; elements in the array of stock class objects remain unchanged. Using the array of pointers, display the list of sorted trading symbols (3 per line, sorted order) to the user and prompt the user for trading symbol. With the user input trading symbol, search the array of stock class objects and if a match is found, display the stock information (call the DisplayData method on the stock class object). If a match cannot be found, print out an error message and exit the program

Your program should present a modular approach -- no global variables; use parameters and return values for transferring data between modules, call class methods for getting/setting/calculating/displaying data on a stock object. Your program should create and properly use (pass parameters, check return values, etc.) at least the following modules (function prototypes and descriptions are given):

  • void main(); This controlling function calls the functions to read the stock data from file and the function to sort the stock data. It then displays the stock trading symbols (3 per line) and prompts the user to input a trading symbol. The search function is then called to locate a valid index. If a valid index is found, the DisplayData method on the stock object is called. If a valid index is not found, print out an error message and exit the program.
  • int ReadStkData(Stock stks[], Stock *stkPtrs[], int maxSize); This module prompts the user for a file name, opens the file, reads data from the file (until end of file or maxSize has been reached, whichever comes first) into the passed array of stock class objects, sets the array of pointers to the array of stock class objects, closes the file, and returns the number of elements read from the file.
  • void SortStkPtrs(Stock *stkPtrs[], int size); This function sorts the array of pointers according to increasing stock trading symbol. Remember to dereference the pointer when accessing the underlying data for sorting.
  • int SearchTrSym(Stock stks[], string keyValue, int size); This function searches all the size elements of stock class objects for a given key value. If a match is found, return the index number. If a match is not found return 1. Remember to access the correct class member when comparing stock trading symbols.

a) Add a block comment at the top of the file to identify your name, file, date, class, assignment, and short description of the program.

b) Use proper alignment and spacing in your source code. Use textbook examples as a guide.

c) Compile the source code until no syntax errors are found. Run the program with a given data file and manually verify the expected and actual results. Debug as appropriate to ensure accuracy.

d) Copy and paste the generated output from the data file as a multi-line comment at the end of the source code file.

e) Attach a copy of the source code file and generated output to this sheet for full possible points as indicated above. Upload a copy of your source code file in Blackboard under the appropriate program assignment.

f) Be prepared to give a demo of your code and describe its functionality at the beginning of class on the listed due date.

Academic Honesty!
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.