Write a class StockHolding The purpose of a StockHolding obj

Write a class StockHolding. The purpose of a StockHolding object is to represent a single stock in someone\'s investment portfolio. The StockHolding class has the following specification:


Write a class StockHoldingMain that contains a main method. The main method should create three StockHolding objects. For each object, print the initial cost, set the current share price, print the current profit, and print the toString value. The following is sample output from a main method creating a StockHolding for 19 shares of Apple at $103.97 and a current price of 105.5:

Solution

import java.util.ArrayList;

// Solution structure

class Interval

{

    int buy, sell;

}

class StockBuySell

{

    // This function finds the buy sell schedule for maximum profit

    void stockBuySell(int price[], int n)

    {

        // Prices must be given for at least two days

        if (n == 1)

            return;

         

        int count = 0;

        // solution array

        ArrayList<Interval> sol = new ArrayList<Interval>();

        // Traverse through given price array

        int i = 0;

        while (i < n - 1)

        {

            // Find Local Minima. Note that the limit is (n-2) as we are

            // comparing present element to the next element.

            while ((i < n - 1) && (price[i + 1] <= price[i]))

                i++;

            // If we reached the end, break as no further solution possible

            if (i == n - 1)

                break;

            Interval e = new Interval();

            e.buy = i++;

            // Store the index of minima

             

            // Find Local Maxima. Note that the limit is (n-1) as we are

            // comparing to previous element

            while ((i < n) && (price[i] >= price[i - 1]))

                i++;

            // Store the index of maxima

            e.sell = i-1;

            sol.add(e);

             

            // Increment number of buy/sell

            count++;

        }

        // print solution

        if (count == 0)

            System.out.println(\"There is no day when buying the stock \"

                                                  + \"will make profit\");

        else

            for (int j = 0; j < count; j++)

                System.out.println(\"Buy on day: \" + sol.get(j).buy

                        +\"        \" + \"Sell on day : \" + sol.get(j).sell);

         

        return;

    }

    public static void main(String args[])

    {

        StockBuySell stock = new StockBuySell();

         

        // stock prices on consecutive days

        int price[] = {100, 180, 260, 310, 40, 535, 695};

        int n = price.length;

        // fucntion call

        stock.stockBuySell(price, n);

    }

}

Write a class StockHolding. The purpose of a StockHolding object is to represent a single stock in someone\'s investment portfolio. The StockHolding class has t
Write a class StockHolding. The purpose of a StockHolding object is to represent a single stock in someone\'s investment portfolio. The StockHolding class has t
Write a class StockHolding. The purpose of a StockHolding object is to represent a single stock in someone\'s investment portfolio. The StockHolding class has t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site