Exemplo n.º 1
0
        private void AddAnimal()
        {
            Console.Write("Enter the name of the animal to add: ");
            var name = Console.ReadLine();

            var currAnimal = _animalService.GetAnimalByName(name);

            if (currAnimal != null)
            {
                ConsoleWrite($"The animal you entered ('{name}') already exists in the file. Please enter a different name.", true);
                return;
            }


            var attrs      = new List <AnimalAttribute>();
            var toContinue = true;

            while (toContinue)
            {
                Console.WriteLine();
                Console.WriteLine($"Select an attribute of animal \"{name}\":");
                Console.WriteLine("[A] Action (verb) e.g. 'fly', 'bark', 'eat cheese'");
                Console.WriteLine("[B] Has e.g. 'four legs', 'wings'");
                Console.WriteLine("[C] Is (adjective) e.g. 'big', 'yellow colour', 'an insect'");
                Console.WriteLine("[X] Cancel");

                var input = GetValidInput(new[] { "A", "B", "C", "X" });

                var message = string.Empty;
                var type    = default(AttributeType);
                switch (input)
                {
                case "A":
                    message = $"A(n) {name} can ";
                    type    = AttributeType.Action;
                    break;

                case "B":
                    message = $"A(n) {name} has ";
                    type    = AttributeType.Has;
                    break;

                case "C":
                    message = $"A(n) {name} is ";
                    type    = AttributeType.Is;
                    break;

                case "X":
                    ConsoleWrite("Cancelled adding animal.", true);
                    return;
                }
                Console.WriteLine();
                Console.Write(message);
                var attr = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(attr))
                {
                    ConsoleWrite("At least one animal attribute must be entered.", true);
                }
                else
                {
                    if (attrs.Exists(
                            a => a.Type == type && a.Text.Equals(attr, StringComparison.OrdinalIgnoreCase)))
                    {
                        ConsoleWrite($"Error: The description you entered already exists for the {name}.", true);
                    }
                    else
                    {
                        attrs.Add(new AnimalAttribute()
                        {
                            Text = attr, Type = type
                        });
                    }
                    Console.WriteLine();
                    Console.Write("Do you want to add another description [Y/N]? ");
                    toContinue = IsYes();
                }
            }

            // save animal to data store (file)
            _animalService.AddAnimal(new Animal()
            {
                Name = name, Attributes = attrs
            });

            // refresh data
            _animals = _animalService.GetAnimals();

            ConsoleWrite($"Done. Successfully added '{name}' to animal list. Data has been refreshed.");
        }