Companies and people often buy and sell stocks. Often they buy the same stock fo
ID: 663973 • 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.
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.
Explanation / Answer
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.
When a stack is first created, it contains no items.
A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:
]import java.util.*;
public class ManageSingleStock{
ObjectStack stack;
public ManageSingleStock(){ //constructor
stack=new ObjectStack();
}
public void addStockPurchase(StockPurchase sp){ //add
stack.push(sp);
}
public double getLIFOCost(int num_shares){ //stack
double cost = 0;
int num_needed = num_shares;
while(num_needed > 0){ //loop until remaining shares needed not equals to zero
StockPurchase sp = (StockPurchase)stack.peek();
if(num_needed >= sp.numOfshares){ //case where we need shares more than the available shares in peek()
cost += sp.numOfshares*sp.purchase_price;
num_needed -= sp.numOfshares;
stack.pop();
}
else{ //case where we need shares less than the available shares in peek()
cost += num_needed*sp.purchase_price;
sp.numOfshares -= num_needed;
num_needed = 0;
}
}
cost = cost/num_shares; //calculate the average cost
return cost;
}
public double getFIFOCost(int num_shares){ //queue
double cost = 0;
int num_needed = num_shares;
ObjectStack stack1 = new ObjectStack();
//stack the stack => queue
while(!stack.isEmpty())
stack1.push(stack.pop());
//same like before
while(num_needed > 0){
StockPurchase sp = (StockPurchase)stack1.peek();
if(num_needed >= sp.numOfshares){
cost += sp.numOfshares*sp.purchase_price;
num_needed -= sp.numOfshares;
stack.pop();
}
else{
cost += num_needed*sp.purchase_price;
sp.numOfshares -= num_needed;
num_needed = 0;
}
}
cost = cost/num_shares;
//stack the queue => stack
while(!stack1.isEmpty())
stack.push(stack1.pop());
return cost;
}
public String toString(){
stack.trimToSize();
return stack.toString();
}
public static void main(String[]args){
ManageSingleStock st=new ManageSingleStock();
Scanner in=new Scanner(System.in);
st.addStockPurchase( new StockPurchase("A", 100, 500));
st.addStockPurchase( new StockPurchase("B", 3, 1000));
st.addStockPurchase( new StockPurchase("C", 4, 250));
System.out.print("Do you want to getFIFOCOST(Press 1) or getLIFOCOST(Press 2) ?");
int num=in.nextInt();
System.out.println();
if(num==1){
double x =st.getFIFOCost(5);
System.out.println("FIFO: " +x );
System.out.println(st);
}
if(num==2){
double y=st.getLIFOCost(5);
System.out.println("LIFO: "+y);
System.out.println(st);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.