Honor and Grading Rules for this Assignment:
This assignment asks you to develop three versions of a Java program on your own. However, you have will be part of a group of 3 or 4 students who will be your support group for this assignment. Anyone in the group can advise, debug, suggest, or help in anyway. The one thing that is not allowed is for someone to give or write large sections of code for another person.
If you need help and no one in your group is available, then you are allowed to get the same kind of help from anyone else in the class.
The group signs off at the end of this assignment that your code works reasonably well. You'll also sign off that you were responsible for its development within the rules stated here.
Your grade for this assignment will be "pass/fail" based on your completing this. Later a quiz or exam will test your knowledge of the concepts that you should have learned in this assignment.
In this program you will use Java to 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.
In addition to the data members described above, we will define the following operations for each of the ADTs. There will be the usual set of constructors, inspectors, mutators, and "support" functions. We will also need a function 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 lets 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.
For this assignment, we'll give you the signatures of the class methods (AKA member functions) and you will need to implement the classes in Java. You will be responsible for deciding the fields (AKA data members) internal mechanics of each class. We expect you to make good design decisions about this, so think!
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 C++ 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. When we call the Insertion operator on this Portfolio object, we might see this:
Portfolio: BlueChipsLLY 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. (Share price examples were reasonably accurate back in 1999!)
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).
Isn't this output pretty? Formatted output in Java is not the easiest thing in the world, so we will not require your output to meet any standards for good formatting.
Here are signatures and comments for the class members you must write. You can use the comments below if you want to add comments, but this is not required for this assignment. Note that Holding is intended to be an immutable class, but Portfolio is not. You'll also need a toString() method for each class, and maybe some other things too. If you have questions about the class definitions, email Prof. Horton at horton@virginia.edu
// Constructor
// Create a new Holding object using a stock's sticker symbol, the
// number of shares, and the price paid at purchase, and its current
// price (the two prices could be the same value)
public Holding(String tick, int shares, double purcPrice, double currPrice)
// GetTicker (inspector)
// Returns the value of the Holding's stock ticker symbol.
public String getTicker()
// GetNumShares (inspector)
// Returns the value of the number of shares in the Holding.
public int getNumShares()
// GetCurrPrice (inspector)
// Returns the current price of the stock stored in the Holding.
public double getCurrPrice()
// GetPurcPrice (inspector)
// Returns the purchase price that we paid for the stock in the Holding.
public double getPurcPrice()
// GetValue
// Returns the total value of the stock Holding (based on
// these current price and the number of shares).
public double getValue()
// updateNumShares
// Returns a new Holding with an updated number of shares
public Holding updateNumShares(int num)
// updateCurrPrice()
// Returns a new Holding with an updated current price
public Holding updateCurrPrice(double price)
// GetGainLoss
// Returns the gain (a positive number) or the loss (a negative number)
// for the stock Holding. This is based on the current total value
// compared to the total value when it was purchased.
public double getGainLoss()
// Constructor
// Create a new Portfolio object given the name you want to use to
// identify the object.
public Portfolio(String name)
// GetName (inspector)
public String getName()
// getNumHoldings
// Returns the number of stock Holdings contained in the Portfolio.
public int getNumHoldings()
// FindHolding
// Purpose: This function searches for a Holding in the Portfolio that
// has the parameter "ticker" as its ticker symbol.
// Return value: The function returns a reference to the Holding, or
// null if no match is found
public Holding findHolding(String ticker)
// 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.
public double getTotalValue()
// 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.
public boolean updatePrice(String ticker, double newPrice)
// SellShares
// Purpose: This function searches the Portfolio for a Holding that
// matches the first argument "ticker". If found then that Holding's
// number of shares is updated by subtracting the second argument "shares"
// to reflect that this number of shares has been sold by the user.
// Also, if this sells all the shares, remove the Holding from the Portfolio.
// Return value: If the Holding is found and no error occurs, return true.
// If not, return false.
// Errors detected: If either of the following errors is detected, print
// an appropriate message and return false.
// Error 1: Second argument is negative. (Impossible to sell a negative
// number of shares.)
// Error 2: Selling the specified number of shares would leave the remaining
// number of shares in the holding less than zero.
//
public boolean sellShares(String ticker, int shares)
// AddHolding
//
// Purpose: A new Holding for the stock given as the first parameter is added
// to the Portfolio. The parameter is a Holding object.
// NOTE: This function makes no guarantee about what position the new Holding
// will occupy. (It may be in the first position, the last, or anywhere.)
// Errors detected: If any of the following errors are detected, the
// function prints an appropriate message about the error, no
// new Holding is added, and the function returns false.
// Error 1: The number of shares is less than zero.
// Error 2: The price for the stock is less than zero.
// Error 3: It is impossible for the Portfolio to store any more Holdings.
// Error 4: There is already a Holding in the Portfolio with this ticker.
//
public boolean addHolding(Holding h)
// DeleteHolding
// Purpose: The Holding with the given ticker is removed from the Portfolio.
// Return value: If the ticker cannot be found in the Portfolio, return false.
// If it can and the Holding is removed, return true.
// NOTE: When this function removes a Holding, the remaining Holdings may
// or may not be in their original order. (The function does not guarantee
// that it preserves the original order.)
//
public boolean deleteHolding(String ticker)