PLEASE INCLUDE COMMENTS AND JAVA DOCS!!!!! /** A quantity and price of a block o
ID: 3914067 • Letter: P
Question
PLEASE INCLUDE COMMENTS AND JAVA DOCS!!!!!
/**
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; }
}
.........
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;
}
}
}
}
.........
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<Block>> 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)
{
. . .
}
}
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.HashMap;
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<Block>> blocks;
/**
Constructor.
*/
public StockSimulator()
{
blocks = new HashMap<String, Queue<Block>>();
}
/**
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)
{
Queue<Block> que = blocks.get(symbol.toUpperCase());
if(que == null){
que = new LinkedList<Block>();
blocks.put(symbol.toUpperCase(), que);
}
que.add(new Block(quantity, 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)
{
int gains = 0;
Queue<Block> que = blocks.get(symbol.toUpperCase());
if(que == null)
System.out.println("No shares of " + symbol);
else
{
//assuming there are enough shares to sell
while(quantity > 0){
Block b = que.peek();
if(b.getQuantity() <= quantity)
{
gains += b.getQuantity() * (price - b.getPrice());
quantity -= b.getQuantity();
que.remove();
}
else
{
gains += quantity * (price - b.getPrice());
b.sell(quantity);
quantity = 0;
}
}
}
if(gains >= 0)
System.out.println("Gains: $" + gains);
else
System.out.println("Loss: $" + (-gains));
}
}
OUTPUT OF SimulationRunner.java:
=====
Stock Simulator Menu
-----------------------------------------------
> buy stock-symbol quantity price
> sell stock-symbol quantity price
> display
> profit
> quit to quit simulation.
> buy ABC 100 12
> buy ABC 100 10
> sell ABC 150 15
Gains: $550
>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.