Use Java for this assignment. You will be using a queue for storing data for FIF
ID: 664272 • Letter: U
Question
Use Java for 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 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 below data.
Ticker:XOM
Name:Exon Mobile
Purchases in the order below.
100 shares at 92.65
50 sharees at 97.23
200 shares at 84.50
100 shares at 86.58
Ticker:D
Name:Dominion Resources
Purchases in the order below.
350 shares at 64.98
200 shares at 66.72
100 shares at 69.00
180 shares at 67.90
Formula : Multiply shares of the stock by the purchase of those shares and divide by total shares of stock. Suppose a user wants to calculate for 550 shares in Dominion Resources. To calculate for stack (LIFO, start from the top of the stack) you would (350(64.98) + 200(66.72)) / 500 and for queue (FIFO, start from the bottom of the stack) you would (180(67.90) +100(69.00) + 200(66.72) + 70(64.98)) / 550
Explanation / Answer
Stack:
package accounts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class StackDemo1{
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public String name;
public StackDemo1(String name)
{
this.name=name;
}
public void main() throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in));
BufferedReader br1=new BufferedReader(
new InputStreamReader(System.in));
Scanner scan=new Scanner(System.in);
System.out.println("enter the size of stack shares in LIFO");
int size=scan.nextInt();
int share[]=new int[size];
int sharecost[]=new int[size];
int top=-1,ch,top1=-1;
do
{
System.out.println("1.Push a share");
System.out.println("2.Pop a share");
System.out.println("3.Display all shares");
System.out.println("4.Display stock share value");
System.out.println("5.Exit");
System.out.print("Enter your choice :-");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
if(top<size-1)
{
System.out.print("Enter share :-");
share[++top]=Integer.parseInt(br.readLine());
System.out.print("Enter share cost :-");
sharecost[++top1]=Integer.parseInt(br1.readLine());
}
else
System.out.println("Stack overflow...");
break;
case 2:
if(top == -1)
System.out.println("Stack underflow....");
else
System.out.println("Poped share and its cost is :-"+share[top--]+":::::"+sharecost[top--]);
break;
case 3:
if(top == -1)
System.out.println("Share Stack underflow....");
else
{
for(int i=0;i<=top;i++)
{
System.out.print("Share is"+share[i]+" and its cost is "+sharecost[i]+" ");
}
System.out.println();
}
break;
case 4:
if(top == 0)
System.out.println("Share Stack underflow....");
else
{
double shareValue=0.0;
double totalshareCost=0.0;
for(int i=0;i<=top;i++)
{
shareValue=shareValue+(share[i]*sharecost[i]);
//System.out.print("Share is"+share[i]+" and its cost is "+sharecost[i]+" ");
}
System.out.println("Final Share value of stock is:: "+shareValue/top);
}
break;
case 5:
System.out.println("Thanks For ....");
break;
default:
System.out.println("Invalid choice enter....");
}
}while(ch != 5);
}
}
QUEUE:
package accounts;
import java.util.*;
/* Class Node */
class Node
{
protected int data;
protected Node link;
/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}
/* Class linkedQueue */
class linkedQueue
{
protected Node front, rear;
public int size;
/* Constructor */
public linkedQueue()
{
front = null;
rear = null;
size = 0;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == null;
}
/* Function to get the size of the queue */
public int getSize()
{
return size;
}
/* Function to insert an element to the queue */
public void insert(int data)
{
Node nptr = new Node(data, null);
if (rear == null)
{
front = nptr;
rear = nptr;
}
else
{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}
/* Function to remove front element from the queue */
public int remove()
{
if (isEmpty() )
throw new NoSuchElementException("FIFO SHARE Underflow Exception");
Node ptr = front;
front = ptr.getLink();
if (front == null)
rear = null;
size-- ;
return ptr.getData();
}
/* Function to check the front element of the queue */
public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return front.getData();
}
/* Function to display the status of the queue */
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();
}
}
class linkedQueue1
{
protected Node front, rear;
public int size;
/* Constructor */
public linkedQueue1()
{
front = null;
rear = null;
size = 0;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == null;
}
/* Function to get the size of the queue */
public int getSize()
{
return size;
}
/* Function to insert an element to the queue */
public void insert(int data)
{
Node nptr = new Node(data, null);
if (rear == null)
{
front = nptr;
rear = nptr;
}
else
{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}
/* Function to remove front element from the queue */
public int remove()
{
if (isEmpty() )
throw new NoSuchElementException("FIFO SHARE Underflow Exception");
Node ptr = front;
front = ptr.getLink();
if (front == null)
rear = null;
size-- ;
return ptr.getData();
}
/* Function to check the front element of the queue */
public int peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return front.getData();
}
/* Function to display the status of the queue */
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();
}
}
/* Class LinkedQueueImplement */
public class LinkedQueueImplement
{
public String name;
public LinkedQueueImplement(String name)
{
this.name=name;
}
public void main()
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedQueue */
linkedQueue lq = new linkedQueue();
linkedQueue1 lq1 = new linkedQueue1();
/* Perform Queue Operations */
System.out.println("Linked Queue Test ");
char ch;
do
{
System.out.println(" Queue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter Share to insert");
lq.insert( scan.nextInt() );
System.out.println("Enter Share cost to insert");
lq1.insert( scan.nextInt() );
break;
case 2 :
try
{
System.out.println("Removed share Element = "+ lq.remove());
System.out.println("Removed share cost Element = "+ lq1.remove());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Share Peek Element = "+ lq.peek());
System.out.println("Share cost Peek Element = "+ lq1.peek());
}
catch (Exception e)
{
System.out.println("Error : " + e.getMessage());
}
break;
case 4 :
System.out.println("Share Empty status = "+ lq.isEmpty());
System.out.println("Share Cost Empty status = "+ lq1.isEmpty());
break;
case 5 :
System.out.println("Size os shares= "+ lq.getSize());
break;
case 6 :
if(lq.isEmpty())
{
System.out.println("No elements.........");
}
else
{
System.out.println(lq.peek()*lq1.peek()/lq.getSize());
}
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* display queue */
lq.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
MAIN:
package accounts;
import java.io.IOException;
import java.util.Scanner;
public class Main {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner scan=new Scanner(System.in);
System.out.println("Eneter LIFO name:::::");
String name1=scan.next();
System.out.println("Eneter FIFO name:::::");
String name2=scan.next();
LinkedQueueImplement linkedQueueImplement=new LinkedQueueImplement(name2);
StackDemo1 stackDemo1=new StackDemo1(name1);
stackDemo1.main();
linkedQueueImplement.main();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.