From fe53e94bf4532820847c977cfa977650a0cab3e4 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Wed, 9 Feb 2022 21:48:05 +0100 Subject: [PATCH] implemented Flight Repository methods --- .../de/ci/exam/project/FlightRepository.java | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java b/src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java index 29eed52..687550b 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/FlightRepository.java @@ -1,5 +1,32 @@ package hs.fulda.de.ci.exam.project; -public interface FlightRepository { - void save(Flight flight); +import java.io.*; +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 ""; + } }