javaLinked list
COMP 2140 Assignment 2: Linked Lists
Ben Li
Due: Friday February 9 at 11:59 p.m.
Objective
The objective of this assignment to program a linked list and use it in an application.
How to Get Help
Your course instructor is helpful: I am available during my office hours at 10:30 a.m. to Noon Mon/Wed in E2 479 EITC (or by appointment). Please DO NOT email me with questions about the assignment. Instead, post them on the discussion forum and I will answer it there. Remember, no question is a stupid question. If you don’t understand something, then ask. There are probably other students that have the same questions that you have.
Course discussion groups on UM Learn: A discussion group for this assignment is available in the COMP 2140 course site on UM Learn (click on “Discussions” under “Communications”). Post questions and comments related to Assignment 2 there, and I will respond. Please do not post solutions, not even snippets of solutions there, or anywhere else. I strongly suggest that you read the assignment discussion group for helpful information.
Computer science help centre: The staff in the help centre can help you (but not give you assignment solutions!). See their website at http://www.cs.umanitoba.ca/undergraduate/computer-science-help-centre. php for location and hours. You can also email them at [email protected].
Programming Standards
When writing code for this course, follow the programming standards, available on this course’s website on UMLearn. Failure to do so will result in the loss of marks.
Question
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:
1
• 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.
2
Output
You should strive to make your output’s format match the output given below. Here is the sample output for the data file given above:
Comp 2140 Assignment 2 Winter 2018
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.
3
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.
Finally, you need to write your application class with nameA2<your last name><your student id> (e.g., A2Wong1234567) that contains the main method and any other additional methods you need to read in the input, and process the input, using your linked list to keep track of your stock portfolio.
Hand-in Instructions
Go to COMP2140 in UM Learn, then click “Dropbox” under “Assessments” at the top. You will find a dropbox folder called “Assignment 2”. (If you cannot see the assignment dropbox, follow the directions under “Honesty Declaration” below.) Click the link and follow the instructions. Please note the following:
• Submit ONE .java file only. The .java file must contain all the source code (note that it is possible to define multiple classes in a single .java file). The .java file must be named A2<your last name><your student id>.java (e.g., A2Wong1234567.java).
• Please do not submit anything else.
• You can submit as many times as you like, but only the most recent submission is kept.
• We only accept homework submissions via UM Learn. Please DO NOT try to email your homework to the instructor or TA or markers — it will not be accepted.
• We reserve the right to refuse to grade the homework or to deduct marks if you do not follow instruc- tions.
• Late assignments will NOT be accepted.
Test Data
The input file testdat1.txt (given above) will be provided initially. More data files and sample outputs will be provided later on.
4