Companies and people often buy and sell stocks Often they bu

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 your queue using Link list and implement your stack using the stack class in java.util. 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 Ticker 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 shares 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 at 64.98 2. 200 shares at 66.72 3. 100 shares at 69.00 4. 180 shares at 71.75. 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.

Solution

PROGRAM CODE:

Purchases.java

package sample;

// This class is for holding the information of number of shares and cost of the purchase.
// Making this a bean class makes it easier to capture the data

public class Purchases {

   int numberOfShares = 0;

   double cost = 0.0;

//constructor for the same. it initializes both the instance variables to the incoming values

   public Purchases(int numberOfShares, double cost) {

       this.numberOfShares = numberOfShares;

       this.cost = cost;

   }

// getters and setters

   public int getNumberOfShares() {

       return numberOfShares;

   }

   public void setNumberOfShares(int numberOfShares) {

       this.numberOfShares = numberOfShares;

   }

   public double getCost() {

       return cost;

   }

   public void setCost(double cost) {

       this.cost = cost;

   }

}

LinkedList.java

package sample;

public class LinkedList {

// this is the Node definition for LinkedList

   class Node {

      public Purchases data; //purchase data in Node.

      public Node next; // points to next Node in list.

  

      public Node(Purchases data){

   this.data = data;

      }

   }

  

   private Node first; // ref to first link on list

     

/**

   * LinkedList constructor

   */

public LinkedList(){

   first = null;

}

// Linked list function to insert data at the end

public void insertLast(Purchases data){

Node newNode = new Node(data); //Creation of New Node.

  

if(first==null){ //means LinkedList is empty, make first point to new Node.

   first=newNode; //first ---> newNode

   return;

}

  

Node tempNode = first; // save reference to first Node in tempNode- so that we could return saved reference.

while(tempNode.next!=null){ //Executes until we don\'t find last Node of LinkedList.

//If next of some Node is pointing to null, that means it\'s a last Node.

   tempNode=tempNode.next; //move to next Node.

}

tempNode.next=newNode; //make last\'s Node next point to new Node

}

// to get the first data from the linked list

public Purchases getFirst()

{

   if(first==null){ //means LinkedList in empty

System.out.println(\"List is empty\");

   }

   Node tempNode = first;

   return tempNode.data; // return tempNode\'s purchase data

}

}

// This class is a bean class for every stock which includes a stack and a queue for purchase prices

StockData.java

package sample;

import java.util.Stack;

public class StockData {

  

   String symbol;

   String name;

   Stack<Purchases> sharePricesStack = new Stack<Purchases>();

   LinkedList QueuePrices = new LinkedList();

// constructor with symbol and name

   public StockData(String symbol, String name) {

       this.symbol = symbol;

       this.name = name;

   }

// adding purchase details to stack and queue

   public void addPurchases(Purchases price)

   {

       sharePricesStack.push(price);

       QueuePrices.insertLast(price);

   }

//setters and getters for the instance varaibles

   public String getSymbol() {

       return symbol;

   }

   public void setSymbol(String symbol) {

       this.symbol = symbol;

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public Stack<Purchases> getsharePricesStack() {

       return sharePricesStack;

   }

   public void setsharePricesStacks(Stack<Purchases> sharePrices) {

       this.sharePricesStack = sharePrices;

   }

   public LinkedList getQueuePrices() {

       return QueuePrices;

   }

   public void setQueuePrices(LinkedList list) {

       this.QueuePrices = list;

   }

  

  

}

// this is the main class

StockAccounting.java

package sample;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.ArrayList;

import java.util.Scanner;

public class StockAccounting {

  

   static ArrayList<StockData> arraylist = new ArrayList<StockData>();

  

   // method to display menu and get the choice from the user

   public static void menu()

