Companies and people often buy and sell stocks Often they bu
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









