예제 #1
0
        public IExtendedMaterialModel CreateBatch(decimal batchAmount, IMaterialUnit preferredBatchUnit, IUnitConversionHelper conversions)
        {
            // Nominal = 1kg
            // Batch = 500g
            var batchUnit = conversions.GetPrefferedUnit(preferredBatchUnit, NominalUnit);                            //g
            var convertedNominalAmount = conversions.ConvertAmount(NominalUnit.Id, batchUnit.Id, NominalAmount);      // 1000g
            var convertedBatchAmount   = conversions.ConvertAmount(preferredBatchUnit.Id, batchUnit.Id, batchAmount); //500g

            var conversionFactor = convertedBatchAmount / convertedNominalAmount;                                     // 0.5

            var batch = new ExtendedMaterial(Adaptee)
            {
                BatchUnit = batchUnit, BatchAmount = convertedBatchAmount
            };

            var batchComponents = new List <MaterialComponent>(m_components.Count);

            foreach (var sourceComponent in m_components)
            {
                var componentBatchAmount = sourceComponent.Amount * conversionFactor;

                var batchedComponentMaterial = sourceComponent.Material.CreateBatch(
                    componentBatchAmount,
                    sourceComponent.Unit,
                    conversions);

                var batchComponent = new MaterialComponent(sourceComponent.Unit, batchedComponentMaterial, componentBatchAmount, null);
                batchComponents.Add(batchComponent);
            }

            return(batch);
        }
예제 #2
0
        //public Amount ConvertToSuitableUnit(Amount source)
        //{
        //    if (source.Unit == null)
        //    {
        //        return source;
        //    }
            
        //    var availableUnits = m_conversionHelper.GetCompatibleUnits(source.Unit.Id).ToList();
        //    if (!availableUnits.Any())
        //    {
        //        return source;
        //    }

        //    var bestUnit = source.Unit;
        //    var readability = StringUtil.GetReadability(source.Value);

        //    foreach (var availableUnit in availableUnits)
        //    {
        //        //var converted = m_conversionHelper.ConvertAmount(source.Unit.Id, availableUnit.Id, )
        //    }

        //    throw new NotImplementedException();
        //}

        private Amount Calculate(Amount a, Amount b, Func<decimal, decimal, decimal> numericOp)
        {
            if ((a == null) && (b == null))
            {
                return null;
            }

            if (a == null)
            {
                a = new Amount(0, b.Unit);
            }

            if (b == null)
            {
                b = new Amount(0, a.Unit);
            }

            if (a.Unit.Id == b.Unit.Id)
            {
                return new Amount(numericOp(a.Value, b.Value), a.Unit);
            }

            var targetUnit = m_conversionHelper.GetPrefferedUnit(a.Unit, b.Unit);

            var convertedA = m_conversionHelper.ConvertAmount(a.Unit.Id, targetUnit.Id, a.Value);
            var convertedB = m_conversionHelper.ConvertAmount(b.Unit.Id, targetUnit.Id, b.Value);

            var result = numericOp(convertedA, convertedB);

            return new Amount(result, targetUnit);
        }