简体   繁体   English

使用Jscience计算n个单位的价格(提供单位价格和数量)

[英]Using Jscience to compute price for n units provided unit price and quantity

I am looking for a better way to compute using Jscience but the generic pattern is much tougher to get a clear solution. 我正在寻找一种使用Jscience进行计算的更好方法,但是通用模式很难获得清晰的解决方案。 I need to compute price for n units provided the unit price for a unit quantity is defined. 如果定义了单位数量的单价,我需要计算n个单位的价格。 like, 喜欢,

    Measure<Double, ? extends Quantity> unitQuantity = Measure.valueOf(1.0,
            Unit.valueOf("kg"));
    Amount<Money> unitPrice = Amount.valueOf(150.0, USD);
    Measure<Double, ? extends Quantity> quantity = Measure.valueOf(500.0,
            MILLI(Unit.valueOf("kg")));
    Amount<Money> amount = unitPrice.times(quantity.to(unitQuantity.getUnit())
                       .getValue() / unitQuantity.getValue());

For the above code I am getting this error: 对于上面的代码,我收到此错误:

The method to(Unit<capture#7-of ? extends Quantity>) in the type 
Measure<Double,capture#7-of ? extends Quantity> is not applicable for 
the arguments (Unit<capture#8-of ? extends Quantity>)

I read about generics at Java Tutorials and tried the following, but still no clear solution: 我在Java Tutorials上了解了泛型,并尝试了以下方法,但仍没有明确的解决方案:

    Amount<Money> amount = compute(unitPrice,unitQuantity,quantity)

    private <T extends Quantity> Amount<Money> compute(Amount<Money> unitPrice,
        Measure<Double, T> unitQuantity, Measure<Double, T> quantity) {
    return unitPrice.times(quantity.to(unitQuantity.getUnit()).getValue()
            / unitQuantity.getValue());
}

Now getting this error: 现在出现此错误:

   The method compute(Amount<Money>, Measure<Double,T>, Measure<Double,T>) 
   in the type JscienceEx is not applicable for the arguments (Amount<Money>, 
   Measure<Double,capture#7-of ? extends Quantity>, Measure<Double,capture#8-of ?    
   extends Quantity>)

The Unit.valueOf("kg") factory method may be convenient in some cases. 在某些情况下, Unit.valueOf("kg")工厂方法可能很方便。 But in this case, it does not retain enough information. 但是在这种情况下,它不会保留足够的信息。 It only returns the unit of "some" Quantity - hence the ? extends Quantity 它仅返回“一些”数量的单位-因此? extends Quantity ? extends Quantity in the return type. 在返回类型中? extends Quantity But you don't know whether this is quantity is, for example, Mass or Power . 但是您不知道这是数量,例如MassPower And as long as you don't know this, you can not say whether the call to quantity.to(unitQuantity.getUnit()) is valid. 而且,只要您不知道这一点,就无法说出对quantity.to(unitQuantity.getUnit())的调用是否有效。

In this case, you can solve this by using a new BaseUnit<Mass>("kg") instead of the Unit.valueOf("kg") : 在这种情况下,可以通过使用new BaseUnit<Mass>("kg")代替Unit.valueOf("kg")来解决此问题:

import static javax.measure.unit.SI.MILLI;

import javax.measure.Measure;
import javax.measure.quantity.Mass;
import javax.measure.unit.BaseUnit;

import org.jscience.economics.money.Currency;
import org.jscience.economics.money.Money;
import org.jscience.physics.amount.Amount;

public class JScienceUnits
{
    public static void main(String[] args)
    {
        BaseUnit<Mass> kg = new BaseUnit<Mass>("kg");
        Measure<Double, Mass> unitQuantity = Measure.valueOf(1.0, kg);
        Amount<Money> unitPrice = Amount.valueOf(150.0, Currency.USD);
        Measure<Double, Mass> quantity = Measure.valueOf(500.0, MILLI(kg));
        Amount<Money> amount = unitPrice.times(
            quantity.to(unitQuantity.getUnit()).getValue() / 
            unitQuantity.getValue());

        System.out.println(
            "Money for "+quantity+
            " with unit price "+unitPrice+
            " is "+amount);
    }
}

Found two approaches. 找到了两种方法。 ConversionException get thrown when conversion fails. 转换失败时将引发ConversionException。

@SuppressWarnings("unchecked")
public static <U extends Quantity, V extends Quantity> Amount<Money> computeAmount(
        Measure<Double, U> unitQuantity, Amount<Money> unitRate,
        Measure<Double, V> netQuantity) {
    return unitRate.times(netQuantity.to((Unit<V>) unitQuantity.getUnit())
            .getValue() / unitQuantity.getValue());
}

- -

public static <U extends Quantity, V extends Quantity> Amount<Money> computeAmount(
        Measure<Double, U> unitQuantity, Amount<Money> unitRate,
        Measure<Double, V> netQuantity) {
    UnitConverter toUnitQuantityUnit = netQuantity.getUnit()
            .getConverterTo(unitQuantity.getUnit());
    return unitRate
            .times(toUnitQuantityUnit.convert(netQuantity.getValue())
                    / unitQuantity.getValue());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM