CS101: Homework 9: Stock Portfolios

Due at start of your Lab during the Week of April 23-27

Pledge: You may work on this pledged assignment work in groups of two or three. Your partners must be from your same lab section. If you violate the section rule, your grade on the assignment will be cut in half. Your group may discuss the content of this assignment and your approach with anyone, but your group must do all if its own work. The group must create the logic of the solution, type the code, and compile, execute, and debug its own program. Your group may not use any code developed by anyone else. Your group's source code submitted electronically must match exactly the code you deliver in your lab section. Only one member of the group should submit the program electronically and only one printed copy should be handed in.  The header comment section of your program should identify all members of the group. A person cannot be added to a group retroactively. So make sure your name is listed as part of your group's submission.

Summary of the Problem

In this program you implement abstract data types (ADTs) that could be the basis for a system that lets a user keep track of what stocks he or she owns in one or more portfolios. (There are numerous systems like this on the Web.) A user may have one or more portfolios, each with a name to identify it and a set of one or more "holdings". Each stock holding includes the ticker symbol for the stock (e.g. "MSFT" for Microsoft), the number of shares the user owns, the purchase price, and the current price. (And some more info we'll describe later.)

Note that there are two abstract data types here that work together: a Portfolio is composed of (or has many) Holdings. Many software systems you will build involve more than one ADT that are used together to make something larger.

We have just described the data (or information) encapsulated in the two ADTs, but what operations will be defined for each? Here's an overview. There will be the usual set of constructors, inspectors, mutators, insertion, and extraction functions. We will also need a functions to add and delete Holdings in a Portfolio, and a function to sell shares in a stock the user owns in a Portfolio. (We won't buy shares of a stock we already own, which let's us avoid some messy accounting.. Who's buying these days, with the stock market like it is?!) We'll need a function to update a stock's price in a Portfolio. Finally, we'll have a function to display the relative proportions of each stock's value in a Portfolio (so we can see if it's "balanced"). This will also show which of our stocks are the biggest losers and biggest gainers. More details on all of these is given elsewhere.

For this assignment, we'll specify the class interfaces that you must build in terms of the member and auxiliary functions. But we'll allow you to decide on how to store data inside each class. For example, you can choose between using arrays or vectors. You have other choices, too. Some inspectors can either return a desired value that is calculated from other data members each time the inspector is called, or store that value in a separate data member. (For example, the GetValue() member function for a Holding may simply multiply the number of shares by the current price without having a data member named Value.)

You'll gain experience using arrays or vectors by doing this assignment. A Portfolio must store a collection of Holdings, so you can choose to use either an array or a vector inside Portfolio. Your choice will be hidden to any client software using your Portfolio class because it will have to use the public interface of Portfolio, which hides the internal details of implementation. Don't forget this is the fundamental principle behind important computing concepts like Abstract Data Types, information hiding, and encapsulation. If you do this right, we'll be able to test your program with our testing client program, no matter how you implement this. (We'll need your version of the header file, of course, so don't forget to turn in four files this time: two header files, and two implementation files.)

Examples of Object Contents When Printed

If you're still a little confused about what's in a Holding and what's in a Portfolio, then here is some sample output from a working version of the program. In this example, we have a Portfolio with the name "BlueChips" and it has four elements in its collection of stock Holding objects. We we call the Insertion operator on this Portfolio object, we might see this:

Portfolio: BlueChips
LLY   300  Cur. Price: $ 76.50  Value:      $22950.00
           Pur. Price: $ 77.00  Gain/Loss:  $ -150.00
KO    250  Cur. Price: $ 45.00  Value:      $11250.00
           Pur. Price: $ 44.00  Gain/Loss:  $  250.00
HD    100  Cur. Price: $ 41.00  Value:      $ 4100.00
           Pur. Price: $ 42.00  Gain/Loss:  $ -100.00
GE    100  Cur. Price: $ 45.50  Value:      $ 4550.00
           Pur. Price: $ 44.00  Gain/Loss:  $  150.00
Total value:     $ 42850.00
Total gain/loss: $   150.00

There are two lines of output for each Holding. The first Holding is 300 shares of Eli Lilly (the pharmaceutical company that makes Prozac, ticker symbol LLY). It was bought at $77 and now is priced at $76.50. The 300 shares are now worth $22,950, and the investor has lost $150 since buying it.

You can see the output showing the stock holdings of Coca Cola (KO), Home Depot (HD), and General Electric (GE). Not that the insertion operator for Portfolio also prints the total value of the Portfolio (the sum of the values of all the Holdings), and the overall gain (or loss).

You'll be very happy to know that we are giving you the Insertion operator for Holding -- it's that code that prints each Holding in nice columns!

Files to Get You Started

Here are individual files you can use to get started. Also, after this list is a self-extracting archive with the Microsoft Visual C++ project already defined. (That's definitely easier to use.)

Important:: The two header files do not have any data members! You have to decide how to implement this part of each class, and then write member functions that access these data members properly.
The two implementation files have empty function "skeletons" for each member function defined in the header file. You have to complete the body of these functions. (A few are already implemented for you.)

Finally, file hw9.exe is a self-extracting archive which creates a folder (subdirectory) named stocks that includes all of these files, plus it has a Visual C++ project ready to go. Run this to unpack the files, and then click on the file stocks.dsw to start up VC++.

What Should Each Function Do?

The implementation files contain detailed comments at the start of each function skeleton telling you exactly what that function should do. For example, here are two examples of function skeletons:

// GetTotalValue (inspector) ---------------------------------- GetTotalValue() 
// Purpose: This returns that grand total of all the Holdings values.
//    Each Holding's value is the number of stocks times the current price.
//
double Portfolio::GetTotalValue() const {
}


// UpdatePrice ---------------------------------------------- UpdatePrice()
// Purpose: This function searches the Portfolio for a Holding that
//   matches the first argument "ticker".  If found that Holding's
//   current price is updated using the second argument "price".
// Return value: If the Holding is found, return true. If not, return false.
// Errors detected:  If following error is detected, print
//   an appropriate message and return false.
//   Error 1:  Second argument is negative.
//
bool Portfolio::UpdatePrice(const string& ticker, double price) {
}

For functions that print output, the comments give examples of the output. If you're still confused, you should be able to student the output of the stocks.exe file and the driver.cpp file to see what some of the operations do. If you have any questions, email Prof. Horton at horton@virginia.edu

Working in Teams

You have 12 small member functions to write for class Holding, and about 5 small and 7 medium-sized functions to write for class Portfolio. Clearly students working in teams should sit down and make some plans at the beginning for to split up this work. Of course you should sit together first and decide how to implement the data members in each class! You can't write any code until you do that.

If you do split up the coding, we'll tell you that the following functions are among the largest or trickiest.
Portfolio::AddHolding
Portfolio::DeleteHolding
Portfolio::Insert
Portfolio::DisplayProportions

Another strategy that works well for small teams is for one person to code and for the other to check that code for correctness right away by either executing it or just by reading and studying it carefully. This second person may be working with a driver program (like the one given above) to test each function as it is written.

Bottom line: Working in teams is most effective if you spend a little time at the beginning planning how to make the best use of your team members. Try it.

Extra Credit

The function Portfolio::DisplayProportions prints information about each stock Holding in a Portfolio object. The information printed is the ticker and two percentages. (See the implementation file for details.) The Holding information is printed in whatever order it's found in the Portfolio (which could be random).

For extra credit, write a sorting member function for class Portfolio that sorts the Holdings by non-increasing Holding value (i.e. number of shares times current price). By non-increasing we mean of course that the Holding with the largest value is printed first. Call your sorting function inside DisplayProportions() so that its output comes out in sorted order.

Those of you using vectors are not allowed to use the built-in sorting functions for C++ container classes. (Sorry.)

Other Requirements