Example #1
0
        //Read data from file
        public static List <KYPopulationBySex> ReadData(string fileName)
        {
            //create a new object using PopulationBySex class
            var population = new List <KYPopulationBySex>();

            //reading data from file
            using (var sr = new StreamReader(fileName))
            {
                //read header line to skip the header line
                sr.ReadLine();
                string line;
                //read data
                while ((line = sr.ReadLine()) != null)
                {
                    //Instantiate class populationBySex
                    var      populationBySex = new KYPopulationBySex();
                    string[] values          = line.Split(',');
                    if (int.TryParse(values[0], out int year))
                    {
                        populationBySex.Year = year;
                    }
                    if (int.TryParse(values[1], out int males))
                    {
                        populationBySex.Males = males;
                    }
                    if (int.TryParse(values[2], out int females))
                    {
                        populationBySex.Females = females;
                    }
                    population.Add(populationBySex);
                }
                return(population);
            }
        }
Example #2
0
        //Add latest population data to missing years
        public static void AddLatestData(List <KYPopulationBySex> fileContents)
        {
            var population = new KYPopulationBySex();
            int maxYear    = GetMaxYear(fileContents);

            int year = maxYear + 1;

            Console.WriteLine("\n\tEnter Latest Data for");
            Console.WriteLine("\t     Year : {0}", year);
            population.Year = year;

            while (true)
            {
                Console.Write("\t Males : ");
                string males = Console.ReadLine();

                if (int.TryParse(males, out int outMales))
                {
                    population.Males = outMales;
                }

                Console.Write("\t Females : ");
                string females = Console.ReadLine();
                if (int.TryParse(females, out int outfemales))
                {
                    population.Females = outfemales;
                }

                if (population.Males > 0 && population.Females > 0)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("\tInvalid Entry, please enter valid population value");
                }
            }

            fileContents.Add(population);

            Console.WriteLine("\n\tFollowing Data added to the Population data \n\t Year : {0} \n\t Males : {1} \n\t Females : {2}", year, population.Males, population.Females);
        }
Example #3
0
        public static void UpdateData(List <KYPopulationBySex> fileContents)
        {
            var    population = new KYPopulationBySex();
            int    minYear    = GetMinYear(fileContents);
            int    maxYear    = GetMaxYear(fileContents);
            int    outYear    = 0;
            int    outMales   = 0;
            int    outFemales = 0;
            string yearString;


            //population.Year = year;
            while (true)
            {
                Console.Write("\n\tEnter the Year Between \"{0}\" \"{1}\"to update : ", minYear, maxYear);
                yearString = Console.ReadLine();

                if (int.TryParse(yearString, out outYear))
                {
                    if (outYear >= minYear && outYear <= maxYear)
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("\tselect a year between \"{0}\" & \"{1}\"", minYear, maxYear);
                    }
                }
            }
            while (true)
            {
                var data = (from y in fileContents
                            where y.Year == outYear
                            select new { maleValue = y.Males, femaleValue = y.Females })
                           .FirstOrDefault();

                Console.WriteLine("\t Existing Male Value is {0}", data.maleValue);
                Console.Write("\t Edit Males : ");
                string males = Console.ReadLine();
                int.TryParse(males, out outMales);

                Console.WriteLine("\t Existing Male Value is {0}", data.femaleValue);
                Console.Write("\t Edit Females : ");
                string females = Console.ReadLine();
                int.TryParse(females, out outFemales);

                if (outYear > 0 && outMales > 0 && outFemales > 0)
                {
                    foreach (var fc in fileContents)
                    {
                        if (fc.Year == outYear)
                        {
                            fc.Males   = outMales;
                            fc.Females = outFemales;
                        }
                    }
                    break;
                }
                else
                {
                    Console.WriteLine("\tInvalid Entry, please enter valid population value");
                }
            }

            //fileContents.Add(population);

            Console.WriteLine("\n\tPopulation data updated with following values \n\t Year : {0} \n\t Males : {1} \n\t Females : {2}", outYear, outMales, outFemales);
        }