Ejemplo n.º 1
0
        public void Load(string fileName, string delimiter)
        {
            char delim = Convert.ToChar(delimiter);

            FileStream   inFile = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(inFile);
            string       recordIn;

            string[] fields;

            recordIn = reader.ReadLine();
            while (recordIn != null)
            {
                fields = recordIn.Split(delim);
                Club c1 = new Club();
                Club.clubNumberValue = 3;
                try
                {
                    if (fields[0] == "")
                    {
                        throw new Exception(String.Format($"Invalid club record. Club with the registration number already exists:\n {fields[0]}, {fields[1]}, {fields[2]}, {fields[3]}, {fields[4]}, {fields[5]}, {fields[6]}"));
                    }
                    c1.ClubNumber = Convert.ToInt32(fields[0]);
                }
                catch (Exception ex)
                {
                    c1.ClubNumber = 0;
                    Console.WriteLine(ex.Message);
                }

                try
                {
                    if (fields[1] == "")
                    {
                        c1.Name = "No";
                        throw new Exception(String.Format("No value entered. It is a mandatory field"));
                    }
                    c1.Name = fields[1];
                }
                catch (Exception)
                {
                    throw;
                }
                c1.Address = new Address {
                    Street = fields[2], City = fields[3], Province = fields[4], PostalCode = fields[5]
                };

                try
                {
                    c1.PhoneNumber = Convert.ToInt64(fields[6]);
                    if (fields[6] == "")
                    {
                        throw new Exception(String.Format($"No value  entered. It is a mandatory field."));
                    }
                }
                catch (System.FormatException)
                {
                    c1.PhoneNumber = 1234567890;
                    Console.WriteLine($"Invalid club record. Phone number wrong format:\n  {fields[0]}, {fields[1]}, {fields[2]}, {fields[3]}, {fields[4]}, {fields[5]}, {fields[6]} ");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                recordIn = reader.ReadLine();
                try
                {
                    Add(c1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            reader.Close();
            inFile.Close();
        }
        public void Load(string fileName, string delimiter)
        {
            char delim = Convert.ToChar(delimiter);

            FileStream   inFile = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(inFile);
            string       recordIn;

            string[] fields;

            recordIn = reader.ReadLine();
            while (recordIn != null)
            {
                fields = recordIn.Split(delim);
                Registrant c1 = new Registrant();
                Registrant.numberValue = 0;
                try
                {
                    if (fields[0] == "")
                    {
                        throw new Exception();//String.Format($"Invalid swimmer record. Invalid Swimmer Name:\n {fields[0]}, {fields[1]}, {fields[2]}, {fields[3]}, {fields[4]}, {fields[5]}, {fields[6]}"));
                    }

                    c1.RegistrationNumber = Convert.ToInt32(fields[0]);
                }
                catch (Exception)
                {
                    c1.RegistrationNumber = 0;
                    Console.WriteLine($"Invalid swimmer record.Invalid Registration Number:\n { fields[0]}, { fields[1]}, { fields[2]}, { fields[3]}, { fields[4]}, { fields[5]}, { fields[6]}, {fields[7]}, {fields[8]}");
                }


                try
                {
                    if (fields[1] == "")
                    {
                        c1.Name = "No";
                        throw new Exception(String.Format($"Invalid swimmer record.Invalid Swimmer Name:\n { fields[0] }, { fields[1]}, { fields[2]}, { fields[3]}, { fields[4]}, { fields[5]}, { fields[6]}, {fields[7]}, {fields[8]}"));
                    }
                    c1.Name = fields[1];
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                try
                {
                    c1.DateOfBirth = Convert.ToDateTime(fields[2]);
                }
                catch (Exception)
                {
                    c1.DateOfBirth = new DateTime(1999, 9, 9);
                    Console.WriteLine($"Invalid swimmer record. BirthDate is Invalid:\n {fields[0]}, {fields[1]}, {fields[2]}, {fields[3]}, {fields[4]}, {fields[5]}, {fields[6]}, {fields[7]}, {fields[8]}");
                }

                c1.Address = new Address {
                    Street = fields[3], City = fields[4], Province = fields[5], PostalCode = fields[6]
                };

                try
                {
                    c1.PhoneNumber = Convert.ToInt64(fields[7]);
                    if (fields[7] == "")
                    {
                        throw new Exception(String.Format($"No value  entered. It is a mandatory field."));
                    }
                }
                catch (System.FormatException)
                {
                    c1.PhoneNumber = 1234567890;
                    Console.WriteLine($"Invalid club record. Phone number wrong format:\n  {fields[0]}, {fields[1]}, {fields[2]}, {fields[3]}, {fields[4]}, {fields[5]}, {fields[6]}, {fields[7]}, {fields[8]} ");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


                try
                {
                    c1.Club = GetByRegNum(Convert.ToInt32(fields[8]));
                }
                catch (Exception)
                {
                    Club c = new Club("not assigned");
                    c1.Club = c;
                    Console.Write("");
                }

                recordIn = reader.ReadLine();
                try
                {
                    Add(c1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            reader.Close();
            inFile.Close();
        }