@matfah/

Flight FR 2008

Java

No description

fork
loading
Files
  • Main.java
  • Flight.java
  • Time.java
  • Trip.java
Main.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.lang.reflect.*;
import java.util.ArrayList;

public class Main {
  
  public static void main(String[] args) {
    testGetDuration();
    System.out.println("Finished testing getDuration() successfully!");
    testShortestLayover();
    System.out.println("Finished testing shortestLayover() successfully!");
    System.out.println("All tests passed!");
  }
  
  public static void testGetDuration() {
    
    Field flightsField = null;
    try {
      flightsField = Trip.class.getDeclaredField("flights");
    }
    catch(Exception e) {
      System.out.println("Did you change the flights instance variable in the Trip class?!");
      System.exit(0);
    }
    flightsField.setAccessible(true);
    
    for(int numFlights = 0; numFlights < 10; numFlights++) {
      
      ArrayList<Flight> flights = new ArrayList<Flight>();
      Trip trip = new Trip();
      
      try {
        flightsField.set(trip, flights);
      }
      catch(Exception e) {
        System.out.println("Unable to initialize flights instance variable in the Trip class");
        System.exit(0);
      }
      
      int firstDepart = 0;
      int lastArrive = 0;
      
      int lastTime = 0;
      
      for(int i = 0; i < numFlights; i++) {
        
        int depart = (int)(Math.random()*60) + lastTime + 1;
        int arrive = (int)(Math.random()*60) + depart + 1;
        
        flights.add(new CustomFlight(depart, arrive));
        
        if(i == 0) firstDepart = depart;
        if(i == numFlights-1) lastArrive = arrive;
        
        lastTime = arrive;
      }
      
      int duration = lastArrive - firstDepart;
      int result = -1;
      
      try {
        result = trip.getDuration();
      }
      catch(Exception e) {
        System.out.println("Error calling getDuration() method of Trip class");
        System.exit(0);
      }
      
      if(duration != result) {
        System.out.println("Value returned by getDuration() is incorrect");
        System.exit(0);
      }
      
    }
    
  }
  
  public static void testShortestLayover() {
    
    Field flightsField = null;
    try {
      flightsField = Trip.class.getDeclaredField("flights");
    }
    catch(Exception e) {
      System.out.println("Did you change the flights instance variable in the Trip class?!");
      System.exit(0);
    }
    flightsField.setAccessible(true);
    
    for(int numFlights = 0; numFlights < 10; numFlights++) {
      
      ArrayList<Flight> flights = new ArrayList<Flight>();
      Trip trip = new Trip();
      
      try {
        flightsField.set(trip, flights);
      }
      catch(Exception e) {
        System.out.println("Unable to initialize flights instance variable in the Trip class");
        System.exit(0);
      }
      
      int shortest = -1;
      
      int lastArrive = -1;
      
      for(int i = 0; i < numFlights; i++) {
        
        int depart = (int)(Math.random()*60) + lastArrive + 1;
        int arrive = (int)(Math.random()*60) + depart + 1;
        
        flights.add(new CustomFlight(depart, arrive));
        
        if(i == 1) {
          shortest = depart - lastArrive;
        }
        else if(i > 1 && depart - lastArrive < shortest) {
          shortest = depart - lastArrive;
        }

        
        lastArrive = arrive;
      }
      
      int result = -1;
      
      try {
        result = trip.getShortestLayover();
      }
      catch(Exception e) {
        System.out.println("Error calling getShortestLayover() method of Trip class");
        System.exit(0);
      }
      
      if(shortest != result) {
        System.out.println("Value returned by getShortestLayover() is incorrect");
        System.exit(0);
      }
      
    }
    
  }
  
}

class CustomTime extends Time {
  
  private int minutes;
  
  public CustomTime(int m) {
    minutes = m;
  }
  
  public int minutesUntil(Time other) {
        return ((CustomTime)other).minutes - minutes;
  }
}

class CustomFlight extends Flight
{
    private Time depart;
    private Time arrive;
    
    public CustomFlight(int d, int a) {
      depart = new CustomTime(d);
      arrive = new CustomTime(a);
    }
  
    public Time getDepartureTime() {
        return depart;
    }
    
    public Time getArrivalTime() {
        return arrive;
    }
}