Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*****Use Java and the method of class and object, constructors, data and methods

ID: 664253 • Letter: #

Question

*****Use Java and the method of class and object, constructors, data and methods 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 implementation is:

import java.util.Scanner;

public class linkstcks {
private int N; // size of the stack
private Node fst; // top of stack

// helper Node class
private class Node {
private String itm;
private Node nxt;
}

// is the stack empty?
public boolean isEmpty() { return fst == null; }

// number of elements on the stack
public int size() { return N; }


// add an element to the stack
public void push(String itm) {
Node oldfst = fst;
fst = new Node();
fst.itm = itm;
fst.nxt = oldfst;
N++;
}

// delete and return the most recently added element
public String pop() {
if (isEmpty()) throw new RuntimeException("Stack underflow");
String itm = fst.itm; // save itm to return
fst = fst.nxt; // delete fst node
N--;
return itm; // return the saved itm
}


// test client
public static void main(String[] args) {
int max = Integer.parseInt(args[0]);
linkstcks s = new linkstcks();
Scanner scipt=new Scanner(System.in);
String stin=scipt.nextLine();
while (!stin.isEmpty()) {
String itm = stin.toString();
if (!itm.equals("-")) s.push(itm);
else if (s.isEmpty())
   System.out.println("wrong Input");
  
else {
   System.out.println(s.pop());
}
}
}


}

Queue Implementation:


import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;


public class queuelink<Item> implements Iterable<Item> {
private int N; // number of elements on queue
private Node fst; // beginning of queue
private Node lst; // end of queue

// helper linked list class
private class Node {
private Item item;
private Node next;
}


public queuelink() {
fst = null;
lst = null;
}
//check for empty
public boolean isEmpty() {
return fst == null;
}

//method to return number of items..
public int size() {
return N;   
}

public int length() {
return N;   
}

//method for peek...
public Item peek() {
if (isEmpty()) throw new RuntimeException("Queue underflow");
return fst.item;
}

//add to Queue
public void enqueue(Item item) {
Node x = new Node();
x.item = item;
if (isEmpty()) { fst = x; lst = x; }
else { lst.next = x; lst = x; }
N++;
}


public Item dequeue() {
if (isEmpty()) throw new RuntimeException("Queue underflow");
Item item = fst.item;
fst = fst.next;
N--;
if (isEmpty()) lst = null; // to avoid loitering
return item;
}

  
public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}


public Iterator<Item> iterator() {
return new ListIterator();
}

// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator<Item> {
private Node current = fst;

public boolean hasNext() { return current != null; }
public void remove() { throw new UnsupportedOperationException(); }

public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}

//main method
public static void main(String[] args) {
queuelink<String> q = new queuelink<String>();
  
Scanner scipt=new Scanner(System.in);
String stin=scipt.nextLine();
  
while (!stin.isEmpty()) {
String item = stin;
if (!item.equals("-")) q.enqueue(item);
else if (!q.isEmpty())
   System.out.println(q.dequeue()+" ");
}
System.out.println("(" + q.size() + " left on queue)");
}
}