92 lines
2.6 KiB
Java
92 lines
2.6 KiB
Java
package controllers;
|
|
|
|
import javafx.collections.FXCollections;
|
|
import javafx.collections.ObservableList;
|
|
import javafx.scene.control.*;
|
|
import models.Flight;
|
|
import models.Flights;
|
|
import models.Pilot;
|
|
import models.Pilots;
|
|
import utils.XMLHelper;
|
|
|
|
|
|
import java.time.Duration;
|
|
import java.time.LocalTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeParseException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class AddFlightController {
|
|
private Flights flights;
|
|
|
|
private DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
|
|
|
|
public AddFlightController() {
|
|
loadFlights();
|
|
}
|
|
|
|
public ObservableList<Pilot> loadPilots() {
|
|
ObservableList<Pilot> pilotList = FXCollections.observableArrayList();
|
|
Pilots pilots = (Pilots) XMLHelper.loadFromXML("pilots.xml");
|
|
if (pilots != null) {
|
|
List<Pilot> pilotEntries = pilots.getPilots();
|
|
pilotList.addAll(pilotEntries);
|
|
}
|
|
return pilotList;
|
|
}
|
|
|
|
public void saveFlight(Flight flight){
|
|
if(flights == null){
|
|
flights = new Flights(new ArrayList<>());
|
|
}
|
|
|
|
boolean flightExists = false;
|
|
for (int i = 0; i < flights.getFlights().size(); i++) {
|
|
if (flights.getFlights().get(i).getMuster().equals(flight.getMuster())) {
|
|
flights.getFlights().set(i, flight);
|
|
flightExists = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!flightExists) {
|
|
flights.getFlights().add(flight);
|
|
}
|
|
|
|
XMLHelper.saveToXML(flights, "flights.xml");
|
|
}
|
|
|
|
public double calculateFlightDuration(String abflugzeit, String ankunftszeit) throws DateTimeParseException {
|
|
LocalTime start = LocalTime.parse(abflugzeit, timeFormatter);
|
|
LocalTime end = LocalTime.parse(ankunftszeit, timeFormatter);
|
|
return (double) Duration.between(start, end).toMinutes() / 60;
|
|
}
|
|
|
|
public boolean isValidTime(String time) {
|
|
try {
|
|
LocalTime.parse(time, timeFormatter);
|
|
return true;
|
|
} catch (DateTimeParseException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void setErrorStyle(Control field) {
|
|
field.setStyle("-fx-border-color: red;");
|
|
}
|
|
|
|
|
|
public void showAlert(String title, String message) {
|
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
|
alert.setTitle(title);
|
|
alert.setHeaderText(null);
|
|
alert.setContentText(message);
|
|
alert.showAndWait();
|
|
}
|
|
|
|
public void loadFlights() {
|
|
flights = (Flights) XMLHelper.loadFromXML("flights.xml");
|
|
}
|
|
}
|