Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Мерна Станица
- import java.text.DateFormat;
- import java.text.Format;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collector;
- import java.util.stream.Collectors;
- import javax.management.RuntimeErrorException;
- class Measurment
- {
- float temperature;
- float wind;
- float humidity;
- float visibility;
- Date date;
- public Measurment(float temperature, float wind, float humidity, float visibility, Date date) {
- // TODO Auto-generated constructor stub
- this.date = date;
- this.temperature = temperature;
- this.wind = wind;
- this.humidity = humidity;
- this.visibility = visibility;
- }
- public long getDate()
- {
- return date.getTime();
- }
- }
- class WeatherStation
- {
- int days;
- ArrayList<Measurment> al;
- int total;
- public WeatherStation(int days) {
- // TODO Auto-generated constructor stub
- this.days = days;
- al = new ArrayList<>();
- total = 0;
- }
- public boolean isEmpty()
- {
- return total==0;
- }
- public int total()
- {
- return total;
- }
- public void addMeasurment(float temperature, float wind, float humidity, float visibility, Date date)
- {
- long x = date.getTime()/(1000*60*60*24);
- long min = date.getTime()/(1000*60);
- boolean temp = true;
- Measurment m = new Measurment(temperature, wind, humidity, visibility, date);
- if(this.isEmpty())
- {
- al.add(m);
- this.total++;
- }
- else
- {
- for(int i=0; i<this.total(); i++)
- {
- if(Math.abs(al.get(i).date.getTime()/(1000*60*60*24)-x) >=days)
- {
- al.remove(i);
- this.total--;
- }
- if(Math.abs(al.get(i).date.getTime()/(1000*60)-min)<2.5)
- {
- temp = false;
- break;
- }
- }
- if(temp)
- {
- al.add(m);
- total++;
- }
- }
- }
- public void status(Date from, Date to)
- {
- StringBuilder sb = new StringBuilder();
- List<Measurment> meas =
- al.stream()
- .sorted(Comparator.comparing(Measurment::getDate))
- .collect(Collectors.toList());
- Iterator<Measurment> it = meas.iterator();
- float avg = 0;
- int count = 0;
- while(it.hasNext())
- {
- Measurment m = it.next();
- if((m.date.after(from) && m.date.before(to)) || (m.date.equals(from) || m.date.equals(to)))
- {
- count++;
- avg+=m.temperature;
- Format sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
- String f = sdf.format(m.date);
- sb.append(String.format("%.1f %.1f km/h %.1f%% %.1f km %s\n",
- m.temperature, m.wind, m.humidity, m.visibility,f));
- }
- }
- if(count == 0)
- throw new RuntimeException();
- sb.append(String.format("Average temperature: %.2f", avg/count));
- System.out.println(sb.toString());
- }
- }
- public class WeatherStationTest {
- public static void main(String[] args) throws ParseException {
- Scanner scanner = new Scanner(System.in);
- DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
- int n = scanner.nextInt();
- scanner.nextLine();
- WeatherStation ws = new WeatherStation(n);
- while (true) {
- String line = scanner.nextLine();
- if (line.equals("=====")) {
- break;
- }
- String[] parts = line.split(" ");
- float temp = Float.parseFloat(parts[0]);
- float wind = Float.parseFloat(parts[1]);
- float hum = Float.parseFloat(parts[2]);
- float vis = Float.parseFloat(parts[3]);
- line = scanner.nextLine();
- Date date = df.parse(line);
- ws.addMeasurment(temp, wind, hum, vis, date);
- }
- String line = scanner.nextLine();
- Date from = df.parse(line);
- line = scanner.nextLine();
- Date to = df.parse(line);
- scanner.close();
- System.out.println(ws.total());
- try {
- ws.status(from, to);
- } catch (RuntimeException e) {
- System.out.println(e);
- }
- }
- }
- // vashiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement