Question

In: Computer Science

Write a program in JAVA "When a share of common stock of some company is sold,...

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

Solutions

Expert Solution

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

..


Related Solutions

In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next five years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $1.29. The dividends are expected to grow at 14 percent over the next five years. The company has a payout ratio of 40 percent and a benchmark PE of 24. The required return...
In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next five years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $1.15. The dividends are expected to grow at 10 percent over the next five years. The company has a payout ratio of 40 percent and a benchmark PE of 19. The required return...
In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next five years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $1.25. The dividends are expected to grow at 20 percent over the next five years. The company has a payout ratio of 40 percent and a benchmark PE of 20. The required return...
In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next five years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $1.65. The dividends are expected to grow at 19 percent over the next five years. In five years, the estimated payout ratio is 30 percent and the benchmark PE ratio is 31. What...
In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next five years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $1.15. The dividends are expected to grow at 10 percent over the next five years. The company has a payout ratio of 40 percent and a benchmark PE of 19. The required return...
In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next five years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $1.36. The dividends are expected to grow at 13 percent over the next five years. In five years, the estimated payout ratio is 40 percent and the benchmark PE ratio is 19. a....
In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next five years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $1.15. The dividends are expected to grow at 10 percent over the next five years. The company has a payout ratio of 40 percent and a benchmark PE of 19. The required return...
In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next five years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $2.10. The dividends are expected to grow at 15 percent over the next five years. In five years, the estimated payout ratio is 34 percent and the benchmark PE ratio is 40. a....
In practice, a common way to value a share of stock when a company pays dividends...
In practice, a common way to value a share of stock when a company pays dividends is to value the dividends over the next seven years or so, then find the “terminal” stock price using a benchmark PE ratio. Suppose a company just paid a dividend of $3.50. The dividends are expected to grow at 8 percent over the next seven years. The company has a payout ratio of 35 percent and a benchmark PE of 45. What is the...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two natural numbers a and b Input:  a and b are given in a line separated by a single space. Output: Output the greatest common divisor of a and b. Constraints: 1 ≤ a, b ≤ 109 Hint: You can use the following observation: For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y) Sample Input 1 54 20 Sample Output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT