public static Amount GetWeightForUsage(IngredientUsage usage) { if (Unit.GetConvType(usage.Form.FormUnitType) == UnitType.Weight) //Already there, just convert to grams { return(UnitConverter.Convert(usage.Amount, Units.Gram)); } if (usage.Ingredient.ConversionType == UnitType.Weight) //Ingredient is sold in weight, so we can use its native amount { var amt = GetNativeAmountForUsage(usage.Ingredient, usage); return(UnitConverter.Convert(amt, Units.Gram)); } if (usage.Ingredient.ConversionType == UnitType.Unit && usage.Ingredient.UnitWeight > 0) //Ingredient sold in units, but we know weight of each { var amt = GetNativeAmountForUsage(usage.Ingredient, usage); amt.Unit = Units.Gram; amt *= usage.Ingredient.UnitWeight; return(amt); } if (Unit.GetConvType(usage.Form.FormAmount.Unit) == UnitType.Weight && usage.Form.FormAmount.SizeHigh > 0) //This form has a gram weight { var amt = UnitConverter.Convert(usage.Amount, usage.Form.FormUnitType); return(new Amount(amt.SizeHigh * usage.Form.FormAmount.SizeHigh, Units.Gram)); } return(null); }
public static Amount operator +(Amount a1, Amount a2) { if (a1.Unit == a2.Unit) //Just add { if (a1.SizeLow.HasValue && a2.SizeLow.HasValue) //Combine the lows, combine the highs { return(new Amount(a1.SizeLow + a2.SizeLow, a1.SizeHigh + a2.SizeHigh, a1.Unit)); } if (a1.SizeLow.HasValue) //(1-2) + 1 = (2-3) { return(new Amount(a1.SizeLow + a2.SizeHigh, a1.SizeHigh + a2.SizeHigh, a1.Unit)); } if (a2.SizeLow.HasValue) //1 + (1-2) = (2-3) { return(new Amount(a1.SizeHigh + a2.SizeLow, a1.SizeHigh + a2.SizeHigh, a1.Unit)); } //just combine the highs return(new Amount(a1.SizeHigh + a2.SizeHigh, a1.Unit)); } if (UnitConverter.CanConvert(a1.Unit, a2.Unit)) { //TODO: Handle range + nonrange var newLow = a2.SizeLow.HasValue ? (float?)UnitConverter.Convert(a2.SizeLow.Value, a2.Unit, a1.Unit) : null; var newHigh = a1.SizeHigh + UnitConverter.Convert(a2.SizeHigh, a2.Unit, a1.Unit); return(new Amount(newLow, newHigh, a1.Unit)); } throw new IncompatibleAmountException(); }
public static Amount operator +(Amount firstAmount, Amount secondAmount) { if (firstAmount.Unit == secondAmount.Unit) //Just add { if (firstAmount.SizeLow.HasValue && secondAmount.SizeLow.HasValue) //Combine the lows, combine the highs { return(new Amount(firstAmount.SizeLow + secondAmount.SizeLow, firstAmount.SizeHigh + secondAmount.SizeHigh, firstAmount.Unit)); } if (firstAmount.SizeLow.HasValue) //(1-2) + 1 = (2-3) { return(new Amount(firstAmount.SizeLow + secondAmount.SizeHigh, firstAmount.SizeHigh + secondAmount.SizeHigh, firstAmount.Unit)); } if (secondAmount.SizeLow.HasValue) //1 + (1-2) = (2-3) { return(new Amount(firstAmount.SizeHigh + secondAmount.SizeLow, firstAmount.SizeHigh + secondAmount.SizeHigh, firstAmount.Unit)); } //just combine the highs return(new Amount(firstAmount.SizeHigh + secondAmount.SizeHigh, firstAmount.Unit)); } if (UnitConverter.CanConvert(firstAmount.Unit, secondAmount.Unit)) { //TODO: Handle range + nonrange var newLow = secondAmount.SizeLow.HasValue ? (float?)UnitConverter.Convert(secondAmount.SizeLow.Value, secondAmount.Unit, firstAmount.Unit) : null; var newHigh = firstAmount.SizeHigh + UnitConverter.Convert(secondAmount.SizeHigh, secondAmount.Unit, firstAmount.Unit); return(new Amount(newLow, newHigh, firstAmount.Unit)); } throw new IncompatibleAmountException(); }
public static Amount GetNativeAmountForUsage(Ingredient ingredient, IngredientUsage usage) { var amount = new Amount(); var usageConvType = Unit.GetConvType(usage.Form.FormUnitType); switch (ingredient.ConversionType) //This is the type we must convert to { case UnitType.Unit: amount.Unit = Units.Unit; if (usageConvType == UnitType.Unit) //Unit to unit version { var equivGrams = UnitConverter.Convert(usage.Form.FormAmount, Units.Gram); //Grams this form is equivelent to amount.SizeHigh = (float)Math.Ceiling((equivGrams.SizeHigh * usage.Amount.SizeHigh) / ingredient.UnitWeight); return(amount); } if (usageConvType == UnitType.Weight) //Weight to unit conversion { var grams = UnitConverter.Convert(usage.Amount, Units.Gram); amount.SizeHigh = (float)Math.Ceiling(grams.SizeHigh / ingredient.UnitWeight); return(amount); } if (usageConvType == UnitType.Volume) //Volume to unit conversion { var likeAmount = UnitConverter.Convert(usage.Amount, usage.Form.FormUnitType); amount.SizeHigh = (float)Math.Ceiling((likeAmount.SizeHigh * usage.Form.FormAmount.SizeHigh) / usage.Ingredient.UnitWeight); //Round up when dealing with whole units return(amount); } break; case UnitType.Weight: amount.Unit = Units.Gram; if (usageConvType == UnitType.Unit) //Unit to weight conversion { amount.SizeHigh = usage.Amount.SizeHigh * usage.Form.FormAmount.SizeHigh; //NOTE: FormAmount will always be in Grams when Ingredient ConvType is weight return(amount); } if (usageConvType == UnitType.Volume) //Volume to weight conversion { var likeAmount = UnitConverter.Convert(usage.Amount, usage.Form.FormUnitType); amount.SizeHigh = likeAmount.SizeHigh * usage.Form.FormAmount.SizeHigh; //NOTE: FormAmount will always be in Grams when Ingredient ConvType is weight return(amount); } break; case UnitType.Volume: amount.Unit = Units.Teaspoon; if (usageConvType == UnitType.Unit) //Unit to volume conversion { amount.SizeHigh = usage.Amount.SizeHigh * usage.Form.FormAmount.SizeHigh; //NOTE: FormAmount will always be in tsp when Ingredient ConvType is volume return(amount); } if (usageConvType == UnitType.Weight) //Weight to volume conversion { var likeAmount = UnitConverter.Convert(usage.Amount, usage.Form.FormUnitType); amount.SizeHigh = likeAmount.SizeHigh * usage.Form.FormAmount.SizeHigh; //NOTE: FormAmount will always be in teaspoons when Ingredient ConvType is Volume return(amount); } break; } //throw new IngredientAggregationDatabaseException("Cannot convert an IngredientUsage into its native form.", ingredient, usage); throw new Exception("Cannot convert an IngredientUsage into its native form."); }