summaryrefslogtreecommitdiff
path: root/src/Q10/Station.java
blob: a2b2ae2aa61ad50a3e9bd59075e77517274ec0de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package Q10;

import java.text.*;
import java.util.*;

public class Station {
  private Date arrival;
  private Date departure;
  private int day;
  private String city;

  public Station(String city, Date arrival, Date departure, int day) {
    this.city = city;
    this.arrival = arrival;
    this.departure = departure;
    this.day = day;
  }

  public Date getArrivalDate() {
    return this.arrival;
  }

  public Date getDepartureDate() {
    return this.departure;
  }

  public int getDay() {
    return day;
  }

  public String getCity() {
    return this.city;
  }

  public void setArrivalDate(Date arrival) {
    this.arrival = arrival;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public void setDay(int day) {
    this.day = day;
  }

  public void setDepartureDate(Date departure) {
    this.departure = departure;
  }

  public String getArrival() {
    return formatDate(this.arrival);
  }

  public String getDeparture() {
    return formatDate(this.departure);
  }

  public void delayBy(int minutes) {
    this.setArrivalDate(advanceDate(this.getArrivalDate(), minutes));
    this.setDepartureDate(advanceDate(this.getDepartureDate(), minutes));

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(this.getDepartureDate());
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    this.setDay(calendar.get(Calendar.DAY_OF_MONTH));
  }

  private String formatDate(Date date) {
    if (date == null) return "-";

    DateFormat format = new SimpleDateFormat("HH:mm");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    return format.format(date);
  }

  private Date advanceDate(Date original, int minutes) {
    return new Date(original.getTime() + (minutes * 60000));
  }
}