예제 #1
0
        public void ShowResult()
        {
            _outputter.WriteLine(PreparedStrings.ResultBegin);
            Values.ForEach(v => _outputter.Write($"{v} "));
            _outputter.WriteLine(Empty);

            _outputter.WriteLine(Format(PreparedStrings.MinValueOutput, Values.Min(), Values.IndexOf(Values.Min())));
            _outputter.WriteLine(Format(PreparedStrings.MaxValueOutput, Values.Max(), Values.IndexOf(Values.Max())));
        }
예제 #2
0
        public void Classify(TriangleSideLengths triangleSideLengths)
        {
            if (triangleSideLengths != null)
            {
                var  errorMessage     = new StringBuilder();
                bool validationFailed = false;
                if (triangleSideLengths.LengthOfSideA <= 0)
                {
                    errorMessage.AppendLine("Length of side A is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (triangleSideLengths.LengthOfSideB <= 0)
                {
                    errorMessage.AppendLine("Length of side B is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (triangleSideLengths.LengthOfSideC <= 0)
                {
                    errorMessage.AppendLine("Length of side C is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (validationFailed)
                {
                    _outputter.WriteLine(errorMessage.ToString());
                }
                else
                {
                    var triangleTypes = new ITriangle[] { new EquilateralTriangle(), new IsoscelesTriangle(), new ScaleneTriangle() };

                    bool matchFound = false;
                    foreach (var triangleType in triangleTypes)
                    {
                        if (triangleType.DoMatch(triangleSideLengths))
                        {
                            matchFound = true;
                            _outputter.WriteLine(triangleType.Description);
                            break;
                        }
                    }
                    if (!matchFound)
                    {
                        _outputter.WriteLine("Congratulations! You found a triangle that is not supposed to exist.");
                    }
                }
            }
        }
예제 #3
0
 private void Output(string key, Func <string> valueFunc, bool @if = true)
 {
     if (@if)
     {
         outputter.WriteLine($"{key}:{valueFunc()}");
     }
 }
예제 #4
0
 public Parser(string fileLocation, IOutputter outputter) : this(outputter)
 {
     _outputter.Write($"Loading character pool from {fileLocation}... ");
     _rawBytes = File.ReadAllBytes(fileLocation);
     _outputter.WriteLine("Success!");
 }
예제 #5
0
        public TriangleSideLengths ParseAndValidateCommandLineArguments(string[] args)
        {
            TriangleSideLengths triangleSideLengths;

            if (args.Length < 3)
            {
                ShowUsage();
                triangleSideLengths = null;
            }
            else
            {
                int  lengthOfSideA;
                bool argumentForLengthOfSideAIsOk = int.TryParse(args[0], out lengthOfSideA);
                int  lengthOfSideB;
                bool argumentForLengthOfSideBIsOk = int.TryParse(args[1], out lengthOfSideB);
                int  lengthOfSideC;
                bool argumentForLengthOfSideCIsOk = int.TryParse(args[2], out lengthOfSideC);

                var  errorMessage     = new StringBuilder();
                bool validationFailed = false;
                if (!argumentForLengthOfSideAIsOk || !argumentForLengthOfSideBIsOk || !argumentForLengthOfSideCIsOk)
                {
                    if (!argumentForLengthOfSideAIsOk)
                    {
                        errorMessage.AppendLine("Argument for length of side A is illegal. Valid arguments are integers greater than 0.");
                    }
                    if (!argumentForLengthOfSideBIsOk)
                    {
                        errorMessage.AppendLine("Argument for length of side B is illegal. Valid arguments are integers greater than 0.");
                    }
                    if (!argumentForLengthOfSideCIsOk)
                    {
                        errorMessage.AppendLine("Argument for length of side C is illegal. Valid arguments are integers greater than 0.");
                    }
                    validationFailed = true;
                }
                if (argumentForLengthOfSideAIsOk && lengthOfSideA <= 0)
                {
                    errorMessage.AppendLine("Length of side A is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (argumentForLengthOfSideBIsOk && lengthOfSideB <= 0)
                {
                    errorMessage.AppendLine("Length of side B is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (argumentForLengthOfSideCIsOk && lengthOfSideC <= 0)
                {
                    errorMessage.AppendLine("Length of side C is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (validationFailed)
                {
                    _outputter.WriteLine(errorMessage.ToString());
                    triangleSideLengths = null;
                }
                else
                {
                    triangleSideLengths = new TriangleSideLengths
                    {
                        LengthOfSideA = lengthOfSideA,
                        LengthOfSideB = lengthOfSideB,
                        LengthOfSideC = lengthOfSideC
                    };
                }
            }

            return(triangleSideLengths);
        }
예제 #6
0
 private void OutputResult(XDocument files, IOutputter outputter)
 {
     outputter.WriteLine(files.ToString());
 }
예제 #7
0
        public void Classify(TriangleSideLengths triangleSideLengths)
        {
            if (triangleSideLengths != null)
            {
                var  errorMessage     = new StringBuilder();
                bool validationFailed = false;
                if (triangleSideLengths.LengthOfSideA <= 0)
                {
                    errorMessage.AppendLine("Length of side A is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (triangleSideLengths.LengthOfSideB <= 0)
                {
                    errorMessage.AppendLine("Length of side B is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (triangleSideLengths.LengthOfSideC <= 0)
                {
                    errorMessage.AppendLine("Length of side C is not valid. Valid lengths are greater than 0.");
                    validationFailed = true;
                }
                if (validationFailed)
                {
                    _outputter.WriteLine(errorMessage.ToString());
                }
                else
                {
                    var tasks  = new Task[3];
                    var result = new ConcurrentDictionary <string, bool>();
                    tasks[0] = Task.Run(() =>
                    {
                        var triangleType = new EquilateralTriangle();
                        if (triangleType.DoMatch(triangleSideLengths))
                        {
                            _outputter.WriteLine(triangleType.Description);
                            result.TryAdd("EquilateralTriangle", true);
                        }
                        else
                        {
                            result.TryAdd("EquilateralTriangle", false);
                        }
                    });
                    tasks[1] = Task.Run(() =>
                    {
                        var triangleType = new IsoscelesTriangle();
                        if (triangleType.DoMatch(triangleSideLengths))
                        {
                            _outputter.WriteLine(triangleType.Description);
                            result.TryAdd("IsoscelesTriangle", true);
                        }
                        else
                        {
                            result.TryAdd("IsoscelesTriangle", false);
                        }
                    });
                    tasks[2] = Task.Run(() =>
                    {
                        var triangleType = new ScaleneTriangle();
                        if (triangleType.DoMatch(triangleSideLengths))
                        {
                            _outputter.WriteLine(triangleType.Description);
                            result.TryAdd("ScaleneTriangle", true);
                        }
                        else
                        {
                            result.TryAdd("ScaleneTriangle", false);
                        }
                    });

                    Task.WaitAll(tasks);
                    if (!result.Any(p => p.Value))
                    {
                        _outputter.WriteLine("Congratulations! You found a triangle that is not supposed to exist.");
                    }
                }
            }
        }