package java8_in_action.chapter5;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by tlanders on 4/6/2016.
 */
public class Quiz5_5 {
    public static void main(String [] args) {
        System.out.println("Quiz 5_5");

        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        // 1.  Find all transactions in the year 2011 and sort them by value (small to high).
        List<Transaction> y2011TxnList = transactions.stream()
                .filter(t -> t.getYear() == 2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .collect(Collectors.toList());

        System.out.println("q1 - 2011 transactions by value");
        y2011TxnList.forEach(System.out::println);

        // 2.  What are all the unique cities where the traders work?
        List<String> uniqueCities = Arrays.asList(raoul, mario, alan, brian).stream()
                .map(Trader::getCity)
                .distinct()
                .collect(Collectors.toList());

        System.out.println("q2 - unique cities");
        uniqueCities.forEach(System.out::println);

        // 3.  Find all traders from Cambridge and sort them by name.

        // 4.  Return a string of all traders’ names sorted alphabetically.

        // 5.  Are any traders based in Milan?

        // 6.  Print all transactions’ values from the traders living in Cambridge.

        // 7.  What’s the highest value of all the transactions?

        // 8.  Find the transaction with the smallest value.

        System.out.println("Quiz 5_5 done.");
    }

    /**
     */
    public static void question1() {
    }
}

@@ -1804,84 +1804,809 @@
 me.%0A
-%0A        // 4.  Return a string of all traders%E2%80%99 names sorted alphabetically.
+        List%3CTrader%3E traders = Arrays.asList(raoul, mario, alan, brian).stream()%0A                .filter(t -%3E t.getCity().equalsIgnoreCase(%22Cambridge%22))%0A                .sorted(Comparator.comparing(Trader::getName))%0A                .collect(Collectors.toList());%0A%0A        System.out.println(%22q3 - traders from Cambridge%22);%0A        traders.forEach(t -%3E System.out.println(t.getName()));%0A%0A        // 4.  Return a string of all traders%E2%80%99 names sorted alphabetically.%0A        String allNames = Arrays.asList(raoul, mario, alan, brian).stream()%0A                .sorted(Comparator.comparing(Trader::getName))%0A                .map(Trader::getName)%0A                .reduce(%22%22, (a,b) -%3E a + %22 %22 + b);%0A%0A        System.out.println(%22q4 - combined trader names%22);%0A        System.out.println(%22allNames=%22 + allNames);
 %0A%0A  

questions 3 and 4 done