DamSi

Untitled

Aug 12th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import java.text.*;
  2. import java.util.*;
  3. import java.util.concurrent.TimeUnit;
  4. //Prereshena - @author: Emilija Gjorgjevska, 2015
  5.  
  6. public class WeatherStationTest {
  7.     public static void main(String[] args) throws ParseException {
  8.         Scanner scanner = new Scanner(System.in);
  9.         DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  10.         int n = scanner.nextInt();
  11.         scanner.nextLine();
  12.         WeatherStation ws = new WeatherStation(n);
  13.         while (true) {
  14.             String line = scanner.nextLine();
  15.             if (line.equals("=====")) {
  16.                 break;
  17.             }
  18.             String[] parts = line.split(" ");
  19.             float temp = Float.parseFloat(parts[0]);
  20.             float wind = Float.parseFloat(parts[1]);
  21.             float hum = Float.parseFloat(parts[2]);
  22.             float vis = Float.parseFloat(parts[3]);
  23.             line = scanner.nextLine();
  24.             Date date = df.parse(line);
  25.             ws.addMeasurment(temp, wind, hum, vis, date);
  26.         }
  27.         String line = scanner.nextLine();
  28.         Date from = df.parse(line);
  29.         line = scanner.nextLine();
  30.         Date to = df.parse(line);
  31.         scanner.close();
  32.         System.out.println(ws.total());
  33.         try {
  34.             ws.status(from, to);
  35.         } catch (RuntimeException e) {
  36.             System.out.println(e);
  37.         }
  38.     }
  39. }
  40. // vashiot kod ovde
  41. class Data implements Comparable<Data> {
  42.  
  43.     private float humidity;
  44.     private float wind;
  45.     private float visibility;
  46.     float temperature;
  47.     Date time;
  48.  
  49.     public Data(float temperature, float humidity, float wind,
  50.             float visibility, Date time) {
  51.         this.temperature = temperature;
  52.         this.humidity = humidity;
  53.         this.wind = wind;
  54.         this.visibility = visibility;
  55.         this.time = time;
  56.     }
  57.  
  58.     public boolean checkMinutes(Date d) {
  59.         if (Double.compare(
  60.                 TimeUnit.MILLISECONDS.toMinutes(d.getTime() - time.getTime()),
  61.                 2.5) < 0)
  62.             return true;
  63.         else
  64.             return false;
  65.     }
  66.  
  67.     public boolean checkDays(Date d, int days) {
  68.         long result = Long.compare(d.getTime() - time.getTime(),
  69.                 TimeUnit.DAYS.toMillis(days));
  70.         if (result >= 0)
  71.             return true;
  72.         else
  73.             return false;
  74.     }
  75.  
  76.     @Override
  77.     public int compareTo(Data o) {
  78.         return time.compareTo(o.time);
  79.     }
  80.  
  81.     public String toString() {
  82.         return String.format("%.1f %.1f km/h %.1f%% %.1f km %s\n", temperature, wind, humidity, visibility, time.toString());
  83.     }
  84. }
  85.  
  86. class WeatherStation {
  87.     private int days;
  88.     private int total;
  89.     private ArrayList<Data> datas;
  90.  
  91.     public WeatherStation(int days) {
  92.         this.days = days;
  93.         this.total = 0;
  94.         this.datas = new ArrayList<Data>();
  95.     }
  96.  
  97.     public void addMeasurment(float temperature, float wind, float humidity,
  98.             float visibility, Date time) {
  99.         for (int i = 0; i < datas.size(); i++)
  100.             if (datas.get(i).checkMinutes(time))
  101.                 return;
  102.         datas.add(new Data(temperature, humidity, wind, visibility, time));
  103.         total++;
  104.         for (int i = 0; i < datas.size(); i++) {
  105.             if (datas.get(i).checkDays(time, days)) {
  106.                 datas.remove(i);
  107.                 i--;
  108.                 total--;
  109.             }
  110.         }
  111.  
  112.     }
  113.  
  114.     public int total() {
  115.         return total;
  116.     }
  117.  
  118.     public void status(Date from, Date to) {
  119.         Collections.sort(datas);
  120.         float average = 0;
  121.         int counter = 0, numelements = 0;
  122.         StringBuilder s = new StringBuilder();
  123.         for (int i = 0; i < datas.size(); i++) {
  124.             if (datas.get(i).time.equals(from) || datas.get(i).time.after(from)
  125.                     || datas.get(i).time.equals(to)) {
  126.                 average += datas.get(i).temperature;
  127.                 s.append(datas.get(i).toString());
  128.                 numelements++;
  129.                 if (datas.get(i).time.equals(from)
  130.                         || datas.get(i).time.equals(to))
  131.                     counter++;
  132.  
  133.                 if (datas.get(i).time.equals(to))
  134.                     break;
  135.             }
  136.         }
  137.         if (counter == 0 || from.after(to))
  138.             throw new RuntimeException();
  139.         s.append(String.format("Average temperature: %.2f", average / numelements));
  140.         System.out.println(s.toString());
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment