SlideShare a Scribd company logo
1 of 7
Download to read offline
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 sharePricesStack = new Stack();
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 getsharePricesStack() {
return sharePricesStack;
}
public void setsharePricesStacks(Stack 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 arraylist = new ArrayList();
// 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

More Related Content

Similar to Companies and people often buy and sell stocks. Often they buy the sa.pdf

Reaction StatisticsBackgroundWhen collecting experimental data f.pdf
Reaction StatisticsBackgroundWhen collecting experimental data f.pdfReaction StatisticsBackgroundWhen collecting experimental data f.pdf
Reaction StatisticsBackgroundWhen collecting experimental data f.pdffashionbigchennai
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdfvenud11
 
Java codes output preliminary level
Java codes output preliminary levelJava codes output preliminary level
Java codes output preliminary levelDr. Vignes Gopal
 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfComputer Programmer
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2ppJ. C.
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxSahajShrimal1
 
This is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdfThis is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdffeetshoemart
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and FunctionsJake Bond
 
computer notes - Reference variables
computer notes -  Reference variablescomputer notes -  Reference variables
computer notes - Reference variablesecomputernotes
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2Warui Maina
 
Oop lect3.pptx
Oop lect3.pptxOop lect3.pptx
Oop lect3.pptxMrMudassir
 
Warehouse InventoriesObjectiveWork with multiple objects and re.pdf
Warehouse InventoriesObjectiveWork with multiple objects and re.pdfWarehouse InventoriesObjectiveWork with multiple objects and re.pdf
Warehouse InventoriesObjectiveWork with multiple objects and re.pdfanandacorp
 
solidity programming.pptx
solidity programming.pptxsolidity programming.pptx
solidity programming.pptxRohiniBagul4
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical HackingBCET
 
Lecture 6 polymorphism
Lecture 6 polymorphismLecture 6 polymorphism
Lecture 6 polymorphismthe_wumberlog
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comBaileya126
 

Similar to Companies and people often buy and sell stocks. Often they buy the sa.pdf (20)

Fewd week5 slides
Fewd week5 slidesFewd week5 slides
Fewd week5 slides
 
Reaction StatisticsBackgroundWhen collecting experimental data f.pdf
Reaction StatisticsBackgroundWhen collecting experimental data f.pdfReaction StatisticsBackgroundWhen collecting experimental data f.pdf
Reaction StatisticsBackgroundWhen collecting experimental data f.pdf
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 
Java codes output preliminary level
Java codes output preliminary levelJava codes output preliminary level
Java codes output preliminary level
 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 
This is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdfThis is a java lab assignment. I have added the first part java re.pdf
This is a java lab assignment. I have added the first part java re.pdf
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
computer notes - Reference variables
computer notes -  Reference variablescomputer notes -  Reference variables
computer notes - Reference variables
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
 
Oop lect3.pptx
Oop lect3.pptxOop lect3.pptx
Oop lect3.pptx
 
Warehouse InventoriesObjectiveWork with multiple objects and re.pdf
Warehouse InventoriesObjectiveWork with multiple objects and re.pdfWarehouse InventoriesObjectiveWork with multiple objects and re.pdf
Warehouse InventoriesObjectiveWork with multiple objects and re.pdf
 
solidity programming.pptx
solidity programming.pptxsolidity programming.pptx
solidity programming.pptx
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical Hacking
 
Lecture 6 polymorphism
Lecture 6 polymorphismLecture 6 polymorphism
Lecture 6 polymorphism
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.com
 

More from rydeberghal13313

Configuration management is particularly important during the transit.pdf
Configuration management is particularly important during the transit.pdfConfiguration management is particularly important during the transit.pdf
Configuration management is particularly important during the transit.pdfrydeberghal13313
 
Cholesterol is an essential component of cellular membranes. It is a(.pdf
Cholesterol is an essential component of cellular membranes. It is a(.pdfCholesterol is an essential component of cellular membranes. It is a(.pdf
Cholesterol is an essential component of cellular membranes. It is a(.pdfrydeberghal13313
 
A correlation coefficient computed for n = 18 and a 10 significance.pdf
A correlation coefficient computed for n = 18 and a 10 significance.pdfA correlation coefficient computed for n = 18 and a 10 significance.pdf
A correlation coefficient computed for n = 18 and a 10 significance.pdfrydeberghal13313
 
1.Using the Internet or other sources to find the definition of CASE.pdf
1.Using the Internet or other sources to find the definition of CASE.pdf1.Using the Internet or other sources to find the definition of CASE.pdf
1.Using the Internet or other sources to find the definition of CASE.pdfrydeberghal13313
 
A fluid of constant density rho enters a duct of width W and height h.pdf
A fluid of constant density rho enters a duct of width W and height h.pdfA fluid of constant density rho enters a duct of width W and height h.pdf
A fluid of constant density rho enters a duct of width W and height h.pdfrydeberghal13313
 
A) Know the four basic tissues, how they differ, and what their gene.pdf
A) Know the four basic tissues, how they differ, and what their gene.pdfA) Know the four basic tissues, how they differ, and what their gene.pdf
A) Know the four basic tissues, how they differ, and what their gene.pdfrydeberghal13313
 
A three point cross was performed using fruit flies. The three loci .pdf
A three point cross was performed using fruit flies. The three loci .pdfA three point cross was performed using fruit flies. The three loci .pdf
A three point cross was performed using fruit flies. The three loci .pdfrydeberghal13313
 
A console lamp in the cabin of a spaceship appears green when the shi.pdf
A console lamp in the cabin of a spaceship appears green when the shi.pdfA console lamp in the cabin of a spaceship appears green when the shi.pdf
A console lamp in the cabin of a spaceship appears green when the shi.pdfrydeberghal13313
 
7. Whats the meaning of these transportation symbols IT Soluti.pdf
7. Whats the meaning of these transportation symbols IT Soluti.pdf7. Whats the meaning of these transportation symbols IT Soluti.pdf
7. Whats the meaning of these transportation symbols IT Soluti.pdfrydeberghal13313
 
A virus is 50 nm in size. Would you recommend using a stereomicrosco.pdf
A virus is 50 nm in size. Would you recommend using a stereomicrosco.pdfA virus is 50 nm in size. Would you recommend using a stereomicrosco.pdf
A virus is 50 nm in size. Would you recommend using a stereomicrosco.pdfrydeberghal13313
 
A division has 12 employees, 8 males and 4 females.Supose that the.pdf
A division has 12 employees, 8 males and 4 females.Supose that the.pdfA division has 12 employees, 8 males and 4 females.Supose that the.pdf
A division has 12 employees, 8 males and 4 females.Supose that the.pdfrydeberghal13313
 
A broth can have more than one type of growth pattern. Explain thi.pdf
A broth can have more than one type of growth pattern. Explain thi.pdfA broth can have more than one type of growth pattern. Explain thi.pdf
A broth can have more than one type of growth pattern. Explain thi.pdfrydeberghal13313
 
2. A safety net can stand the fall of the total of 2700 pounds of obj.pdf
2. A safety net can stand the fall of the total of 2700 pounds of obj.pdf2. A safety net can stand the fall of the total of 2700 pounds of obj.pdf
2. A safety net can stand the fall of the total of 2700 pounds of obj.pdfrydeberghal13313
 
(Linux)A) What is a fixup A) What is Source tree dir. and where.pdf
(Linux)A) What is a fixup A) What is Source tree dir. and where.pdf(Linux)A) What is a fixup A) What is Source tree dir. and where.pdf
(Linux)A) What is a fixup A) What is Source tree dir. and where.pdfrydeberghal13313
 
Cobol Error Its states that my patron-line wasnt defined as a data .pdf
Cobol Error Its states that my patron-line wasnt defined as a data .pdfCobol Error Its states that my patron-line wasnt defined as a data .pdf
Cobol Error Its states that my patron-line wasnt defined as a data .pdfrydeberghal13313
 

More from rydeberghal13313 (15)

Configuration management is particularly important during the transit.pdf
Configuration management is particularly important during the transit.pdfConfiguration management is particularly important during the transit.pdf
Configuration management is particularly important during the transit.pdf
 
Cholesterol is an essential component of cellular membranes. It is a(.pdf
Cholesterol is an essential component of cellular membranes. It is a(.pdfCholesterol is an essential component of cellular membranes. It is a(.pdf
Cholesterol is an essential component of cellular membranes. It is a(.pdf
 
A correlation coefficient computed for n = 18 and a 10 significance.pdf
A correlation coefficient computed for n = 18 and a 10 significance.pdfA correlation coefficient computed for n = 18 and a 10 significance.pdf
A correlation coefficient computed for n = 18 and a 10 significance.pdf
 
1.Using the Internet or other sources to find the definition of CASE.pdf
1.Using the Internet or other sources to find the definition of CASE.pdf1.Using the Internet or other sources to find the definition of CASE.pdf
1.Using the Internet or other sources to find the definition of CASE.pdf
 
A fluid of constant density rho enters a duct of width W and height h.pdf
A fluid of constant density rho enters a duct of width W and height h.pdfA fluid of constant density rho enters a duct of width W and height h.pdf
A fluid of constant density rho enters a duct of width W and height h.pdf
 
A) Know the four basic tissues, how they differ, and what their gene.pdf
A) Know the four basic tissues, how they differ, and what their gene.pdfA) Know the four basic tissues, how they differ, and what their gene.pdf
A) Know the four basic tissues, how they differ, and what their gene.pdf
 
A three point cross was performed using fruit flies. The three loci .pdf
A three point cross was performed using fruit flies. The three loci .pdfA three point cross was performed using fruit flies. The three loci .pdf
A three point cross was performed using fruit flies. The three loci .pdf
 
A console lamp in the cabin of a spaceship appears green when the shi.pdf
A console lamp in the cabin of a spaceship appears green when the shi.pdfA console lamp in the cabin of a spaceship appears green when the shi.pdf
A console lamp in the cabin of a spaceship appears green when the shi.pdf
 
7. Whats the meaning of these transportation symbols IT Soluti.pdf
7. Whats the meaning of these transportation symbols IT Soluti.pdf7. Whats the meaning of these transportation symbols IT Soluti.pdf
7. Whats the meaning of these transportation symbols IT Soluti.pdf
 
A virus is 50 nm in size. Would you recommend using a stereomicrosco.pdf
A virus is 50 nm in size. Would you recommend using a stereomicrosco.pdfA virus is 50 nm in size. Would you recommend using a stereomicrosco.pdf
A virus is 50 nm in size. Would you recommend using a stereomicrosco.pdf
 
A division has 12 employees, 8 males and 4 females.Supose that the.pdf
A division has 12 employees, 8 males and 4 females.Supose that the.pdfA division has 12 employees, 8 males and 4 females.Supose that the.pdf
A division has 12 employees, 8 males and 4 females.Supose that the.pdf
 
A broth can have more than one type of growth pattern. Explain thi.pdf
A broth can have more than one type of growth pattern. Explain thi.pdfA broth can have more than one type of growth pattern. Explain thi.pdf
A broth can have more than one type of growth pattern. Explain thi.pdf
 
2. A safety net can stand the fall of the total of 2700 pounds of obj.pdf
2. A safety net can stand the fall of the total of 2700 pounds of obj.pdf2. A safety net can stand the fall of the total of 2700 pounds of obj.pdf
2. A safety net can stand the fall of the total of 2700 pounds of obj.pdf
 
(Linux)A) What is a fixup A) What is Source tree dir. and where.pdf
(Linux)A) What is a fixup A) What is Source tree dir. and where.pdf(Linux)A) What is a fixup A) What is Source tree dir. and where.pdf
(Linux)A) What is a fixup A) What is Source tree dir. and where.pdf
 
Cobol Error Its states that my patron-line wasnt defined as a data .pdf
Cobol Error Its states that my patron-line wasnt defined as a data .pdfCobol Error Its states that my patron-line wasnt defined as a data .pdf
Cobol Error Its states that my patron-line wasnt defined as a data .pdf
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

Companies and people often buy and sell stocks. Often they buy the sa.pdf

  • 1. 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.
  • 2. 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.
  • 3. 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
  • 4. 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 sharePricesStack = new Stack(); 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() {
  • 5. return name; } public void setName(String name) { this.name = name; } public Stack getsharePricesStack() { return sharePricesStack; } public void setsharePricesStacks(Stack 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 arraylist = new ArrayList(); // 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.
  • 6. 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') {
  • 7. 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