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 from
the second batch, for a profit of $250, yielding a total profit of
$550. Write a program that can make these calculations for
arbitrary purchases and sales of shares in a portfolio. The user
enters commands buy symbol quantity price, sell symbol quantity
price (which causes the gain on that transaction to be displayed),
and quit. Hint: Keep a Map> that manages a separate queue for
each stock symbol.
Use the included templates for the program in the included zip
file. You do not need to add any additional methods other than what
are included. There is a sample of an input script so you can see
how I may test your program. Be sure to fully test the program
before submitting it. I should not be able to crash it.
Follow all directions in the COP3337 Class Rules for Submitting
Programs.
Please remove any package statements in your program. The program
must compile and run from the command line no matter what IDE you
use.
Note that a program that does not compile and run and do some part
of the assignment will not earn any points.
The submission should be called FirstnameLastnameA10.zip'
also Use A test.in file
THE BLOCK CLASS
/**
A quantity and price of a block of stocks.
*/
public class Block
{
private final int price;
private int quantity;
/**
Constructor.
@param quantity the quantity of this block.
@param price the price of this block.
*/
public Block(int quantity, int price)
{
this.price = price;
this.quantity = quantity;
}
public int getQuantity() { return quantity; }
public int getPrice() { return price; }
public void sell(int shares) { quantity -= shares; }
}
aCLASS sIMULATION rUNNER
import java.util.Scanner;
/**
Runs a Stock Trading Simulation
*/
public class SimulationRunner
{
public static void main(String[] args)
{
StockSimulator sim = new StockSimulator();
Scanner in = new Scanner(System.in);
boolean done = false;
System.out.println("Stock Simulator Menu");
System.out.println("-----------------------------------------------");
System.out.println(" > buy stock-symbol quantity price");
System.out.println(" > sell stock-symbol quantity price");
System.out.println(" > quit to quit simulation.");
System.out.println();
while (!done)
{
System.out.print(" > ");
String action = in.next();
if (action.equals("buy"))
{
String symbol = in.next();
int quantity = in.nextInt();
int price = in.nextInt();
sim.buy(symbol, quantity, price);
}
else if (action.equals("sell"))
{
String symbol = in.next();
int quantity = in.nextInt();
int price = in.nextInt();
sim.sell(symbol, quantity, price);
}
else if (action.equals("quit"))
{
done = true;
}
}
}
}
cLASS sTOCK sIMULATOR
import java.util.LinkedList;
import java.util.Queue;
import java.util.Map;
import java.util.TreeMap;
/**
Class for simulating trading a single stock at varying
prices.
*/
public class StockSimulator
{
private Map> blocks;
/**
Constructor.
*/
public StockSimulator()
{
. . .
}
/**
Handle a user buying a given quantity of stock at a given
price.
@param quantity how many to buy.
@param price the price to buy.
*/
public void buy(String symbol, int quantity, int price)
{
. . .
}
/**
Handle a user selling a given quantity of stock at a given
price.
@param symbol the stock to sell
@param quantity how many to sell.
@param price the price to sell.
*/
public void sell(String symbol, int quantity, int price)
{
. . .
}
}
Block.java
/**
* A quantity and price of a block of stocks.
*/
public class Block {
private final int price;
private int quantity;
/**
* Constructor.
*
* @param quantity the quantity of this block.
* @param price the price of this block.
*/
public Block(int quantity, int price) {
this.price = price;
this.quantity = quantity;
}
public int getQuantity() {
return quantity;
}
public int getPrice() {
return price;
}
public void sell(int shares) {
quantity -= shares;
}
}
SimulationRunner.java
import java.util.Scanner;
/**
* Runs a Stock Trading Simulation
*/
public class SimulationRunner {
public static void main(String[] args) {
StockSimulator sim = new StockSimulator();
Scanner in = new Scanner(System.in);
boolean done = false;
System.out.println("Stock Simulator Menu");
System.out.println("-----------------------------------------------");
System.out.println(" > buy stock-symbol quantity price");
System.out.println(" > sell stock-symbol quantity price");
System.out.println(" > quit to quit simulation.");
System.out.println();
while (!done) {
System.out.print(" > ");
String action = in.next();
if (action.equals("buy")) {
String symbol = in.next();
int quantity = in.nextInt();
int price = in.nextInt();
sim.buy(symbol, quantity, price);
} else if (action.equals("sell")) {
String symbol = in.next();
int quantity = in.nextInt();
int price = in.nextInt();
sim.sell(symbol, quantity, price);
} else if (action.equals("quit")) {
done = true;
}
}
}
}
StockSimulator.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Map;
import java.util.TreeMap;
/**
* Class for simulating trading a single stock at varying prices.
*/
public class StockSimulator {
private Map<String, Queue<Integer>> blocks = new TreeMap<>();
/**
* Constructor.
*/
public StockSimulator() {
}
/**
* Handle a user buying a given quantity of stock at a given price.
*
* @param quantity how many to buy.
* @param price the price to buy.
*/
public void buy(String symbol, int quantity, int price) {
if (blocks.containsKey(symbol)) {
Queue<Integer> q = blocks.get(symbol);
for (int i = 0; i < quantity; i++)
q.add(price);
blocks.replace(symbol, q);
} else {
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < quantity; i++)
q.add(price);
blocks.put(symbol, q);
}
System.out.println("Bought Succesfully");
}
/**
* Handle a user selling a given quantity of stock at a given price.
*
* @param symbol the stock to sell
* @param quantity how many to sell.
* @param price the price to sell.
*/
public void sell(String symbol, int quantity, int price) {
if (blocks.containsKey(symbol) && blocks.get(symbol).size() >= quantity) {
Queue<Integer> q = blocks.get(symbol);
Integer profit = 0;
for (int i = 0; i < quantity; i++)
profit += price - q.remove();
blocks.replace(symbol, q);
System.out.println("Profit = " + profit);
} else
System.out.println("Not enough stocks");
}
}
EXPLANATION :
Here, I have added the buy and sell function to calculate the profit gained by the user while selling their stocks. The prices of the differents stock-symbols are stored in a Map named blocks which is a TreeMap. When the user tries to buy a stock, we first check if the user already owns stocks of that symbol. If they do, then we get the queue which relates to that particular stock from the Map and we add the newly bought stock to that queue and add the queue back to the Map. If they dont own any stock of that particular symbol, we create a new queue and add the newly boughts stocks to that queue and add the queue into the Map with the key as the symbol. When the user want to sell their stock, we first check if the user owns any stock of that symbol and that they own more stock than they want to sell. If true, we take out the elements from the queue by removing them from the queue and add the profit. We then replace the queue in the Map with the updated queue which does not include the sold stocks. We then print the profit.
THANKS,
PLEASE UPOVTE THE ANSWER AS IT WOULD BE OF GREAT HELP TO ME.
This is an additional assignment from the chapter on the Java Collections Framework. Suppose you buy...
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...
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...
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...
Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>(); // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString()); //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234"); // printing information System.out.println(part2.toString()); //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...
I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public class ItemToPurchase { // instance variables private String itemName; private String itemDescription; private int itemPrice; private int itemQuantity; // default constructor public ItemToPurchase() { this.itemName = "none"; this.itemDescription = "none"; this.itemPrice = 0; this.itemQuantity = 0; } public ItemToPurchase(String itemName, int itemPrice, int itemQuantity,String itemDescription) { ...
Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that works like the combination lock in a gym locker (Consult the API provided to look for the methods needed). The locker is constructed with a combination - three numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right....
java Operating System Scheduler Two scheduling strategies for an operating system scheduler are first come first serve (FCFS) and fixed priority pre-emptive scheduling (FPPS). Since queues operate on a first come first serve basis, FCFS is implemented using a queue. Similarly, FPPS is implemented using a priority queue. The operating system scheduler simulation is already provided for you. To use it you simply need to modify the main method to run the simulator using either the LinkedListQueue or the PriorityQueue. Run...
(Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1,000,000 times) Given below that the class die, oddstester, and gameSimulator(partially complete and the bold missing parts need to be done), need to find the probability of winning game 1 and 2 (after the 1 million plays) hence the getWins and...
Objectives
Problem solving using arrays and ArrayLists. Abstraction.
Overview
The diagram below illustrates a banner constructed from
block-letters of size 7. Each block-letter is
composed of 7 horizontal line-segments of width 7 (spaces
included):
SOLID
as in block-letters F, I, U, M, A, S and the
blurb RThere are six distinct line-segment
types:
TRIPLE
as in block-letter M
DOUBLE
as in block-letters U, M, A
LEFT_DOT
as in block-letters F, S
CENTER_DOT as
in block-letter...
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...