From 7f8955472c5d0c419eec533fade102e21a10b3c1 Mon Sep 17 00:00:00 2001 From: Pastor Date: Mon, 7 Oct 2024 00:01:22 +0300 Subject: [PATCH] Clever lector --- .../practice/vol1/agent/model/Institute.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/vol1/src/main/java/ru/mifi/practice/vol1/agent/model/Institute.java b/vol1/src/main/java/ru/mifi/practice/vol1/agent/model/Institute.java index d33d708..054e52e 100644 --- a/vol1/src/main/java/ru/mifi/practice/vol1/agent/model/Institute.java +++ b/vol1/src/main/java/ru/mifi/practice/vol1/agent/model/Institute.java @@ -36,6 +36,10 @@ public void tick() { environment.tick(); } + enum Messages implements Transport.Replay { + NOISE, WAT + } + private record Human(String name, Class klass) implements Agent.Factory { public Agent create(Transport transport) { try { @@ -58,7 +62,23 @@ public Optional call(Transport.Message message) { @Override public void onEvent(Event event) { - throw new UnsupportedOperationException(); + if (event instanceof Event.EventMessage eventMessage + && eventMessage.message() instanceof Messages message + && eventMessage.source() != this) { + switch (message) { + case NOISE: { + transport.send(Messages.WAT); + break; + } + case WAT: { + //Skip + break; + } + default: { + break; + } + } + } } @Override @@ -71,12 +91,31 @@ private record Student(String id, Transport transport) implements Agent { @Override public Optional call(Transport.Message message) { + if (message instanceof Messages msg) { + return Optional.empty(); + } return Optional.empty(); } @Override public void onEvent(Event event) { - throw new UnsupportedOperationException(); + if (event instanceof Event.EventMessage eventMessage + && eventMessage.message() instanceof Messages message + && eventMessage.source() != this) { + switch (message) { + case NOISE: { + //Skip + break; + } + case WAT: { + //TODO: + break; + } + default: { + break; + } + } + } } @Override