Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Controller.java import java.util.LinkedList; import java.util.Scanner; public class Controller { public Controller() { LinkedList googList = new LinkedList(); LinkedList amazList = new LinkedList(); Scanner input =
Controller.java import java.util.LinkedList; import java.util.Scanner; public class Controller { public Controller() { LinkedList googList = new LinkedList(); LinkedList amazList = new LinkedList(); Scanner input = new Scanner(System.in); do { System.out.print("Enter 1 for Google stock or 2 for Amazon, 3 to quit: "); int stockSelect = input.nextInt(); if(stockSelect == 3) break; System.out.print("Input 1 to buy, 2 to sell: "); int controlNum = input.nextInt(); System.out.print("How many stocks: "); int quantity = input.nextInt(); if(controlNum == 1) { System.out.print("At what price: "); double price = input.nextDouble(); if(stockSelect == 1) { Controller.buyStock(googList, "Google", quantity, price); } else Controller.buyStock(amazList, "Amazon", quantity, price); } else { System.out.print("Press 1 for LIFO accounting, 2 for FIFO accounting: "); controlNum = input.nextInt(); if(controlNum == 1) { if(stockSelect == 1) Controller.sellLIFO(googList, quantity); else Controller.sellLIFO(amazList, quantity); } else { if(stockSelect == 1) Controller.sellFIFO(googList, quantity); else Controller.sellFIFO(amazList, quantity); } } } while(true); input.close(); } public static void buyStock(LinkedList list, String name, int quantity, double price) { Stock temp = new Stock(name,quantity,price); list.push(temp); System.out.printf("You bought %d shares of %s stock at $%.2f per share %n", quantity, name, price); } public static void sellLIFO(LinkedList list, int numToSell) { // You need to write the code to sell the stock using the LIFO method (Stack) // You also need to calculate the profit/loss on the sale double total = 0; // this variable will store the total after the sale double profit = 0; // the price paid minus the sale price, negative # means a loss System.out.printf("You sold %d shares of %s stock at %.2f per share %n", numToSell, list.element().getName(), totalumToSell); System.out.printf("You made $%.2f on the sale %n", profit); } public static void sellFIFO(LinkedList list, int numToSell) { // You need to write the code to sell the stock using the FIFO method (Queue) // You also need to calculate the profit/loss on the sale double total = 0; // this variable will store the total after the sale double profit = 0; // the price paid minus the sale price, negative # means a loss System.out.printf("You sold %d shares of %s stock at %.2f per share %n", numToSell, list.element().getName(), totalumToSell); System.out.printf("You made $%.2f on the sale %n", profit); } }
Stock.java public class Stock { private String name; private int quantity; private double price; public Stock() { this.name = "undefined"; this.quantity = 0; this.price = 0.0; } public Stock(String name, int quantity, double price) { this.name = name; this.quantity = quantity; this.price = price; } public Stock(String name) { this.name = name; this.quantity = 0; this.price = 0.0; } public Stock(int quantity) { this.name = "undefined"; this.quantity = quantity; this.price = 0.0; } public Stock(double price) { this.name = "undefined"; this.quantity = 0; this.price = price; } public Stock(String name, int quantity) { this.name = name; this.quantity = quantity; this.price = 0.0; } public Stock(String name, double price) { this.name = name; this.quantity = 0; this.price = price; } public Stock(int quantity, double price) { this.name = "undefined"; this.quantity = quantity; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String toString() { return "Stock: " + this.getName() + " Quantity: " + this.getQuantity() + " Price: " + this.getPrice(); } }
Driver.java
public class Driver { public static void main(String[] args) { System.out.println("Java Stock Exchange"); new Controller(); } }
Incomplete Stock Analysis Application Take over an existing software project. The previous developer has been fired, and this project has now been assigned to you. The project is nearly done, you will simply complete it Companies and people often buy and sell stocks. Often, they buy the same stock for different prices at different times. Say a person owns 1000 shares a certain stock (such as Google) they 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 information is typically calculated when a stock is sold to determined if a profit or loss was made. In our version of FIFO accounting, the price of a stock is averaged starting with the first purchase of that item. Say we sell 250 shares of a stock, according to this method the 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 purchase price is determined by averaging the prices on the last 250 shares bought. We decided to use a queue for storing data for FIFO accounting, and a stack for LIFO accounting. We used a linked list for the implementation of the stack and queue. We decided to test the system with just two stocks, Google and Amazon We built a simple command line interface to test our program design. Both our stack and queue have Stock objects with the following fields: - The name of the stock - The number of shares - The purchase price You can assume that the first element of the structure is the stock bought first, the second was bought second, etc. Bob who just finished his part wrote the user interface. He made it so the user is able to enter information about various stocks, the number 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. It's a simple command line interface that goes something like this: "Enter 1 for Google stock or 2 for Amazon, 3 to quit:" Next, the system will prompt, Input 1 to buy, 2 to sell:" After that we get a prompt to enter "How many stocks:" the user wants to work with. If the user is placing an order to buy, they will enter the number of shares and the price. If the user wants to sell, the user needs to enter the number of shares and the selling price. Bob forgot to ask for the selling price. He is only asking for the number of stocks. He left out a lot of the calculations as well. We fired him because he didn't put any error checking in the program. A user can attempt to sell a stock they haven't bought. Right now, this is causing a runtime exception. We hired you to fix these issues and make some minor modifications to the features. 1a. Add the missing functionality in Controller.java to finish the sellLIFO and sellFIFO methods. 1b. Provide error checking so users can't sell a stock they don't own or try to sell more stock than they own. Eliminate that Runtime exception. 1c. Change the user input, so the user enters the stock name they wish to buy or sell instead of just choosing between Google and Amazon. Keep the command line interface since we are only in the testing phase. Eventually a GUI can be added. Remember, the project design dictates the layout and design of the application. You are not redesigning this project, so don't change ANY other methods or objects. You may only edit the Controller.java file. The other files, Stock.java and Driver.java, are for your reference and testing. DO NOT make any changes to these two support objects. Incomplete Stock Analysis Application Take over an existing software project. The previous developer has been fired, and this project has now been assigned to you. The project is nearly done, you will simply complete it Companies and people often buy and sell stocks. Often, they buy the same stock for different prices at different times. Say a person owns 1000 shares a certain stock (such as Google) they 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 information is typically calculated when a stock is sold to determined if a profit or loss was made. In our version of FIFO accounting, the price of a stock is averaged starting with the first purchase of that item. Say we sell 250 shares of a stock, according to this method the 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 purchase price is determined by averaging the prices on the last 250 shares bought. We decided to use a queue for storing data for FIFO accounting, and a stack for LIFO accounting. We used a linked list for the implementation of the stack and queue. We decided to test the system with just two stocks, Google and Amazon We built a simple command line interface to test our program design. Both our stack and queue have Stock objects with the following fields: - The name of the stock - The number of shares - The purchase price You can assume that the first element of the structure is the stock bought first, the second was bought second, etc. Bob who just finished his part wrote the user interface. He made it so the user is able to enter information about various stocks, the number 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. It's a simple command line interface that goes something like this: "Enter 1 for Google stock or 2 for Amazon, 3 to quit:" Next, the system will prompt, Input 1 to buy, 2 to sell:" After that we get a prompt to enter "How many stocks:" the user wants to work with. If the user is placing an order to buy, they will enter the number of shares and the price. If the user wants to sell, the user needs to enter the number of shares and the selling price. Bob forgot to ask for the selling price. He is only asking for the number of stocks. He left out a lot of the calculations as well. We fired him because he didn't put any error checking in the program. A user can attempt to sell a stock they haven't bought. Right now, this is causing a runtime exception. We hired you to fix these issues and make some minor modifications to the features. 1a. Add the missing functionality in Controller.java to finish the sellLIFO and sellFIFO methods. 1b. Provide error checking so users can't sell a stock they don't own or try to sell more stock than they own. Eliminate that Runtime exception. 1c. Change the user input, so the user enters the stock name they wish to buy or sell instead of just choosing between Google and Amazon. Keep the command line interface since we are only in the testing phase. Eventually a GUI can be added. Remember, the project design dictates the layout and design of the application. You are not redesigning this project, so don't change ANY other methods or objects. You may only edit the Controller.java file. The other files, Stock.java and Driver.java, are for your reference and testing. DO NOT make any changes to these two support objects
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started