-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlight_Delay.java
258 lines (233 loc) · 11.4 KB
/
Flight_Delay.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package fligt_delay;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class FlightDelay<T> {
static ArrayList<String[]> List = new ArrayList<String[]>();
static LinkedList<String[]> linkedList = new LinkedList<>();
static Map<String, List<String>> sortedMap = new TreeMap<String, List<String>>();
private static HashMap<String, List<String> > map = new HashMap<>();
private static HashMap<String, List<String> > map2 = new HashMap<>();
private static HashMap<String, List<String> > map3 = new HashMap<>();
private static HashMap<String, Integer > mapWithCount = new HashMap<>();
private static HashMap<String, Integer > mapWithCount2 = new HashMap<>();
static String arrayLine[];
static String array[];
static int count=0;
static int count2 = 0;
static int counter3 = 0;
static int counter4=0;
static double sum = 0;
static double sum2 = 0;
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
System.out.println("---------");
System.out.println("Part1");
Part1();
System.out.println("\n---------");
System.out.println("Part2");
Part2();
System.out.println("\n---------");
System.out.println("Part3");
Part3();
}
//6 seconds
public static void Part1() {
String fileName = "flights.csv";
long startTime = System.currentTimeMillis();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
br.readLine();
String data;
while ((data = br.readLine()) != null) {
array = data.split(",");
count ++;
for(int i=0; i<15; i++) {
if(array[i].equals("")) {
count2++;
break;
}
}
}
} catch(Exception e) {
System.out.println("Error");
}
int good = count-count2;
System.out.println("Number of total: " + count );
System.out.println("Number of Eliminated: " +count2);
System.out.println("Number of Good : " + good);
long endTime = System.currentTimeMillis();
long totalTime = endTime-startTime;
System.out.println("\nTotal Time : "+totalTime/1000 + " seconds");
}
//16 seconds
public static void Part2() throws ParseException {
String file = "flights.csv";
Scanner s= new Scanner(System.in);
System.out.println("Enter Fist Date: (Format: MM/dd/yyyy) ");
String first = s.next();
System.out.println("Enter Second Date: (Format: MM/dd/yyyy) ");
String second = s.next();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse(first);
Date secondDate = sdf.parse(second);
long startProcessingTime = System.currentTimeMillis();
try (BufferedReader br = new BufferedReader(new FileReader(file))){
br.readLine();
String data;
while((data = br.readLine()) != null) {
count++;
array = data.split(",");
String temp = array[1] + "/" +array[2] + "/" + array[0];
Date tempF = sdf.parse(temp);
if(tempF.compareTo(firstDate) >= 0 && secondDate.compareTo(tempF) >=0) {
if(!array[4].equals("") && !array[5].equals("") && !array[11].equals("")) {
String flightNo = array[4] + array[5];
if (!map.containsKey(flightNo)) {
map.put(flightNo, new LinkedList<String>());
map.get(flightNo).add(array[11]);
counter3++;
}
else if(map.containsKey(flightNo)) {
map.get(flightNo).add(array[11]);
}
}
}
}
}
catch(Exception e){
System.out.println("Error");
}
for(Map.Entry<String, List<String>> entry : map.entrySet()){
String flight = entry.getKey();
int entries = entry.getValue().size();
mapWithCount.put(flight, entries);
}
Map<String,Integer> sortedFlightMap = sortByValues(mapWithCount);
long endProcessingTime = System.currentTimeMillis();
System.out.println("Flight Number:\t\tEntries:\t\tMax Delay:\t\tMin Delay:\t\tAverage Delay:");
for(String flight: sortedFlightMap.keySet()){
List<String> v = map.get(flight);
int maxValue = Integer.parseInt(v.get(0));
int minValue = Integer.parseInt(v.get(0));
sum = Double.parseDouble(v.get(0));
for(int j = 1; j<v.size(); j++) {
if(maxValue<Integer.parseInt(v.get(j))) {
maxValue = Integer.parseInt(v.get(j));
}
if(Integer.parseInt(v.get(j)) < minValue) {
minValue = Integer.parseInt(v.get(j));
}
sum += Double.parseDouble(v.get(j));
}
System.out.println(flight + "\t\t\t" + v.size() + "\t\t\t" + maxValue + "\t\t\t" + minValue + "\t\t\t" + String.format("%.2f", sum/v.size()) );
}
System.out.println("Airline Flight Count without Repitition: "+counter3);
System.out.println("Airline Flight Count with Repitition: "+count);
long endTotalTime = System.currentTimeMillis();
long totalProcessingTime = endProcessingTime - startProcessingTime;
long totalTime = endTotalTime-startProcessingTime;
System.out.println("\nTime with printing: "+totalTime/1000 + "seconds");
System.out.println("Time without printing: "+totalProcessingTime/1000+ "seconds");
}
//8 seconds
public static void Part3() throws ParseException {
String file = "flights.csv";
Scanner s= new Scanner(System.in);
System.out.println("Enter Fist Date: (Format: MM/dd/yyyy) ");
String first = s.next();
System.out.println("Enter Second Date: (Format: MM/dd/yyyy) ");
String second = s.next();
System.out.println("Enter Airlane Name: ");
String airline = s.next();
long startProcessingTime = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse(first);
Date secondDate = sdf.parse(second);
try (BufferedReader br = new BufferedReader(new FileReader(file))){
br.readLine();
String data;
while((data = br.readLine()) != null) {
count++;
array = data.split(",");
String temp = array[1] + "/" +array[2] + "/" + array[0];
Date tempF = sdf.parse(temp);
if(array[4].equals(airline)) {
if(tempF.compareTo(firstDate) >= 0 && secondDate.compareTo(tempF) >=0) {
if(!array[5].equals("") && !array[11].equals("")
&& !array[8].equals("") && !array[7].equals("")){
String flightNo = airline + array[5] + " " + array[7] + " " +array[8];
if (!map2.containsKey(flightNo)) {
map2.put(flightNo, new LinkedList<String>());
map2.get(flightNo).add(array[11]);
counter4++;
}
else if(map2.containsKey(flightNo)) {
map2.get(flightNo).add(array[11]);
}
}
}
}
}
} catch(Exception e) {
System.out.println("error");
}
for(Map.Entry<String, List<String>> entry : map2.entrySet()){
String flight = entry.getKey();
int entries = entry.getValue().size();
mapWithCount2.put(flight, entries);
}
Map<String,Integer> sortedFlightMap = sortByValues(mapWithCount2);
long endProcessingTime = System.currentTimeMillis();
System.out.println("Original & Dest & Flight:\t\tEntries:\t\tMax Delay:\t\tMin Delay:\t\tAverage Delay:");
for(String flight: sortedFlightMap.keySet()){
List<String> v = map2.get(flight);
int maxValue = Integer.parseInt(v.get(0));
int minValue = Integer.parseInt(v.get(0));
sum2 = Double.parseDouble(v.get(0));
for(int j = 1; j<v.size(); j++) {
if(maxValue<Integer.parseInt(v.get(j))) {
maxValue = Integer.parseInt(v.get(j));
}
if(Integer.parseInt(v.get(j)) < minValue) {
minValue = Integer.parseInt(v.get(j));
}
sum2 += Double.parseDouble(v.get(j));
}
System.out.println(flight + "\t\t\t\t" + v.size() + "\t\t\t" + maxValue + "\t\t\t" + minValue + "\t\t\t" + String.format("%.2f", sum2/v.size()) );
}
System.out.println("Airlines & Flight Count without Repitition: "+counter4);
System.out.println("Airline Flight Count with Repitition: "+count);
long endTotalTime = System.currentTimeMillis();
long totalProcessingTime = endProcessingTime - startProcessingTime;
long totalTime = endTotalTime-startProcessingTime;
System.out.println("\nTime with printing: "+totalTime/1000 + "seconds");
System.out.println("Time without printing: "+totalProcessingTime/1000+ "seconds");
}
public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
Comparator<K> valueComparator = (K k1, K k2) -> {
int compare =
map.get(k2).compareTo(map.get(k1));
if (compare == 0)
return 1;
else
return compare;
};
Map<K, V> sortedByValues = new TreeMap<>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
}