diff --git a/src/lab9_responsibility_chain/ex1/Email.java b/src/lab9_responsibility_chain/ex1/Email.java new file mode 100644 index 0000000..35eaaff --- /dev/null +++ b/src/lab9_responsibility_chain/ex1/Email.java @@ -0,0 +1,9 @@ +package lab9_responsibility_chain.ex1; + +public record Email(String subject, EmailType type) { + + @Override + public String toString() { + return "<" + type + " Email: '" + subject + "'>"; + } +} diff --git a/src/lab9_responsibility_chain/ex1/EmailType.java b/src/lab9_responsibility_chain/ex1/EmailType.java new file mode 100644 index 0000000..657d57f --- /dev/null +++ b/src/lab9_responsibility_chain/ex1/EmailType.java @@ -0,0 +1,8 @@ +package lab9_responsibility_chain.ex1; + +public enum EmailType { + SPAM, + FAN, + COMPLAINT, + NEW_LOC +} diff --git a/src/lab9_responsibility_chain/ex1/Handler.java b/src/lab9_responsibility_chain/ex1/Handler.java new file mode 100644 index 0000000..b9c492f --- /dev/null +++ b/src/lab9_responsibility_chain/ex1/Handler.java @@ -0,0 +1,11 @@ +package lab9_responsibility_chain.ex1; + +public abstract class Handler { + protected Handler successor; + + public void setSuccessor(Handler successor) { + this.successor = successor; + } + + public abstract void handleEmail(Email email); +} diff --git a/src/lab9_responsibility_chain/ex1/Main.java b/src/lab9_responsibility_chain/ex1/Main.java new file mode 100644 index 0000000..ec1c902 --- /dev/null +++ b/src/lab9_responsibility_chain/ex1/Main.java @@ -0,0 +1,26 @@ +package lab9_responsibility_chain.ex1; + +import lab9_responsibility_chain.ex1.handlers.*; + +public class Main { + public static void main(String[] args) { + Handler spamHandler = new SpamHandler(); + Handler fanHandler = new FanHandler(); + Handler complaintHandler = new ComplaintHandler(); + Handler newLocHandler = new NewLocHandler(); + + spamHandler.setSuccessor(fanHandler); + fanHandler.setSuccessor(complaintHandler); + complaintHandler.setSuccessor(newLocHandler); + + Email email1 = new Email("You WON the LoTtErY !!!!!", EmailType.SPAM); + Email email2 = new Email("Your company is awesome !", EmailType.FAN); + Email email3 = new Email("My children are addicted :(", EmailType.COMPLAINT); + Email email4 = new Email("Please come to Sion :)", EmailType.NEW_LOC); + + spamHandler.handleEmail(email1); + spamHandler.handleEmail(email2); + spamHandler.handleEmail(email3); + spamHandler.handleEmail(email4); + } +} diff --git a/src/lab9_responsibility_chain/ex1/handlers/ComplaintHandler.java b/src/lab9_responsibility_chain/ex1/handlers/ComplaintHandler.java new file mode 100644 index 0000000..4106acb --- /dev/null +++ b/src/lab9_responsibility_chain/ex1/handlers/ComplaintHandler.java @@ -0,0 +1,16 @@ +package lab9_responsibility_chain.ex1.handlers; + +import lab9_responsibility_chain.ex1.Email; +import lab9_responsibility_chain.ex1.EmailType; +import lab9_responsibility_chain.ex1.Handler; + +public class ComplaintHandler extends Handler { + @Override + public void handleEmail(Email email) { + if (email.type() == EmailType.COMPLAINT) { + System.out.println("Forwarding complaint to legal department (" + email + ")"); + } else if (successor != null) { + successor.handleEmail(email); + } + } +} diff --git a/src/lab9_responsibility_chain/ex1/handlers/FanHandler.java b/src/lab9_responsibility_chain/ex1/handlers/FanHandler.java new file mode 100644 index 0000000..33dbd56 --- /dev/null +++ b/src/lab9_responsibility_chain/ex1/handlers/FanHandler.java @@ -0,0 +1,16 @@ +package lab9_responsibility_chain.ex1.handlers; + +import lab9_responsibility_chain.ex1.Email; +import lab9_responsibility_chain.ex1.EmailType; +import lab9_responsibility_chain.ex1.Handler; + +public class FanHandler extends Handler { + @Override + public void handleEmail(Email email) { + if (email.type() == EmailType.FAN) { + System.out.println("Forwarding fan email to CEO (" + email + ")"); + } else if (successor != null) { + successor.handleEmail(email); + } + } +} diff --git a/src/lab9_responsibility_chain/ex1/handlers/NewLocHandler.java b/src/lab9_responsibility_chain/ex1/handlers/NewLocHandler.java new file mode 100644 index 0000000..5cb0a32 --- /dev/null +++ b/src/lab9_responsibility_chain/ex1/handlers/NewLocHandler.java @@ -0,0 +1,16 @@ +package lab9_responsibility_chain.ex1.handlers; + +import lab9_responsibility_chain.ex1.Email; +import lab9_responsibility_chain.ex1.EmailType; +import lab9_responsibility_chain.ex1.Handler; + +public class NewLocHandler extends Handler { + @Override + public void handleEmail(Email email) { + if (email.type() == EmailType.NEW_LOC) { + System.out.println("Forwarding request to business development (" + email + ")"); + } else if (successor != null) { + successor.handleEmail(email); + } + } +} diff --git a/src/lab9_responsibility_chain/ex1/handlers/SpamHandler.java b/src/lab9_responsibility_chain/ex1/handlers/SpamHandler.java new file mode 100644 index 0000000..a786079 --- /dev/null +++ b/src/lab9_responsibility_chain/ex1/handlers/SpamHandler.java @@ -0,0 +1,16 @@ +package lab9_responsibility_chain.ex1.handlers; + +import lab9_responsibility_chain.ex1.Email; +import lab9_responsibility_chain.ex1.EmailType; +import lab9_responsibility_chain.ex1.Handler; + +public class SpamHandler extends Handler { + @Override + public void handleEmail(Email email) { + if (email.type() == EmailType.SPAM) { + System.out.println("Deleting spam email (" + email + ")"); + } else if (successor != null) { + successor.handleEmail(email); + } + } +}