Program 5 - Stock Portfolio Management System

From Maryville College CS Wiki
Jump to: navigation, search


Introduction

Congratulations! That you've made it this far is impressive. Let's see if you can go just a little bit farther...

Every programmer must leave the safe confines of cute little logic problems from time to time, descend the ivory tower, and write some sort of application for the use of the unexceptional rabble below. (We do this mainly out of a desire to continue eating and wearing clothes.) For you, that moment is now! You will write an application which will manage a stock portfolio.

We have seen bits and pieces of this application from the very beginning, and the wiser among you will look back through your labs and your class examples for usable code. This program will basically use everything that we've learned this semester, and so it will be a good way to review for your final exam!

So let's dig in!

General Requirements

First, some general requirements for this program. These will apply throughout the specification, so keep these in mind.

  1. All numeric inputs require validation. This includes validating for both type and range. (like with iofun.h!)
  2. Your solution must be object oriented.
  3. The program must save its state when it exits and restore it when it runs again.
  4. You must adhere to best practices for your code. Style and naming will count!

The Application

When you run this program, you will be greeted with a menu:

   Stock Portfolio Management System
       Please Make a Selection
   1 -- Buy a Stock
   2 -- Sell a Stock
   3 -- Report Current Holdings
   4 -- Report Gains and Losses
   5 -- Remove a Current Holding
   6 -- Done! (quit) 
   
   Choice? 1

You should format this menu nicely, and validate its input. The menu should repeat itself until the user selects option 6, and it should never get into an infinite loop!

Buy A Stock

When the user buys a stock, they should be prompted for the date. if the stock does not yet exist, they should be asked for the company name:

 Date> 8/20
 Symbol> AAPL
 Company> Apple Inc.
 Quantity> 100
 Price> 214.58

Also notice that company names can contain spaces!

However, if we buy more shares of the same stock, it should not prompt for the company name:

 Date> 9/6
 Symbol> AAPL
 Quantity> 15
 Price> 210.50

Also, when we purchase a stock, the system will maintain an average price of each stock. All purchases and sales will be logged!

Selling A Stock

When you sell a stock, the system asks for the date and the stock. If the stock is not in your current holdings, it will simply say that it is not found.

 Date> 11/11
 Symbol> AAC
 Stock not found

However, if it does find it, it facilitates the sale.

 Date> 11/11
 Symbol> AAPL
 You have 65 shares.
 Quantity> 20
 Price> 262.20

Reporting Current Holdings

This report displays a nicely formatted list showing the stocks currently owned in your portfolio:

 Symbol  Company                  Qty      Avg Price     Held Value
 ==================================================================
 TYC     Tyco International        25          40.00        1000.00
 IBM     IBM                       10         129.17        1291.70
 AAPL    Apple Inc.                65         213.64       13886.50
 ==================================================================
                                        Total Value:       16178.20

Please note that the price displayed here is an average price. This is the basis for computing gains and losses when a stock is sold.

So when a stock is purchased, what you must do is figure out the total amount spent on your current holdings. Then you will divide this by the total number of stocks held. When you sell a stock, you will track the gain or the loss based on the current average price of the stocks in your portfolio.

Reporting Gains and Losses

Gains and losses show a history of your sales and how much money you gained or lost on each sale.

 Date      Symbol       Qty     Sell Price      Gain/Loss
 ========================================================
 9/3       AAPL          50         204.17        -520.50
 11/11     AAPL          20         262.20         971.23
 11/11     IBM           10         135.87          67.00
 11/11     TYC           25          45.59         139.75
 ========================================================
                          Total Gain/Loss:         657.48

Removing a Current Holding

This option removes all record of a stock from the system. This includes current holdings, as well as the gains and losses brought on by that stock.

 Symbol> TYC
 This will completely remove any record of this stock.
 Are you sure you want to do this (y/n)? y
 Deleted

Note that this should ask the user if they are sure. If they answer 'y', then it deletes the stock. Otherwise the deletion is cancelled.

Some Design Notes

Clearly, this is a rather large program. When constructing this, you'll want to think about all the objects and methods that the program will needed. I created the following classes:

  • Stock
  • Transaction
  • Portfolio

This is not the only way to organize it, but you will definitely want to do a little object oriented analysis to prepare for this. The listing of my source files follows:

 iofun.cpp
 iofun.h
 main.cpp
 portfolio.cpp
 portfolio.h
 stock.cpp
 stock.h
 transaction.cpp
 transaction.h

You will also need to invent some sort of file format to store your portfolio. What I did was I stored all of my transactions in a file. Then, when the program loads, it plays back these transactions to get the portfolio back into its state.

Extra Credit Opportunity

