You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.6 KiB
52 lines
1.6 KiB
package hs.fulda.de.ci.exam.project;
|
|
|
|
import java.io.*;
|
|
import java.util.ArrayList;
|
|
import java.util.Scanner;
|
|
|
|
public class FlightRepository {
|
|
boolean save(Flight flight) throws IOException {
|
|
FileWriter fw = new FileWriter("flights.txt", true);
|
|
BufferedWriter bw = new BufferedWriter(fw);
|
|
bw.write(flight.toString());
|
|
bw.newLine();
|
|
bw.close();
|
|
return true;
|
|
};
|
|
String findFlightByFlightNumber(String flightNumber){
|
|
File file = new File("flights.txt");
|
|
|
|
try {
|
|
Scanner scanner = new Scanner(file);
|
|
|
|
while (scanner.hasNextLine()) {
|
|
String line = scanner.nextLine();
|
|
if(line.matches("(.*)"+flightNumber+"(.*)")) {
|
|
return line;
|
|
}
|
|
}
|
|
} catch(FileNotFoundException e) {
|
|
System.out.println("There are no flights added yet. Please add a flight");
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public ArrayList<String> findbyDepartureArrival(Airport departure, Airport arrival) {
|
|
ArrayList<String> flights = new ArrayList<>();
|
|
File file = new File("flights.txt");
|
|
|
|
try {
|
|
Scanner scanner = new Scanner(file);
|
|
|
|
while (scanner.hasNextLine()) {
|
|
String line = scanner.nextLine();
|
|
if(line.matches("(.*)"+departure+"(.*)") && line.matches("(.*)"+arrival+"(.*)")) {
|
|
flights.add(line);
|
|
}
|
|
}
|
|
} catch(FileNotFoundException e) {
|
|
System.out.println("There are no flights added yet. Please add a flight");
|
|
}
|
|
return flights;
|
|
}
|
|
}
|