Beispiel #1
0
        }//eom

        static void LoadData(List <Babydata> babydatas, int year)
        {
            if (File.Exists(STR_PATH + "yob" + year + ".txt"))
            {
                string       name, input;
                char         gender;
                int          number;
                StreamReader reader = new StreamReader(STR_PATH + "yob" + year + ".txt");
                try
                {
                    while ((input = reader.ReadLine()) != null)
                    {
                        string[] parts = input.Split(',');
                        name   = parts[0];
                        gender = char.Parse(parts[1]);
                        number = int.Parse(parts[2]);
                        Babydata babydata = new Babydata(name, gender, number);
                        babydatas.Add(babydata);
                    }
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    throw new Exception(ex.Message);
                }
                finally
                {
                    reader.Close();
                }
            }
            else
            {
                throw new Exception("Enter a Valid year");
            }
        } //eom
Beispiel #2
0
        }//eom

        static void LoadData(List <Babydata> babydatas, int year, List <string> femaleNames, List <string> maleNames, int amount)
        {
            int fCount = 0, mCount = 0;

            if (File.Exists(STR_PATH + "yob" + year + ".txt"))
            {
                string       name, input;
                char         gender;
                int          number;
                StreamReader reader = new StreamReader(STR_PATH + "yob" + year + ".txt");
                try
                {
                    while ((input = reader.ReadLine()) != null)
                    {
                        string[] parts = input.Split(',');
                        name   = parts[0];
                        gender = char.Parse(parts[1]);
                        number = int.Parse(parts[2]);
                        Babydata babydata = new Babydata(name, gender, number);
                        babydatas.Add(babydata);
                    }
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ex.Message);
                    Console.ResetColor();
                }
                finally
                {
                    reader.Close();
                }
            }
            else
            {
                throw new Exception("Enter a Valid year");
            }
            foreach (Babydata babydata in babydatas)
            {
                if (babydata.Gender == 'F' && fCount < amount)
                {
                    femaleNames.Add(babydata.Name);
                    fCount++;
                }
            }
            foreach (Babydata babydata in babydatas)
            {
                if (babydata.Gender == 'M' && mCount < amount)
                {
                    maleNames.Add(babydata.Name);
                    mCount++;
                }
            }
        }//eom