You may have noticed that the stock holdings are not sorted. If you make them sort alphabetically, I will give you some extra credit.

You can also earn extra credit by adding date validation and/or some advanced date handling (like sorting, or date-based gain loss analysis.) You can also earn extra credit by making more reports, or adding interesting features to the program.

Sample Run

You can run my sample application by executing the following command on the server:

 stock-solution

This will also create a file in your current directory so you can investigate my file format a bit.

Here is a sample run using my solution to this program. The run concludes with a listing of my data file.

 $ ./stock
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 1
 
 Date> 6/27
 Symbol> TYC
 Company> Tyco International
 Quantity> 25
 Price> 40.00
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 1
 
 Date> 8/15
 Symbol> IBM
 Company> IBM
 Quantity> 10
 Price> 129.17
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 1
 
 Date> 8/20
 Symbol> AAPL
 Company> Apple Inc.
 Quantity> 100
 Price> 214.58
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 3
 
 Symbol  Company                  Qty      Avg Price     Held Value
 ==================================================================
 TYC     Tyco International        25          40.00        1000.00
 IBM     IBM                       10         129.17        1291.70
 AAPL    Apple Inc.               100         214.58       21458.00
 ==================================================================
                                        Total Value:       23749.70
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 2
 
 Date> 9/3
 Symbol> AAPL
 You have 100 shares.
 Quantity> 50
 Price> 204.17
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 1
 
 Date> 9/6
 Symbol> AAPL
 Quantity> 15
 Price> 210.50
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 3
 
 Symbol  Company                  Qty      Avg Price     Held Value
 ==================================================================
 TYC     Tyco International        25          40.00        1000.00
 IBM     IBM                       10         129.17        1291.70
 AAPL    Apple Inc.                65         213.64       13886.50
 ==================================================================
                                        Total Value:       16178.20
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 2
 
 Date> 11/11
 Symbol> AAC
 Stock not found
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 2
 
 Date> 11/11
 Symbol> AAPL
 You have 65 shares.
 Quantity> 20
 Price> 262.20
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 2
 
 Date> 11/11
 Symbol> IBM
 You have 10 shares.
 Quantity> 10
 Price> 135.87
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 2
 
 Date> 11/11
 Symbol> TYC
 You have 25 shares.
 Quantity> 25
 Price> 45.59
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 4
 
 Date      Symbol       Qty     Sell Price      Gain/Loss
 ========================================================
 9/3       AAPL          50         204.17        -520.50
 11/11     AAPL          20         262.20         971.23
 11/11     IBM           10         135.87          67.00
 11/11     TYC           25          45.59         139.75
 ========================================================
                          Total Gain/Loss:         657.48
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 6
 
 $ ./stock
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 3
 
 Symbol  Company                  Qty      Avg Price     Held Value
 ==================================================================
 AAPL    Apple Inc.                45         213.64        9613.73
 ==================================================================
                                        Total Value:        9613.73
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 3
 
 Symbol  Company                  Qty      Avg Price     Held Value
 ==================================================================
 AAPL    Apple Inc.                45         213.64        9613.73
 ==================================================================
                                        Total Value:        9613.73
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 4
 
 Date      Symbol       Qty     Sell Price      Gain/Loss
 ========================================================
 9/3       AAPL          50         204.17        -520.50
 11/11     AAPL          20         262.20         971.23
 11/11     IBM           10         135.87          67.00
 11/11     TYC           25          45.59         139.75
 ========================================================
                          Total Gain/Loss:         657.48
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 5
 
 Symbol> TYC
 This will completely remove any record of this stock.
 Are you sure you want to do this (y/n)? y
 Deleted
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 4
 
 Date      Symbol       Qty     Sell Price      Gain/Loss
 ========================================================
 9/3       AAPL          50         204.17        -520.50
 11/11     AAPL          20         262.20         971.23
 11/11     IBM           10         135.87          67.00
 ========================================================
                          Total Gain/Loss:         517.73
 
     Stock Portfolio Management System
         Please Make a Selection
     1 -- Buy a Stock
     2 -- Sell a Stock
     3 -- Report Current Holdings
     4 -- Report Gains and Losses
     5 -- Remove a Current Holding
     6 -- Done! (quit) 
 
     Choice? 6
 
 $ cat STOCKS.DAT 
 8/15
 IBM
 IBM
 0
 10
 129.17
 8/20
 AAPL
 Apple Inc.
 0
 100
 214.58
 9/3
 AAPL
 Apple Inc.
 1
 50
 204.17
 9/6
 AAPL
 Apple Inc.
 0
 15
 210.5
 11/11
 AAPL
 Apple Inc.
 1
 20
 262.2
 11/11
 IBM
 IBM
 1
 10
 135.87