Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Goal: To maintain a list of company stocks and to process a list of transactions. Your task is to maintain a portfolio of stocks you

Goal: To maintain a list of company stocks and to process a list of transactions.

Your task is to maintain a portfolio of stocks you own using a doubly-linked list (with no dummy nodes). Your program will begin by prompting the user (using JOptionPane.showInputDialog()) for the name of the input data file that contains the stocks you own and a list of stock transactions. The data file has the following format:

the first line contains the number of stocks you own. Call this number n.

the next n lines has the form symbol,shares which contains the number of shares of a given stock you own. eg) BCE 500. These lines represent your stock portfolio.

the remaining lines are transactions you wish to perform, which are describe below. Each transaction can be one of the following:

d - this will print the stocks and number of shares that you currently own, one on each line.

b symbol x - this is a transaction to buy x shares of the specified stock given by symbol. eg) b BMO 100.

s symbol x - this is a transaction to sell x shares of the specified stock given by symbol. eg) s RY 200.

Here is an actual sample data file:

5

SU 100

BCE 50

AAPL 10

RY 200

SLF 95

d

b BNS 100

s RY 200

s BMO 1000

d

b SU 250

b RY 100

s AAPL 50

s AAPL 9

Upon opening the data file, your program should read in the value n from the first line, then proceed to read in the next n lines and create a doubly-linked list in which the nodes are ordered by increasing stock symbol. This linked list must not contain any dummy nodes. Once you have read in the your stock portfolio and stored it in a linked list, it is time to read in the transactions and process them one by one. We briefly discuss each operation.

The d operation - This simple dumps the contents of the linked list from beginning to end with one line per node. See sample output below.

The b symb x operation - If the symb (the stock symbol) is already in the linked list, then add x to the current number of shares owned to the corresponding node in the list. Otherwise, if the stock symbol symb does not exists in the linked list, you will have to create a new node for this symbol with x shares and add it into the appropriate place in the linked list.

The s symb x operation - If you do not currently own the stock symb, then you cannot perform this transaction. In this case, you should print the message Error:Cannot perform sell operation. You do not own this stock. If you do currently own this stock but own less than x shares, you cannot perform this operation. In this case, you should print the message Error:Cannot perform sell operation of x shares of stock symbol symb. You only own y shares. Otherwise, you should update the number of shares you own by updating the appropriate node (containing symbol symb) in your linked list (by subtracting x from the number of shares you currently own of that stock). If the number of shares you own for that stock after subtracting x is zero, you must delete this node from your linked list (since you no longer own any shares of this stock).

Note that you are not allowed to change which stock symbol is stored in an existing node. Instead, you must unlink and relink nodes within the list. You must create a new node only when you are adding a stock that does not already exist in the list.

Output

You should strive to make your outputs format match the output given below. Here is the sample output for the data file given above:

Input File:testdata1.txt

****************************************

executing command:d

Contents of Portfolio

AAPL:10 shares.

BCE:50 shares.

RY:200 shares.

SLF:95 shares.

SU:100 shares.

executing command:b BNS 100

executing command:s RY 200

executing command:s BMO 1000

Error: Cannot perform sell operation of stock BMO. You do not own this stock.

executing command:d

Contents of Portfolio

AAPL:10 shares.

BCE:50 shares.

BNS:100 shares.

SLF:95 shares.

SU:100 shares.

executing command:b SU 250

executing command:b RY 100

executing command:s AAPL 50

Error: Cannot perform sell 50 of AAPL. You only own 10 shares.

executing command:s AAPL 9

executing command:d

Contents of Portfolio

AAPL:1 shares.

BCE:50 shares.

BNS:100 shares.

RY:100 shares.

SLF:95 shares.

SU:350 shares.

End of Processing.

Program Organization

You should write a Node class to represent a node in a linked list. Each node will contain a stock symbol (a String) and the number of shares owned (an int) as well as back (to the previous node) and forward (to the next node) pointers.

You should create a class to represent a linked list that will store your stock portfolio. You will also need to implement operations (find, insert, delete, print) to support the maintenance of the linked list. Note that your linked list class should not have any routines that processes a transaction. In other words, the linked list should not be specific to this application (other than the parameters to find,delete has to be a String and the parameters to insert has to be a String and an int). You can introduce additional methods and variables in your linked list class as needed.

testdata1.txt

5 SU 100 BCE 50 AAPL 10 RY 200 SLF 95 d b BNS 100 s RY 200 s BMO 1000 d b SU 250 b RY 100 s AAPL 50 s AAPL 9 d 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions