Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Companies and people often buy and sell stocks. Often they buy the same stock fo

ID: 3606171 • Letter: C

Question

Companies and people often buy and sell stocks. Often they buy the same stock for different prices at different times. Say one owns 1000 shares a certain stock (such as Checkpoint), one may have bought the stock in amounts of 100 shares over 10 different times with 10 different prices.

We will analyze two different methods of accounting, FIFO and LIFO accounting used for determining the “cost” of a stock. This is also known as the dollar cost average basis. This information is typically calculated when a stock is sold to determine if a profit or loss was made. In our version of FIFO accounting, the price of a commodity is averaged starting with the first purchase of that item. Say we sell 250 shares of a stock, according to this method the dollar cost averaged purchase price is determined by averaging the prices on the first 250 shares bought. In our version of LIFO accounting, the price of a commodity is averaged starting with the last purchase of that item. Say we sell 250 shares of a stock, according to this method the dollar cost average purchase price is determined by averaging the prices on the last 250 shares bought.

In this assignment, you will be using a queue for storing data for FIFO accounting, and a stack for LIFO accounting. Implement you queue using Link list and implement your stack using arrays. Make sure you use the stack and queue abstract data types (ADT) defined for stacks and queues. Both your stack and queue should have records with the following fields:

The stock Tticker symbol (a string).

The Stock Name ( a string).

The number of shares of a stock (an int).

The purchase price for those shares (decimal).

You can assume that the first element of the structure is the security bought first, the second was bought second, etc.

You can assume that the first element of the structure is the security bought first, the second was bought second, etc.

Your program should have the user able to enter information about various stocks, the amount of shares, and the price. The user can then enter a query about a certain stock and the cost according to the LIFO and FIFO accounting methods for a certain number of shares.

The following could be your menu:

Press 1 to enter a new stock
Press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold.

If 1 is pressed, the user needs to enter the stock symbol, Stock name, the number of shares bought, and the price per share when purchased.

If 2 is pressed, the user needs to enter the stock symbol being queried and the number of shares you wish to sell. Once that information is entered the dollar cost averaged price per share for both the LIFO method and the FIFO method should be displayed.

Implement the program described above using the Java programming language to meet the overall requirements described above. Use the blow data.

Ticker: XOM

Name: Exon Mobile

Purchases in the order below:

1. 100 shares at 92.65

2. 50 sharees at 97.23

3. 200 shares at 84.50

4. 100 shares at 86.58

Ticker: D

Name: Dominion Resources

Purchases in the order below.

1. 350 shares at64.98

2. 200 shares at 66.72

3. 100 shares at 69.00

4. 180 shares at 67.90

After you enter the XOM data have the software calculate and display the dollar cost average basis for both the LIFO and FIFO methods when selling 300 shares.

After you enter the D data have the software calculate and display the dollar cost average basis using both the LIFO and FIFO methods when selling 400 shares.

Use the code below:

package package4;

public class Stock

{

String TickerSymbol; // Exchange Ticker symbol example BP

String EquityName; // Full company name;

double PricePerSharePaid; // Prie pershare paid for this equity in dollars and cents.

int NumberOfSharesBought; //

double TotalPaid; // Total paid including $6.99 comission.

// Constructors

// Default constructor.

public Stock()

{

TickerSymbol = "";

EquityName = "";

PricePerSharePaid = 0.0;

NumberOfSharesBought = 0;

}

public Stock(String TS, String EQNAME, double PPS, int NOS )

{

// Assign passed in values.

this.TickerSymbol=TS;

this.EquityName=EQNAME;

this.NumberOfSharesBought=NOS;

this.PricePerSharePaid=PPS;

this.TotalPaid=this.NumberOfSharesBought*this.PricePerSharePaid;

}

public String getTickerSymbol()

{

return TickerSymbol;

}

public void setTickerSymbol(String tickerSymbol)

{

TickerSymbol = tickerSymbol;

}

public String getEquityName()

{

return EquityName;

}

public void setEquityName(String equityName)

{

EquityName = equityName;

}

public double getPricePerSharePaid()

{

return PricePerSharePaid;

}

public void setPricePerSharePaid(double pricePerSharePaid)

{

PricePerSharePaid = pricePerSharePaid;

}

public int getNumberOfSharesBought()

{

return NumberOfSharesBought;

}

public void setNumberOfSharesBought(int numberOfSharesBought)

{

NumberOfSharesBought = numberOfSharesBought;

}

public double getTotalPaid()

{

return TotalPaid;

}

public void setTotalPaid(double totalPaid)

{

TotalPaid = totalPaid;

}

@Override

public String toString() {

return "Stock [TickerSymbol=" + TickerSymbol + " " + "EquityName=" + EquityName + " " + "PricePerSharePaid="

+ PricePerSharePaid + " " + "NumberOfSharesBought=" + NumberOfSharesBought + " " + "TotalPaid=" + TotalPaid

+ "] ";

}

}

Explanation / Answer

The storing of details is into stack or queue is not clear. We can create a POJO and keep all variables in it. The following implementations of Queue and Stack are generic hence can be utilized in above scenarios

StackArray.java

import java.util.Arrays;

//class StackArray for storing data for LIFO accounting
public class StackArray<T> {

private static final int DEFAULT_CAPACITY = 10;
private T[] store;
private int size = 0;
private int capacity;

public StackArray() {
this.capacity = DEFAULT_CAPACITY;
store = (T[]) new Object[DEFAULT_CAPACITY];
}

public StackArray(int capacity) {
this.capacity = capacity;
store = (T[]) new Object[capacity];
}

public boolean push(T value) {
if (this.size >= store.length) {
int newSize = size + (size >> 1);
store = Arrays.copyOf(store, newSize);
}
store[size++] = value;
return true;
}

public T pop() {
if (size <= 0) {
return null;
}
T value = store[--size];
store[size] = null;
int reducedSize = size;
if (size >= capacity && size < (reducedSize + (reducedSize << 1))) {
System.arraycopy(store, 0, store, 0, size);
}
return value;
}

public int size() {
return size;
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = size - 1; i >= 0; i--) {
sb.append(store[i]);
if (i > 0) {
sb.append(",");
}
}
sb.append("]");
return sb.toString(); }

}

QueueList.java

import java.util.*;

//Class representing each node
class Node<T>{
protected T data;
protected Node link;

public Node(){
link = null;
data = null;
}   
public Node(T d,Node n){
data = d;
link = n;
}   

//Corresponding getter() and setter()
public void setLink(Node n){
link = n;
}   
public void setData(T d){
data = d;
}   

public Node getLink(){
return link;
}
public T getData(){
return data;
}
}

/* Class QueueList for storing data for FIFO accounting */
public class QueueList<T>{
protected Node front, rear;
public int size;

public QueueList(){
front = null;
rear = null;
size = 0;
}   

public boolean isEmpty(){
return front == null;
}   

public int getSize(){
return size;
}   

public void insert(T data){
Node nptr = new Node(data, null);
if (rear == null){
front = nptr;
rear = nptr;
}
else{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}
  
public T remove(){
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
Node ptr = front;
front = ptr.getLink();
if (front == null)
rear = null;
size-- ;
return (T)ptr.getData();
}   

public T peek(){
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return (T)front.getData();
}   

public void display(){
System.out.print(" Queue = ");
if (size == 0) {
System.out.print("Empty ");
return ;
}
Node ptr = front;
while (ptr != rear.getLink() ){
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote