Skip to content

Instantly share code, notes, and snippets.

@pato
Created August 24, 2017 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pato/0b9515423b8fe01c1f7bc59cebe1b10f to your computer and use it in GitHub Desktop.
Save pato/0b9515423b8fe01c1f7bc59cebe1b10f to your computer and use it in GitHub Desktop.
Couch depreciation and payment calculator for nerd roomates
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment