Exemple #1
0
        /// <summary>
        /// Writes the expected program outputs to the stdout.
        /// </summary>
        /// <param name="railwaySystem">An instance of <see cref="IRailwaySystem"/>.</param>
        private static void WriteOutput(IRailwaySystem railwaySystem)
        {
            var tripService = new TripService();
            var pathBuilder = new PathBuilder();

            Console.WriteLine();
            SafeWriteOutput(1, () => tripService.ResolveDistance(pathBuilder.Build(railwaySystem, "A", "B", "C")).ToString());
            SafeWriteOutput(2, () => tripService.ResolveDistance(pathBuilder.Build(railwaySystem, "A", "D")).ToString());
            SafeWriteOutput(3, () => tripService.ResolveDistance(pathBuilder.Build(railwaySystem, "A", "D", "C")).ToString());
            SafeWriteOutput(4, () => tripService.ResolveDistance(pathBuilder.Build(railwaySystem, "A", "E", "B", "C", "D")).ToString());
            SafeWriteOutput(5, () => tripService.ResolveDistance(pathBuilder.Build(railwaySystem, "A", "E", "D")).ToString());
            Console.WriteLine($"Output #6: {tripService.Search(railwaySystem.GetTownByName("C"), railwaySystem.GetTownByName("C"), trip => trip.Stops > 3).Count()}");
            Console.WriteLine($"Output #7: {tripService.Search(railwaySystem.GetTownByName("A"), railwaySystem.GetTownByName("C"), trip => trip.Stops > 4, trip => trip.Stops == 4).Count()}");
            SafeWriteOutput(8, () => tripService.FindShortest(railwaySystem, railwaySystem.GetTownByName("A"), railwaySystem.GetTownByName("C")).TotalDistance.ToString());
            SafeWriteOutput(9, () => tripService.FindShortest(railwaySystem, railwaySystem.GetTownByName("B"), railwaySystem.GetTownByName("B")).TotalDistance.ToString());
            Console.WriteLine($"Output #10: {tripService.Search(railwaySystem.GetTownByName("C"), railwaySystem.GetTownByName("C"), trip => trip.TotalDistance >= 30).Count()}");
            Console.WriteLine();
        }
        /// <summary>
        /// Builds an <see cref="IPath"/> instance based on the provided <paramref name="railwaySystem"/> and <paramref name="townsNames"/>.
        /// </summary>
        /// <param name="railwaySystem">An instance of <see cref="RailwaySystem"/>.</param>
        /// <param name="townsNames">An <see cref="string"/> array with the sequence of towns that constitutes the <see cref="IPath"/>.</param>
        /// <returns>An instance of <see cref="IPath"/>.</returns>
        public IPath Build(IRailwaySystem railwaySystem, params string[] townsNames)
        {
            var path = new Path();

            foreach (var townName in townsNames)
            {
                path.AddStop(railwaySystem.GetTownByName(townName));
            }
            return(path);
        }