/*
************ OUTPUT ************************
NOT PROVIDED. UTILISE THE COMMENTED STRINGS TO TEST THE
EXECUTION.
*** CODE ***
/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs
online.
*/

public class Main
{
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("This will be simple example at moment demonstrating if there is arbitrage");
        System.out.println("To keep it simple, it will initially compare each currency against only one other currency");
        System.out.println("Then it will branch to another and then return back to original currency");
        System.out.println("I will try to explore several routes to return back to original currency");
        System.out.println("The program will report arbitrage and loss.");
        // not sure how to store the integer arrays into a larger container..
        // perhaps create a 3d array... but not worrying at moment.
        // this is to verify no arbitrage
        // this is the same on horizontal and vertical
        // This is not the best measure since if the position varies on the matrix, it will fail.
        // However if this is introduced in the array, it will be heterogenous elements.
        // Will need to use an object or similar.......
        //and perform conversions to doubles.....
        // The code is currently designed to support matrix of currencies with conversion factors
        // The data is inputted to exactly match the currencies below on the horizontal and vertical axis
        // The program could be extended to change position of these on either axis.
        // This would entail creating another character array for currencies.
        //use this as alternative if simplified version of symbols are required....
        //String [] currency = {'$','€','¥','£','S','C','A','H'};
        String originalInput;
        /*
        //char[] currency = {'£','$','€','D'};
        double currencies [][] = {
        {1.00,1.25,1.16,2.28},
        {0.80,1.00,0.93,1.82},
        {0.86,1.08,1.00, 1.96},
        {0.44,0.55,0.51,1.00},
        };
        */
        String[] currency = {"$","€","¥","£","CHF","CA$","AU$","HK$"};
        double currencies [][] = 
        {
        {1.0, 0.9277, 155.7380, 0.7984, 0.9072, 1.3681,
        1.5141, 7.8141},
        {1.0779, 1.0, 167.8700, 0.8606, 0.9779, 1.4747,
        1.6320, 8.4228},
        {0.0064, 0.0060, 1.0, 0.0051, 0.0058, 0.0088,
        0.0097, 0.0502},
        {1.2525, 1.1620, 195.0616, 1.0, 1.1363, 1.7136,
        1.8964, 9.7871},
        {1.1023, 1.0226, 171.6638, 0.8800, 1.0, 1.5080,
        1.6689, 8.6132},
        {0.7309, 0.6781, 113.8333, 0.5836, 0.6631, 1.0,
        1.1067, 5.7115},
        {0.6605, 0.6127, 102.8615, 0.5273, 0.5992, 0.9036,
        1.0, 5.1610},
        {0.1280, 0.1187, 19.9304, 0.1022, 0.1161, 0.1751,
        0.1938, 1.0},
        };
        
        double[] conversionUnits = new double[currencies[0].length];
        // since no indication of which currency is which, it will use indexof- at first index of 2d array.
        // NORMAL CONDITIONS ************************
        String input = "£";
        String output = "$";
        double amount = 3.00;
        int inputRow=0;
        int outputRow=0;
        int column=0;
        int inputCurrency=0;
        int outputCurrency=0;
        double convertedValue=0;
        double convertedBack=0;
        double convertedBack1=0;
        double convertedValue_initial=0;
        double reverse_conversion=0;
        //double temp=0;
        double temp1=0;
        
