diff --git a/src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java b/src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java new file mode 100644 index 0000000..0735a28 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/EmailNotification.java @@ -0,0 +1,36 @@ +package hs.fulda.de.ci.exam.project; + +import java.util.regex.Pattern; + +public class EmailNotification extends Notification { + String email; + String content; + + public EmailNotification(String email, String content) { + super(); + this.email = email; + this.content = content; + } + + @Override + public boolean sendNotification() { + if (isValidEmail(email)) { + System.out.println("Email is sent to " + email); + return true; + } + System.out.println("Invalid Email Address"); + return false; + } + + private boolean isValidEmail(String email) { + String regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; + return patternMatches(email, regexPattern); + } + + public static boolean patternMatches(String emailAddress, String regexPattern) { + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } +} 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 f288009..55c0bb8 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 @@ -18,7 +18,7 @@ public class FlightReservation { this.status = status; } - public FlightReservation(){ + public FlightReservation() { } @@ -60,4 +60,17 @@ public class FlightReservation { credit_card_payment.status = Completed; status = Confirmed; } + + public boolean notifyUser(String type, String to, String content) { + Notification notification = null; + switch (type) { + case "email": + notification = new EmailNotification(to, content); + break; + case "sms": + notification = new SmsNotification(to, content); + break; + } + return notification.sendNotification(); + } } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Notification.java b/src/main/java/hs/fulda/de/ci/exam/project/Notification.java new file mode 100644 index 0000000..4821060 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/Notification.java @@ -0,0 +1,5 @@ +package hs.fulda.de.ci.exam.project; + +public abstract class Notification { + public abstract boolean sendNotification(); +} diff --git a/src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java b/src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java new file mode 100644 index 0000000..73f5d50 --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/SmsNotification.java @@ -0,0 +1,37 @@ +package hs.fulda.de.ci.exam.project; + +import java.util.regex.Pattern; + +public class SmsNotification extends Notification { + String to; + String content; + + public SmsNotification(String to, String content) { + super(); + this.to = to; + this.content = content; + } + + @Override + public boolean sendNotification() { + if (isValidEmail(to)) { + System.out.println("SMS is sent to " + to); + return true; + } + System.out.println("Invalid Number"); + return false; + } + + private boolean isValidEmail(String to) { + String regexPattern = "(\\(?([\\d \\-\\)\\–\\+\\/\\(]+){6,}\\)?([ .\\-–\\/]?)([\\d]+))"; + return patternMatches(to, regexPattern); + } + + public static boolean patternMatches(String emailAddress, String regexPattern) { + return Pattern.compile(regexPattern) + .matcher(emailAddress) + .matches(); + } + + +} diff --git a/src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java b/src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java new file mode 100644 index 0000000..b1600f2 --- /dev/null +++ b/src/test/java/hs/fulda/de/ci/exam/project/NotificationTest.java @@ -0,0 +1,60 @@ +package hs.fulda.de.ci.exam.project; + +import org.junit.Test; + +import static hs.fulda.de.ci.exam.project.ReservationStatus.Confirmed; +import static org.assertj.core.api.Assertions.assertThat; + +public class NotificationTest { + + EmailNotification email = new EmailNotification("test@gmail.com", "HelloWorld"); + EmailNotification emailInvalid = new EmailNotification("testgmail.com", "HelloWorld"); + FlightReservation reservation = new FlightReservation(); + SmsNotification sms = new SmsNotification("01788370107", "Reservation confirmed"); + SmsNotification smsInvalid = new SmsNotification("123", "Some Wrong Number"); + + + @Test + public void emailValidiationShouldReturnTrueForValidEmail() { + boolean result = email.sendNotification(); + boolean expected = true; + assertThat(expected).isEqualTo(result); + } + + @Test + public void emailValidiationShouldReturnFalseForInValidEmail() { + boolean result = emailInvalid.sendNotification(); + boolean expected = false; + assertThat(expected).isEqualTo(result); + } + + @Test + public void emailNotificationShouldBeSentWhenFlighReservationCompleted() { + reservation.setStatus(Confirmed); + boolean result = reservation.notifyUser("email", "test@gmail.com", "Reservation is Confirmed!"); + boolean expected = true; + assertThat(expected).isEqualTo(result); + } + + @Test + public void smsNotificationShouldBeSentWhenSmsTypeIsChosen() { + reservation.setStatus(Confirmed); + boolean result = reservation.notifyUser("sms", "01788370107", "Reservation is successfull"); + boolean expected = true; + assertThat(expected).isEqualTo(result); + } + + @Test + public void smsIsSentWhenNumberIsValid() { + boolean result = sms.sendNotification(); + boolean expected = true; + assertThat(expected).isEqualTo(result); + } + + @Test + public void smsShouldNotBeSentWhenNumberIsInvalid() { + boolean result = smsInvalid.sendNotification(); + boolean expected = false; + assertThat(expected).isEqualTo(result); + } +}