Use Java and the method of class and object, constructors, data and methods for
ID: 664251 • Letter: U
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
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; public class StocksTrading { public static void main(String[] args) { System.out.println("Welcome to the stock trading menu!"); System.out.println("-----------------------------"); System.out.println("Press 1 to enter a new stock"); System.out.println("Press 2 to find the LIFO and FIFO price for a stock."); System.out.println("Press 3 to quit"); Stack stockTradingStack = new Stack(); Queue stockTradingQueue = new LinkedList(); Scanner scanner = new Scanner(System.in); while(true){ String option = scanner.nextLine(); System.out.println(option); switch(option){ case "1": { System.out.println("Enter stock symbol, no of shares and price :"); System.out.println("Ex: PNQ 20 13.5"); String record = scanner.nextLine(); StringTokenizer stringTokenizer = new StringTokenizer(record); StockRecord stockRecord = new StockRecord((String)stringTokenizer.nextElement(), (String)stringTokenizer.nextElement(), (String)stringTokenizer.nextElement()); stockTradingStack.push(stockRecord); stockTradingQueue.offer(stockRecord); break; } case "2": { System.out.println("Enter stock symbol and no. of shares:"); String record = scanner.nextLine(); StringTokenizer stringTokenizer = new StringTokenizer(record); String[] input = new String[2]; input[0] = (String)stringTokenizer.nextElement(); input[1] = (String)stringTokenizer.nextElement(); Integer shares = new Integer(input[1]); StockRecord recStack = stockTradingStack.pop(); System.out.println("Average price by FIFO:" + new Float(shares.intValue()/recStack.priceRec).toString()); StockRecord recQueue = stockTradingQueue.poll(); System.out.println("Average price by LIFO:" + new Float(shares.intValue()/recQueue.priceRec).toString()); break; } case "3": { System.out.print("Existing program ..... "); scanner.close(); System.exit(0); } } } } private static class StockRecord{ private String nameRec; private int sharesRec; private float priceRec; StockRecord(String name, String shares, String price){ nameRec=name; Integer sharesInt = new Integer(shares); sharesRec = sharesInt.intValue(); Float priceFloat = new Float(price); priceRec = priceFloat.floatValue(); } public String getName(){ return nameRec; } @Override public String toString(){ return "Record: " + " Name-"+nameRec + " shares-"+sharesRec + " price-"+priceRec; } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.