/*
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.
       
       char[] currency = {'£','$','€'};
       
       char originalInput;
      
      double currencies [][] =   {
                                 
                                 {1.00,1.25,1.17},
                                 {0.80,1.00,0.94},
                                 {0.85,1.07,1.00},
                                 
                                  };
      
      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 ************************
      
      char input = '£';
      char 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];  // this will be £
      output=currency[j]; // this will be $
      originalInput=currency[i];
      
      System.out.println(input+"  " +String.format("%.2f",amount) + " to be converted to " + output+"\n");
      
      for (char 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] )));
     
     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])));
     
     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 (char 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] )));
     
     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/currencies[inputCurrency][outputCurrency])));
     
     convertedValue=convertedValue/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 (char 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])));
     
     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 (char 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])));
     
     
     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");
}    
     
     
}
}
     
     
}
}
        
