From 6ab9545d1a90f5c3804da93c03e6297aa8b06deb Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 15:47:15 +0100 Subject: [PATCH 1/8] refactor Person --- .../java/hs/fulda/de/ci/exam/project/Admin.java | 8 -------- .../hs/fulda/de/ci/exam/project/Customer.java | 4 ---- .../java/hs/fulda/de/ci/exam/project/Person.java | 15 +++++++++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) 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/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/Person.java b/src/main/java/hs/fulda/de/ci/exam/project/Person.java index f8aad64..31d72dd 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,16 @@ 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 getItineraries() { + return itineraryRepository.findAll(); + } } From 37804111a58dce2afa1af135315feec98df2d6c3 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 16:58:59 +0100 Subject: [PATCH 2/8] should not make reservation when passenger name is blank --- .../hs/fulda/de/ci/exam/project/Itinerary.java | 7 +++++++ .../hs/fulda/de/ci/exam/project/Passenger.java | 17 +++++++++++++++++ .../fulda/de/ci/exam/project/ItineraryTest.java | 16 ++++++++++++++++ 3 files changed, 40 insertions(+) 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..81d9a26 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 @@ -42,6 +42,7 @@ public class Itinerary { } public boolean makeReservation(Passenger passenger){ + validatePassengerDetails(passenger); List flightReservations = getReservations(); FlightReservation flight = new FlightReservation(); flightReservationRepository.save(flight); @@ -86,5 +87,11 @@ 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(); + } + + } 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..69b2093 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,21 @@ package hs.fulda.de.ci.exam.project; +import java.util.Date; + 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() { + throw new RuntimeException(("Name cannot be null or empty")); + } + + } 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..bbaa039 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,9 +1,13 @@ 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.mockito.InjectMocks; import org.mockito.Mock; + +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; import org.mockito.junit.jupiter.MockitoExtension; import java.util.ArrayList; @@ -46,4 +50,16 @@ public class ItineraryTest { assertEquals(true, actualWithCheck, "The Payment method is successfully chosen"); assertEquals(false, actualEmpty, "The Payment method is wrong"); } + + @Test + @DisplayName("Should Not Make Reservation when Passenger name is null") + public void shouldThrowRuntimeExceptionWhenNameIsNull(){ + String msg = null; + try { + item1.makeReservation(new Passenger("", "Ab", new Date())); + } catch (Exception e) { + msg = e.getMessage(); + } + assertEquals(msg, "Name cannot be null or empty"); + } } \ No newline at end of file From dac5318ef9f368d66717ad93bee6efa2338ee104 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 19:35:06 +0100 Subject: [PATCH 3/8] should not make reservation when passenger passport number is invalid --- .../fulda/de/ci/exam/project/Itinerary.java | 1 + .../fulda/de/ci/exam/project/Passenger.java | 15 +++++++++++ .../de/ci/exam/project/ItineraryTest.java | 25 ++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) 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 81d9a26..71ebcb9 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 @@ -90,6 +90,7 @@ public class Itinerary { public void validatePassengerDetails(Passenger passenger){ passenger.validateName(); + passenger.validatePassportNumber(); } 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 69b2093..d4ea2dd 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,6 +1,8 @@ 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; @@ -14,7 +16,20 @@ public class Passenger { } 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"); + } } 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 bbaa039..dc2a480 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 @@ -4,6 +4,8 @@ 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; @@ -11,7 +13,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; import org.mockito.junit.jupiter.MockitoExtension; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -60,6 +66,23 @@ public class ItineraryTest { } catch (Exception e) { msg = e.getMessage(); } - assertEquals(msg, "Name cannot be null or empty"); + 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 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"); } } \ No newline at end of file From 89a20474f53bbc936b1b00d25f59c771d5add954 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 19:44:23 +0100 Subject: [PATCH 4/8] should not make reservation when passenger birthdate is null --- .../java/hs/fulda/de/ci/exam/project/Itinerary.java | 1 + .../java/hs/fulda/de/ci/exam/project/Passenger.java | 6 +++++- .../hs/fulda/de/ci/exam/project/ItineraryTest.java | 12 ++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) 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 71ebcb9..cdfe540 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 @@ -91,6 +91,7 @@ public class Itinerary { 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 d4ea2dd..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 @@ -32,5 +32,9 @@ public class Passenger { } } - + public void validateDate() { + if(dateOfBirth.equals(null)) { + throw new RuntimeException("Birthdate cannot be null"); + } + } } 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 dc2a480..3b9f9da 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 @@ -59,7 +59,7 @@ public class ItineraryTest { @Test @DisplayName("Should Not Make Reservation when Passenger name is null") - public void shouldThrowRuntimeExceptionWhenNameIsNull(){ + public void shouldThrowRuntimeExceptionWhenNameIsNull() { String msg = null; try { item1.makeReservation(new Passenger("", "Ab", new Date())); @@ -72,7 +72,7 @@ public class ItineraryTest { @DisplayName("Should Not Make Reservation when Passenger passport number is invalid") @ParameterizedTest @MethodSource("passportNumberList") - public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String passportNumber){ + public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String passportNumber) { String msg = null; try { item1.makeReservation(new Passenger("John", passportNumber, new Date())); @@ -85,4 +85,12 @@ public class ItineraryTest { 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 Passenger("John", "AB127389", null)); + }); + } } \ No newline at end of file From e4f7c68e2ad68a8b5148c339d3df7b64c04f5737 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 21:24:20 +0100 Subject: [PATCH 5/8] implemented flight reservation repository --- .../project/FlightReservationRepository.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) 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); } From ab409115b072b2dfa68fa7571278096772a73d10 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 21:24:46 +0100 Subject: [PATCH 6/8] refactor make reservation --- .../de/ci/exam/project/FlightReservation.java | 3 +-- .../fulda/de/ci/exam/project/Itinerary.java | 16 ++++++------- .../de/ci/exam/project/ItineraryTest.java | 23 +++++++++++++------ 3 files changed, 24 insertions(+), 18 deletions(-) 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/Itinerary.java b/src/main/java/hs/fulda/de/ci/exam/project/Itinerary.java index cdfe540..2719856 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,10 +16,6 @@ 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; @@ -41,13 +38,14 @@ public class Itinerary { return false; } - public boolean makeReservation(Passenger passenger){ + public FlightReservation makeReservation(FlightSeat flightSeat, Passenger passenger){ + HashMap seatHashMap= new HashMap(); + seatHashMap.put(passenger, flightSeat); validatePassengerDetails(passenger); - List flightReservations = getReservations(); FlightReservation flight = new FlightReservation(); - flightReservationRepository.save(flight); - - return false; + flight.setSeatMap(seatHashMap); + flight.setStatus(ReservationStatus.Requested); + return flight; } public Airport getFinal_airport() { 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 3b9f9da..c430585 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 @@ -9,9 +9,11 @@ import org.junit.jupiter.params.provider.MethodSource; import org.mockito.InjectMocks; import org.mockito.Mock; -import static org.junit.jupiter.api.Assertions.assertThrows; +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; @@ -19,22 +21,22 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.junit.jupiter.api.Assertions.assertEquals; - @ExtendWith(MockitoExtension.class) 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() { @@ -62,7 +64,7 @@ public class ItineraryTest { public void shouldThrowRuntimeExceptionWhenNameIsNull() { String msg = null; try { - item1.makeReservation(new Passenger("", "Ab", new Date())); + item1.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("", "Ab", new Date())); } catch (Exception e) { msg = e.getMessage(); } @@ -75,7 +77,7 @@ public class ItineraryTest { public void shouldThrowRuntimeExceptionWhenPhoneNumberIsNull(String passportNumber) { String msg = null; try { - item1.makeReservation(new Passenger("John", passportNumber, new Date())); + item1.makeReservation(new FlightSeat(1000.0, "5"), new Passenger("John", passportNumber, new Date())); } catch (Exception e) { msg = e.getMessage(); } @@ -90,7 +92,14 @@ public class ItineraryTest { @DisplayName("Should Not Make Reservation when Passenger Birthdate is null") public void shouldThrowRuntimeExceptionWhenDateIsNull() { assertThrows(RuntimeException.class, () -> { - item1.makeReservation(new Passenger("John", "AB127389", null)); + 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 From 5767a0df24af8a29cbb387f63976040150a645fe Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 21:44:58 +0100 Subject: [PATCH 7/8] refactor make payment --- .../fulda/de/ci/exam/project/Itinerary.java | 23 +++++++++---------- .../de/ci/exam/project/ItineraryTest.java | 15 ++++++++---- 2 files changed, 22 insertions(+), 16 deletions(-) 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 2719856..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 @@ -21,20 +21,19 @@ public class Itinerary { 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; } 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 c430585..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 @@ -47,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"); @@ -59,6 +59,13 @@ public class ItineraryTest { 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() { From 92b8141bd63863c2294269dc0689d2b087480e40 Mon Sep 17 00:00:00 2001 From: Sona Markosyan Date: Thu, 17 Feb 2022 22:09:49 +0100 Subject: [PATCH 8/8] search all flights by criteria --- .../hs/fulda/de/ci/exam/project/Airport.java | 4 ++ .../hs/fulda/de/ci/exam/project/Flight.java | 4 ++ .../de/ci/exam/project/FlightRepository.java | 20 ++++++++++ .../hs/fulda/de/ci/exam/project/Person.java | 8 ++++ .../fulda/de/ci/exam/project/AdminTest.java | 7 ---- .../fulda/de/ci/exam/project/PersonTest.java | 38 +++++++++++++++++++ 6 files changed, 74 insertions(+), 7 deletions(-) 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/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/Person.java b/src/main/java/hs/fulda/de/ci/exam/project/Person.java index 31d72dd..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 @@ -79,6 +79,14 @@ public class Person { 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/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()); + } }