Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. package main;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Couch {
  8. public static void main(final String... args) {
  9. final float original = 800;
  10. final float dpm = .02f;
  11.  
  12. final List<Person> people = new ArrayList<>(6);
  13. people.add(new Person("Pato", 0, 23, 200));
  14. people.add(new Person("Ryan", 0, 11, 200));
  15. people.add(new Person("Jpo", 0, 15, 200));
  16. people.add(new Person("Haoyi", 0, 17, 200));
  17. people.add(new Person("Aedhan", 11, 23));
  18. people.add(new Person("Rando", 17, 23));
  19.  
  20. final int startMonth = people.stream().mapToInt(p -> p.start).min().getAsInt();
  21. final int endMonth = people.stream().mapToInt(p -> p.end).max().getAsInt();
  22.  
  23. float currValue = original;
  24. for (int i = startMonth; i <= endMonth; i++) {
  25. currValue = currValue - original * dpm;
  26. final int month = i;
  27.  
  28. final List<Person> peopleLeaving = people
  29. .stream()
  30. .filter(p -> month != endMonth && p.end == month)
  31. .collect(Collectors.toList());
  32.  
  33. final List<Person> peopleComingIn = people
  34. .stream()
  35. .filter(p -> month != startMonth && p.start == month)
  36. .collect(Collectors.toList());
  37.  
  38. final List<Person> peopleThatWereThere = people
  39. .stream()
  40. .filter(p -> month > p.start && month <= p.end)
  41. .collect(Collectors.toList());
  42.  
  43. final List<Person> peoplePaying = peopleThatWereThere
  44. .stream()
  45. .filter(p -> !peopleLeaving.contains(p))
  46. .collect(Collectors.toList());
  47.  
  48. final float valuePerPerson = currValue / peopleThatWereThere.size();
  49.  
  50. if (peopleLeaving.size() > 0 || peopleComingIn.size() > 0 && month != 0) {
  51. System.out.printf("\nValue at month: %d is : $%.2f. Per Person: %.2f\n",
  52. i, currValue, valuePerPerson);
  53. System.out.println("Leaving: " + peopleLeaving.stream()
  54. .map(p -> p.name).collect(Collectors.toList()));
  55. }
  56.  
  57. peopleLeaving.forEach(personLeaving -> {
  58. final float toPay = valuePerPerson / peoplePaying.size();
  59. peoplePaying.forEach(personPaying -> {
  60. personPaying.paid += toPay;
  61. personLeaving.got += toPay;
  62. System.out.printf("%s pays %s total of $%.2f\n",
  63. personPaying.name, personLeaving.name, toPay);
  64. });
  65. });
  66.  
  67. final List<Person> peopleToPay = people
  68. .stream()
  69. .filter(p -> !peopleComingIn.contains(p))
  70. .filter(p -> month >= p.start && month < p.end)
  71. .collect(Collectors.toList());
  72.  
  73. peopleComingIn.forEach(personComingIn -> {
  74. final float toPay = valuePerPerson / peopleToPay.size();
  75. peopleToPay.forEach(personStaying -> {
  76. personStaying.got += toPay;
  77. personComingIn.paid += toPay;
  78. System.out.printf("%s pays %s total of $%.2f\n",
  79. personComingIn.name, personStaying.name, toPay);
  80. });
  81. });
  82.  
  83. }
  84.  
  85. System.out.println("\n\nFinal Results: ");
  86. people.forEach(person ->
  87. System.out.printf("- %8s\tpaid:\t$%.2f\tgot:\t$%.2f\tfor a total paid of:\t$%.2f\n",
  88. person.name,
  89. person.paid,
  90. person.got,
  91. person.paid - person.got));
  92. }
  93.  
  94. final class Person {
  95. final int start;
  96. final int end;
  97. final String name;
  98.  
  99. float paid;
  100. float got;
  101.  
  102. Person(final String name, final int start, final int end) {
  103. this(name, start, end, 0);
  104. }
  105.  
  106. @Override
  107. public boolean equals(final Object o) {
  108. if (this == o) return true;
  109. if (o == null || getClass() != o.getClass()) return false;
  110.  
  111. final Person person = (Person) o;
  112.  
  113. if (start != person.start) return false;
  114. if (end != person.end) return false;
  115. return name.equals(person.name);
  116. }
  117.  
  118. @Override
  119. public int hashCode() {
  120. int result = start;
  121. result = 31 * result + end;
  122. result = 31 * result + name.hashCode();
  123. return result;
  124. }
  125.  
  126. @Override
  127. public String toString() {
  128. return "Person{" +
  129. "name='" + name + '\'' +
  130. ", paid=" + paid +
  131. ", got=" + got +
  132. ", net=" + (paid - got) +
  133. '}';
  134. }
  135.  
  136. Person(final String name, final int start, final int end, final float paid) {
  137. this.name = name;
  138. this.start = start;
  139. this.end = end;
  140. this.paid = paid;
  141. this.got = 0;
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement