/*
Online Java - IDE, Code Editor, Compiler

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

//I can see straight away looking at the challenge and my notes....
//Requirement is to return the array as oppose to returning individual
//formatted population.

import java.util.*;

public class Main
{
    static long checkNegativeNumber;
    static long smallPortion;
    static int i;
    static long roundingUnit;
    static long smallPortionRounded;
    static long formattedRoundedNumber;
    static String millionsRoundingFormatted[][];
    static String millionsRounding[][];
    static long roundingToNearest=1000000;
    static long largePortion;
    static long divisbleByRoundingUnit;
    static long population;
    static long frequencyRoundedNumber;
    static long frequencyRoundedNumberInLargePortion;
    static boolean roundtoNearestOddNumber;
    
    public static void main(String[] args) 
    {
        /*
        //ORIGINAL TEST CASES
        millionsRounding = new String[][]
                                        {
                                        {"Nice","942208"},
                                        {"Abu Dhabi", "1482816"},
                                        {"Naples", "2186852"},
                                        {"Vatican City", "572"}
                                        };
          */
         
        //FURTHER TEST CASES AS PER EXAMPLE  ***PASS
        millionsRounding = new String[][]
                                        {
                                        {"Nice","6"},
                                        {"Abu Dhabi", "18"},
                                        {"Naples", "20"},
                                        {"Vatican City", "21"}
                                        };
        
         /*
         //FURTHER DEVISED TEST CASES  *****PASS
         millionsRounding = new String[][]
                                        {
                                        {"Nice","1000000"},
                                        {"Abu Dhabi", "500001"},
                                        {"Naples", "1"},
                                        {"Vatican City", "0"},
                                        {"London", "10234865"},
                                        {"Birmingham", "1532896"}
                                        };
        */
         
        /* 
         //FURTHER DEVISED TEST CASES   ***PASS
         millionsRounding = new String[][]
                                        {
                                        {"Nice","4"},
                                        {"Abu Dhabi", "6"},
                                        {"Naples", "55"},
                                        {"Vatican City", "33"},
                                        {"London", "3315"},  //error
                                        {"Birmingham", "600067"}
                                        };
        */
        
        /*
        //DEVISED TEST CASES AS PER LOGIC DOCUMENTATION  ***PASS
        millionsRounding = new String[][]
                                        {
                                        {"Nice","274214"},
                                        {"Abu Dhabi", "500001"},
                                        {"Naples", "1"},
                                        {"Vatican City", "0"},
                                        {"London", "10234865"},
                                        {"Birmingham", "1532896"}
                                        };
        */
         
        //DO NOT COMMENT THIS OUT
        millionsRoundingFormatted = new String[][]
                                        {
                                        {"Nice",""},
                                        {"Abu Dhabi", ""},
                                        {"Naples", ""},
                                        {"Vatican City", ""},
                                        {"London", ""},
                                        {"Birmingham", ""},
                                        };
          
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("Original population:");
        getPopulation(millionsRounding);
        
        if (roundingToNearest%2==0)
        {
            roundingUnit=roundingToNearest/2;
        }
        
        if (roundingToNearest%2==1)
        {
            roundingUnit=roundingToNearest;
            roundtoNearestOddNumber=true;
        }
        
        for (i=0; i<millionsRounding.length;i++)
        {
            //this will display populations
            System.out.println("\nProcessing population for: " + millionsRounding[i][0] + "("+millionsRounding[i][1]+")");
            population = Long.valueOf(millionsRounding[i][1]);
            
            //conditions where it is even rounded to like the example
            //round to 1,000,000
            
            frequencyRoundedNumber = population/roundingUnit;
            smallPortion =  population - (frequencyRoundedNumber * roundingUnit);
            largePortion = frequencyRoundedNumber * roundingUnit;  
            frequencyRoundedNumberInLargePortion = largePortion/roundingUnit;
        
            //this part here is the logic identified when performing operation
            //on a number less than roundingUnit (see documentation for number 274,214)
	        //unfortunately this was error on my documented logic. It shows it is sometimes easy
	        //to confuse / with %.
	        
            if (smallPortion < 0)
            {
                System.out.println("Small portion less than 0");
                smallPortion = population;
                largePortion = 0;
                frequencyRoundedNumber = 0;
            }
        
            roundSmallPortion();
        } //end for
        
        System.out.println("\nPopulation rounded to nearest " + roundingToNearest);
        getPopulation(millionsRoundingFormatted);
    }
    
    public static void getPopulation(String [][] populationData)
    {
        for (String[]m:populationData)
        {
            System.out.println(Arrays.asList(m));
        }
    }
    
    //I can see that in my logic, I made a few critical mistakes.
    //But we are no longer dealing with number (formattedRoundedNumber = number + 500,000)
    //it has been partitioned into largePortion and smallPortion
    //also we know that 500,000 is considered to be part of largePortion
    //I will add it across to this part.
    //I will need to pay close attention know just to ensure it has no impact
    //elsewhere
    
    public static void roundSmallPortion()
    {
        divisbleByRoundingUnit = population%roundingUnit;
        System.out.println("Processing the small portion: " + smallPortion);
        System.out.println("This is large portion: " + largePortion);
        
        checkNegativeNumber = smallPortion - roundingToNearest;
        System.out.println("Checking negative number: " + checkNegativeNumber);
        
        //as per logic, this can occur numerous reasons
        if (divisbleByRoundingUnit==0 && !roundtoNearestOddNumber)
        {
            System.out.println("Population is exactly divisible by: " + roundingUnit);
            //here we know number is 500,000, 1,500,000,  2,500,000
            // this is roundingUnit(500,000) and then cumulative total of
            //roundingUnit(500,000) + roundingToNearest (1,000,000);
            formattedRoundedNumber=largePortion+roundingUnit;
        }
        
        else
        {
            System.out.println("Small population is not divisible by: " + roundingUnit);
            ///we then know we are dealing with largePortion such as    
            //500,000,  1,500,000,   2,500,000  since each are
            //multiple of 500,000 and can be divided by it 1,3,5 times respectively
            //so the condition would be true
            //in the case of the Nice population, largePortion = 500,000. Likewise if the largePortion
            //was 1,500,000 I have no logic that I presented in my logic, ironically 
            //for a relatively fundemental process.
            //If ((frequencyRoundedNumberInLargePortion%2) == 1)  
        
            System.out.println("What is frequencyRoundedNumberInLargePortion: " + frequencyRoundedNumberInLargePortion);
         
            if(frequencyRoundedNumberInLargePortion%2==1  && !roundtoNearestOddNumber)
            {
                System.out.println("Large portion population is in this sequence: " + roundingUnit+ ", " + (roundingUnit+roundingToNearest)+ ", " + (roundingUnit+roundingToNearest+roundingToNearest+" ....."));
                formattedRoundedNumber = largePortion + roundingUnit;
            }
            else
            {
                //the small portion is less than 500,000, so rounds downwards...
                if (checkNegativeNumber< (-1*roundingUnit))
                {
                    System.out.println("Small portion is less than: " + roundingUnit);
                    smallPortionRounded = 0;
                    formattedRoundedNumber =  largePortion + smallPortionRounded;
                }
                
                 //it should not enter here, otherwise there is error somewhere.
                // since checkNegativeNumber = smallPortion - roundingToNearest;
	            //it would mean smallPortion is negative or smallPortion is zero.
	            //not possible at all unless calculation has failed elsewhere.
            
                else  //(if checkNegativeNumber>=  -500,000)
                {
                    if (smallPortion<=((roundingToNearest -1)/2))
                    {
                        formattedRoundedNumber= largePortion;
                        
                    }
                    else
                    {
                        formattedRoundedNumber=largePortion+roundingToNearest;
                    }
                    
                } //end else
            } //end of else
        }//end of big if-else
        
        if (smallPortion==0 && largePortion==0 && divisbleByRoundingUnit==0)
        {
            System.out.println("The initial population is 0: " + millionsRoundingFormatted[i][1]);
            formattedRoundedNumber = 0;
            //this is a way to validate if the population is 0
            //but since this method is no longer returning,
            //it will be moved just before the method end 
        }
        
        millionsRoundingFormatted[i][1] = String.valueOf(formattedRoundedNumber);
        System.out.println("Population: " + millionsRounding[i][1] + "=> " + millionsRoundingFormatted[i][1] + " (" + "rounded to nearest " + roundingToNearest + ")");
    } //end of method
}//end of class