获取对象的最大值,java

问题描述 投票:0回答:0

我有一个名为 MealImpl 的类,它有一个带有价格的属性。膳食保存在另一个名为 Menu 的类中的 Set 中。在另一个类中,我想访问价格属性并在菜单中返回一个具有最高价格的 Optionalwithmessage。我怎样才能通过流获得价格?

公共类 MealImplementation 实现 Meal {

private String name;
private int price;


public MealImplementation(String name, int price) {
    this.name = name;
    this.price = price;
}


@Override
public String getName() {
    return name;
}

@Override
public int getPrice() {
    return price;
}

@Override
public String toString() {
    double priceDecimal = price / 100.0;
    DecimalFormat df = new DecimalFormat("#,##0.00");
    String euro = "(" + df.format(priceDecimal) + "\u20ac)";
    return name + " " + euro;
}

}

私有 LocalDate 日期; 私人套餐;

public MenuImplementation(LocalDate date, Set<Meal> meals) {
    this.date = date;
    this.meals = meals;
}

public LocalDate getDate() {
    return date;
}

public Set<Meal> getMeals() {
    return meals;
}

// datum ; <gericht_preis ;>
public String toCsvLine() {
    String headerDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + ";";
    List<String> meals = getMeals().stream().map((Meal meal) -> meal.getName() + "_" + meal.getPrice()).collect(Collectors.toList());

    String result = headerDate + String.join(";", meals);
    return result;
}

public String toString() {
    return toCsvLine();}

公共类 MaxPriceAnalyzer 实现分析器 {

@Override
public OptionalWithMessage<Meal> analyze_unsafe(List<Menu> data) {
???

}

@Override
public String toString() {
    return "MaxPriceMealAnalyzer";
}

}

list stream set
© www.soinside.com 2019 - 2024. All rights reserved.