-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBestFlightFinder.txt
More file actions
94 lines (81 loc) · 3.77 KB
/
BestFlightFinder.txt
File metadata and controls
94 lines (81 loc) · 3.77 KB
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
package Test1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
public class BestFlightFinder {
public static void findBestFlight(String departureCity, String arrivalCity, List<String> csvFiles, String airline) {
String bestFlight = null;
double minPrice = Double.MAX_VALUE;
Duration minDuration = Duration.ofDays(1); // initialize with a large duration
for (String csvFile : csvFiles) {
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
if (values.length < 6) {
continue; // skip invalid rows
}
String departureTimeStr = values[0].trim();
String arrivalTimeStr = values[1].trim();
double price = parsePrice(values[2].trim());
String flightAirline = values[3].trim().toLowerCase();
String departure = values[4].trim().toLowerCase();
String arrival = values[5].trim().toLowerCase();
try {
int departureTime = convertTo24HourFormat(departureTimeStr);
int arrivalTime = convertTo24HourFormat(arrivalTimeStr);
Duration duration = calculateDuration(departureTime, arrivalTime);
if (departure.equals(departureCity.toLowerCase()) && arrival.equals(arrivalCity.toLowerCase())) {
if (airline == null || airline.equals(flightAirline)) {
if ((price < minPrice) || (price == minPrice && duration.compareTo(minDuration) < 0)) {
minPrice = price;
minDuration = duration;
bestFlight = line;
}
}
}
} catch (Exception e) {
// Continue to next line if there's an error parsing times
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (bestFlight != null) {
System.out.println("\nBest flight:");
System.out.println(bestFlight);
System.out.println("Duration of flight: " + minDuration.toMinutes() + " minutes");
} else {
System.out.println("\nNo flights found matching the criteria.");
}
}
private static double parsePrice(String priceStr) {
// Remove any non-numeric characters except for the decimal point
String cleanedPriceStr = priceStr.replaceAll("[^\\d.]", "");
return Double.parseDouble(cleanedPriceStr);
}
private static int convertTo24HourFormat(String timeStr) {
String[] parts = timeStr.split(" ");
String timePart = parts[0];
String periodPart = parts[1];
String[] timeParts = timePart.split(":");
int hour = Integer.parseInt(timeParts[0]);
int minute = Integer.parseInt(timeParts[1]);
if (periodPart.equalsIgnoreCase("PM") && hour != 12) {
hour += 12;
} else if (periodPart.equalsIgnoreCase("AM") && hour == 12) {
hour = 0;
}
return hour * 60 + minute;
}
private static Duration calculateDuration(int departureTime, int arrivalTime) {
if (arrivalTime < departureTime) {
// Arrival time is on the next day
arrivalTime += 24 * 60;
}
return Duration.ofMinutes(arrivalTime - departureTime);
}
}