        for (int i=0; i<currencies[0].length; i++)
        {
            //NEED SOMETHING IN HERE TO KEEP CONVERSION FACTOR. SO WHEN IT HITS THE k LOOP, IT CAN USE IT TO GET BACK.
            //IT KNOWS HOW TO CONVERT BACK TO J LOOP.
            // SO THIS VALUE HAS TO BE STORED IN J LOOP.
            column=0;
            
            for (int j=0; j<currencies[0].length; j++)
            {
                System.out.println("\n********In the J loop********");
                column=0;
                
                if (i!=currencies.length-1)
                {
                    if (currency[i]==currency[j])
                    {
                        j++;
                    }
                }
                
                // this is used to find position of the currency on the matrix.
                // although strictly speaking it should be alligned to the table of data
            
                input=currency[i];
                output=currency[j];
                originalInput=currency[i];
            
                System.out.println(input+" " +String.format("%.2f",amount) + " to be converted to " + output+"\n");
                // Does it really have to do this, if i, j and m loops are exactly alligned against length of currency array
                // and the data in currencies array is alligned against currency array?
            
                for (String c: currency)
                {
                    //System.out.println("This is the value of c: " + c);
                    //System.out.println("Converted from: " + input);
                    //System.out.println("Converted to: " + output);
                
                    if (c==output) // in this test case $
                    {
                        //System.out.println("***Output****");
                        //System.out.println("Detected a: " + output + " in row: " + (column));
                        outputCurrency=column;
                    }
                    if (c==input) // in this test case £
                    {
                        //System.out.println("***Input***");
                        //System.out.println("Detected a: " + input + " in column: " + (column));
                        inputCurrency=column;
                    }
                    column++;
                }
            
                System.out.println("CONVERSION**************");
                System.out.println("This is the initial amount: " + input+ String.format("%.2f",amount));
                System.out.println("This is the conversion factor: " +
                currencies[outputCurrency][inputCurrency]);
                System.out.println("This is conversion: " + output +
                String.format("%.2f",(amount/currencies[outputCurrency][inputCurrency] )) + " " + input+"=>"+output);
                convertedValue = (amount / currencies[outputCurrency][inputCurrency]);
                //conversionUnits[j]= currencies[inputCurrency][outputCurrency]; // THIS WILL BE USED IN LOOP K SO THAT IT CAN GET BACK TO I.
                // NEED EXTRA CODE HERE TO TAKE IT BETWEEN ADDITIONAL CURRENCIES (PERHAPS ANOTHER FOR LOOP)
                // BUT THIS TIME IT HAS TO SKIP IF ITS THE SAME CURRENCY OR IF IT IS THE CURRENCY PROCESSED ABOVE.
                // THE PREVIOUS CURRENCY PROCESSED WILL BE POSITION AT C. THERE IS UCCRENLT NOT INDEX NOTATION DUE TO FOR EACH LOOP
                // THE COLUMN VARIABLE REFERS
                System.out.println("This is the conversion factor: " + currencies[inputCurrency][outputCurrency]);
                System.out.println("This is conversion: " + input +
                String.format("%.2f",(convertedValue/currencies[inputCurrency][outputCurrency])) + " " + output+"=>"+input);
                convertedBack = convertedValue/currencies[inputCurrency][outputCurrency];
            
                if (convertedBack>amount)
                {
                    System.out.println("************ARBITRAGE.....***********");
                    System.out.println("Difference of: " + input+ String.format("%.2f",(convertedBack -
                    amount)));
                }
                if (convertedBack<amount)
                {
                    System.out.println("************LOSS OF CURRENCY.....***********");
                    System.out.println("Difference of: " + input+ String.format("%.2f",(amount - convertedBack)));
                }
            
                System.out.println("\n\n");
                //----------------------------------
                for (int k=0; k<currencies[0].length; k++) // the first location will be (0,1), this is £ to $
                {
                    System.out.println("In the K loop");
                    column=0;
                    
                    if (k!=currencies.length-1)
                    {
                        /*
                        System.out.println("This is currency k: " + currency[k]);
                        System.out.println("This is input: " + input);
                        System.out.println("This is output: " + output);
                        System.out.println("This is currency i: " + currency[i]);
                        System.out.println("This is currency j: " + currency[j]);
                        */
                        
                        if (currency[k]==currency[j] || currency[k]==currency[i] || currency[k]==input || currency[k]==output)
                        {
                            System.out.println("INSIDE!!!!!");
                            continue;
                        }
                    }
                    // this is used to find position of the currency on the matrix.
                    // although strictly speaking it should be alligned to the table of data
                    //***********************************
                    // NEED EXTREME WORK HERE SINCE IN THE K LOOP, IT SHOULD NOT TRY TO CONVERT AGAIN TO SAME CURRENCY PROCESSED IN IN
                    // This is unrealted to if (currency[k]==currency[j] || currency[k]==currency[i] )
                    //SO AS PART OF I LOOP, NEED TO STORE COLUMN IN AN ARRAY AND ENSURE THE INPUT CAN NOT BE THE SAME....
                    // ALSO AS PART OF THE J LOOP, NEED TO KEEP TRACK OF THE OUTPUTS TO ENSURE IT IS NOT CONVERTED AGAIN TO THESE.....
                    //****************************************************************************************************************
                    input=currency[j]; // this will be £
                    output=currency[k]; // this will be $
                    // it will use convertedValue from loop j
                    System.out.println(input+" " +String.format("%.2f",convertedValue) + " to be converted to " + output+"\n");
                
                    for (String c: currency)
                    {
                        //System.out.println("This is the value of c: " + c);
                        //System.out.println("Converted from: " + input);
                        //System.out.println("Converted to: " + output);
                    
                        if (c==output) // in this test case $
                        {
                            //System.out.println("***Output****");
                            //System.out.println("Detected a: " + output + " in row: " + (column));
                            outputCurrency=column;
                        }
                    
                        if (c==input) // in this test case £
                        {
                            //System.out.println("***Input***");
                            //System.out.println("Detected a: " + input + " in column: " + (column));
                            inputCurrency=column;
                        }
                        column++;
                    }
                    
                    System.out.println("\nCONVERSION**************1234");
                    System.out.println("This is the initial amount: " + input+
                    String.format("%.2f",convertedValue));
                    System.out.println("This is the conversion factor: " +
                    currencies[outputCurrency][inputCurrency]);
                    System.out.println("1This is conversion: " + output +
                    String.format("%.2f",(amount/currencies[outputCurrency][inputCurrency] )) + " " + currency[i]+"=>"+input+"=>"+output);
                    temp1=convertedValue;
                    //reverse_conversion = currencies[inputCurrency][outputCurrency];
                    convertedValue_initial = (amount / currencies[outputCurrency][inputCurrency]);
                    //temp=convertedValue;
                    // NEED EXTRA CODE HERE TO TAKE IT BETWEEN ADDITIONAL CURRENCIES (PERHAPS ANOTHER FOR LOOP)
                    // BUT THIS TIME IT HAS TO SKIP IF ITS THE SAME CURRENCY OR IF IT IS THE CURRENCY PROCESSED ABOVE.
                    // THE PREVIOUS CURRENCY PROCESSED WILL BE POSITION AT C. THERE IS UCCRENLT NOT INDEX NOTATION DUE TO FOR EACH LOOP
                    // THE COLUMN VARIABLE REFERS
                    System.out.println("This is the conversion factor: " + currencies[inputCurrency][outputCurrency]);
                    System.out.println("2This is conversion: " + input +
                    String.format("%.2f",(convertedValue_initial/currencies[inputCurrency][outputCurrency])) 
                    + "  " + currency[i]+"=>"+input+"=>"+output+"=>"+input);
                
                    convertedValue=convertedValue_initial/currencies[inputCurrency][outputCurrency];
                
                    if (temp1<convertedValue)
                    {
                        System.out.println("************ARBITRAGE.....***********");
                        System.out.println("Difference of: " + input+ String.format("%.2f",(convertedValue - temp1)));
                    }
                
                    if (temp1>convertedValue)
                    {
                        System.out.println("************LOSS OF CURRENCY.....***********");
                        System.out.println("Difference of: " + input+ String.format("%.2f",(temp1 - convertedValue)));
                    }
            
                    //It does not know conversion factor euro to pound since its not done this conversion or reverse yet.
                    column=0;
            
                    for (String c: currency)
                    {
                        //System.out.println("This is the value of c: " + c);
                        //System.out.println("Converted from: " + input);
                        //System.out.println("Converted to: " + output);
                
                        if (c==output) // in this test case $
                        {
                            //System.out.println("***Output****");
                            //System.out.println("Detected a: " + output + " in row: " + (column));
                            outputCurrency=column;
                        }
                
                        if (c==currency[i]) // in this test case £
                        {
                            //System.out.println("***Input***");
                            //System.out.println("Detected a: " + input + " in column: " + (column));
                            inputCurrency=column;
                        }
                        column++;
                    }
                
                    System.out.println("\n1This is " + output+ String.format("%.2f",convertedValue_initial) 
                    + "with conversion factor " + currencies[inputCurrency][outputCurrency] + " back to original: " + currency[i]);
                    System.out.println("This is conversion: " + originalInput +
                    String.format("%.2f",(convertedValue_initial/currencies[inputCurrency][outputCurrency])) 
                    + "  " + currency[i]+"=>"+input+"=>"+output+"=>" + currency[i]);
                    convertedBack = convertedValue_initial/currencies[inputCurrency][outputCurrency];
            
                    if (convertedBack>amount)
                    {
                        System.out.println("************ARBITRAGE.....***********");
                        System.out.println("Difference of: " + originalInput +String.format("%.2f",(convertedBack- amount)));
                    }
            
                    if (convertedBack<amount)
                    {
                        System.out.println("************LOSS OF CURRENCY.....***********");
                        System.out.println("Difference of: " + originalInput +String.format("%.2f",(amount - convertedBack)));
                    }
                    // This is repeated of above... For instance in current flow above it is doing:
                    //£ => $ => Euro => $ => £
                    //However need to do:
                    //£ => $ => Euro => £
            
                    System.out.println("\n");
                    System.out.println("This will now convert: " + input + String.format("%.2f",convertedValue) + " back to original " + currency[i]);
                    // it has to process this again to find correct conversion factor to initial currency....
                    column=0;
            
                    for (String c: currency)
                    {
                        if (c==currency[i]) // in this test case $
                        {
                            //System.out.println("***Output****");
                            //System.out.println("Detected a: " + output + " in row: " + (column));
                            inputCurrency=column;
                        }
                
                        if (c==input) // in this test case £
                        {
                            //System.out.println("***Input***");
                            //System.out.println("Detected a: " + input + " in column: " + (column));
                            outputCurrency=column;
                        }
                        column++;
                    }
            
                    conversionUnits[j]= currencies[inputCurrency][outputCurrency];
            
                    System.out.println("This is the conversion factor back to original: " + conversionUnits[j]);
                    System.out.println("This is conversion: " + originalInput +
                    String.format("%.2f",(convertedValue/conversionUnits[j])) + " " +
            
                    currency[i]+"=>"+input+"=>"+output+"=>" + "=>" + input + "=>" + currency[i]);
                    convertedBack1 = convertedValue/conversionUnits[j];
            
                    if (convertedBack1>amount)
                    {
                        System.out.println("************ARBITRAGE.....***********");
                        System.out.println("Difference of: " + originalInput +String.format("%.2f",(convertedBack1 - amount)));
                    }
            
                    if (convertedBack1<amount)
                    {
                        System.out.println("************LOSS OF CURRENCY.....***********");
                        System.out.println("Difference of: " + originalInput +String.format("%.2f",(amount -
                        convertedBack1)));
                    }
            
                    System.out.println("\n\n");
            
                    for (int m=0; m<currencies[0].length; m++) // the first location will be (0,1), this is £ to $
                    {
                        System.out.println("In the m loop");
                        column=0;
                
                        if (m!=currencies.length-1)
                        {
                            if (currency[m]==currency[k] || currency[m]==currency[i] || currency[m]==input ||
                            currency[m]==output)
                            {
                                System.out.println("INSIDE!!!!!");
                                continue;
                            }
                        }
                        input=currency[k]; // this will be £
                        output=currency[m]; // this will be $
                        // it will use convertedValue from loop j
                        System.out.println(input+" " +String.format("%.2f",convertedValue_initial) 
                        + " to be converted to " + output+"\n");
                
                        for (String c: currency)
                        {
                            //System.out.println("This is the value of c: " + c);
                            //System.out.println("Converted from: " + input);
                            //System.out.println("Converted to: " + output);
                    
                            if (c==output) // in this test case $
                            {
                                //System.out.println("***Output****");
                                //System.out.println("Detected a: " + output + " in row: " + (column));
                                outputCurrency=column;
                            }
                    
                            if (c==input) // in this test case £
                            {
                                //System.out.println("***Input***");
                                //System.out.println("Detected a: " + input + " in column: " + (column));
                                inputCurrency=column;
                            }
                            column++;
                        }
                
                        System.out.println("\nCONVERSION**************4567");
                        System.out.println("This is the initial amount: " + input+ String.format("%.2f",convertedValue_initial));
                
                        System.out.println("This is the conversion factor: " + currencies[outputCurrency][inputCurrency]);
                        System.out.println("1This is conversion: " + output 
                        + String.format("%.2f",(convertedValue_initial/currencies[outputCurrency][inputCurrency] )) 
                        + " "+ currency[i]+"=>"+input+"=>"+output+"=>" + currency[i] + "=>" + output);
                
                        temp1=convertedValue_initial;
                        reverse_conversion = convertedValue_initial/currencies[outputCurrency][inputCurrency];
                        //convertedValue_initial = (amount / currencies[outputCurrency][inputCurrency]);
                        //temp=convertedValue;
                        // NEED EXTRA CODE HERE TO TAKE IT BETWEEN ADDITIONAL CURRENCIES (PERHAPS ANOTHER FOR LOOP)
                        // BUT THIS TIME IT HAS TO SKIP IF ITS THE SAME CURRENCY OR IF IT IS THE CURRENCY PROCESSED ABOVE.
                        // THE PREVIOUS CURRENCY PROCESSED WILL BE POSITION AT C. THERE IS UCCRENLT NOT INDEX NOTATION DUE TO FOR EACH LOOP
                        // THE COLUMN VARIABLE REFERS
                        System.out.println("This is the conversion factor: " + currencies[inputCurrency][outputCurrency]);
                        System.out.println("2This is conversion: " + input + 
                        String.format("%.2f",(convertedValue_initial/currencies[outputCurrency][inputCurrency]/currencies[inputCurrency][outputCurrency])) + " " + currency[i]+"=>"+input+"=>"+output+"=>" +
                
                        currency[i] + "=>" + output +"=>"+ input);
                        convertedValue=convertedValue_initial/currencies[inputCurrency][outputCurrency]/currencies[outputCurrency][inputCurrency];
                
                        if (temp1<convertedValue)
                        {
                            System.out.println("************ARBITRAGE.....***********");
                            System.out.println("Difference of: " + input+ String.format("%.2f",(convertedValue - temp1)));
                        }
                
                        if (temp1>convertedValue)
                        {
                            System.out.println("************LOSS OF CURRENCY.....***********");
                            System.out.println("Difference of: " + input+ String.format("%.2f",(temp1 -
                            convertedValue)));
                        }
                        //It does not know conversion factor euro to pound since its not done this conversion or
                        //reverse yet.
                        column=0;
                
                        for (String c: currency)
                        {
                            //System.out.println("This is the value of c: " + c);
                            //System.out.println("Converted from: " + input);
                            //System.out.println("Converted to: " + output);
                    
                            if (c==output) // in this test case $
                            {
                                //System.out.println("***Output****");
                                //System.out.println("Detected a: " + output + " in row: " + (column));
                                outputCurrency=column;
                            }
                    
                            if (c==currency[i]) // in this test case £
                            {
                                //System.out.println("***Input***");
                                //System.out.println("Detected a: " + input + " in column: " + (column));
                                inputCurrency=column;
                            }
                            column++;
                        }
                
                        //address this
                        System.out.println("\n1This is " + output+ String.format("%.2f",reverse_conversion) 
                        + "with conversion factor " + currencies[inputCurrency][outputCurrency] + " back to original: " +
                        currency[i]);
                
                        System.out.println("This is conversion: " + originalInput +
                        String.format("%.2f",(reverse_conversion/currencies[inputCurrency][outputCurrency])) + " "
                        + currency[i]+"=>"+input+"=>"+output+"=>" + currency[i] + "=>" + output + "=>" +
                        originalInput);
                        convertedBack = reverse_conversion/currencies[inputCurrency][outputCurrency];
                
                        if (convertedBack>amount)
                        {
                            System.out.println("************ARBITRAGE.....***********");
                            System.out.println("Difference of: " + originalInput +String.format("%.2f",(convertedBack
-                           amount)));
                        }
                
                        if (convertedBack<amount)
                        {
                            System.out.println("************LOSS OF CURRENCY.....***********");
                            System.out.println("Difference of: " + originalInput +String.format("%.2f",(amount -
                            convertedBack)));
                        }
                        // This is repeated of above... For instance in current flow above it is doing:
                        //£ => $ => Euro => $ => £
                        //However need to do:
                        //£ => $ => Euro => £
                        System.out.println("\n");
                        System.out.println("This will now convert: " + input + String.format("%.2f",convertedValue)
                        + " back to original " + currency[i]);
                        // it has to process this again to find correct conversion factor to initial currency....
                
                        column=0;
                    
                        for (String c: currency)
                        {
                            if (c==currency[i]) // in this test case $
                            {
                                //System.out.println("***Output****");
                                //System.out.println("Detected a: " + output + " in row: " + (column));
                                inputCurrency=column;
                            }
                        
                            if (c==input) // in this test case £
                            {
                                //System.out.println("***Input***");
                                //System.out.println("Detected a: " + input + " in column: " + (column));
                                outputCurrency=column;
                            }
                            column++;
                        }
                        
                        conversionUnits[j]= currencies[inputCurrency][outputCurrency];
                        System.out.println("This is the conversion factor back to original: " + conversionUnits[j]);
                        System.out.println("This is conversion: " + originalInput +
                        String.format("%.2f",(convertedValue/conversionUnits[j])) + " " +
                        currency[i]+"=>"+input+"=>"+output+"=>" + currency[i] + "=>" + output + "=>" + input + "=>" +
                        originalInput);
                        convertedBack1 = convertedValue/conversionUnits[j];
            
                        if (convertedBack1>amount)
                        {
                            System.out.println("************ARBITRAGE.....***********");
                            System.out.println("Difference of: " + originalInput +String.format("%.2f",
                            (convertedBack1 - amount)));
                        }
            
                        if (convertedBack1<amount)
                        {
                            System.out.println("************LOSS OF CURRENCY.....***********");
                            System.out.println("Difference of: " + originalInput +String.format("%.2f",(amount -
                            convertedBack1)));
                        }
                        System.out.println("\n\n");
                    }
                }
            }
        }
    }
}