Esempio n. 1
0
        public void AssembleTrain_SetDestination_RemovesAllCarsFromSortingLines()
        {
            // arrange
            var map = Yard.GetLinesMap(Destination);

            // act
            var steps = Yardmaster.AssembleTrain(map);

            // assert
            var sortingLines = Yard.GetSortingLines();

            Assert.IsNotNull(sortingLines);
            Assert.IsFalse(sortingLines.Any(line => line.ContainsCarToDestination(Destination)));
        }
Esempio n. 2
0
        public void AssembleTrain_SetDestination_HasExpectedCarsInTrainLine()
        {
            // arrange
            var map = Yard.GetLinesMap(Destination);

            // act
            Yardmaster.AssembleTrain(map);

            // assert
            var trainLine = Yard.TrainLine;

            Assert.IsNotNull(trainLine);

            var cars = trainLine.ToString().ToCharArray();

            Assert.AreEqual(2, cars.Count());
            Assert.IsTrue(cars.All(car => Destination.Equals(car)));
        }
Esempio n. 3
0
        public void AssembleTrain_NotEnoughRoomInSortingLines_DoesNotMakeAnyMovement()
        {
            // arrange
            var lines = new List<char[]>
            {
                "0BDCA".ToCharArray(),
                "0CBDA".ToCharArray(),
                "0DCBA".ToCharArray()
            };
            var yard = new Yard(IdGenerator, Configuration.Object, lines);
            var yardmaster = yard.Yardmaster;
            var map = yard.GetLinesMap(Destination);

            // act
            var steps = yardmaster.AssembleTrain(map);

            // assert
            Assert.IsFalse(steps.Any());
        }
Esempio n. 4
0
        public void AssembleTrain_NotEnoughRoomInSortingLines_DoesNotMakeAnyMovement()
        {
            // arrange
            var lines = new List <char[]>
            {
                "0BDCA".ToCharArray(),
                "0CBDA".ToCharArray(),
                "0DCBA".ToCharArray()
            };
            var yard       = new Yard(IdGenerator, Configuration.Object, lines);
            var yardmaster = yard.Yardmaster;
            var map        = yard.GetLinesMap(Destination);

            // act
            var steps = yardmaster.AssembleTrain(map);

            // assert
            Assert.IsFalse(steps.Any());
        }
Esempio n. 5
0
        public static void Main(string[] args)
        {
            var configuration = new Configuration();
            var idGenerator = new SequentialIdGenerator();

            var fileReader = new FileReader();
            var ui = new ConsoleUtil(configuration, fileReader);

            do
            {
                ui.ShowMessage("Welcome to the Marshalling Yard!");

                // Load sorting lines from file or prompt the user for file location
                ui.BeginSection();
                var lines = ui.GetSortingLines();
                IYard yard = null;
                try
                {
                    yard = new Yard(idGenerator, configuration, lines);
                }
                catch (InvalidOperationException)
                {
                    var message = "Cannot create marshalling yard! Check the application's parameters.";
                    ui.ShowErrorMessage(message);
                    break;
                }

                var yardmaster = yard.Yardmaster;

                // Show initial sorting lines
                ui.ShowMessage("Initial sorting lines:");
                var sortingLines = yard.GetSortingLines().Select(line => line.ToString());
                ui.ShowList(sortingLines);

                // Prompt user for destination
                ui.BeginSection();
                var destination = ui.GetDestination();

                // Create navigational map and move cars
                var linesMap = yard.GetLinesMap(destination);
                var steps = yardmaster.AssembleTrain(linesMap);

                // Show list of steps
                ui.BeginSection();
                ui.ShowMessage($"Selected destination: {destination}");
                ui.ShowMessage("Movements:");
                ui.ShowList(steps.Select(step => step.ToString()));

                // Show total of movements
                steps.Select(step => step.ToString());
                ui.ShowMessage($"Total of movements: {steps.Count()}.");

                // Show final sorting lines
                ui.BeginSection();
                ui.ShowMessage("Final sorting lines:");
                sortingLines = yard.GetSortingLines().Select(line => line.ToString());
                ui.ShowList(sortingLines);

                // Show train line
                ui.BeginSection();
                ui.ShowMessage("Train line:");
                var trainLine = yard.TrainLine.ToString();
                ui.ShowList(new[] { trainLine });
                ui.ShowMessage($"Total of cars in train line: {trainLine.Count()}.");

            } while (!ui.HasUserChosenToExit());
        }