-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchService.java
More file actions
60 lines (55 loc) · 2.4 KB
/
Copy pathSearchService.java
File metadata and controls
60 lines (55 loc) · 2.4 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
import backend.classes.Flight;
import backend.classes.Database;
import backend.interfaces.DatabaseInterface;
import backend.interfaces.FlightInterface;
import enums.AirlineTable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SearchService {
public static void main(String[] args) {
try (DatabaseInterface flightRepo = new Database("jdbc:mysql://127.0.0.1:3306/flightdata", "root", "root")) {
Scanner scanner = new Scanner(System.in);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = null;
while (startDate == null) {
System.out.print("Enter a date to depart (yyyy-MM-dd): ");
String userInput = scanner.nextLine();
try {
startDate = LocalDate.parse(userInput, formatter);
} catch (DateTimeParseException e) {
System.out.println("Invalid date format. Please enter the date in yyyy-MM-dd format.");
}
}
System.out.println("Enter the three letter ID code for your departure airport");
String departureAirport = scanner.nextLine().substring(0, 3).toUpperCase();
System.out.println("Enter the three letter ID code for your arrival airport");
String arriveAirport = scanner.nextLine().substring(0, 3).toUpperCase();
String depart = startDate + " 00:00:00";
String latest = startDate + " 23:59:59";
Timestamp startTime = Timestamp.valueOf(depart);
Timestamp endTime = Timestamp.valueOf(latest);
String sort = "traveltime"; // from GUI
List<Object> params = new ArrayList<>();
params.add(departureAirport);
params.add(arriveAirport);
params.add(startTime);
params.add(endTime);
List<AirlineTable> airlines = new ArrayList<>();
airlines.add(AirlineTable.DELTAS);
airlines.add(AirlineTable.SOUTHWESTS);
List<FlightInterface> flights = flightRepo.selectFlights(airlines, sort, params);
flights.forEach(System.out::println);
if (flights.isEmpty()) {
System.out.println("No flights found.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}