Block.java /** A quantity and price of a block of stocks. */ public class Block
ID: 3859598 • Letter: B
Question
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<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)
{
. . .
}
}
INPUT SCRIPT
test.in
buy ATT 100 10
buy APPL 10 50
sell ATT 50 13
buy ATT 100 15
sell APPL 5 55
buy GOOG 100 6
sell APPL 5 56
sell ATT 120 18
sell ATT 30 20
buy APPL 10 50
sell GOOG 80 8
sell GOOG 20 5
sell APPL 10 53
quit
Explanation / Answer
/**
*
* @author Sam
*/
import java.util.LinkedList;
import java.util.Queue;
import java.util.Map;
import java.util.TreeMap;
/**
A quantity and price of a block of stocks.
*/
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; }
}
/**
Class for simulating trading a single stock at varying prices.
*/
public class StockSimulator
{
private Map<String, Queue<Block>> blocks;
private int profit;
/**
Constructor.
*/
public StockSimulator()
{
blocks = new TreeMap();
profit = 0;
}
/**
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)) {
Block newBlock = new Block(quantity, price);
blocks.get(symbol).add(newBlock);
} else {
Queue<Block> newQueue = new LinkedList();
newQueue.add(new Block(quantity, price));
blocks.put(symbol, newQueue);
}
}
/**
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)){
Queue<Block> queue = blocks.get(symbol);
while (quantity > 0) {
if (queue.peek().getQuantity() > quantity) {
profit += price*quantity - queue.peek().getPrice()*quantity;
queue.peek().sell(quantity);
quantity = 0;
}
else {
profit += price*queue.peek().getQuantity() - queue.peek().getPrice()*queue.peek().getQuantity();
quantity -= queue.poll().getQuantity();
}
}
}
}
}
This should serve your purpose.... let me know if this helps you or you need an update.