Question: Write a java program (StockTransaction.java) that uses double linked lists to calculate capital gains of stocks. You are NOT allowed to use any of the

Write a java program (StockTransaction.java) that uses double linked lists to calculate capital gains of stocks. You are NOT allowed to use any of the Java Collections Framework classes such as Java ArrayList, LinkedList, HashMap, etc to implement this. You will read in two input text files stocks.txt and transactions.txt

Name your main program as StockTransaction.java

Sample Runs

Java StockTransaction

--successfully read stocks.txt and transactions.txt

Please enter a input stock quote for realized gain(or loss) for the stock : AAPL

Congratulations, your realized gain for Apple Inc. is : $150

Java StockTransaction

--successfully read stocks.txt and transactions.txt

Please enter a input stock quote for realized gain(or loss) for the stock : FB

Sorry, your realized loss for Facebook Inc. is : $500

Text Files

The stocks.txt file contains stock codes and descriptions in the following format:

AAPL;Apple Inc.

IBM; International Business Machines Corp.

KO; The Coca-Cola Company

FB; Facebook Inc.

SBUX;Starbucks Corp.

The transactions.txt file contains different transactions in chronological order a user conducts on shares. The format will be as follows

AAPL;buy;10;$450

KO;buy;100;$30

IBM;buy;50;$150

FB;buy;10;$30

AAPL;sell;5;$480

KO;sell;50;$50

FB;buy;30;$40

IBM;buy;50;$50

FB;sell;20;$10

SBUX;sell;30;$50

Implement the following codes

public class DList {

protected DLLNode header;

protected DLLNode trailer;

protected int size;

public DList() {

header = null;

trailer = null;

size = 0;

}

/**

* @return the header

*/

public DLLNode getHeader() {

return header;

}

/**

* @return the trailer

*/

public DLLNode getTrailer() {

return trailer;

}

/**

* @return the size

*/

public int getSize() {

return size;

}

// add element to the front of the list

public void addToFront(T elem) {

DLLNode newNode = new DLLNode(elem);

if (header == null) {

header = newNode;

}

if (trailer == null)

trailer = newNode;

else {

newNode.setLink(header);

header.setBack(newNode);

header = newNode;

}

size++;

}

// add element to the end of the list

public void addToLast(T elem) {

DLLNode newNode = new DLLNode(elem);

newNode.setLink(null);

if (trailer == null) {

trailer = newNode;

header = newNode;

} else {

trailer.setLink(newNode);

newNode.setBack(trailer);

trailer = newNode;

}

size++;

}

public boolean isEmpty() {

return header == null;

}

public String toString() {

DLLNode node = header;

String result = "";

while(node!= null) {

result+=node.getInfo();

result+="<=>";

node = (DLLNode)node.getLink();

}

result = result.substring(0,result.length()-3);

return result;

}

public void print() {

DLLNode v = header;

while (v != null) {

System.out.println(v.getInfo());

v = (DLLNode) v.getLink();

}

}

//2) Define a recursive method, boolean search (T element) in DList

//class that returns true if the element is

// found in the double linked list.

public boolean search(T element) {

return search (header,element);

}

private boolean search(DLLNode node, T element) {

if(node == null)

return false;

if(node.getInfo().equals(element))

return true;

else

return search((DLLNode) node.getLink(),element);

}

//Given the DList class, implement a void removeLast() method

//which removes the last node in the double linked list

public void removeLast() {

if (isEmpty())

throw new IllegalStateException("cannot remove from empty DList");

DLLNode u = trailer.getBack();

u.setLink(null);

trailer = u;

}

// implement stack operations void push(T element) and T pop()

public void push(T element) {

DLLNode newNode = new DLLNode(element);

addToLast(element);

}

public T pop() {

return null;

}

}

2

public class DLLNode extends LLNode {

private DLLNode back;

public DLLNode(T info)

{

super(info);

back = null;

}

public void setBack(DLLNode back)

// Sets back link of this DLLNode.

{

this.back = back;

}

public DLLNode getBack()

// Returns back link of this DLLNode.

{

return back;

}

}

3

public class LLNode{

private LLNode link;

private T info;

public LLNode(T info)

{

this.info = info;

link = null;

}

public void setInfo(T info)

// Sets info of this LLNode.

{

this.info = info;

}

public T getInfo()

// Returns info of this LLONode.

{

return info;

}

public void setLink(LLNode link)

// Sets link of this LLNode.

{

this.link = link;

}

public LLNode getLink()

// Returns link of this LLNode.

{

return link;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!