Question
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
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.
Its 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 didnt put any error checking in the program. A user can attempt to sell a stock they havent 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 sellLIFO and sellFIFO methods.
2. Provide error checking so users cant sell a stock they dont 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 dont 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.
Bottom is the imcomplete Controller.java:
// 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(), total/numToSell); 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(), total/numToSell); System.out.printf("You made $%.2f on the sale %n", profit); } }
Please answer in Java language, Thank you!!
-------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the Stock
// This is the Stock object that represents the concept of a stock // This object is finished and has passed all testing. // Do not make any changes to this object, its perfect as-is.
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(); } }
--------------------------------------------------------------------------------------------------------------------------------------------------
Here is Driver
// This is the Main class that starts the program. // This object is finished and has passed all testing. // Do not make any changes to this object, its perfect as-is.
public class Driver {
public static void main(String[] args) { System.out.println("Java Stock Exchange"); new Controller(); }
}
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