Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java. .java files mentioned below. ControllerJava // The Stock program is following the MVC design template and this is our controller object. // The

In Java. .java files mentioned below.

image text in transcribed

image text in transcribed

ControllerJava

// The Stock program is following the MVC design template and this is our controller object. // The main functionality for buying and selling the stocks are in this controller object. // This is the ONLY file you may edit

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); } }

Assignment 1: You are hired to fix a program that someone else wrote. The company fired this person and hired you to take over their work. Luckily the program is small, simple and almost done. The Incomplete Stock Analysis Application 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) he/she 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 of the stock 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. Specifically, here is what we want you to do. 1. Add the missing functionality in "controller.java" to finish the "sellLIED." and "sellFIED." methods. 2. 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. 3. We think its sort of limited to only work with just two stocks. You need to 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 we will have you create a GUI for this program but right now, we prefer a simple command line interface. 4. We like the layout and design of the application so don't change any of the other methods or objects. Only edit the Controller.java file. We have given you the other files (Stock.java and Driver.java) for your reference and testing. DO NOT make any changes to these two support objects. 5. When you are done, just give me the finished Controller.java file. Assignment 1: You are hired to fix a program that someone else wrote. The company fired this person and hired you to take over their work. Luckily the program is small, simple and almost done. The Incomplete Stock Analysis Application 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) he/she 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 of the stock 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. Specifically, here is what we want you to do. 1. Add the missing functionality in "controller.java" to finish the "sellLIED." and "sellFIED." methods. 2. 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. 3. We think its sort of limited to only work with just two stocks. You need to 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 we will have you create a GUI for this program but right now, we prefer a simple command line interface. 4. We like the layout and design of the application so don't change any of the other methods or objects. Only edit the Controller.java file. We have given you the other files (Stock.java and Driver.java) for your reference and testing. DO NOT make any changes to these two support objects. 5. When you are done, just give me the finished Controller.java file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

Prove Equation (5.22).

Answered: 1 week ago