Advertisement
lameski

Untitled

Jun 8th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 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. total++;
  71. }
  72. else
  73. {
  74. for(int i=0; i<this.total(); i++)
  75. {
  76. // System.out.println("DAYS: " + (al.get(i).date.getTime()/(1000*60*60*24)-x) + " Mins: " + (al.get(i).date.getTime()/(1000*60)-min));
  77. if(Math.abs(al.get(i).date.getTime()/(1000*60*60*24)-x) >days)
  78. {
  79. al.remove(i);
  80. total--;
  81. }
  82. if(Math.abs(al.get(i).date.getTime()/(1000*60)-min)<2.5)
  83. {
  84. temp = false;
  85. break;
  86. }
  87. }
  88. if(temp)
  89. {
  90. al.add(m);
  91. total++;
  92. }
  93. }
  94.  
  95. }
  96.  
  97. public void status(Date from, Date to)
  98. {
  99. StringBuilder sb = new StringBuilder();
  100. List<Measurment> meas =
  101. al.stream()
  102. .sorted(Comparator.comparing(Measurment::getDate))
  103. .collect(Collectors.toList());
  104.  
  105. Iterator<Measurment> it = meas.iterator();
  106. float avg = 0;
  107. int count = 0;
  108.  
  109. while(it.hasNext())
  110. {
  111. Measurment m = it.next();
  112.  
  113. if((m.date.after(from) && m.date.before(to)) || (m.date.equals(from) || m.date.equals(to)))
  114. {
  115. count++;
  116. avg+=m.temperature;
  117. Format sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
  118. /* Format formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
  119. String today = formatter.format(new Date());
  120. System.out.println("Today : " + today);*/
  121. String f = sdf.format(m.date);
  122. //System.out.println(f);
  123. sb.append(String.format("%.1f %.1f km/h %.1f%% %.1f km %s\n",
  124. m.temperature, m.wind, m.humidity, m.visibility,f));
  125. //System.out.println("KUR");
  126. }
  127. }
  128. if(count == 0)
  129. throw new RuntimeException();
  130.  
  131. sb.append(String.format("Average temperature: %.2f", avg/count));
  132.  
  133. System.out.println(sb.toString());
  134. }
  135. }
  136.  
  137. public class WeatherStationTest {
  138. public static void main(String[] args) throws ParseException {
  139. Scanner scanner = new Scanner(System.in);
  140. DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  141. int n = scanner.nextInt();
  142. scanner.nextLine();
  143. WeatherStation ws = new WeatherStation(n);
  144. while (true) {
  145. String line = scanner.nextLine();
  146. if (line.equals("=====")) {
  147. break;
  148. }
  149. String[] parts = line.split(" ");
  150. float temp = Float.parseFloat(parts[0]);
  151. float wind = Float.parseFloat(parts[1]);
  152. float hum = Float.parseFloat(parts[2]);
  153. float vis = Float.parseFloat(parts[3]);
  154. line = scanner.nextLine();
  155. Date date = df.parse(line);
  156. ws.addMeasurment(temp, wind, hum, vis, date);
  157. }
  158. String line = scanner.nextLine();
  159. Date from = df.parse(line);
  160. line = scanner.nextLine();
  161. Date to = df.parse(line);
  162. scanner.close();
  163. System.out.println(ws.total());
  164. try {
  165. ws.status(from, to);
  166. } catch (RuntimeException e) {
  167. System.out.println(e);
  168. }
  169. }
  170. }
  171.  
  172. // vashiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement