Пример #1
0
        /// <summary>
        /// This is the Generic Method for length, weight, volume and temperature unit conversion
        /// </summary>
        /// <param name="measurementType">It contains the Measurement Type</param>
        /// <param name="conversionType">It contains the Conversion Type</param>
        /// <param name="value">It contains the value to be convert</param>
        /// <returns>It returns the converted unit result</returns>
        private double Conversion(string measurementType, string conversionType, double value)
        {
            try
            {
                // Check if Measurement type is length
                if (measurementType == "length")
                {
                    Length      length = new Length();
                    Length.Unit unit   = length.SetUnitAndConvertLength(conversionType);

                    // Check enum units and conversion Type
                    if (unit == Length.Unit.FeetToInch || unit == Length.Unit.YardToInch || unit == Length.Unit.CentimeterToInch || unit == Length.Unit.Inch)
                    {
                        return(length.ConvertLength(unit, value));
                    }
                }
                else if (measurementType == "weight")
                {
                    Weight      weight = new Weight();
                    Weight.Unit unit   = weight.SetUnitAndConvertWeights(conversionType);

                    // Check enum units and conversion Type
                    if (unit.Equals(Weight.Unit.GramsToKilogram) || unit.Equals(Weight.Unit.TonneToKilograms) || unit.Equals(Weight.Unit.kilogram))
                    {
                        return(weight.ConvertWeigths(unit, value));
                    }
                }
                else if (measurementType == "volume")
                {
                    Volume      volume = new Volume();
                    Volume.Unit unit   = volume.SetUnitAndConvertVolume(conversionType);

                    // Check enum units and conversion Type
                    if (unit.Equals(Volume.Unit.GallonToLitre) || unit.Equals(Volume.Unit.MiliLitreToLitre) || unit.Equals(Volume.Unit.Litre))
                    {
                        return(volume.ConvertValueToLitre(unit, value));
                    }
                }
                else if (measurementType == "temperature")
                {
                    Temperature      temperature = new Temperature();
                    Temperature.Unit unit        = temperature.SetUnitAndConvertTemperature(conversionType);

                    // Check enum units and conversion Type
                    if (unit.Equals(Temperature.Unit.FahrenheitToCelsius))
                    {
                        return(temperature.ConvertTemperature(unit, value));
                    }
                }

                return(0);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Пример #2
0
        private double Conversion(string MeasurementType, string conversionType, double value)
        {
            Length length = new Length();
            Weight weight = new Weight();
            Volume volume = new Volume();
            double result = 0.0;

            if (MeasurementType == "length")
            {
                Length.Unit unit = length.SetUnitAndConvertLength(conversionType);

                if (unit == Length.Unit.FeetToInch || unit == Length.Unit.YardToInch || unit == Length.Unit.CentimeterToInch || unit.Equals(Length.Unit.Inch))
                {
                    return(length.ConvertLength(unit, value));
                }
                return(result);
            }
            else if (MeasurementType == "weight")
            {
                Weight.Unit unit = weight.SetUnitAndConvertWeights(conversionType);

                if (unit.Equals(Weight.Unit.GramsToKilogram) || unit.Equals(Weight.Unit.TonneToKilograms) || unit.Equals(Weight.Unit.kilogram))
                {
                    return(weight.ConvertWeigths(unit, value));
                }
                return(result);
            }
            else if (MeasurementType == "volume")
            {
                Volume.Unit unit = volume.SetUnitAndConvertVolume(conversionType);
                if (unit.Equals(Volume.Unit.GallonToLitre) || unit.Equals(Volume.Unit.MiliLitreToLitre) || unit.Equals(Volume.Unit.Litre))
                {
                    return(volume.ConvertValue(unit, value));
                }
                return(result);
            }
            else if (MeasurementType == "temperature")
            {
                Temperature      temperature = new Temperature();
                Temperature.Unit unit        = temperature.SetUnitAndConvertTemperature(conversionType);
                if (unit.Equals(Temperature.Unit.FahrenheitToCelsius))
                {
                    return(temperature.ConvertTemperature(unit, value));
                }
                return(result);
            }

            return(value);
        }