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 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
Class given Below:
/**
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;
}
}
}
}
Edits This Class only:
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)
{
. . .
}
}
/**
*
* @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.
COP3337 Assignment 10 This is an additional assignment from the chapter on the Java Collections Framework....
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...
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...
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....
Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util.HashMap; import java.util.Map; import java.util.Scanner; class Role { String user, password, role; public Role(String user, String password, String role) { super(); this.user = user; this.password = password; this.role = role; } /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } /** *...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...
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...
Modify the Triangle class (from previous code, will post under this) to throw an exception in the constructor and set routines if the triangle is not valid (i.e., does not satisfy the triangle inequality). Modify the main routine to prompt the user for the sides of the triangle and to catch the exception and re-prompt the user for valid triangle sides. In addition, for any valid triangle supply a method to compute and display the area of the triangle using...
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...