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:
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 “sellLIFO” and “sellFIFO” 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.
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();
}
}
Following major fixes/changes have been done :
private static void sellStock(boolean lifo, LinkedList list, String name, int numToSell, double price)Following is the final Controller.java file:
// 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.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Scanner; public class Controller { public Controller() { Map stockLists = new HashMap(); Scanner input = new Scanner(System.in); do { System.out.printf("%n%nEnter name of stock, -1 to quit:"); String stockSelect = input.next(); if(stockSelect.equals("-1")) break; System.out.print("Input 1 to buy, 2 to sell: "); int controlNum = input.nextInt(); if((controlNum != 1) && (controlNum != 2)) { continue; } System.out.print("How many stocks: "); int quantity = input.nextInt(); /* * CHANGE-1 : * ========== * * Ask for the price, irrespective. */ if(true) { System.out.print("At what price: "); double price = input.nextDouble(); if(stockLists.containsKey(stockSelect) == false) { stockLists.put(stockSelect, new LinkedList()); } if(controlNum == 1) { Controller.buyStock((LinkedList) stockLists.get(stockSelect), stockSelect, quantity, price); } else if(controlNum == 2) { System.out.print("Press 1 for LIFO accounting, 2 for FIFO accounting: "); controlNum = input.nextInt(); if(controlNum == 1) { Controller.sellLIFO((LinkedList) stockLists.get(stockSelect), stockSelect, quantity, price); } else { Controller.sellFIFO((LinkedList) stockLists.get(stockSelect), stockSelect, quantity, price); } } else { break; } } } while(true); input.close(); } public static void buyStock(LinkedList list, String name, int quantity, double price) { Stock temp = new Stock(name,quantity,price); /* * CHANGE-2 : * ========== * * Use "addLast" method, to explicitly specify that the latest stock is at the end of list. */ list.addLast(temp); System.out.printf("You bought %d shares of %s stock at $%.2f per share %n", quantity, name, price); } /* * The "list" here consists of linked-list of stock of "name". * The list is of the form : * * Block(1) <-> Block(2) <-> Block(3) <-> ..... Block(n) * * * * Each block(i) contains "count(i)" number of shares of "name", each of unit-price "price(i)" * * * * When dealing with LIFO, we start checking from the last-block. * When dealing with FIFO, we start checking from the first-block. * */ private static void sellStock(boolean lifo, LinkedList list, String name, int numToSell, double price) { double profit = 0; // the price paid minus the sale price, negative # means a loss double totalShareCost = 0; int stocksFetchingCountRemaining = numToSell; while(stocksFetchingCountRemaining > 0) { /* * Which block of stock to process, depends on whether we are using LIFO or FIFO. * Rest all processing remains same. */ Stock stock = null; if(lifo) { /* * LIFO case */ try { stock = (Stock) list.getLast(); } catch (Exception e) { } } else { /* * FIFO case */ try { stock = (Stock) list.getFirst(); } catch (Exception e) { } } if(stock == null) { System.out.printf("Out of stock for [%s] %n", name); break; } int currentStockQuantity = stock.getQuantity(); double currentStockPrice = stock.getPrice(); if(currentStockQuantity >= stocksFetchingCountRemaining) { /* * The latest stock-block gave us the remaining count. */ stock.setQuantity(currentStockQuantity - numToSell); totalShareCost = totalShareCost + (stocksFetchingCountRemaining * currentStockPrice); stocksFetchingCountRemaining = 0; break; } else { totalShareCost = totalShareCost + (currentStockQuantity * currentStockPrice); /* * The latest stock-block has been exhausted, we must keep looking for more .. */ stocksFetchingCountRemaining = stocksFetchingCountRemaining - stock.getQuantity(); if(lifo) { list.removeLast(); } else { list.removeFirst(); } } } if(stocksFetchingCountRemaining != 0) { System.out.printf("No/Partial stocks [%d] available, selling the same %n", numToSell - stocksFetchingCountRemaining); } profit = ((numToSell - stocksFetchingCountRemaining) * price) - totalShareCost; System.out.printf("You sold %d shares of %s stock at %.2f per share %n", numToSell - stocksFetchingCountRemaining, name, price); System.out.printf("You made $%.2f on the sale %n", profit); } public static void sellLIFO(LinkedList list, String name, int numToSell, double price) { sellStock(true, list, name, numToSell, price); } public static void sellFIFO(LinkedList list, String name, int numToSell, double price) { sellStock(false, list, name, numToSell, price); } }
Companies and people often buy and sell stocks. Often, they buy the same stock for different...
4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...
(C Programming language, not C++ and MUST Compile) Companies and people often buy and sell stocks. Often they buy the same stock for different prices at different times. Say one owns 1000 shares a certain stock (such as Checkpoint), one 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 is...
This is an additional assignment from the chapter on the Java Collections Framework. Suppose you buy 100 shares of a stock at $12 per share, then another 100 at $10 per share, then you sell 150 shares at $15. You have to pay taxes on the gain, but exactly what is the gain? In the United States, the FIFO rule holds: You first sell all shares of the first batch for a profit of $300, then 50 of the shares...
COP3337 Assignment 10 This is an additional assignment from the chapter on the Java Collections Framework. Suppose you buy 100 shares of a stock at $12 per share, then another 100 at $10 per share, then you sell 150 shares at $15. You have to pay taxes on the gain, but exactly what is the gain? In the United States, the FIFO rule holds: You first sell all shares of the first batch for a profit of $300, then 50...
The 4th deliverable is to create the program the makes the buy recommendation. It uses the class Stock to store and retrieve the stock information. I need to make this program compatible with my stocks class. Program I need to change: #include <iostream> #include <cmath> #include <fstream> #include <iomanip> #include <string> #include <stdlib.h> using namespace std; // read the data file, store in arrays int openDatafile(ifstream&,string [], float[], float[], int[]); // opens the data file void readData(ifstream &, string[], float[],...
Problem Statement Last month Tian purchased some stock in Acme Software, Inc. Here are the details of the purchase: The number of shares that Tian purchased was 1,000 When Tian purchased the stock, he paid $33.92 per share Tian paid his stockbroker a commission that amounted to 2% of the amount he paid for the stock. One week later, Tian sold the stock. Here are the details of the sale: The number of shares sold was 1,000...
Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...
JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization { /* *...
Fix this program
package chapter8_Test;
import java.util.Scanner;
public class Chapter8
{
public static void main(String[] args)
{
int[] matrix = {{1,2},{3,4},{5,6},{7,8}};
int columnChoice;
int columnTotal = 0;
double columnAverage = 0;
Scanner input = new Scanner(System.in);
System.out.print("Which column would you like to average (1 or
2)? ");
columnChoice = input.nextInt();
for(int row = 0;row < matrix.length;++row)
{
columnTotal += matrix[row][columnChoice];
}
columnAverage = columnTotal / (float) matrix.length;
System.out.printf("\nThe average of column %d is %.2f\n",
columnAverage, columnAverage);
}
}
This program...
Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...