package modernjavainaction.chap06;

import static java.util.stream.Collectors.reducing;
import static modernjavainaction.chap06.Dish.menu;

public class Reducing {

  public static void main(String... args) {
    System.out.println("Total calories in menu: " + calculateTotalCalories());
    System.out.println("Total calories in menu: " + calculateTotalCaloriesWithMethodReference());
    System.out.println("Total calories in menu: " + calculateTotalCaloriesWithoutCollectors());
    System.out.println("Total calories in menu: " + calculateTotalCaloriesUsingSum());
  }

  private static int calculateTotalCalories() {
    return menu.stream().collect(reducing(0, Dish::getCalories, (Integer i, Integer j) -> i + j));
  }

  private static int calculateTotalCaloriesWithMethodReference() {
    return menu.stream().collect(reducing(0, Dish::getCalories, Integer::sum));
  }

  private static int calculateTotalCaloriesWithoutCollectors() {
    return menu.stream().map(Dish::getCalories).reduce(Integer::sum).get();
  }

  private static int calculateTotalCaloriesUsingSum() {
    return menu.stream().mapToInt(Dish::getCalories).sum();
  }

}