   {

       int choice;

       System.out.println(\"\ 1. Enter new stock\ 2. Find FIFO and LIFO dollar cost average\ 3. Quit\");

       System.out.println(\"\ Enter your choice:\");

       Scanner sc = new Scanner(System.in);

       choice = sc.nextInt();

       sc.nextLine();

      

       switch(choice)

       {

       case 1: readStockData();

               menu();

               break;

       case 2: dollarCostAverage();

               menu();

               break;

       case 3: break;

          

       default: System.out.println(\"Wrong choice. Try again.\");

               menu();

       }

   }

  

   //function to read the data from the user.

   public static void readStockData()

   {

       String symbol, name;

       Scanner input;

       StockData data;

       System.out.println(\"Enter the stock symbol: \");

       input = new Scanner(System.in);

       symbol = input.nextLine();

       System.out.println(\"Enter the stock name: \");

       name = input.nextLine();

       data = new StockData(symbol, name);

       char choice = \'y\';

       while(choice == \'Y\' || choice == \'y\')

       {

           int shares;

           double price;

           // reading all the shares and the cost from the user everytime

           System.out.println(\"Enter number of Shares: \");

           shares = input.nextInt();

           System.out.println(\"Enter the cost : \");

           price = input.nextDouble();

           Purchases purchase = new Purchases(shares, price);

           data.addPurchases(purchase);

           System.out.println(\"Do you want to continue entering shares and price? Y or N\");

           choice = input.next().charAt(0);

       }

       arraylist.add(data);

   }

  

   // calculating the average dollar cost using FIFO and LIFO method

   public static void dollarCostAverage()

   {

       Scanner inputSc = new Scanner(System.in);

       double AvgPrice = 0.0;

       System.out.println(\"Enter the stock Symbol: \");

       String stockSymb = inputSc.next();

       System.out.println(\"Enter the number of shares to sell: \");

       int numSellShares = inputSc.nextInt();

       //LIFO starts

       System.out.println(\"LIFO Accounting: Dollar Cost Average - \");

       for(int i=0; i<arraylist.size(); i++)

       {

           if(arraylist.get(i).getSymbol().equals(stockSymb)) // when the symbol matches the user entered symbol

           {

               int sizeOfStack = arraylist.get(i).getsharePricesStack().size(); // getting the size of the stack

               double price = arraylist.get(i).getsharePricesStack().get(sizeOfStack-1).getCost(); // getting the last element of the stack. -1 is because all arrays and vectors start with 0

               int numOfShares = arraylist.get(i).getsharePricesStack().get(sizeOfStack-1).getNumberOfShares(); // getting the number of shares from for the last element in the stack

               AvgPrice = price/numOfShares; // calculating average

           }

       }

       // rounding off to 2 decimal places

       BigDecimal bd = new BigDecimal(AvgPrice*numSellShares);

       bd = bd.setScale(2, RoundingMode.HALF_UP);

       System.out.println(\"$\"+bd.doubleValue());

       //LIFO ends

       //FIFO starts - Doing the same for Queue but the first element is taken

       System.out.println(\"FIFO Accounting: Dollar Cost Average - \");

       for(int i=0; i<arraylist.size(); i++)

       {

           if(arraylist.get(i).getSymbol().equals(stockSymb))

           {

               double price = arraylist.get(i).getQueuePrices().getFirst().getCost();

               int numOfShares = arraylist.get(i).getQueuePrices().getFirst().getNumberOfShares();

               AvgPrice = price/numOfShares;

           }

       }

       //Rounding off to two decimal places

       BigDecimal bdFIFO = new BigDecimal(AvgPrice*numSellShares);

       bdFIFO = bdFIFO.setScale(2, RoundingMode.HALF_UP);

       System.out.println(\"$\"+bdFIFO.doubleValue());

       //FIFO ends

   }

   public static void main(String args[])

   {

       menu(); calling menu function here.

   }

}

OUTPUT:

1. Enter new stock

2. Find FIFO and LIFO dollar cost average

3. Quit

Enter your choice:

1

Enter the stock symbol:

XOD

Enter the stock name:

Exon Mobile

Enter number of Shares:

100

Enter the cost :

92.65

Do you want to continue entering shares and price? Y or N

y

Enter number of Shares:

50

Enter the cost :

97.23

Do you want to continue entering shares and price? Y or N

y

Enter number of Shares:

200

Enter the cost :

84.50

Do you want to continue entering shares and price? Y or N

y

Enter number of Shares:

100

Enter the cost :

86.58

Do you want to continue entering shares and price? Y or N

n

1. Enter new stock

2. Find FIFO and LIFO dollar cost average

3. Quit

Enter your choice:

1

Enter the stock symbol:

D

Enter the stock name:

Dominion Resources

Enter number of Shares:

350

Enter the cost :

64.98

Do you want to continue entering shares and price? Y or N

y

Enter number of Shares:

200

Enter the cost :

66.72

Do you want to continue entering shares and price? Y or N

y

Enter number of Shares:

100

Enter the cost :

69

Do you want to continue entering shares and price? Y or N

y

Enter number of Shares:

180

Enter the cost :

71.75

Do you want to continue entering shares and price? Y or N

n

1. Enter new stock

2. Find FIFO and LIFO dollar cost average

3. Quit

Enter your choice:

2

Enter the stock Symbol:

XOD

Enter the number of shares to sell:

300

LIFO Accounting: Dollar Cost Average -

$259.74

FIFO Accounting: Dollar Cost Average -

$277.95

1. Enter new stock

2. Find FIFO and LIFO dollar cost average

3. Quit

Enter your choice:

2

Enter the stock Symbol:

D

Enter the number of shares to sell:

400

LIFO Accounting: Dollar Cost Average -

$159.44

FIFO Accounting: Dollar Cost Average -

$74.26

1. Enter new stock

2. Find FIFO and LIFO dollar cost average

3. Quit

Enter your choice:

3

 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
 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
 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
 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
 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
 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
 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
 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
 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site