コード例 #1
0
        public void AddCSVFile(string fileName)
        {
            try
            {
                using (TextFieldParser parser = new TextFieldParser(@fileName))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");

                    while (!parser.EndOfData)
                    {
                        string[] animalTrait = parser.ReadFields();

                        Animal animalToAdd = new Animal();
                        animalToAdd.Name = animalTrait[0].Trim('"');
                        int?intParseValue = UserInterface.GetCsvIntData(animalTrait[2]);
                        animalToAdd.Weight = intParseValue;
                        string testedInput = UserInterface.CsvNullChecker(animalTrait[1]);
                        while (testedInput == null)
                        {
                            UserInterface.DisplayUserOptions("Please enter the animal`s species: ");
                            string input = Console.ReadLine();
                            intParseValue = Query.GetCategoryId(input);
                            if (intParseValue != null)
                            {
                                testedInput = "Not null";
                            }
                        }
                        animalToAdd.CategoryId = intParseValue;
                        intParseValue          = UserInterface.GetCsvIntData(animalTrait[3]);
                        animalToAdd.Age        = intParseValue;
                        intParseValue          = UserInterface.GetCsvIntData(animalTrait[4]);
                        animalToAdd.DietPlanId = intParseValue;
                        animalToAdd.Demeanor   = animalTrait[5].Trim('"');
                        bool?newFriendlyStatus = UserInterface.GetCsvBoolData(animalTrait[6]);
                        animalToAdd.KidFriendly    = newFriendlyStatus;
                        newFriendlyStatus          = UserInterface.GetCsvBoolData(animalTrait[7]);
                        animalToAdd.PetFriendly    = newFriendlyStatus;
                        animalToAdd.Gender         = animalTrait[8];
                        animalToAdd.AdoptionStatus = animalTrait[9].Trim('"');
                        intParseValue          = UserInterface.GetCsvIntData(animalTrait[10]);
                        animalToAdd.EmployeeId = intParseValue;

                        if (Query.CheckIfEmptyRoom())
                        {
                            Query.AddAnimal(animalToAdd);
                            Query.PlaceAnimalIntoRoom(animalToAdd.AnimalId);
                        }
                        else
                        {
                            UserInterface.DisplayUserOptions("There are no open rooms at this time. The animal has not been admitted. Press any key to continue.");
                            Console.ReadLine();
                            RunUserMenus();
                        }
                    }
                    RunUserMenus();
                }
            }
            catch
            {
                UserInterface.DisplayUserOptions("There is something wrong with the file you are attempting to load or the file does not exist. Please check it and try again. Press any key to continue.");
                Console.ReadLine();
                RunUserMenus();
            }
        }