
/*
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);
        
        System.out.println("\nPopulation rounded to nearest " + roundingToNearest);
        //getPopulation(millionsRoundingFormatted);
                                        
        for (i=0; i<millionsRounding.length;i++)
        {
            //this will display populations
            //System.out.println(millionsRounding[i][1]);
        
        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)
        {
            smallPortion = population;
            largePortion = 0;
            frequencyRoundedNumber = 0;
        }
        
        roundSmallPortion();
        
        //for now, but will return array later on
        
    }
    
    getPopulation(millionsRoundingFormatted);
    
    }
    
    public static void getPopulation(String [][] populationData)
    {
        for (String[]m:populationData)
        {
            System.out.println(Arrays.asList(m));
        }
        
        //for (String s[][]: millionsRoundingFormatted)
        //{
         //   for (String m[]:s)
         //   //this should print out each row?
         //   return s[];
        //}
        
    }
    
    public static void roundSmallPortion()
    {
        divisbleByRoundingUnit = population%500000;
        
        //as per logic, this can occur numerous reasons
        if (divisbleByRoundingUnit==0)
        {
            //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)
            {
                largePortion = largePortion + 500000;
            }
            
            
            
            
            
        }
        
        
        else
        {
        
        if (checkNegativeNumber<roundingUnit)
        {
            smallPortionRounded = 0;
            formattedRoundedNumber =  largePortion + smallPortionRounded;
            
            
        }
        else  //(if checkNegativeNumber>=  -500,000)
        {
            smallPortionRounded=1000000;
            formattedRoundedNumber = largePortion + smallPortionRounded;
           
        }
        millionsRoundingFormatted[i][1] = String.valueOf(formattedRoundedNumber);
    }
        
        
        if (smallPortion==0 && largePortion==0 && divisbleByRoundingUnit==0)
        {
            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 
        }
    }
}
