diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Admin.java b/src/main/java/hs/fulda/de/ci/exam/project/Admin.java index ce8cb1e..236cd83 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Admin.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Admin.java @@ -28,14 +28,6 @@ public class Admin extends Person{ return aircraftDetails; } - public String searchFlights(String flightNumber){ - String flightDetails = flightRepository.findFlightByFlightNumber(flightNumber); - if(flightDetails.isBlank()){ - throw new RuntimeException("Flight does not exist."); - } - return flightDetails; - } - public Flight addFlight(String flightNumber, Airport departure, Airport arrival, int durationInMinutes ) throws IOException { if(flightNumber.isBlank()) throw new RuntimeException("FlightNumber cannot be null or empty"); if(departure.equals(null)) throw new RuntimeException("Departure cannot be null or empty"); diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Airport.java b/src/main/java/hs/fulda/de/ci/exam/project/Airport.java index 387711c..5cf4bfd 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Airport.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Airport.java @@ -15,6 +15,10 @@ public class Airport { this.code = code; } + public Airport() { + + } + public String getName() { return this.name; } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Customer.java b/src/main/java/hs/fulda/de/ci/exam/project/Customer.java index 4d8491f..a092797 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Customer.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Customer.java @@ -6,7 +6,6 @@ import java.util.ArrayList; public class Customer extends Person{ private String frequentFlyerNumber; - private ItineraryRepository itineraryRepository; @@ -22,7 +21,4 @@ public class Customer extends Person{ return frequentFlyerNumber; } - public ArrayList getItineraries() { - return itineraryRepository.findAll(); - } } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Flight.java b/src/main/java/hs/fulda/de/ci/exam/project/Flight.java index 4e6604d..e41111a 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Flight.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Flight.java @@ -18,6 +18,10 @@ public class Flight { this.durationInMinutes = durationInMinutes; } + public Flight() { + + } + public HashSet getInstances() { return this.flightInstances; } 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 687550b..e8b32ad 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,6 +1,7 @@ package hs.fulda.de.ci.exam.project; import java.io.*; +import java.util.ArrayList; import java.util.Scanner; public class FlightRepository { @@ -29,4 +30,23 @@ public class FlightRepository { } return ""; } + + public ArrayList findbyDepartureArrival(Airport departure, Airport arrival) { + ArrayList 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; + } } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java index 2ccfa34..d110357 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservation.java @@ -20,11 +20,10 @@ public class FlightReservation { this.status = status; } - public FlightReservation() { + public FlightReservation(){ } - public String getReservationNumber() { return reservationNumber; } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservationRepository.java b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservationRepository.java index 5a1a071..c169649 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/FlightReservationRepository.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/FlightReservationRepository.java @@ -1,8 +1,36 @@ package hs.fulda.de.ci.exam.project; -import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class FlightReservationRepository { + Map flightReservationList = new ConcurrentHashMap(); + + public boolean checkIfFlightAlreadyExist(FlightReservation flightReservation){ + if(flightReservationList.containsKey(generateKey(flightReservation))){ + return true; + } + return false; + } + FlightReservation findById(String id){ + FlightReservation flightReservation = flightReservationList.get(id); + return flightReservation; + } + + private String generateKey(FlightReservation flightReservation) { + return String.format("%s", flightReservation.getReservationNumber()); + } + public Collection findAll() { + return flightReservationList.values(); + } + public boolean save(FlightReservation flightReservation){ + flightReservationList.put(generateKey(flightReservation), flightReservation); + return true; + } + + public void delete(FlightReservation flightReservation) { + flightReservationList.remove(generateKey(flightReservation)); + } -public interface FlightReservationRepository { - ArrayList findAll(); - void save(FlightReservation flightReservation); } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java b/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java index 00f9b5a..126aa8b 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java @@ -1,6 +1,7 @@ package hs.fulda.de.ci.exam.project; import java.util.Date; +import java.util.HashMap; import java.util.List; public class Itinerary { @@ -15,38 +16,35 @@ public class Itinerary { this.creationDate = creationDate; } - public List getReservations(){ - return flightReservationRepository.findAll(); - } - public float makeDiscount(float fare, float discountRate){ fare = fare - (fare * discountRate/100); return fare; } - public boolean makePayment(String transactionType, Customer customer, float fare){ + public boolean makePayment(FlightReservation flightReservation, String transactionType, Customer customer, float fare){ if(customer.getItineraries().size() >= 2) { - fare = makeDiscount(fare, 10); - if(transactionType == "Credit"){ - fare = makeDiscount(fare, 15); - System.out.println("Your discount rate is 15%. The total amount of: " + fare + "Euro"); - return true; - } - else if(transactionType == "Cash" || transactionType == "Check") { - System.out.println("Your discount rate is 10%. The total amount of: " + fare + "Euro"); - return true; - } + fare = makeDiscount(fare, 10); + if (transactionType == "Credit") { + fare = makeDiscount(fare, 15); + System.out.println("Your discount rate is 15%. The total amount of: " + fare + "Euro"); + return true; + } else if (transactionType == "Cash" || transactionType == "Check") { + System.out.println("Your discount rate is 10%. The total amount of: " + fare + "Euro"); + return true; + } } - + flightReservation.setStatus(ReservationStatus.Pending); return false; } - public boolean makeReservation(Passenger passenger){ - List flightReservations = getReservations(); + public FlightReservation makeReservation(FlightSeat flightSeat, Passenger passenger){ + HashMap seatHashMap= new HashMap(); + seatHashMap.put(passenger, flightSeat); + validatePassengerDetails(passenger); FlightReservation flight = new FlightReservation(); - flightReservationRepository.save(flight); - - return false; + flight.setSeatMap(seatHashMap); + flight.setStatus(ReservationStatus.Requested); + return flight; } public Airport getFinal_airport() { @@ -86,5 +84,13 @@ public class Itinerary { if(this.creationDate.equals(null)) throw new RuntimeException(("Creation Date should not be null or empty")); } + + public void validatePassengerDetails(Passenger passenger){ + passenger.validateName(); + passenger.validatePassportNumber(); + passenger.validateDate(); + } + + } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Passenger.java b/src/main/java/hs/fulda/de/ci/exam/project/Passenger.java index 8c85690..b7b9d89 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Passenger.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Passenger.java @@ -1,4 +1,40 @@ package hs.fulda.de.ci.exam.project; +import java.util.Date; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class Passenger { + private String name; + private String passportNumber; + private Date dateOfBirth; + + public Passenger(String name, String passportNumber, Date dateOfBirth) { + this.name = name; + this.passportNumber = passportNumber; + this.dateOfBirth = dateOfBirth; + } + + public void validateName() { + if(name.isBlank()) { + throw new RuntimeException(("Name cannot be null or empty")); + } + } + + public void validatePassportNumber() { + Pattern pattern = Pattern.compile("^(?!^0+$)[a-zA-Z0-9]{3,20}$"); + Matcher matcher = pattern.matcher(this.passportNumber); + if(passportNumber.isBlank()){ + throw new RuntimeException("Passport number cannot be blank"); + } + if(!matcher.matches()){ + throw new RuntimeException("Passport number cannot be null or empty"); + } + } + + public void validateDate() { + if(dateOfBirth.equals(null)) { + throw new RuntimeException("Birthdate cannot be null"); + } + } } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Person.java b/src/main/java/hs/fulda/de/ci/exam/project/Person.java index f8aad64..95b4c15 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Person.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Person.java @@ -1,5 +1,6 @@ package hs.fulda.de.ci.exam.project; +import java.util.ArrayList; import java.util.HashSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -9,6 +10,8 @@ public class Person { private Address address; private String email; private String phone; + private FlightRepository flightRepository; + private ItineraryRepository itineraryRepository; public Person(String name, Address address, String email, String phone) { this.name = name; @@ -67,4 +70,24 @@ public class Person { throw new RuntimeException("Phone Number should start with 0"); } } + + public String searchFlights(String flightNumber){ + String flightDetails = flightRepository.findFlightByFlightNumber(flightNumber); + if(flightDetails.isBlank()){ + throw new RuntimeException("Flight does not exist."); + } + return flightDetails; + } + + public ArrayList searchAllFlightsByCriteria(Airport departure, Airport arrival){ + ArrayList flights = flightRepository.findbyDepartureArrival(departure, arrival); + if(flights.isEmpty()){ + throw new RuntimeException("Flights do not exist."); + } + return flights; + } + + public ArrayList getItineraries() { + return itineraryRepository.findAll(); + } } diff --git a/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java b/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java index 9a6f2e9..2bf044f 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/AdminTest.java @@ -98,13 +98,6 @@ public class AdminTest { }); } - @Test - public void test_searchFlight(){ - when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn(flight1.toString()); - assertNotNull(admin.searchFlights("1")); - assertEquals(admin.searchFlights("1"), flight1.toString()); - } - @Test public void test_searchFlightThrowsExceptionWhenBlank(){ when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn(""); diff --git a/src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java b/src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java index a3b75b7..bf1e52f 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/ItineraryTest.java @@ -1,15 +1,25 @@ package hs.fulda.de.ci.exam.project; +import org.junit.Assert; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.InjectMocks; import org.mockito.Mock; + +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; - -import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @ExtendWith(MockitoExtension.class) public class ItineraryTest { @@ -17,14 +27,16 @@ public class ItineraryTest { final Address address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany"); final Airport airport_fr = new Airport("Fraport", address1, "1234"); final Airport airport_be = new Airport("Berlin", address1, "5678"); + final Itinerary item2 = new Itinerary(airport_be, airport_fr, new Date()); @InjectMocks final Customer person1 = new Customer("Max Mustermann", address1, "max.mustermann@gmail.com", "015147890206"); final Itinerary item1 = new Itinerary(airport_fr, airport_be, new Date()); - final Itinerary item2 = new Itinerary(airport_be, airport_fr, new Date()); @Mock private ItineraryRepository itineraryRepo; + @Mock + private FlightReservationRepository flightReservationRepository; @Test public void test_makePayment() { @@ -35,10 +47,10 @@ public class ItineraryTest { when(person1.getItineraries()).thenReturn(itineraries); - boolean actualWithCredit = item1.makePayment("Credit", person1, 450); - boolean actualWithCash = item1.makePayment("Cash", person1, 450); - boolean actualWithCheck = item1.makePayment("Check", person1, 450); - boolean actualEmpty = item1.makePayment(" ", person1, 450); + boolean actualWithCredit = item1.makePayment(new FlightReservation(),"Credit", person1, 450); + boolean actualWithCash = item1.makePayment(new FlightReservation(), "Cash", person1, 450); + boolean actualWithCheck = item1.makePayment(new FlightReservation(), "Check", person1, 450); + boolean actualEmpty = item1.makePayment(new FlightReservation(), " ", person1, 450); assertEquals(true, actualWithCash, "The Payment method is successfully chosen"); assertEquals(true, actualWithCash, "The Payment method is successfully chosen"); @@ -46,4 +58,55 @@ public class ItineraryTest { assertEquals(true, actualWithCheck, "The Payment method is successfully chosen"); assertEquals(false, actualEmpty, "The Payment method is wrong"); } + + @Test + public void test_makePaymentStatus(){ + FlightReservation flightReservation = new FlightReservation(); + item1.makePayment(flightReservation,"Credit", person1, 450); + assertEquals(flightReservation.getStatus(), ReservationStatus.Pending); + } + + @Test + @DisplayName("Should Not Make Reservation when Passenger name is null") + public void shouldThrowRuntimeExceptionWhenNameIsNull() { + String msg = null; + try { + item1.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("", "Ab", new Date())); + } catch (Exception e) { + msg = e.getMessage(); + } + assertEquals("Name cannot be null or empty", msg); + } + + @DisplayName("Should Not Make Reservation when Passenger passport number is invalid") + @ParameterizedTest + @MethodSource("passportNumberList") + public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String passportNumber) { + String msg = null; + try { + item1.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("John", passportNumber, new Date())); + } catch (Exception e) { + msg = e.getMessage(); + } + assertEquals("Passport number cannot be null or empty", msg); + } + + private static List passportNumberList() { + return Arrays.asList("A2", "000000", "AB231837%8"); + } + + @Test + @DisplayName("Should Not Make Reservation when Passenger Birthdate is null") + public void shouldThrowRuntimeExceptionWhenDateIsNull() { + assertThrows(RuntimeException.class, () -> { + item1.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("John", "AB127389", null)); + }); + } + + @Test + public void test_makeReservation() { + FlightReservation flightreservation = item2.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("John", "AB2389", new Date())); + assertNotNull(flightreservation); + assertEquals(flightreservation.getStatus(), ReservationStatus.Requested); + } } \ No newline at end of file diff --git a/src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java b/src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java index 6a08e97..0d6157e 100644 --- a/src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java +++ b/src/test/java/hs/fulda/de/ci/exam/project/PersonTest.java @@ -2,15 +2,37 @@ package hs.fulda.de.ci.exam.project; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +@ExtendWith(MockitoExtension.class) public class PersonTest { + Address address_fr = new Address("Frankfurt str", "Frankfurt", "Hessen", "63023", "Germany"); + Airport airport_fr = new Airport("Fraport", address_fr, "1234"); + + + Address address1_ist = new Address("Istanbul str", "Istanbul", "Fatih", "9019", "Turkey"); + Airport airport1_ist = new Airport("Istanbul", address1_ist, "5678"); + Address address1 = new Address("Fuldaer str", "Fulda", "Hessen", "36037", "Germany"); + @InjectMocks Person person1 = new Person("Max Mustermann", address1, "max.mustermann@gmail.com", "015147890206"); + @Mock + private FlightRepository flightRepository; + @Test void test_getName() { assertThat(person1.getName()).describedAs("get person name").isEqualTo("Max Mustermann"); @@ -29,4 +51,20 @@ public class PersonTest { void test_getPhone() { assertThat(person1.getPhone()).describedAs("get person phone").isEqualTo("015147890206"); } + + @Test + public void test_searchAllFlightsByCriteria(){ + ArrayList flights = new ArrayList<>(); + flights.add("Flight1"); + when(flightRepository.findbyDepartureArrival(airport_fr, airport1_ist)).thenReturn(flights); + assertNotNull(person1.searchAllFlightsByCriteria(airport_fr, airport1_ist)); + } + + @Test + public void test_searchFlight(){ + Flight flight1 = new Flight("1", airport_fr, airport1_ist, 140); + when(flightRepository.findFlightByFlightNumber(any(String.class))).thenReturn(flight1.toString()); + assertNotNull(person1.searchFlights("1")); + assertEquals(person1.searchFlights("1"), flight1.toString()); + } }