In: Computer Science
Write a program in JAVA
"When a share of common stock of some company is sold, the capital gain(or,sometimes, loss) is the difference between the share’s selling price and the price originally paid to buy it. This rule is easy to understand for a single share, but if we sell multiple shares of stock bought over a long period of time, then we must identify the shares actually being sold. A standard accounting principle for identifying which shares of a stock were sold in such a case is to use a FIFOprotocol—the shares sold are the ones that have been held the longest (indeed,this is the default method built into several personal finance software packages).For example, suppose we buy 100 shares at $20 each on day 1, 20 shares at $24on day 2, 200 shares at $36 on day 3, and then sell 150 shares on day 4 at $30each. Then applying the FIFO protocol means that of the 150 shares sold, 100were bought on day 1, 20 were bought on day 2, and 30 were bought on day3. The capital gain in this case would therefore be 100·10+20·6+30·(−6),or $940. Write a program that takes as input a sequence of transactions of the form “buy x share(s) at $y each” or “sell x share(s) at $y each,” assuming that the transactions occur on consecutive days and the values x and y are integers. Given this input sequence, the output should be the total capital gain(or loss) for the entire sequence, using the FIFO protocol to identify shares."
Your program should prompt the user to input a single string that represents a sequence of transactions. Use semicolon (‘;’) to separate the transactions in your input string. For example, your program should support the following input string:
“buy 100 share(s) at $20 each;buy 20 share(s) at $24 each;buy 200 share(s) at $36 each;sell 150 share(s) at $30 each;buy 50 share(s) at $25 each;sell 200 share(s) at $35 each;”
The output of your program should be the total capital gain (or loss) for the entire sequence, using the FIFO protocol as explained in P-6.36. For instance, the output for the example input should be: 1070
• Consider using built-in Java classes, e.g., classes that implement the interface java.util.Queue
code in java
(code to copy)
import java.util.*;
public class Main {
public static class Share{
public String type;
public int amount;
public int price;
public Share(String query){
//read info from the query string
type = query.substring(0, query.indexOf(" "));
amount = Integer.parseInt(query.substring(query.indexOf(" ")+1, query.indexOf("share")-1));
price = Integer.parseInt(query.substring(query.indexOf("$")+1, query.indexOf("each")-1));
}
}
public static void main(String[] args) {
//declare scanner object to read user input
Scanner sc = new Scanner(System.in);
//get input from user
String input = sc.nextLine();
//create scanner with this input
sc = new Scanner(input);
//set delimeter as semicolon to read different queries separated by semicolon
sc.useDelimiter(";");
//declare queue to store queries
Queue<Share> queue = new LinkedList<>();
//declare variable to calculate capital gain
int capital_gain = 0;
//read all queries and process it
while(sc.hasNext()){
//read query in buffer
String buffer = sc.next();
Share x = new Share(buffer);
//if buy then add in queue
if(x.type.equals("buy")){
queue.add(x);
}else{
// update capital gain since we are selling
capital_gain+=x.amount*x.price;
int remaining_amount = x.amount;
//if sell then process queue
Share y = queue.peek();
while(y.amount<=remaining_amount){
// update capital gain since we are selling
capital_gain-=y.amount*y.price;
remaining_amount -=y.amount;
queue.remove();
y = queue.peek();
}
//remove this much amount
y.amount = y.amount-remaining_amount;
// update capital gain for remaining amount
capital_gain-=remaining_amount*y.price;
//check if amount in y is 0 then pop this share
if(y.amount==0){
queue.remove();
}
}
}
//print the capital gain
System.out.println("Total Capital gain = "+capital_gain);
}
}
code screenshot
sample console input/output screenshot
..