Advertisement
lameski

Untitled

Jun 8th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. Мерна Станица
  2.  
  3. import java.text.DateFormat;
  4. import java.text.Format;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.time.LocalDateTime;
  8. import java.util.ArrayList;
  9. import java.util.Comparator;
  10. import java.util.Date;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import java.util.Scanner;
  14. import java.util.stream.Collector;
  15. import java.util.stream.Collectors;
  16.  
  17. import javax.management.RuntimeErrorException;
  18.  
  19. class Measurment
  20. {
  21. float temperature;
  22. float wind;
  23. float humidity;
  24. float visibility;
  25. Date date;
  26. public Measurment(float temperature, float wind, float humidity, float visibility, Date date) {
  27. // TODO Auto-generated constructor stub
  28. this.date = date;
  29. this.temperature = temperature;
  30. this.wind = wind;
  31. this.humidity = humidity;
  32. this.visibility = visibility;
  33. }
  34.  
  35. public long getDate()
  36. {
  37. return date.getTime();
  38. }
  39. }
  40.  
  41. class WeatherStation
  42. {
  43. int days;
  44. ArrayList<Measurment> al;
  45. int total;
  46. public WeatherStation(int days) {
  47. // TODO Auto-generated constructor stub
  48. this.days = days;
  49. al = new ArrayList<>();
  50. total = 0;
  51. }
  52. public boolean isEmpty()
  53. {
  54. return total==0;
  55. }
  56. public int total()
  57. {
  58. return total;
  59. }
  60. public void addMeasurment(float temperature, float wind, float humidity, float visibility, Date date)
  61. {
  62. long x = date.getTime()/(1000*60*60*24);
  63. long min = date.getTime()/(1000*60);
  64. boolean temp = true;
  65. Measurment m = new Measurment(temperature, wind, humidity, visibility, date);
  66.  
  67. if(this.isEmpty())
  68. {
  69. al.add(m);
  70. this.total++;
  71. }
  72. else
  73. {
  74. for(int i=0; i<this.total(); i++)
  75. {
  76. if(Math.abs(al.get(i).date.getTime()/(1000*60*60*24)-x) >=days)
  77. {
  78. al.remove(i);
  79. this.total--;
  80. }
  81. if(Math.abs(al.get(i).date.getTime()/(1000*60)-min)<2.5)
  82. {
  83. temp = false;
  84. break;
  85. }
  86. }
  87. if(temp)
  88. {
  89. al.add(m);
  90. total++;
  91. }
  92. }
  93.  
  94. }
  95.  
  96. public void status(Date from, Date to)
  97. {
  98. StringBuilder sb = new StringBuilder();
  99. List<Measurment> meas =
  100. al.stream()
  101. .sorted(Comparator.comparing(Measurment::getDate))
  102. .collect(Collectors.toList());
  103.  
  104. Iterator<Measurment> it = meas.iterator();
  105. float avg = 0;
  106. int count = 0;
  107.  
  108. while(it.hasNext())
  109. {
  110. Measurment m = it.next();
  111.  
  112. if((m.date.after(from) && m.date.before(to)) || (m.date.equals(from) || m.date.equals(to)))
  113. {
  114. count++;
  115. avg+=m.temperature;
  116. Format sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
  117. String f = sdf.format(m.date);
  118. sb.append(String.format("%.1f %.1f km/h %.1f%% %.1f km %s\n",
  119. m.temperature, m.wind, m.humidity, m.visibility,f));
  120. }
  121. }
  122. if(count == 0)
  123. throw new RuntimeException();
  124.  
  125. sb.append(String.format("Average temperature: %.2f", avg/count));
  126.  
  127. System.out.println(sb.toString());
  128. }
  129. }
  130.  
  131. public class WeatherStationTest {
  132. public static void main(String[] args) throws ParseException {
  133. Scanner scanner = new Scanner(System.in);
  134. DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  135. int n = scanner.nextInt();
  136. scanner.nextLine();
  137. WeatherStation ws = new WeatherStation(n);
  138. while (true) {
  139. String line = scanner.nextLine();
  140. if (line.equals("=====")) {
  141. break;
  142. }
  143. String[] parts = line.split(" ");
  144. float temp = Float.parseFloat(parts[0]);
  145. float wind = Float.parseFloat(parts[1]);
  146. float hum = Float.parseFloat(parts[2]);
  147. float vis = Float.parseFloat(parts[3]);
  148. line = scanner.nextLine();
  149. Date date = df.parse(line);
  150. ws.addMeasurment(temp, wind, hum, vis, date);
  151. }
  152. String line = scanner.nextLine();
  153. Date from = df.parse(line);
  154. line = scanner.nextLine();
  155. Date to = df.parse(line);
  156. scanner.close();
  157. System.out.println(ws.total());
  158. try {
  159. ws.status(from, to);
  160. } catch (RuntimeException e) {
  161. System.out.println(e);
  162. }
  163. }
  164. }
  165.  
  166. // vashiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement