Exemple #1
0
        public void ShouldReturnNullForBandColorThatHasNoTolerance()
        {
            IOhmValueCalculator ohmValueCalculator = new OhmValueCalculatorImpl();
            OhmValue            ohmValue           = ohmValueCalculator.CalculateOhmValue("red", "violet", "green", "orange");

            ohmValue.Should().BeNull();
        }
Exemple #2
0
        public void ShouldReturnNullForInvalidColor()
        {
            IOhmValueCalculator ohmValueCalculator = new OhmValueCalculatorImpl();
            OhmValue            ohmValue           = ohmValueCalculator.CalculateOhmValue("red", "violet", "indigo", "brown");

            ohmValue.Should().BeNull();
        }
Exemple #3
0
        public void ShouldReturnNullForBandColorThatHasNoSigFigs()
        {
            IOhmValueCalculator ohmValueCalculator = new OhmValueCalculatorImpl();
            OhmValue            ohmValue           = ohmValueCalculator.CalculateOhmValue("pink", "violet", "green", "brown");

            ohmValue.Should().BeNull();
        }
Exemple #4
0
        public OhmValue CalculateOhmValue(string bandAColor, string bandBColor, string bandCColor, string bandDColor)
        {
            try
            {
                BandInformation bandA = bandInformationDictionary[bandAColor];
                BandInformation bandB = bandInformationDictionary[bandBColor];
                BandInformation bandC = bandInformationDictionary[bandCColor];
                BandInformation bandD = bandInformationDictionary[bandDColor];

                if (bandA.SignificantFigures == null || bandB.SignificantFigures == null || bandD.Tolerance == null)
                {
                    return(null);
                }

                double Resistance = Int32.Parse(bandA.SignificantFigures.ToString() + bandB.SignificantFigures.ToString()) * bandC.Multiplier;
                double Variance   = Resistance * (double)bandD.Tolerance;

                OhmValue ohmValue = new OhmValue
                {
                    Resistance = Resistance,
                    Tolerance  = (double)bandD.Tolerance,
                    Minimum    = Resistance - Variance,
                    Maximum    = Resistance + Variance
                };

                return(ohmValue);
            } catch (Exception)
            {
                return(null);
            }
        }
Exemple #5
0
        public void ShouldCorrectlyCalculateOhmValue()
        {
            IOhmValueCalculator ohmValueCalculator = new OhmValueCalculatorImpl();
            OhmValue            ohmValue           = ohmValueCalculator.CalculateOhmValue("red", "violet", "green", "brown");

            ohmValue.Resistance.Should().Be(2700000);
            ohmValue.Minimum.Should().Be(2673000);
            ohmValue.Maximum.Should().Be(2727000);
        }
        public IActionResult Get(string bandAColor, string bandBColor, string bandCColor, string bandDColor)
        {
            OhmValue value = _ohmValueCalculator.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor);

            if (value == null)
            {
                return(BadRequest());
            }

            return(Ok(value));
        }
Exemple #7
0
        /// <summary>
        /// Calculates the Ohm value of a resistor based on the band colors.
        /// </summary>
        /// <param name="bandAColor">The color of the first figure of component value band.</param>
        /// <param name="bandBColor">The color of the second significant figure band.</param>
        /// <param name="bandCColor">The color of the decimal multiplier band.</param>
        /// <param name="bandDColor">The color of the tolerance value band.</param>
        public OhmValue CalculateOhmValue(string bandAColor, string bandBColor, string bandCColor, string bandDColor)
        {
            if (!_colorCodeDict.ContainsKey(bandAColor))
            {
                throw new InvalidParameterException("invalid parameters");
            }
            var colorACode = _colorCodeDict[bandAColor];

            if (!_colorCodeDict.ContainsKey(bandBColor))
            {
                throw new InvalidParameterException("invalid parameters");
            }
            var colorBCode = _colorCodeDict[bandBColor];

            if (!_colorCodeDict.ContainsKey(bandCColor))
            {
                throw new InvalidParameterException("invalid parameters");
            }
            var colorCCode = _colorCodeDict[bandCColor];

            if (!string.IsNullOrEmpty(bandDColor) && !_colorCodeDict.ContainsKey(bandDColor))
            {
                throw new InvalidParameterException("invalid parameters");
            }

            if (!colorACode.isSignificant || !colorBCode.isSignificant || !colorCCode.isMultiplier)
            {
                throw new InvalidParameterException("invalid parameters");
            }

            double abValue  = colorACode.significantDigits.Value * 10.0 + colorBCode.significantDigits.Value;
            double abcValue = abValue * colorCCode.multiplier.Value;
            var    ohmValue = new OhmValue();
            var    tolerate = GetTolerate(bandDColor);

            ohmValue.minValue = Math.Round(abcValue * (1 - tolerate));
            ohmValue.maxValue = Math.Round(abcValue * (1 + tolerate));

            return(ohmValue);
        }