/*
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 = 500000;
    static long smallPortionRounded;
    static long formattedRoundedNumber;
    static String millionsRoundingFormatted[][];
    static long roundingToNearest=1000000;
    static long largePortion;
    static long divisbleByRoundingUnit;
    static long population;
    static long frequencyRoundedNumber;
    
    public static void main(String[] args) 
    {
        String millionsRounding[][] = new String[][]
                                        {
                                        {"Nice","942208"},
                                        {"Abu Dhabi", "1482816"},
                                        {"Naples", "2186852"},
                                        {"Vatican City", "572"}
                                        };
                                        
        millionsRoundingFormatted = new String[][]
                                        {
                                        {"Nice",""},
                                        {"Abu Dhabi", ""},
                                        {"Naples", ""},
                                        {"Vatican City", ""}
                                        };
                                        
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("Original population:");
        getPopulation(millionsRounding);
        
        
        for (i=0; i<millionsRounding.length;i++)
        {
            //this will display populations
            System.out.println("\nProcessing population for: " + millionsRounding[i][0]);
            population = Long.valueOf(millionsRounding[i][1]);
            frequencyRoundedNumber = population/roundingUnit;
        
            smallPortion =  population - (frequencyRoundedNumber * roundingUnit);
            largePortion = frequencyRoundedNumber * roundingUnit;  
        
            //this part here is the logic identified when performing operation
            //on a number less than roundingUnit (see documentation for number 274,214)
            if (smallPortion < 0)
            {
                System.out.println("Small portion less than 0");
                smallPortion = population;
                largePortion = 0;
                frequencyRoundedNumber = 0;
            }
        
            roundSmallPortion();
        }
        
        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));
        }
    }
    
    public static void roundSmallPortion()
    {
        divisbleByRoundingUnit = population%500000;
        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)
        {
            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);
            
            //I can see that in my logic, I made a few critical mistakes.
            //I had the below... But we are no longer dealing with number
            //it has been partitioned into largePortion and smallPortion
            //formattedRoundedNumber = number + 500,000
            //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
            
            if(frequencyRoundedNumber%2== 1)
            {
                System.out.println("Population is 500,000 , 1,500,000,  2,500,000  etc");
                largePortion = largePortion + 500000;
            }
        }
        
        else
        {
            System.out.println("Population is not divisible by: " + roundingUnit);
            
            //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;
            }
            
            else  //(if checkNegativeNumber>=  -500,000)
            {
                System.out.println("Small portion is greater than or equal to: " + roundingUnit);
                smallPortionRounded=1000000;
                formattedRoundedNumber = largePortion + smallPortionRounded;
            }
            millionsRoundingFormatted[i][1] = String.valueOf(formattedRoundedNumber);
            
        }
        
        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 
        }
        
    }
}
