Search code examples
javaarraysfor-loopstock

How do i get multiple stock profits?


This is my solution to find the maximum profit i can make from stocks.

int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9}; is an array where the index is minutes after the stock market opens, and the values are the price of the stock.

So for example aktiePris[60] = 300 means that the value of the stock is 300, one hour after the stock market opens.

Right now my code returns the maximum possible profit i can make from buying and selling one single stock. I want to be able to se more than one single stock. How can i find all the possible profits and print them?

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class ProfitCalculator {
    static int minValue, maxValue, maxDiff;
    static Calendar timeMin, timeMax;
    static int indeksMinMinut, indeksMaxMinut;

    public static void main(String args[]) {

        int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9};
        int profit = findProfit(aktiePris);

        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

        timeMin = findTime();
        timeMin.add(timeMin.MINUTE, indeksMinMinut);
        timeMax = findTime();
        timeMax.add(timeMax.MINUTE, indeksMaxMinut);
        System.out.println("Best time & price for buying is " + timeFormat.format(timeMin.getTime()) + " for " + minValue + " EUR." + "\n"
                + "Best time & price for selling is " + timeFormat.format(timeMax.getTime()) + " for " + maxValue + " EUR." + "\n"
                + "Profit: " + profit);
    }

    public static int findProfit(int[] inputArray) {

        if (inputArray.length < 1)
            return 0;

        maxDiff = 0;
        minValue = inputArray[0];
        maxValue = minValue;

        for (int i = 1; i < inputArray.length; i++) {
            if (inputArray[i] > maxValue) {
                maxValue = inputArray[i];
                indeksMaxMinut = i;
                int priceDiff = maxValue - minValue;
                if (priceDiff > maxDiff) {
                    maxDiff = priceDiff;
                }
            } else if (inputArray[i] < minValue) {
                minValue = maxValue = inputArray[i];
                indeksMinMinut = i;
            }
        }

        return maxDiff;
    }

    public static Calendar findTime() {

        Calendar calendar = Calendar.getInstance();
        calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 30);

        return calendar;
    }
}

Solution

  • Make a share analyzer class, which encapsulates the functionality you want:

    class ShareAnalyzer {
    
        private final int[] aktiePris;
        private int sellMinut = 0, buyMinut = 0, buyPrice,  sellPrice;
    
        ShareAnalyzer(int[] aktiePris) {
    
            this.aktiePris = aktiePris;
            findProfit();
        }
    
        private void findProfit() {
    
            if (aktiePris.length < 1) return ;
            int minValue = aktiePris[0];
            int maxValue = minValue;
            int indeksMaxMinut = 0, indeksMinMinut = 0;
    
            for (int i = 1; i < aktiePris.length; i++) {
                if (aktiePris[i] > maxValue) {
                    maxValue = aktiePris[i];
                    indeksMaxMinut = i;
                    int priceDiff = maxValue - minValue;
                    if (priceDiff > getProfit()) {
                        sellPrice = maxValue;
                        buyPrice  = minValue;
                        sellMinut = indeksMaxMinut;
                        buyMinut = indeksMinMinut;
                    }
                } else if (aktiePris[i] < minValue) {
                    minValue = maxValue = aktiePris[i];
                    indeksMinMinut = i;
                }
            }
        }
    
        //time in minutes from opening
        int getSellTime() {return sellMinut;}
    
        int getSellPrice() {return sellPrice;}
    
        //time in minutes from opening
        int getBuyTime() {return buyMinut;  }
    
        int getBuyPrice() {return buyPrice;}
    
        int getProfit() {   return sellPrice - buyPrice;    }
    }
    

    You could also add a method to print out share analyzer info:

        //this could be implemented as `toString` of ShareAnalyzer
        private static void printShareInfo(ShareAnalyzer shareAnalyzer){
            System.out.println("Best time & price for buying is " + shareAnalyzer.getBuyTime() + " minutes after opening for " + shareAnalyzer.getBuyPrice() + " EUR." + "\n"
                    + "Best time & price for selling is " + shareAnalyzer.getSellTime() + " minutes after opening for " + shareAnalyzer.getSellPrice() + " EUR." + "\n"
                    + "Profit: " + shareAnalyzer.getProfit());
        }
    

    Construct a new ShareAnalyzer instance for each share you want to analyze.

    public static void main(String args[]) {
    
        int[] aktiePris1 = new int[]{10, 7, 5, 8, 11, 9};
        ShareAnalyzer shareAnalyzer1 = new ShareAnalyzer(aktiePris1);
        printShareInfo(shareAnalyzer1);
    
        int[] aktiePris2 = new int[]{2, 12, 4, 7 , 11, 9, 1 , 8};
        ShareAnalyzer shareAnalyzer2 = new ShareAnalyzer(aktiePris2);
        printShareInfo(shareAnalyzer2); 
    }
    

    You can run the code and test it using this link