
/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

//so far this has examined all stocks
//it looks at the buying and selling. and keeps track of difference
// need to address why it does not perform checks with last two stocks


public class Main
{
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        int[] stock = new int[]{1,3,2,8,4,10};
        //int[] stock = new int[]{5,2,4,0,1};
        
        int[][] difference = new int[stock.length][20];
        int count=0;
        int temp=0;
        int k;
        boolean profitPossible=false;
        int currentStart=-0;
        int tempStore=0;
        int runningTotal=0;
        
        for (int i=0; i<stock.length;i++)
        {
            System.out.println("**************************");
            System.out.println("\nStock being evaluated: " + stock[i]);
            System.out.println("**************************");
            
            System.out.println("current running total:" + runningTotal);
            
            System.out.println(stock[i]);
            count=0;
            runningTotal=0;
            
            for (int j=i+1; j<stock.length;j++)  // this ensures stock is not compared against itself
            {
                
                
                
                if (j!=stock.length-1)
                {
                    if (j==i)
                {
                    j++;
                }
               
               
                 if (stock[j]>stock[i]&& stock[j+1]<stock[j]) // this will check all occurences of potential point for sale
                {
                    profitPossible=true;
                   
                    System.out.println("Next highest stock is:" + stock[j]);
                    difference[i][count]=stock[j]-stock[i];
                    runningTotal=runningTotal + difference[i][count];
                    System.out.println("this is the difference: " + difference[i][count] + "("+stock[j] + "-" + stock[i]+")");
                    currentStart=stock[j];
                    
                    
                    
                    // this will check all other occurences if applicable point for sale
                    for (temp=j+1;temp<stock.length;temp++) 
                    {
                        if (stock[temp]>stock[j] && stock[j+1]<stock[j])
                        {
                            if (temp+1==stock.length-1)
                            {
                                tempStore=difference[i][count];
                                difference[i][count]=difference[i][count] + (stock[temp+1]-stock[temp]);
                                runningTotal=runningTotal + difference[i][count];
                                System.out.println("this is the difference: " + difference[i][count] + "("+ stock[temp+1] + "-" + stock[temp] +"+"+ tempStore + ")");
                                
                                break;
                                
                            }
                            
                            System.out.println("This is the next highest one: " + stock[temp] + "after " + stock[temp-1]);
                            tempStore=difference[i][count];
                            difference[i][count]=difference[i][count] + (stock[temp]-stock[temp-1]);
                            System.out.println("this is the difference: " + difference[i][count] + "("+ stock[temp] + "-" + stock[temp-1] +"+"+ tempStore + ")");
                            runningTotal=runningTotal + difference[i][count];
                        }
                        
                    }
                    
                    
                }
                
                
                
                }
                
                if (stock[j]>stock[j-1] && profitPossible && j==stock.length-1 && i==stock.length-2)
                {
                    System.out.println("This should be penultimate stock:" + stock[i]);
                    System.out.println("This should be last stock:" + stock[j]);
                    
                    tempStore=difference[i][count];
                    runningTotal = runningTotal + (stock[j]-stock[j-1]);
                    
                    System.out.println("this is the difference: " + runningTotal + "("+stock[j] +"-"+ stock[j-1]+")");
                    System.out.println("************REACH********");
                    break;
                }
                
                    
                
                 
                 
        }
        System.out.println("This is the stock:" + stock[i]);
                    System.out.println("This is all possible routes to make profit:" + difference[i][count]);
                    count++;
        
       
    }
}
}
