예제 #1
0
        private static void Edit()
        {
            //collect the id of the trail to edit
            Console.Write("ID of trail to edit: ");
            int.TryParse(Console.ReadLine(), out var trailEditId);

            //get the list of trails
            _trailList = _trailService.GetAll();
            Trail trailToEdit = _trailList.Find(s => s.Trail_Id == trailEditId);

            //prompt the user for the name of the edited trail and save the result as newName
            Console.Write("Trail Name: ");
            string newName = Console.ReadLine();

            //prompt the user for the trail location and save the result as newLocation
            Console.Write("Trail Location: ");
            string newLocation = Console.ReadLine();

            //prompt the user for the trail length
            Console.Write("Trail Length (miles): ");

            //create a loop to check that the trail length is greater than 0
            bool success;
            int  length;
            int  newLength = 1;

            do
            {
                success = int.TryParse(Console.ReadLine(), out length);
                //if trail length > 0, save the result as newLength
                if (success && length > 0)
                {
                    newLength = length;
                }
                //if trail length is an invalid number prompt the user for another input
                else
                {
                    Console.WriteLine("You did not enter a correct value for length");
                    Console.Write("Trail Length: ");
                }
            } while (success == false || length <= 0);

            //prompt the user for the trail summary and save the result as newSummary
            Console.Write("Trail Summary: ");
            string newSummary = Console.ReadLine();

            //make sure a trail with the specified id exists before attempting to edit it
            if (trailToEdit != null)
            {
                //edit the trail
                _trailService.Edit(trailToEdit, _trailList, newName, newLocation, newLength, newSummary);

                Console.Write($"Trail ID: {trailEditId} was edited.");
                System.Threading.Thread.Sleep(2000);
            }
            else
            {
                //if a trail with that id doesn't exist show an error
                Console.Write($"ERROR: Could not find trail with ID: {trailEditId}.");
                System.Threading.Thread.Sleep(2000);
            }
        }