You are on page 1of 2

Practical Four COMP200 - Practicals 2023

COMP200: Practical 4
This practical aims to develop your understanding of inheritance. It will assess your ability to code solutions
to a given real-world problem, using OOP. It is also a refresher on reading from textfiles.

Stock Market
Download the project “Stocks”. Stocks are shares in a company that an individual owns. Stocks may be
bought and sold on the stock market.

1. Stock
The Stock class keeps track of a single stock. It contains fields for the symbol used to identify the
stock, the number of shares of that stock that the individual owns and the total cost of the shares.
The class contains a toString() method as well as accessor and mutator methods.
The price of stocks changes over time, based on the market. Add instance methods to the Stock
class (i) to buy shares (given the number of shares and the current market price of the share) (ii) sell
shares (given the number of shares and the current market price of the share) and (iii) to calculate the
profit that the owner can get if all shares are sold (given the current market price per share). Hint:
you will need to use both totalShares and totalCost in your calculations.
2. Dividend Stock
Now write a DividendStock class that extends the Stock class. Dividends are profit-sharing payments
that a corporation pays its shareholders on a regular basis (such as annually). The amount that each
shareholder receives is proportional to the number of shares that person owns. Not every stock pays
dividends, so you wouldn’t add this functionality directly to the Stock class. Instead, you should create
a new class called DividendStock that extends Stock to add this new behaviour.
Each DividendStock object will inherit the symbol, total shares, and total cost from the Stock superclass.
You will simply need to (i) add a field totalDividends to record the total amount of the dividends paid
(ii) add a constructor that takes in symbol as a parameter (the totalDividends field can be set to 0
and will be incremented later).
(iii) Add the following behaviour to the DividendStock class:
• Write a payDividend method that increments the total dividends received, given the amount paid
per share. Hint: you will need to use the totalShares field in your calculation.
• The dividend payments that are being recorded should be considered as a profit for the stock-
holder. The overall profit of a DividendStock object is equal to the profit from the Stock price
plus any dividends. Hint: consider method overriding.
3. Application
Now write an application class that creates a list of Stock objects, and carries out transactions as
specified in the text file “transactions.txt”. An example file is:
N appl
D fcebk
D amzn
****
buy appl 100 274.30
buy amzn 60 293.34
sell appl 10 341.20
buy fcebk 120 294.30
sell fcebk 80 314.26
div fcebk 18.42
div amzn 10.78

The first part of the file, separated by **** contains a list of stocks that we own. Each line contains
“N” or “D” followed by the stock symbol. The “N” means it is a normal stock while the “D” means it
is a dividend stock. Tokens on a single line are separated by a tab.

COMP200 1 2023
Practical Four COMP200 - Practicals 2023

Create the stocks and store the Stock objects in an ArrayList (or an array of Stock objects). After the
line with **** is a list of transactions, one per line. The first word on each line is the transaction type:
“buy”, “sell” or “div” (pay a dividend). The “buy” and “sell” lines each contain the stock to buy/sell,
the number of shares to buy and the current price of the stock. The “div” lines contain the number of
dividends to pay per share. After each “sell” transaction your application should print out the profit.
Your application program should (i) create the Stock objects and then (ii) execute the transactions
from the textfile Hint: you will need to create a method that finds the index of the specific stock from
the list of stocks to make sure you execute the transaction on the correct stock. Also, make sure that
you convert correctly from String to numerical types. (iii) create a method calcTotalDividends that
calculates the total dividends paid for ALL shares, taking in the list of stocks as a parameter.
Hint: you will need to use typecasting and the instanceOf operator. (iv) After all the transactions
have been completed, your program should print a report of all stocks, the amount of stock owned,
the total cost and lastly the total amount of dividends that have been received to date.
If you did not edit the textfile given to you your output should look something like this:
Created appl stock object
Created fcebk dividend stock object
Createdamzn dividend stock object
#############################################################################
B o u g h t 100 a p p l s t o c k s at R 2 7 4 .30/ s h a r e
B o u g h t 60 a m z n s t o c k s at R 2 9 3 .34/ s h a r e
S o l d 10 a p p l s t o c k s at R 3 4 1 .20/ s h a r e
P r o f i t i f t h e r e m a i n i n g 90 a p p l s t o c k s a r e s o l d at R 3 4 1 .20/ s h a r e : R 6 6 9 0 .0
B o u g h t 120 f c e b k s t o c k s at R 2 9 4 .30/ s h a r e
S o l d 80 f c e b k s t o c k s at R 3 1 4 .26/ s h a r e
P r o f i t i f t h e r e m a i n i n g 40 f c e b k s t o c k s a r e s o l d at R 3 1 4 .26/ s h a r e : R 2 3 9 5 .20
P a i d f c e b k d i v i d e n d s at R 1 8 .42/ s h a r e
P a i d a m z n d i v i d e n d s at R 1 0 .78/ s h a r e
#############################################################################
F I N A L D E T A I L S OF S T O C K S :
[ s y m b o l = appl , t o t a l S h a r e s =90 , t o t a l C o s t = R 2 4 0 1 8 .00]
[ s y m b o l = f c e b k , t o t a l S h a r e s =40 , t o t a l C o s t = R 1 0 1 7 5 .20]; [ t o t a l D i v i d e n d s = R 7 3 6 .80]
[ s y m b o l = amzn , t o t a l S h a r e s =60 , t o t a l C o s t = R 1 7 6 0 0 .40]; [ t o t a l D i v i d e n d s = R 6 4 6 .80]
T o t a l d i v i d e n d s r e c e i v e d f r o m a l l s t o c k s : R 1 3 8 3 .60

COMP200 2 2023

You might also like