added lab9 ex1

This commit is contained in:
Louis Heredero 2024-11-04 09:16:59 +01:00
parent d46443f6d5
commit 42c63045b0
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
8 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package lab9_responsibility_chain.ex1;
public record Email(String subject, EmailType type) {
@Override
public String toString() {
return "<" + type + " Email: '" + subject + "'>";
}
}

View File

@ -0,0 +1,8 @@
package lab9_responsibility_chain.ex1;
public enum EmailType {
SPAM,
FAN,
COMPLAINT,
NEW_LOC
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}