Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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. Show 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. Additionally, add in two new choices to the menu, display which will show a list of all the stocks and how many of each are in the portfolio, and profit which would give the total of all the gains. Use the included templates. 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.

/* Input samples

*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

 

*/

 

 

 

 

/**

  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> 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)

  {

   . . .

  }

}

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92791273
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Computer Engineering

Question suppose f is a function that returns the result of

Question : Suppose f is a function that returns the result of reversing the string of symbols given as its input, and g is a function that returns the concatenation of the two strings given as its input. If x is the stri ...

Question suppose that the bcr stores only one bit value

Question : Suppose that the BCR stores only one bit value. Calculate the total number of memory bytes present in the system. Do not include the BCR register in the total.

Can someone please help me with this pleasetaskthe purpose

Can Someone please help me with this please: Task: The purpose of this assignment is to empirically measure attributes for binary search trees. Steps: Create a program that will build a binary search tree (BST) by adding ...

Question 1-what is cost and benefit analysis explain the

Question: 1- What is cost and benefit analysis? Explain the two different types of costs and two different types of benefits. 2- Discuss root-cause analysis as a BPA activity. What are the strengths and limitations of th ...

Question suppose there are 150 users sharing a

Question : Suppose there are 150 users sharing a communication link. Suppose each user transmits data only 12% of the time. Give the (compact) formula for finding the probability that at most 20 users are transmitting si ...

Run the huffman coding algorithm on the following

Run the Huffman coding algorithm on the following input: Alphabet C = {p, r, a, i, s, e, d}, with frequencies f as percentages= {12, 16, 16, 12, 16, 16, 12}. Draw the optimal coding tree and the (weighted) average code l ...

Strengths and weaknesses of university that attracts and

Strengths and Weaknesses of University that attracts and distracts students from joining that university with respect to the theory of planned behaviour

Do you need computers or information and communication

Do you need computers or information and communication technologies to store, organize, and manage data in organizations? Explain how the present day organizations in a developed country like the USA store and manage the ...

Explain the risk of having hacking tools installed on your

Explain the risk of having hacking tools installed on your computer and why you should contact local law enforcement agencies before installing those tools.

Question suppose that in addition to edge capacities a flow

Question : Suppose that, in addition to edge capacities, a flow network has vertex capacities. That is each vertex v has a limit l(v) on how much flow can pass through v. Show how to transform a flow network G = (V, E) w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As