EECS 370

Machine Problem 3, Spring 2000

Stock Manager

Due: Thursday, February 24, 2000 at 11:59 pm
Due: Wednesday, March 1, 2000 at 11:59 pm

Stock prices are written as a rational number, where the dollar portion is represented as a integer and the cent portion is represented as a fraction.  The denominator of the fraction is also aways reduced to the lowest possible power of 2 (i.e. 2, 4, 8, 16, ...) and the numerator is always less than than demoninator.  For this assignment, you are required to write a C++ class called stockPrice that will maintain the price in its proper form.  This class must also overload the following operators. You may overload any other operators that you feel are needed. This program will need to interact with two files while running:
  1. A stock file

    This file will keep track of the stocks in a single stock portfolio. A portfolio may contain information about any number of stock purchases. The stock file is to have a file extension of .sf and will have the following format:

  2. A price file

    The price file will list the prices for various stocks. The price file is to have the file extension of .pf and will have the following format:

    The price file will be used to get the initial stock price when buying a stock, to update the current stock price and to get the final stock price when selling a stock.
When you start your program from the UNIX prompt, the user may specify the stock file and price file to be used with your program. These filenames are to be accessed using the argc and argv parameters of the main() function in your program. Since the file extensions are different between the stock files and price files, your program must be able to accept them in any order. There may be only one stock file and one price file specified on the UNIX prompt. If multiple stock files or price files are given, output an appropriate error message and exit the program. The user does not have to specify either file on the command line. In this case, the user will specify the files using the program's internal commands. Any price file specified must already exist. If the price file does not exist, output an appropriate error message and exit the program. The stock file specified does not need to already exist. If a stock file does not exist, the user wishes to create a new stock file. See the Open a Stock File command for the actions to be performed when creating a new stock file.

Your program will run interactively with the following commands.  Extra space or tab characters may be included in any command.  All commands consist of a single character.  Only one command will be issued on a line; however, a command may span multiple lines. If the character immediately preceding the newline character is the back-slash character "\", the current coomand is to continue onto the following line. Commands will be separated from any following information by space or tab characters.  Unknown commands or when a command is not immediately followed by a space, tab or newline character are errors and should result in an appropriate error message. All alphabetic commands can be given in either upper or lower case. Also the stock symbols are not case sensitive. The stock symbols should be converted to upper case if given in lower case.

Help - h
Provide help about the interactive commands for the program.

Quit the Program - q
The program is to quit. If there are any unsave changes made to the current stock file, prompt the user to see if they wish to save the changes before quitting the program.

Open a stock file - o <filename>
The program is to close the current stock file (if one is opened) and open another one. If the current stock file has any unsaved changes, prompt the user to see if they wish to save the changes before closing the file. The <filename> must have the .sf extension, if not output an appropriate error message and ingore the command. If the <filename> does not currently exist, you are to create a new stock portfolio file. When creating a new stock portfolio file, the current dollar balance is to be set to zero and you are to first prompt the user for a description to describe the new stock portfolio. This description is to be eighty characters or less in length. If the <filename> does exist, read in the information from the file and store it in some class (or classes) in your program. This newly opened stock file is to become the current stock file.

Write out (save) a stock file - w [<filename>]
The program is to write out the current stock portfolio information to a file. If the <filename> is not given with the command (the square brackets shown in the command indicate the filename is optional), write out the information to the current stock file. If the <filename> is given with the command, write out to that file and make that filename the current stock file. Also if the filename is given, it must have hte file extension of .sf. If it does not have this file extension, output an appropriate error message and ignore the command.

View and Modify the Description - d
List the description for the current stock file and then prompt the user to change the description. The prompt should allow for a Yes/No answer (given as "y" or "n"). If the answer is Yes, prompt the user for a new description. The description must be eighty characters or less.

Change the Current Price File - p <filename>
Change the file used to get the current stock price information. The <filename> must have a .pf extenstion and must currently exist. If either of these are not true, output an appropriate error message and ignore the command.

