fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.math.BigDecimal;
  7. import java.time.LocalDate;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.UUID;
  11. import java.util.stream.Collectors;
  12. import java.util.stream.Stream;
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone
  16. {
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19. //------------------ Test Data ------------------
  20. List<Exon> listExon = new ArrayList<>(List.of(
  21. new Exon(1L, new IncomeCode("45"), LocalDate.of(2021, 1, 1), "4", BigDecimal.valueOf(10)),
  22. new Exon(2L, new IncomeCode("21"), LocalDate.of(2022, 1, 1), "5", BigDecimal.valueOf(15)),
  23. new Exon(3L, new IncomeCode("33"), LocalDate.of(2023, 1, 1), "2", BigDecimal.valueOf(20)),
  24. new Exon(4L, new IncomeCode("45"), LocalDate.of(2021, 1, 1), "4", BigDecimal.valueOf(25))
  25. ));
  26.  
  27. List<Sup> listSup = new ArrayList<>(List.of(
  28. new Sup(1L, new IncomeCode("45"), null, LocalDate.of(2021, 1, 1), "4", BigDecimal.valueOf(30)),
  29. new Sup(2L, new IncomeCode("21"), null, LocalDate.of(2022, 1, 1), "5", BigDecimal.valueOf(40)),
  30. new Sup(3L, new IncomeCode("33"), null, LocalDate.of(2023, 1, 1), "2", BigDecimal.valueOf(50))
  31. ));
  32.  
  33.  
  34. //------------------ Creating List of Mixed Objects Exon and Sup ------------------
  35. List<List<Object>> listMixed = new ArrayList<>(Stream.concat(listExon.stream(), listSup.stream())
  36. .collect(Collectors.groupingBy(obj -> {
  37. if (obj.getClass() == Exon.class) {
  38. Exon exon = (Exon) obj;
  39. return String.format("%s-%s-%s", exon.getIncomeCode(), exon.getEndDate(), exon.getCodeRef());
  40. }
  41. Sup sup = (Sup) obj;
  42. return String.format("%s-%s-%s", sup.getIncomeCode(), sup.getEndDate(), sup.getCodeRef());
  43. })).values());
  44.  
  45. System.out.println("List of lists of Exon and Sup: ");
  46. for (List<Object> l : listMixed) {
  47. System.out.println(l);
  48. }
  49.  
  50. //Creating List of GlobalData from Exon & Sup
  51. List<GlobalData> listGlobalData2 = listMixed.stream()
  52. .map(list -> {
  53. Object firstObj = list.stream().findFirst().orElse(null);
  54. if (firstObj == null) {
  55. return null;
  56. }
  57. GlobalData gd = null;
  58. if (firstObj.getClass() == Exon.class) {
  59. Exon exon = (Exon) firstObj;
  60. gd = new GlobalData(exon.getIncomeCode(), exon.getEndDate(), exon.getCodeRef());
  61. } else {
  62. Sup sup = (Sup) firstObj;
  63. gd = new GlobalData(sup.getIncomeCode(), sup.getEndDate(), sup.getCodeRef());
  64. }
  65. for (Object o: list){
  66. if (o.getClass() == Exon.class){
  67. Exon exon = (Exon) o;
  68. gd.setValue(gd.getValue().add(exon.getValue()));
  69. if (exon.getXxx() > 100){
  70. gd.setIncome(calcMethod(exon));
  71. }
  72. } else {
  73. Sup sup = (Sup) o;
  74. gd.setValue(gd.getValue().add(sup.getValue()));
  75. if (sup.getXxx() > 100){
  76. gd.setIncome(calcMethod(sup));
  77. }
  78. }
  79. }
  80. return gd;
  81. })
  82. .filter(obj -> obj != null)
  83. .collect(Collectors.toList());
  84.  
  85. System.out.println("\nList of GlobalData: ");
  86. for (GlobalData gd : listGlobalData2) {
  87. System.out.println(gd);
  88. }
  89. }
  90.  
  91. private static BigDecimal calcMethod(Exon exon) {
  92. return exon.getValue().multiply(BigDecimal.valueOf(1.1));
  93. }
  94.  
  95. private static BigDecimal calcMethod(Sup sup) {
  96. return sup.getValue().multiply(BigDecimal.valueOf(1.3));
  97. }
  98.  
  99. static class Exon {
  100. private Long id;
  101. private IncomeCode incomeCode;
  102. private LocalDate endDate;
  103. String codeRef;
  104. private Integer xxx;
  105. private BigDecimal value;
  106.  
  107. public Exon(Long id, IncomeCode incomeCode, LocalDate endDate, String codeRef, BigDecimal value) {
  108. this.id = id;
  109. this.incomeCode = incomeCode;
  110. this.endDate = endDate;
  111. this.codeRef = codeRef;
  112. this.xxx = ((int) (Math.random() * 100 + 50));
  113. this.value = value;
  114. }
  115.  
  116. public IncomeCode getIncomeCode() {
  117. return incomeCode;
  118. }
  119.  
  120. public LocalDate getEndDate() {
  121. return endDate;
  122. }
  123.  
  124. public String getCodeRef() {
  125. return codeRef;
  126. }
  127.  
  128. public Integer getXxx() {
  129. return xxx;
  130. }
  131.  
  132. public BigDecimal getValue() {
  133. return value;
  134. }
  135.  
  136. @Override
  137. public String toString() {
  138. return String.format("%s{%s %s %s - Value: %s xxx: %s}", getClass().getSimpleName(), incomeCode, endDate, codeRef, value, xxx);
  139. }
  140. }
  141.  
  142. static class Sup {
  143. private Long id;
  144. private IncomeCode incomeCode;
  145. private LocalDate startDate;
  146. private LocalDate endDate;
  147. String codeRef;
  148.  
  149. private Integer xxx;
  150. private BigDecimal value;
  151.  
  152. public Sup(Long id, IncomeCode incomeCode, LocalDate startDate, LocalDate endDate, String codeRef, BigDecimal value) {
  153. this.id = id;
  154. this.incomeCode = incomeCode;
  155. this.startDate = startDate;
  156. this.endDate = endDate;
  157. this.codeRef = codeRef;
  158. this.xxx = ((int) (Math.random() * 50 + 50));
  159. this.value = value;
  160. }
  161.  
  162. public IncomeCode getIncomeCode() {
  163. return incomeCode;
  164. }
  165.  
  166. public LocalDate getEndDate() {
  167. return endDate;
  168. }
  169.  
  170. public String getCodeRef() {
  171. return codeRef;
  172. }
  173.  
  174. public Integer getXxx() {
  175. return xxx;
  176. }
  177.  
  178. public BigDecimal getValue() {
  179. return value;
  180. }
  181.  
  182. @Override
  183. public String toString() {
  184. return String.format("%s{%s %s %s - Value: %s xxx: %s}", getClass().getSimpleName(), incomeCode, endDate, codeRef, value, xxx);
  185. }
  186. }
  187.  
  188. static class IncomeCode {
  189. private String code;
  190.  
  191. public IncomeCode(String code) {
  192. this.code = code;
  193. }
  194.  
  195. @Override
  196. public String toString() {
  197. return code;
  198. }
  199. }
  200.  
  201. static class GlobalData {
  202.  
  203. private UUID id = UUID.randomUUID();
  204. private IncomeCode incomeCode;
  205. private LocalDate endDate;
  206. private String codeRef;
  207. private boolean taxable;
  208. private String pw = "";
  209. private BigDecimal area = BigDecimal.ZERO;
  210. //private BigDecimal taxableIncome = BigDecimal.ZERO;
  211. private BigDecimal value = BigDecimal.ZERO;
  212. private BigDecimal exonValueType = BigDecimal.ZERO;
  213. private BigDecimal Income = BigDecimal.ZERO;
  214. private BigDecimal exemptedIncome = BigDecimal.ZERO;
  215. private BigDecimal refIncome = BigDecimal.ZERO;
  216.  
  217. public GlobalData(IncomeCode incomeCode, LocalDate endDate, String codeRef) {
  218. this.incomeCode = incomeCode;
  219. this.endDate = endDate;
  220. this.codeRef = codeRef;
  221. }
  222.  
  223. public IncomeCode getIncomeCode() {
  224. return incomeCode;
  225. }
  226.  
  227. public void setIncomeCode(IncomeCode incomeCode) {
  228. this.incomeCode = incomeCode;
  229. }
  230.  
  231. public LocalDate getEndDate() {
  232. return endDate;
  233. }
  234.  
  235. public void setEndDate(LocalDate endDate) {
  236. this.endDate = endDate;
  237. }
  238.  
  239. public String getCodeRef() {
  240. return codeRef;
  241. }
  242.  
  243. public void setCodeRef(String codeRef) {
  244. this.codeRef = codeRef;
  245. }
  246.  
  247. public BigDecimal getValue() {
  248. return value;
  249. }
  250.  
  251. public void setValue(BigDecimal value) {
  252. this.value = value;
  253. }
  254.  
  255. public BigDecimal getIncome() {
  256. return Income;
  257. }
  258.  
  259. public void setIncome(BigDecimal income) {
  260. Income = income;
  261. }
  262.  
  263. public BigDecimal getRefIncome() {
  264. return refIncome;
  265. }
  266.  
  267. public void setRefIncome(BigDecimal refIncome) {
  268. this.refIncome = refIncome;
  269. }
  270.  
  271. @Override
  272. public String toString() {
  273. return String.format("%s{%s %s %s - value: %s Income: %s}", getClass().getSimpleName(), incomeCode, endDate, codeRef, value, Income);
  274. }
  275. }
  276. }
Time limit exceeded #stdin #stdout 5s 57932KB
stdin
Standard input is empty
stdout
List of lists of Exon and Sup: 
[Exon{45 2021-01-01 4 - Value: 10 xxx: 122}, Exon{45 2021-01-01 4 - Value: 25 xxx: 132}, Sup{45 2021-01-01 4 - Value: 30 xxx: 55}]
[Exon{33 2023-01-01 2 - Value: 20 xxx: 100}, Sup{33 2023-01-01 2 - Value: 50 xxx: 66}]
[Exon{21 2022-01-01 5 - Value: 15 xxx: 146}, Sup{21 2022-01-01 5 - Value: 40 xxx: 89}]