import java.util.List;
import java.util.Random;
public enum Party {
AfD(true), CSU(true), DieGruenen(false), FreieWaehler(true), DieLinke(false), FDP(false);
private final boolean conservative;
Party(boolean isConservative) {
this.conservative = isConservative;
}
public boolean isConservative() {
return conservative;
}
public static Party getRandomOrNull() {
final Random random = new Random();
if(random.nextInt(3) % 3 == 0 ) {
return allParties.get(random.nextInt(allParties.size()));
}
return null;
}
static Party getRandom() {
final Random random = new Random();
return allParties.get(random.nextInt(allParties.size()));
}
public static final List<Party> allParties = List.of(AfD, CSU, DieGruenen, FreieWaehler, DieLinke, FDP);
}
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
public class Voter {
private final Ward ward;
private final int age;
private Party party = null;
private BiFunction<Voter, Integer, Party> behaviour = VotingBehaviours.NO_VOTER;
public Voter(int age, Party party, Ward ward) {
this.age = age;
this.party = party;
this.ward = Objects.requireNonNull(ward);
}
public int getAge() {
return age;
}
public Ward getWard() {
return ward;
}
public Optional<Party> getParty() {
final Party party = Party.getRandomOrNull();
return Optional.ofNullable(party);
}
public void setParty(Party party) {
this.party = party;
}
@Override
public int hashCode() {
int hash = 3;
hash = 97 * hash + this.getAge();
hash = 97 * hash + Objects.hashCode(this.getParty());
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Voter other = (Voter) obj;
if (this.getAge() != other.getAge())
return false;
if (this.getParty() != other.getParty())
return false;
return true;
}
@Override
public String toString() {
return "Voter{" + " Alter=" + getAge() + ", Partei= " + getParty() + ", Wahlkreis= " + ward + '}';
}
public void setBehaviour(BiFunction<Voter, Integer, Party> behaviour) {
this.behaviour = behaviour;
}
public Party vote(int year) {
return this.behaviour.apply(this, year);
}
}
import java.util.List;
import java.util.Random;
public enum Ward {
Oberbayern, Niederbayern, Oberpfalz, Oberfranken, Mittelfranken, Unterfranken, Schwaben;
public static Ward getRandom() {
final Random random = new Random();
return allWards.get(random.nextInt(allWards.size()));
}
public static final List<Ward> allWards = List.of(Oberbayern, Niederbayern, Oberpfalz, Oberfranken, Mittelfranken, Unterfranken, Schwaben);
}
import java.util.List;
import java.util.function.BiFunction;
public class VotingBehaviours {
public static final BiFunction<Voter, Integer, Party> NO_VOTER = (voter, year) -> {
throw new RuntimeException("I cannot decide yet");
};
public static final BiFunction<Voter, Integer, Party> AMIGO_VOTER = (voter, year) -> Party.CSU;
public static final BiFunction<Voter, Integer, Party> MY_PARTY_OR_RANDOM = (voter, year) -> voter.getParty().orElse(Party.getRandom());
public static Party partyOfTheYear(int year) {
int random = year % Party.allParties.size();
return Party.allParties.get(random);
}
public static final BiFunction<Voter, Integer, Party> PARTY_OF_THE_YEAR = (voter, year) -> partyOfTheYear(year);
public static final BiFunction<Voter, Integer, Party>VOTE_GAGA = (voter, Integer) -> Party.getRandom();
public static final List<BiFunction<Voter, Integer, Party>> VALID_BEHAVIOURS = List.of(MY_PARTY_OR_RANDOM, PARTY_OF_THE_YEAR,
VOTE_GAGA, AMIGO_VOTER);
}
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Voters {
private List<Voter> voters;
private Random random = new Random();
public Voters(int limit) {
this.voters = Stream.generate(() -> new Voter(random.nextInt(72) + 18, Party.getRandomOrNull(), Ward.getRandom()))
.limit(limit)
.collect(Collectors.toList());
}
public List<Voter> getVoters(){
return Collections.unmodifiableList(voters);
}
public void print() {
final Stream<Voter> streamVoter = this.voters.stream();
streamVoter.forEach(System.out::println);
}
}
import java.util.Optional;
import java.util.Random;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class Main {
static final int currentYear = 2018;
public static void main(String[] args) {
Random random = new Random();
Voters voters = new Voters(100);
voters.print();
final Predicate<Voter> isOldies = o -> o.getAge() >=80;
final Stream<Voter> oldies = voters.getVoters().stream()
.filter(isOldies);
System.out.println("-----Oldies------");
oldies.forEach(System.out::println);
System.out.println("---Wahlkreis Niederbayern----");
System.out.println(voters.getVoters().stream()
.filter(w -> w.getWard().equals(Ward.Niederbayern))
.count());
final Predicate<Voter> allConservative = c -> c.getParty().isPresent() && c.getParty().get().isConservative();
System.out.println("---Alle konservativen Wähler---");
System.out.println(voters.getVoters().stream()
.filter(allConservative)
.count());
voters.getVoters().stream().forEach(v -> v.setBehaviour(VotingBehaviours.VALID_BEHAVIOURS.get(random.nextInt(VotingBehaviours.VALID_BEHAVIOURS.size()))));
System.out.println("---Stimmen für AfD---");
System.out.println(voters.getVoters().stream()
.filter(c -> c.vote(currentYear) == Party.AfD)
.count() + "/" + voters.getVoters().size());
System.out.println("---Stimmen für CSU---");
System.out.println(voters.getVoters().stream()
.filter(c -> c.vote(currentYear) == Party.CSU)
.count() + "/" + voters.getVoters().size());
System.out.println("---Stimmen für DieGruenen---");
System.out.println(voters.getVoters().stream()
.filter(c -> c.vote(currentYear) == Party.DieGruenen)
.count() + "/" + voters.getVoters().size());
System.out.println("---Stimmen für AfD---");
System.out.println(voters.getVoters().stream()
.filter(c -> c.vote(currentYear) == Party.AfD)
.count() + "/" + voters.getVoters().size());
System.out.println("---Stimmen für FreieWaehler---");
System.out.println(voters.getVoters().stream()
.filter(c -> c.vote(currentYear) == Party.FreieWaehler)
.count() + "/" + voters.getVoters().size());
System.out.println("---Stimmen für DieLinke---");
System.out.println(voters.getVoters().stream()
.filter(c -> c.vote(currentYear) == Party.DieLinke)
.count() + "/" + voters.getVoters().size());
System.out.println("---Stimmen für FDP---");
System.out.println(voters.getVoters().stream()
.filter(c -> c.vote(currentYear) == Party.FDP)
.count() + "/" + voters.getVoters().size());
}
}