Buy Stock - b <ss> <num>
The user wishes to buy <num> shares of stock with the symbol <ss>. This command requires that the program has a current (open) stock file and current (open) price file. If either one of these two files are not open, output and appropriate error message and ignore the command. The stock price is to be found in the current price file. If the current price file does not have a listing for that stock symbol, an error message is to be printed and the purchase is to be ignored. The number of shares must be an integer value greater than or equal to zero (purchasing zero shares allows the portfolio to track a stock's price changes without actually owning that stock). The stock price used for the purchase will become the initial/purchase price for the stock. This price will also be the first values for the stock price at the last update and the current stock price.

When each stock is purchased the current dollar balance of the portfolio must be updated by subtracting the value of the purchase from the current dollar balance of the portfolio. The value of the purchase is determined by multiplying the number of shared bought by the purchase stock price.

Note: since we are maintaining the purchase price for each stock buy, thing get a little complicated when a portfolio makes two separate purchases of the same stock. Since the purchase price will vary, we may buy the same stock multiple times at different purchase prices. Therefore, each stock purchase will be kept as a separate section of information by your program even if this stock has been previously purchased.

Sell Stock - s <ss> <num>
The user wishes to sell <num> shares of the stock with the symbol <ss>. This command requires that the program has a current (open) stock file and current (open) price file. If either one of these two files are not open, output and appropriate error message and ignore the command. The stock price is to be found in the current price file. If the current price file does not have a listing for that stock symbol, an error message is to be printed and the purchase is to be ignored. The number of shares must be an integer value greater than or equal to zero (selling zero shares allows the portfolio to stop tracking a stock's price changes without actually owning that stock) and less than or equal to the number of shares owned of the stock from a single purchase.

When the stock is sold the current dollar balance of the portfolio must be updated by adding the value of the sale to the current dollar balance of the portfolio. The value of the sale is determined by multiplying the number of shares sold by the stock price from the current (open) price file.

When the portfolio has made multiple purchases of the same stock, your program is to list all purchases whose number of shares owned is greater than or equal to the number of shares to be sold. Each purchase listed should be preceded by an integer value. The user should select which stock purchase it to be used for the sale by typing in the integer value associated with the desired stock purchase. This list of stocks must show:

  1. the stock symbol,
  2. the number of stock owned,
  3. the purchase price,
  4. the purchase value and
  5. the difference between the purchase price and the current price of the stock.

If the sale of the purchase reduces the number of shares owned to zero, information about this purchase (stock symbol, number of shares owned, purchase price, last update price, current price) is to be removed from the portfolio.

Update Current Stock Prices - u
This command is to update the current stock price for all purchased stocks that are known in the current price file. For this command, the previous current stock price is to become the stock price of the last update and the stock price in the current price file is to become the current stock price.

View Price Info - v [-s|-p] [<ss1> [<ss2>] ...]
This command is to allow the user to look at the stock prices contained in the current (open) price file. The stock symbols and associated stock prices are to be displayed to the terminal screen. If a price file is not currently open, an appropriate error message is to be output and ignore the command.

The flags of -s and -p are to specify that the stocks are to be sorted in a specific order before displaying the information to the terminal screen. If neither flag is given, display the information in the order given in the price file. If the -s flag is given sort the information by the stock symbol. If the -p flag is given sort the information by the stock price. If multiple flags are given, an appropriate error message is to be displayed and ignore the command.

By default, the command will display all stock prices given in the file. The user can list any number of stock symbols at the end of the command, i.e. "<ss1> [<ss2>] ...". This will cause only information for these stock symbols to be displayed. If a listed stock symbol is not found in the price file, no information for that stock symbol is to be listed (this does not cause an error).

List Stock Info - l [-s|-n|-p|-v|-g|-d] [<ss1> [<ss2>] ...]
This command is to list information about about the current stock portfolio. If a stock file is not currently open, an appropriate error message is to be output and ignore the command. For each stock purchase, the following items of information is to be listed:
  • The stock symbol
  • The number of shares owned
  • The purchase price
  • The purchase value
  • The last update price
  • The last update value
  • The current price
  • The current value
  • The difference between the current and purchase price
  • The difference between the current and purchase value
  • The difference between the current and last update price
  • The difference between the current and last update value
The value of the stock is determined by multiplying the number of shares owned by the stock price. Obviously, this information will need to displayed on more than one line. You will need to arrange the output is some readable format. Also if the output is more than one screen in length, the output should stop at every screenful until the user presses the enter key to continue.

The flags of -s, -n, -p, -v, -g and -d are to specify the information is to be sorted in a specific order. If no flag is given the stocks are to be listed in the order of the purchases made (newest purchase last). The -s flag indicates the stocks are to be sorted by the stock symbol. The -n flag indicates the stocks are to be sorted by the number of shares owned in increasing order. The -p flag indicates the stocks are to be sorted by the current stock price in increasing order. The -v flag indicates the stocks are to be sorted by the current stock value in increasing order. The -g flag indicates the stocks are to be sorted by the difference between the current price and the purchase price of the stock in increasing order. The -d flag indicates the stocks are to be sorted by the difference between the current value and the purchase value of the stock in increasing order. If multiple flags are given, an appropriate error message is to be displayed and ignore the command.

By default, the command will display all stock purchases given in the portfolio. The user can list any number of stock symbols at the end of the command, i.e. "<ss1> [<ss2>] ...". This will cause only information for these stock symbols to be displayed. If a listed stock symbol is not found in the portfolio, no information for that stock symbol is to be listed (this does not cause an error).

Show Total Value of Portfolio - t
This command is worth 5 points extra credit. The extra credit is because I forgot to add include it with the original write up for the assignment. Also without this command, keeping track of the current dollar balance is of little use since the balance is never used for anything.

This command shows the total value of the Portfolio. If a stock file is not currently open, an appropriate error message is to be output and ignore the command. the command is to list three pieces of information:

  1. The current dollar balance of the portfolio.
  2. The sum of the current value of all stocks owned in the portfolio
  3. The sum of the current dollar balance and the current value of all stocks owned in the portfolio.
All three values are to be written as dollar amounts. They must be preceded by the dollar sign "$" and must have two decimal places after the the floating point. The third item printed will show the total current value of the portfolio.

For this program, you must write all the classes yourself with exception of the string class. You may not use any Standard Template Library classes (except for the string class), RogueWave Classes or any class that you have not typed-in yourself. You may not use C style structs and all instantiated data members of every class must be non-public. In addition to the stockPrice class, you may wish to write a linked list class (perhaps the Queue class from lecture) that has each node containing the information for each stock purchase. The main purpose of this program is to immerse you into class writing. How well you understand how to create and use classes will have a big impact on how well you do on the midterm exam. So use this assignment to prepare yourself for the midterm.

The following is a list of links to some price files. These price files contain information on the stock from the Dow Jones Average from various day in February. The name "dow207" means "Dow from 2/07/2000".

The following link contains a file that shows the company name for each stock symbol for the stocks on the Dow Jones Average. You are not required to include this information in your program, but you may if you wish. The following link shows a sample stock file. The stock files contains information about 9 stock purchases. Five stocks were bought on 2/7: AXP, T, IBM, XOM and DIS (using the information in dow207.pf). One of these "purchases" was for zero shares (XOM). This was done to allow the track of this stock by the program without having to actually by the stock. The stock file was updated on 2/9 and three more purchases were made: PG, CAT and DIS (all using the information from dow209.pf). Note: the stock DIS was bought on both 2/7 and 2/9, so there are two purchases of the stock DIS in the file. The stock file was updated on 2/11 and one purchase was made: MMM (using information from dow211.pf). In the stock file, there is no record of when the stocks were purchased or when the file was updated.

This program will require the use of multiple source code files, separate compilation and the use of a makefile. The division of the subroutines between the multiple source code files must be logical. One suggestion would be to have each class in its own source code file and the command interface commands in another source code file. Note: a "source code file" is not the same as a "header file". Source code files will have a file extension of .cpp (or .c, .C, .CC, etc.), while a header file will have a file extension of .h. Your program must separately compile the source code files and then link them together. Using a #include statement with a source code file will NOT satisfy this requirement.

This program will also require a 1-2 page write up of the data structures used in the program and the logical division of your program into multiple source code files (i.e. which routines are where). Remember that this write-up is to be written in ASCII format and is to be electronically turned in with your program. The name of this file should be "readme.txt". Also recall that your program will be given to another student to write a critique. Therefore, it is suggested that you do not include your Social Security Number in your program. Instead use your name and your EECS User ID to identify yourself.

Your program must be written in good programming style. This includes (but is not limited to) meaningful identifier names, a file header at the beginning of each source code file, a function header at the beginning of the function, proper use of blank lines and indentation to aide in the reading of your code, explanatory "value-added" in-line comments, etc.

The work you turn in must be 100% your own. You are not allowed to share code with any other person (inside this class or not). You may discuss the project with other persons; however, you may not show any code you write to another person nor may you look at any other person's written code.

You are to submit this project using the EECS Department's UNIX machine's turnin command. The project name for this assignment is mp3. Be sure to submit all source code, header files, make file as well as your program description. Failure to turnin all required pieces will result in a lower grade for the assignment.