import java.util.Scanner;
import java.util.Arrays;
/*
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 :)");
        
        char[] acceptedNumerals = new char[]{'M','D','C','L','X', 'V','I'};
        //These are all accepted roman numerals
         
        int number; //This is set once end user specifies number numerals in their number to be inputted
     
        boolean noMatch=true;  // this flag is used to check if the numeral entered is a valid one
       
        int j;  //used to increment the character array containing user input of numeral
        int count=0;  // this keeps track number times end user has entered a character. It can be decremented if it fails validation.
        
       
        
        //int pos;
        
        Scanner reader=null;
        
        
        // This is used to ascertain how many digits end user wants
        reader = new Scanner(System.in); // Reading from System.in
        System.out.println("Enter number numerals in the number to be converted to decimal:");
        number=reader.nextInt();
        
        char inputtedNumerals[] = new char[number];   // this creates character array with end user specified size
        int [][] posValues = new int [number][2];  // this is used to keep track of:
        
         // this is 2D array. 'number' simply increments with array index of the roman numeral in character array 
        // the multidimensal aspect at 0 index will store:  index of which numeral i.e  M is 0, D is 1
                             //aspect at 1 index will store:  decimal conversion
        // This will support later analysis.
        
        
do
{
reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter roman numeral " + (count+1) + " of " + number + ":");   // end user prompted to enter roman numeral
inputtedNumerals[count]=reader.next().charAt(0);
count++;

for (j=0; j<inputtedNumerals.length;j++)  // this will check each item in the inputted area against 7 roman numerals.
                                          // the condition could equally be set to count for better loop efficiency if the 
                                          //number entered is long
{

    for (int k=0; k<acceptedNumerals.length;k++)  // this goes through roman numerals
    {
    
    if (inputtedNumerals[j]==acceptedNumerals[k])  // this checks to see if the roman numerals entered are valid
    {
        
        posValues[count-1][0]=k;             
        
        
        // This switch statement now checks 'k'validposition in hierarcy of the 7 numerals.
        //'M','D','C','L','X', 'V','I'
        
        switch(k)
        {
            case 0:
                posValues[count-1][1]=1000;
                break;
            
            case 1:
                posValues[count-1][1]=500;
                break;
                
            case 2:
                posValues[count-1][1]=100;
                break;
                
            case 3:
                posValues[count-1][1]=50;
                break;
                
            case 4:
                posValues[count-1][1]=10;
                break;
                
            case 5:
                posValues[count-1][1]=5;
                break;
                
            case 6:
                posValues[count-1][1]=1;
                break;
        }
        
        //pos=k;
        
        noMatch=false;   // This sets flag stating match has been found.
        //k=acceptedNumerals.length-1;
        break;
        
        }
    
}
// This will now give information if the user input is valid for each numeral.
// If found to be incorrect, the count will go back and end user will need to enter it again.

 if (noMatch==true)
    {
       System.out.println("Please input a successful roman numeral:  M  D  C  L  X  V  I");
       count=count-1;
       break;
    }
    
}

} while (count<inputtedNumerals.length);

String str = String.valueOf(inputtedNumerals);
System.out.println("This will be converted to decimal:" + str);  // This informs end user of the roman numerals to be converted

performConversion pc = new performConversion(acceptedNumerals, inputtedNumerals,posValues); // calls constructor in class to perform conversion
}
}

class performConversion
{
    char[] inputtedNumerals;
    char[] acceptedNumerals;
    int [][] posValues;
    int total;
    int sum;
    int consecutiveOccurrences;
    
     // This is the constructor used to call the converter method. 
    public performConversion (char[] aNumerals, char [] inputNumerals, int [][] pValues)
    {
        this.acceptedNumerals=aNumerals;
        this.inputtedNumerals=inputNumerals;
        this.posValues=pValues;
        converter();
    }
    
    
    public void converter()
    {
        // There will be NO issues in performing conversion of a single roman numeral
        
        if (inputtedNumerals.length==1)    // This functions
        {
                System.out.println("Conversion is:" + posValues[0][1]);
        }
        
        // this is 2D array. count simply increments with array index of the roman numeral in character array 
        // the multidimensal aspect at 0 index will store:  which numeral i.e  M is 0, D is 1
                             //aspect at 1 index will store:  decimanl conversion
                             
            
       // ONCE ALL PRINCIPLES ARE UNDERSTOOD FOR A VALID ROMAN NUMERAL, THE CODE WILL BE IMPLEMENTED FURTHER     
            
            
            if (inputtedNumerals.length==2)
        {
            if (posValues[0][0]==posValues[1][0])
            {
                
            System.out.println("Conversion is:" + (posValues[0][1] + posValues[1][1]));
            }
           
        }
        
        //Rule 1: When certain numerals are repeated, the number represented by them is their sum. For example, II = 1 + 1 = 2, or XX = 10 + 10 = 20, or, XXX = 10 + 10 + 10 = 30.
        System.out.println("numbers to be examined:" + posValues.length);
        
        for (int m=1; m<posValues.length; m++)
        {
                //this will examine decimal number stored. If identical numerals in consecutive it will add to the total. 
                if (posValues[m][1]==posValues[m-1][1])
                {
                    total=posValues[m][1]+posValues[m][1];
                    sum=sum+total;
                    
                    consecutiveOccurrences++;
                    System.out.println("sum so far");
                    
                    if (consecutiveOccurrences>1)
                    {
                        System.out.println("does this happen");
                        sum=sum-(total/2);
                        total=0;
                    }
                }
        }
        System.out.println("this is the sum: " + sum);
        
        
        // This is the best logic found on the internet for roman numeral conversion.
        // Logic will be coded one by one and see if it works overall
        
    /*
    
    It is necessary for us to remember the rules for reading and writing Roman numbers in order to avoid mistakes. Here is a list of the basic rules for Roman numerals.

    Rule 1: When certain numerals are repeated, the number represented by them is their sum. For example, II = 1 + 1 = 2, or XX = 10 + 10 = 20, or, XXX = 10 + 10 + 10 = 30.
    Rule 2: It is to be noted that no Roman numerals can come together more than 3 times. For example, we cannot write 40 as XXXX
    Rule 3: The letters V, L, and D are not repeated.
    Rule 4: Only I, X, and C can be used as subtractive numerals. There can be 6 combinations when we subtract. These are IV = 5 - 1 = 4; IX = 10 - 1 = 9; XL = 50 - 10 = 40; XC = 100 - 10 = 90; CD = 500 - 100 = 400; and CM = 1000 - 100 = 900
    Rule 5: When a Roman numeral is placed after another Roman numeral of greater value, the result is the sum of the numerals. For example, VIII = 5 + 1 + 1 + 1 = 8, or, XV = 10 + 5 = 15,
    Rule 6: When a Roman numeral is placed before another Roman numeral of greater value, the result is the difference between the numerals. For example, IV = 5 - 1 = 4, or, XL = 50 - 10 = 40, or XC = 100 - 10 = 90
    Rule 7: When a Roman numeral of a smaller value is placed between two numerals of greater value, it is subtracted from the numeral on its right. For example, XIV = 10 + (5 - 1) = 14, or, XIX = 10 + (10 - 1) = 19
    Rule 8: To multiply a number by a factor of 1000 a bar is placed over it.
    Rule 9: Roman numerals do not follow any place value system.
    Rule 10: There is no Roman numeral for zero (0).

*/
      
        
        
    }
    
   
}
