Advertisement
astaspasta

Untitled

Dec 6th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. package client;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6.  
  7. import datamodel.IResult;
  8. import datamodel.MeasurementRecord;
  9.  
  10. public class Result implements IResult {
  11.  
  12. private ArrayList<MeasurementRecord> inputMeasurements;
  13. private String aggregatorType;
  14. private String aggFunction;
  15. private String description;
  16.  
  17. public Result(ArrayList<MeasurementRecord> inputMeasurements, String aggregatorType, String aggFunction, String description) {
  18. this.inputMeasurements = inputMeasurements;
  19. this.aggregatorType = aggregatorType;
  20. this.aggFunction = aggFunction;
  21. this.description = description;
  22. }
  23.  
  24. @Override
  25. public int add(String timeUnit, MeasurementRecord record) {
  26. inputMeasurements.add(record);
  27. return inputMeasurements.size();
  28. }
  29.  
  30. @Override
  31. public String getDescription() {
  32. return description;
  33. }
  34.  
  35. @Override
  36. public HashMap<String, ArrayList<MeasurementRecord>> getDetailedResults() {
  37. HashMap<String, ArrayList<MeasurementRecord>> result = new HashMap<String, ArrayList<MeasurementRecord>>();
  38.  
  39. for (MeasurementRecord record : inputMeasurements) {
  40. if (result.containsKey(record.getMonth().name())) {
  41. result.get(record.getMonth().name()).add(record);
  42. } else {
  43. ArrayList<MeasurementRecord> records = new ArrayList<MeasurementRecord>();
  44. records.add(record);
  45. result.put(record.getMonth().name(), records);
  46. }
  47. }
  48.  
  49. return result;
  50. }
  51.  
  52. @Override
  53. public HashMap<String, Double> getAggregateMeterKitchen() {
  54. HashMap<String, Double> result = new HashMap<String, Double>();
  55. HashMap<String, Integer> sz = new HashMap<String, Integer>();
  56.  
  57. for (MeasurementRecord record : inputMeasurements) {
  58. String aggType = "";
  59. if (aggregatorType.equalsIgnoreCase("season")) {
  60. aggType = record.getSeason();
  61. } else if (aggregatorType.equalsIgnoreCase("month")) {
  62. aggType = new DecimalFormat("00").format(record.getMonth().getValue());
  63. } else if (aggregatorType.equalsIgnoreCase("dayofweek")) {
  64. aggType = new DecimalFormat("00").format(record.getDay().getValue());
  65. } else if (aggregatorType.equalsIgnoreCase("period")) {
  66. aggType = record.getTimePeriod();
  67. }
  68.  
  69. if (result.containsKey(aggType)) {
  70. result.put(aggType, result.get(aggType)+record.getKitchen());
  71. sz.put(aggType, sz.get(aggType)+1);
  72. } else {
  73. result.put(aggType, record.getKitchen());
  74. sz.put(aggType, 1);
  75. }
  76. }
  77.  
  78. if (aggFunction.equalsIgnoreCase("avg")) {
  79. for (String str : result.keySet()) {
  80. result.put(str, result.get(str)/sz.get(str));
  81. }
  82. }
  83.  
  84. return result;
  85. }
  86.  
  87. @Override
  88. public HashMap<String, Double> getAggregateMeterLaundry() {
  89. HashMap<String, Double> result = new HashMap<String, Double>();
  90. HashMap<String, Integer> sz = new HashMap<String, Integer>();
  91.  
  92. for (MeasurementRecord record : inputMeasurements) {
  93. String aggType = "";
  94. if (aggregatorType.equalsIgnoreCase("season")) {
  95. aggType = record.getSeason();
  96. } else if (aggregatorType.equalsIgnoreCase("month")) {
  97. aggType = new DecimalFormat("00").format(record.getMonth().getValue());
  98. } else if (aggregatorType.equalsIgnoreCase("dayofweek")) {
  99. aggType = new DecimalFormat("00").format(record.getDay().getValue());
  100. } else if (aggregatorType.equalsIgnoreCase("period")) {
  101. aggType = record.getTimePeriod();
  102. }
  103.  
  104. if (result.containsKey(aggType)) {
  105. result.put(aggType, result.get(aggType)+record.getLaundry());
  106. sz.put(aggType, sz.get(aggType)+1);
  107. } else {
  108. result.put(aggType, record.getLaundry());
  109. sz.put(aggType, 1);
  110. }
  111. }
  112.  
  113. if (aggFunction.equalsIgnoreCase("avg")) {
  114. for (String str : result.keySet()) {
  115. result.put(str, result.get(str)/sz.get(str));
  116. }
  117. }
  118.  
  119. return result;
  120. }
  121.  
  122. @Override
  123. public HashMap<String, Double> getAggregateMeterAC() {
  124. HashMap<String, Double> result = new HashMap<String, Double>();
  125. HashMap<String, Integer> sz = new HashMap<String, Integer>();
  126.  
  127. for (MeasurementRecord record : inputMeasurements) {
  128. String aggType = "";
  129. if (aggregatorType.equalsIgnoreCase("season")) {
  130. aggType = record.getSeason();
  131. } else if (aggregatorType.equalsIgnoreCase("month")) {
  132. aggType = new DecimalFormat("00").format(record.getMonth().getValue());
  133. } else if (aggregatorType.equalsIgnoreCase("dayofweek")) {
  134. aggType = new DecimalFormat("00").format(record.getDay().getValue());
  135. } else if (aggregatorType.equalsIgnoreCase("period")) {
  136. aggType = record.getTimePeriod();
  137. }
  138.  
  139. if (result.containsKey(aggType)) {
  140. result.put(aggType, result.get(aggType)+record.getHeating());
  141. sz.put(aggType, sz.get(aggType)+1);
  142. } else {
  143. result.put(aggType, record.getHeating());
  144. sz.put(aggType, 1);
  145. }
  146. }
  147.  
  148. if (aggFunction.equalsIgnoreCase("avg")) {
  149. for (String str : result.keySet()) {
  150. result.put(str, result.get(str)/sz.get(str));
  151. }
  152. }
  153.  
  154. return result;
  155. }
  156.  
  157. @Override
  158. public String getAggregateFunction() {
  159. return aggFunction;
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement