added lab9 ex1
This commit is contained in:
parent
d46443f6d5
commit
42c63045b0
9
src/lab9_responsibility_chain/ex1/Email.java
Normal file
9
src/lab9_responsibility_chain/ex1/Email.java
Normal 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 + "'>";
|
||||
}
|
||||
}
|
8
src/lab9_responsibility_chain/ex1/EmailType.java
Normal file
8
src/lab9_responsibility_chain/ex1/EmailType.java
Normal file
@ -0,0 +1,8 @@
|
||||
package lab9_responsibility_chain.ex1;
|
||||
|
||||
public enum EmailType {
|
||||
SPAM,
|
||||
FAN,
|
||||
COMPLAINT,
|
||||
NEW_LOC
|
||||
}
|
11
src/lab9_responsibility_chain/ex1/Handler.java
Normal file
11
src/lab9_responsibility_chain/ex1/Handler.java
Normal 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);
|
||||
}
|
26
src/lab9_responsibility_chain/ex1/Main.java
Normal file
26
src/lab9_responsibility_chain/ex1/Main.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
16
src/lab9_responsibility_chain/ex1/handlers/FanHandler.java
Normal file
16
src/lab9_responsibility_chain/ex1/handlers/FanHandler.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
16
src/lab9_responsibility_chain/ex1/handlers/SpamHandler.java
Normal file
16
src/lab9_responsibility_chain/ex1/handlers/SpamHandler.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user