// Exercise 3: PECS — wildcards for reading vs writing (hardest)
//
// Implement:
//   - static double sumOf(List<? extends Number> source): reads every
//     element as Number and sums the double values. `? extends Number`
//     because this method only ever PRODUCES values for you to read —
//     it must never call add() on `source`.
//   - static <T> void copy(List<? extends T> source, List<? super T> dest):
//     copies every element from source into dest, in order. `source` is a
//     producer (? extends T — read-only), `dest` is a consumer
//     (? super T — write-only), matching PECS exactly.

import java.util.ArrayList;
import java.util.List;

public class java_generics_exercise_3 {

    static double sumOf(List<? extends Number> source) {
        // TODO: implement — iterate source as Number, sum doubleValue()
        return 0;
    }

    static <T> void copy(List<? extends T> source, List<? super T> dest) {
        // TODO: implement — for each element in source, dest.add(element)
    }

    // --- self-check: do not modify below this line ---
    public static void main(String[] args) {
        boolean ok = true;

        ok &= sumOf(List.of(1, 2, 3)) == 6.0;            // List<Integer>
        ok &= sumOf(List.of(1.5, 2.5)) == 4.0;            // List<Double>

        List<Integer> ints = List.of(1, 2, 3);
        List<Number> numbers = new ArrayList<>();
        copy(ints, numbers); // source: List<Integer> extends T=Number; dest: List<Number> super T=Number
        ok &= numbers.equals(List.of(1, 2, 3));

        List<String> strings = List.of("a", "b");
        List<Object> objects = new ArrayList<>();
        copy(strings, objects); // source: List<String> extends T=Object; dest: List<Object> super T=Object
        ok &= objects.equals(List.of("a", "b"));

        System.out.println(ok ? "PASS" : "FAIL");
    }
}
