import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Ch3 {

    public void all() {
        List<Apple> inventory = createInventoryOfApples();

        attempt1(inventory);
        attempt2(inventory);
        attempt3(inventory);
        attempt4(inventory);
    }

    private void attempt4(List<Apple> inventory) {
        inventory.sort(Comparator.comparing(Apple::getWeight));

        System.out.print("Attempt4: ");
        printAppleWeights(inventory);
    }

    private void attempt3(List<Apple> inventory) {
        inventory.sort(Comparator.comparing(a -> a.getWeight()));

        System.out.print("Attempt3: ");
        printAppleWeights(inventory);
    }

    private void attempt2(List<Apple> inventory) {
        inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()));

        System.out.print("Attempt2: ");
        printAppleWeights(inventory);
    }

    private void attempt1(List<Apple> inventory) {
        inventory.sort(new AppleComparator());

        System.out.print("Attempt1: ");
        printAppleWeights(inventory);
    }
    public class AppleComparator implements Comparator<Apple> {
        @Override
        public int compare(Apple a1, Apple a2) {
            return a1.getWeight().compareTo(a2.getWeight());
        }
    }

    private void printAppleWeights(List<Apple> inventory) {
        for(Apple a: inventory) {
            System.out.print(" " + a.getWeight() + " ");
        }
        System.out.println("grams.");
    }

    private List<Apple> createInventoryOfApples() {
        Apple a1 = new Apple(100, "red");
        Apple a2 = new Apple(240, "red");
        Apple a3 = new Apple(160,  "green");

        List<Apple> inventory = new ArrayList<Apple>();
        inventory.add(a1);
        inventory.add(a2);
        inventory.add(a3);

        return inventory;
    }
}
