Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Couch {
- public static void main(final String... args) {
- final float original = 800;
- final float dpm = .02f;
- final List<Person> people = new ArrayList<>(6);
- people.add(new Person("Pato", 0, 23, 200));
- people.add(new Person("Ryan", 0, 11, 200));
- people.add(new Person("Jpo", 0, 15, 200));
- people.add(new Person("Haoyi", 0, 17, 200));
- people.add(new Person("Aedhan", 11, 23));
- people.add(new Person("Rando", 17, 23));
- final int startMonth = people.stream().mapToInt(p -> p.start).min().getAsInt();
- final int endMonth = people.stream().mapToInt(p -> p.end).max().getAsInt();
- float currValue = original;
- for (int i = startMonth; i <= endMonth; i++) {
- currValue = currValue - original * dpm;
- final int month = i;
- final List<Person> peopleLeaving = people
- .stream()
- .filter(p -> month != endMonth && p.end == month)
- .collect(Collectors.toList());
- final List<Person> peopleComingIn = people
- .stream()
- .filter(p -> month != startMonth && p.start == month)
- .collect(Collectors.toList());
- final List<Person> peopleThatWereThere = people
- .stream()
- .filter(p -> month > p.start && month <= p.end)
- .collect(Collectors.toList());
- final List<Person> peoplePaying = peopleThatWereThere
- .stream()
- .filter(p -> !peopleLeaving.contains(p))
- .collect(Collectors.toList());
- final float valuePerPerson = currValue / peopleThatWereThere.size();
- if (peopleLeaving.size() > 0 || peopleComingIn.size() > 0 && month != 0) {
- System.out.printf("\nValue at month: %d is : $%.2f. Per Person: %.2f\n",
- i, currValue, valuePerPerson);
- System.out.println("Leaving: " + peopleLeaving.stream()
- .map(p -> p.name).collect(Collectors.toList()));
- }
- peopleLeaving.forEach(personLeaving -> {
- final float toPay = valuePerPerson / peoplePaying.size();
- peoplePaying.forEach(personPaying -> {
- personPaying.paid += toPay;
- personLeaving.got += toPay;
- System.out.printf("%s pays %s total of $%.2f\n",
- personPaying.name, personLeaving.name, toPay);
- });
- });
- final List<Person> peopleToPay = people
- .stream()
- .filter(p -> !peopleComingIn.contains(p))
- .filter(p -> month >= p.start && month < p.end)
- .collect(Collectors.toList());
- peopleComingIn.forEach(personComingIn -> {
- final float toPay = valuePerPerson / peopleToPay.size();
- peopleToPay.forEach(personStaying -> {
- personStaying.got += toPay;
- personComingIn.paid += toPay;
- System.out.printf("%s pays %s total of $%.2f\n",
- personComingIn.name, personStaying.name, toPay);
- });
- });
- }
- System.out.println("\n\nFinal Results: ");
- people.forEach(person ->
- System.out.printf("- %8s\tpaid:\t$%.2f\tgot:\t$%.2f\tfor a total paid of:\t$%.2f\n",
- person.name,
- person.paid,
- person.got,
- person.paid - person.got));
- }
- final class Person {
- final int start;
- final int end;
- final String name;
- float paid;
- float got;
- Person(final String name, final int start, final int end) {
- this(name, start, end, 0);
- }
- @Override
- public boolean equals(final Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- final Person person = (Person) o;
- if (start != person.start) return false;
- if (end != person.end) return false;
- return name.equals(person.name);
- }
- @Override
- public int hashCode() {
- int result = start;
- result = 31 * result + end;
- result = 31 * result + name.hashCode();
- return result;
- }
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", paid=" + paid +
- ", got=" + got +
- ", net=" + (paid - got) +
- '}';
- }
- Person(final String name, final int start, final int end, final float paid) {
- this.name = name;
- this.start = start;
- this.end = end;
- this.paid = paid;
- this.got = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement