public void CalculateOhmValueForAllBlackIsZero()
        {
            int result = _OhmValueCalculator.CalculateOhmValue(Convert.ToString((int)BandColor.Black),
                                                               Convert.ToString((int)BandColor.Black),
                                                               Convert.ToString((int)BandColor.Black),
                                                               Convert.ToString((int)BandColor.Black));

            Assert.True(result.Equals(0));
        }
Example #2
0
        public void TestCalculateOhmValueSuccess()
        {
            string bandAColor = "Yellow";
            string bandBColor = "Violet";
            string bandCColor = "Red";
            string bandDColor = "Gold";

            int ohmValue = _ohmValCalculator.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor);

            Assert.AreEqual(4700, ohmValue);
        }
Example #3
0
        public void CalculateOhmValuePositiveTest()
        {
            string bandAClr = "Yellow";
            string bandBclr = "Voilet";
            string bandCclr = "Red";
            string bandDclr = "Gold";

            ResistanceValues actualValue = ohmCalcService.CalculateOhmValue(bandAClr, bandBclr, bandCclr, bandDclr);

            Assert.AreEqual(4465, actualValue.minValue);
            Assert.AreEqual(4935, actualValue.maxValue);
        }
        public void Test0Bands()
        {
            var bands = new[] { string.Empty, null, null, null };
            var ex    = null as Exception;

            try { var result = calculator.CalculateOhmValue(bands[0], bands[1], bands[2], bands[3]); }
            catch (Exception e) { ex = e; }

            Assert.IsNotNull(ex);
        }
        // GET: ResistanceColorDecoder
        public ActionResult Index()
        {
            if (null != _IOhmValueCalculator)
            {
                // Calculate the resistance value

                var results = _IOhmValueCalculator.CalculateOhmValue(
                    _ResistorModel.ColorBandA, _ResistorModel.ColorBandB, _ResistorModel.ColorBandC, _ResistorModel.ColorBandD);
                if (results[0] > 0 && results[1] > 0)
                {
                    _ResistorModel.SetResistanceValue(results[0]);
                    _ResistorModel.SetToleranceValue(results[1]);
                }
            }
            return(View(_ResistorModel));
        }
        public CalculatedOhmForResistor Get(string bandACode, string bandDCode, string bandBCode = "", string bandCCode = "")
        {
            CalculatedOhmForResistor returnValue;

            try
            {
                returnValue = _ohmValueCalculatorService.CalculateOhmValue(bandACode, bandDCode, bandBCode, bandCCode);
            }
            catch (BandNotFoundException ex)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NoContent)
                {
                    Content      = new StringContent(ex.ExceptionMessage),
                    ReasonPhrase = "Color band not found."
                };
                throw new HttpResponseException(resp);
            }
            catch (WrongColorBandSelectedException ex)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NoContent)
                {
                    Content      = new StringContent(ex.ExceptionMessage),
                    ReasonPhrase = "Wrong Color band selected. We can't rate the resistor."
                };
                throw new HttpResponseException(resp);
            }
            return(returnValue);
        }
Example #7
0
        public override void CalculatorTest(string bandA, string bandB, string bandC, string bandD, int ohms)
        {
            IOhmValueCalculator instance = CreateInstance();
            int sutOhms = instance.CalculateOhmValue(bandA, bandB, bandC, bandD);

            Assert.AreEqual(sutOhms, ohms);
        }
Example #8
0
        public async Task <ActionResult> Index(IndexViewModel indexVM)
        {
            var response = new IndexResponseModel();

            try
            {
                ValidateJsonAntiForgeryToken(indexVM.__RequestVerificationToken);

                if (ModelState.IsValid)
                {
                    response.OhmValue = await Task.Run(() => ohmCalculator.CalculateOhmValue(indexVM.BandAColor, indexVM.BandBColor, indexVM.BandCColor, indexVM.BandDColor));
                }
                else
                {
                    throw new ECCException("Invalid parameters");
                }
            }
            catch (ECCException ec)
            {
                response.IsError      = true;
                response.ErrorMessage = ec.Message;
            }
            catch
            {
                response.IsError      = true;
                response.ErrorMessage = "Unexpected Error has occurred";
            }

            return(Json(response));
        }
Example #9
0
 public int GetOhmValue(OhmRequest request)
 {
     return(_OhmValueCalculator.CalculateOhmValue(Convert.ToString((int)request.bandAColor),
                                                  Convert.ToString((int)request.bandBColor),
                                                  Convert.ToString((int)request.bandCColor),
                                                  Convert.ToString((int)request.bandDColor)));
 }
        public String Calculate(string model)
        {
            //Color[] selectedColors = JsonConvert.DeserializeObject<Color[]>("[{Name: \"Yellow\"}, {Name: \"Violet\"}, {Name: \"Red\"}, {Name: \"Gold\"}]");
            Color[] selectedColors = JsonConvert.DeserializeObject <Color[]>(Convert.ToString(model));
            int     resistance     = _calculator.CalculateOhmValue(selectedColors[0].Name, selectedColors[1].Name, selectedColors[2].Name, selectedColors[3].Name);

            return(resistance.ToString() + " ohms");
        }
 public int CalculateOhmValue(
     [FromQuery] string a,
     [FromQuery] string b,
     [FromQuery] string c,
     [FromQuery] string d
     )
 {
     return(calculator.CalculateOhmValue(a, b, c, d));
 }
Example #12
0
 public string Get(string colorA, string colorB, string multiplier, string percent)
 {
     return(_ohm.CalculateOhmValue(
                colorA,
                colorB,
                multiplier,
                percent,
                _rt).ToString());
 }
Example #13
0
        /// <summary>
        /// Calulate the resistance value based on band selected band colors
        /// </summary>
        /// <param name="BandA"></param>
        /// <param name="BandB"></param>
        /// <param name="BandC"></param>
        /// <param name="BandD"></param>
        /// <returns>Returns a Json result to populate on the view</returns>
        public JsonResult Calculate(string BandA, string BandB, string BandC, string BandD)
        {
            //Calculate OhmValue here
            var ohmValueRange = bandEntities.CalculateOhmValue(BandA, BandB, BandC, BandD);

            //return OhmValue as range
            return(Json(new { maxValue = ohmValueRange["maxValue"].ToString(),
                              minValue = ohmValueRange["minValue"].ToString() }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult LookupOhmValue(LookupOhmModel model)
        {
            //if (!ModelState.IsValid)

            int ohmValue = _ohmValCalculator.CalculateOhmValue(model.BandAColor, model.BandBColor, model.BandCColor, model.BandDColor);

            return(Json(new LookupOhmValueResponse {
                Status = true,
                OhmValue = ohmValue
            }));
        }
        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));
        }
        /// <summary>
        /// Calculates the Ohm value of a resistor based on the band colors and the tolerance value band.
        /// </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>
        /// <returns>Json Result</returns>
        public JsonResult GetOhmValue(string bandAValue, string bandBValue, string bandCValue, string bandDValue)
        {
            Regex regex = new Regex(@"^[0-9]\d*(\.\d+)?$");

            if (regex.IsMatch(bandAValue) && regex.IsMatch(bandBValue) && regex.IsMatch(bandCValue) && regex.IsMatch(bandDValue))
            {
                int retVal = 0;
                //_ohmCalculator.FinalValue = _ohmValueCalculator.CalculateOhmValue(bandAValue, bandBValue, bandCValue, bandDValue);
                retVal = _ohmValueCalculator.CalculateOhmValue(bandAValue, bandBValue, bandCValue, bandDValue);
                _ohmCalculator.FinalValue = retVal * Convert.ToDecimal(bandCValue);
                _ohmCalculator.Tolerance  = "ohms and " + bandDValue + "% tolerance";
            }
            return(Json(_ohmCalculator, JsonRequestBehavior.AllowGet));
        }
        public void CalculateOhmValueTest()
        {
            IOhmValueCalculator target = CreateIOhmValueCalculator(); // TODO: Initialize to an appropriate value
            string bandAColor          = string.Empty;                // TODO: Initialize to an appropriate value
            string bandBColor          = string.Empty;                // TODO: Initialize to an appropriate value
            string bandCColor          = string.Empty;                // TODO: Initialize to an appropriate value
            string bandDColor          = string.Empty;                // TODO: Initialize to an appropriate value
            int    expected            = 0;                           // TODO: Initialize to an appropriate value
            int    actual;

            actual = target.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Example #18
0
        public JsonResult CalculateOhmValue(string bandAColor, string bandBColor, string bandCColor, string bandDColor)
        {
            int    ohmValue = ohmCalculator.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor);
            string result;

            if (ohmValue != 0)
            {
                result = ohmCalculator.DecodeOhmValue(ohmValue);
            }
            else
            {
                result = "An unexpected input was received, please try again";
            }

            return(Json(result));
        }
 public IActionResult Get(string bandAColor, string bandBColor, string bandCColor, string bandDColor)
 {
     if (string.IsNullOrEmpty(bandAColor) || string.IsNullOrEmpty(bandBColor) || string.IsNullOrEmpty(bandCColor))
     {
         return(BadRequest("All colors must be filled."));
     }
     try
     {
         var result = calculator.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor);
         return(Ok(new ResistantResult(result, calculator.ResistanceValue.Tolorance,
                                       calculator.ResistanceValue.Minimum, calculator.ResistanceValue.Maximum)));
     }catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
 public ActionResult ResistanceValue([FromBody] FourBands bandcolors)
 {
     try
     {
         if (bandcolors == null)
         {
             throw new ArgumentNullException();
         }
         CalculatorResult resultObject = _ohmValueCalculator.CalculateOhmValue(bandcolors.bandAColor,
                                                                               bandcolors.bandBColor, bandcolors.bandCColor, bandcolors.bandDColor);
         return(Json(new { min = resultObject.MinResistance, max = resultObject.MaxResistance }));
     }
     catch (Exception ex)
     {
         return(Json("Exception ocurred while calculating resistance value: " + ex.Message));
     }
 }
Example #21
0
        public ActionResult Index(RingColorModel model)
        {
            var response = new ResultModel();

            try
            {
                ValidateJsonAntiForgeryToken(model.__RequestVerificationToken);
                if (ModelState.IsValid)
                {
                    response.OhmValue = calc.CalculateOhmValue(model.BandAColor, model.BandBColor, model.BandCColor, model.BandDColor);
                }
            }
            catch (Exception ex)
            {
                response.IsError      = true;
                response.ErrorMessage = ex.Message;
            }
            return(Json(response));
        }
        public void TestOhmValueCalculation()
        {
            string color_band_A = ResistanceColorCode.ValueToString(ResistanceColorCode.ColorValueCode.Brown);
            string color_band_B = ResistanceColorCode.ValueToString(ResistanceColorCode.ColorValueCode.Green);
            string color_band_C = ResistanceColorCode.ValueToString(ResistanceColorCode.ColorValueCode.Red);
            string color_band_D = ResistanceColorCode.ToleranceValueToString(ResistanceColorCode.ColorPercentToleranceCode.Silver);

            double[] results = calculator.CalculateOhmValue(
                color_band_A, color_band_B, color_band_C, color_band_D);

            // Check value = 1500
            double expectedVal = 1500;

            Assert.IsTrue(results[0] == expectedVal);

            // check tolerance = 10
            double expectedTol = 10;

            Assert.IsTrue(results[1] == expectedTol);
        }
        public ActionResult GetResistanceValue(string bandAColor, string bandBColor, string bandCColor, string bandDColor)
        {
            try
            {
                ColorCodes colorCodes = new ColorCodes();
                // Call library method to calculcate the resistance value.
                double ohmValue = _ohmCalculator.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor);

                var tolerance = colorCodes.Tolerance[bandDColor].Item2;

                string resistance = Formatters.FormatResistance(ohmValue, tolerance);

                /// return JSON response
                return(Json(new { resistance = resistance }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                throw ex;
            }
        }
Example #24
0
        private void buttonCalculate_Click(object sender, RoutedEventArgs e)
        {
            if ((comboBandA.SelectedItem == null) || (comboBandA.SelectedItem == null) || (comboBandA.SelectedItem == null))
            {
                MessageBox.Show("Please select colors for Bands A, B and C");
                return;
            }

            string bandAColor = ((KeyValuePair <string, int>)comboBandA.SelectedItem).Key;
            string bandBColor = ((KeyValuePair <string, int>)comboBandB.SelectedItem).Key;
            string bandCColor = ((KeyValuePair <string, string>)comboBandC.SelectedItem).Key;
            string bandDColor = ((KeyValuePair <string, double>)comboBandD.SelectedItem).Key;

            _ohmValue = _ohmValueCalculator.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor);

            double tolValue = ((KeyValuePair <string, double>)comboBandD.SelectedItem).Value;

            string unitValue = ((OhmValueCalculator)_ohmValueCalculator).UnitValue;

            textBoxOhmValue.Text = string.Format("{0} {1}. Tolerance: +/- {2}. ", (((KeyValuePair <string, string>)comboBandC.SelectedItem).Value.Contains(".")) ? ((OhmValueCalculator)_ohmValueCalculator).ActualValue:_ohmValue,
                                                 (string.IsNullOrEmpty(unitValue)? "ohms" : unitValue + " ohms"), tolValue);
        }
        public void MinLimitTest()
        {
            var val = ohmValueCalculator.CalculateOhmValue("Black", "Black", "Black", "Black");

            Assert.AreEqual(0, val);
        }
        public void MinLimitTest()
        {
            var result = ObjOhmValueCalculator.CalculateOhmValue("BK", "BK", "BK", "BK");

            Assert.AreEqual(0, result);
        }
Example #27
0
 public void CalculateOhmValueTest()
 {
     Assert.AreEqual(calculator.CalculateOhmValue("YE", "VT", "RD"), 4700);
 }
Example #28
0
 public async Task <OhmValueResponse> CalculateOhmValue([FromBody] InputColorCodesModel inputColorCodes)
 {
     return(await _ohmValueCalculator.CalculateOhmValue(inputColorCodes.BandAColor, inputColorCodes.BandBColor,
                                                        inputColorCodes.BandCColor, inputColorCodes.BandDColor));
 }
 public IActionResult Calculate(string bandAColor, string bandBColor, string bandCColor, string bandDColor
                                , [FromServices] IOhmValueCalculator fourBandResistorCalculator
                                , [FromServices] IFourColorCodeBandsViewModel fourColorCodeBandsViewModel) =>
 Json(fourBandResistorCalculator.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor)
      .ToFormattedOhms(bandDColor, fourColorCodeBandsViewModel));
Example #30
0
 public IActionResult CalculateOhmValue(string bandAColor, string bandBColor, string bandCColor, string bandDColor)
 {
     return(ProcessRequest(() => {
         return Ok(_calculator.CalculateOhmValue(bandAColor, bandBColor, bandCColor, bandDColor));
     }));
 }