【问题标题】:How to calc an item property in a list with java? [duplicate]如何用java计算列表中的项目属性? [复制]
【发布时间】:2018-06-01 23:14:21
【问题描述】:

我正在开发一个用于管理背包库存的 restful api,仅用于学习。 但是我正在努力使用负责保持新库存的创建功能。 因此,每次我向我的 api 发出 POST 请求时,我都会收到一个指向我的新库存的项目列表,然后我必须处理每个项目的数量,并由此获得项目对象属性的值,称为“点”。

我的 api 接收到的 json 对象是这样的:

{
    "inventory": [
        {
            "name": "Meat",
            "quantity": 4
        },
        {
            "name": "Water",
            "quantity": 1
        },
        {
            "name": "Wood",
            "quantity": 7
        },
        {
            "name": "Iron",
            "quantity": 3
        }
    ]
}

系统只有四种类型的物品,每种类型我都会获得特定数量的积分。

例子:

Meat - 6 points
Water - 7 points
Wood - 3 points
Iron - 2 points

现在我必须处理每个项目的数量以获得我的积分。 问题是我的 java 方法没有正确验证我的项目名称。因为那样,当我持久化新库存时,该方法将 null 赋给我的 points 属性。

我的 Item 类看起来像这样:

public class Item {
    private String name;
    private Integer quantity;
    private Integer points;

    //get and set omitted;
}

我的背包课是这样的:

public class Backpack{
    private List<Item> inventory;

    //get and set ommited;
}

负责接收和处理我的积分的方法是这样的

@PostMapping()
public void createInventory(@RequestBody Backpack backpack) {

    backpack.getInventory().stream().forEach(i -> i.setPoints(calcPoints(i)));

    backpackDao.persistInventory(backpack);

}

public Integer calcPoints(Item item) {

    if (item.getName().toLowerCase() == "meat")
        return item.getQuantity * 6;

    if (item.getName().toLowerCase() == "water")
        return item.getQuantity * 7;

    if (item.getName().toLowerCase() == "wood")
        return item.getQuantity * 3;

    if (item.getName().toLowerCase() == "iron")
        return item.getQuantity * 2;

    return null;
}

我将项目名称设置为小写只是为了使条件检查更容易一些。万一有人可以尝试坚持使用大写字符串。 如果我在流操作后尝试打印每个项目,我可以获得我的项目名称和项目数量,但点为空。 我尝试打印一个简单的日志,以了解我的方法 calcPoints 是否正在调用,并且确实如此。但由于某种原因,条件句不起作用。 我想要一些关于如何解决这个问题的建议或建议。我认为这可能很简单,但我真的还没有看到。

【问题讨论】:

  • 您无需串流列表即可致电forEach()
  • 感谢您的建议@shmosel。我改变了。

标签: java list rest api stream


【解决方案1】:

不要使用== 进行字符串比较。请改用equals()

试试这个:

item.getName().equalsIgnoreCase("meat")

这可能会有所帮助How do I compare strings in Java?

【讨论】:

    【解决方案2】:

    karakales 的回答是对的。 但你也可以使用 switch-case:

    switch (item.getName().toLowerCase()) {
        case "meat":
            return item.getQuantity * 6;
        ...
    }
    

    【讨论】:

      【解决方案3】:

      public Integer calcPoints(Item item) {

      if (item.getName().equalsIgnoreCase("meat"))
          return item.getQuantity * 6;
      else if (item.getName().equalsIgnoreCase("water"))
          return item.getQuantity * 7;
      else if (item.getName().equalsIgnoreCase("wood"))
          return item.getQuantity * 3;
      else if (item.getName().equalsIgnoreCase("iron"))
          return item.getQuantity * 2;
      
      return 0;
      

      }

      【讨论】:

        猜你喜欢
        • 2017-05-04
        • 2023-03-14
        • 2019-11-07
        • 2019-08-17
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多