package Testate.Stream;

import java.util.*;
import java.util.stream.Collectors;

public class Person {
    private String name;
    private int age;
    private String city;

    // Konstruktor
    public Person(String name, int age, String city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }

    // Getter-Methoden
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getCity() {
        return city;
    }

    // toString-Methode für eine sinnvolle Ausgabe
    @Override
    public String toString() {
        return "Person{" +
               "name='" + name + '\'' +
               ", age=" + age +
               ", city='" + city + '\'' +
               '}';
    }
    
	public static void main(String[] args) {
		// Erstellen der Personenliste
		List<Person> persons = new ArrayList<>();
		persons.add(new Person("Alice", 25, "Berlin"));
		persons.add(new Person("Bob", 35, "Hamburg"));
		persons.add(new Person("Charlie", 40, "München"));
		persons.add(new Person("Diana", 28, "Berlin"));
		persons.add(new Person("Eve", 32, "Köln"));
		persons.add(new Person("Anna", 30, "Berlin"));
		
		Comparator<Person> com = (a,b) -> a.getName().compareTo(b.getName());
		
		persons.stream()
		.filter(p -> p.getCity().equals("Berlin"))
		.filter(p -> p.getAge() >= 30)
		.sorted(com)
		.forEach(p -> System.out.println(p.toString()));
		
		persons.stream()
		.mapToDouble(p -> p.getAge())
		.average()
		.orElse(0.0);
		
		persons.stream()
		.collect(Collectors.averagingLong(null));
	}
}