Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- abstract class TelemetryEvent {
- private int id, timestamp;
- public TelemetryEvent(int id, int timestamp) {
- this.id = id;
- this.timestamp = timestamp;
- }
- protected String getCommon() {
- return "id: " + id + ", timestamp: " + timestamp;
- }
- public abstract boolean shouldAlert();
- public abstract String getAlertMessage();
- public abstract String getSummary();
- }
- class LocationEvent extends TelemetryEvent {
- private final float lat, lon;
- public LocationEvent(int id, int timestamp, float lat, float lon) {
- super(id, timestamp);
- this.lat = lat;
- this.lon = lon;
- }
- @Override
- public boolean shouldAlert() {
- return lat < -90 || lat > 90 || lon < -180 || lon > 180;
- }
- @Override
- public String getAlertMessage() {
- return " !!CORRUPTED COORDINATES!! ";
- }
- @Override
- public String getSummary() {
- return "LocationEvent: " + getCommon() + ", lat: " + lat + ", lon: " + lon;
- }
- }
- class SpeedEvent extends TelemetryEvent {
- private final float speed;
- public SpeedEvent(int id, int timestamp, float speed) {
- super(id, timestamp);
- this.speed = speed;
- }
- @Override
- public boolean shouldAlert() {
- return speed > 110;
- }
- @Override
- public String getAlertMessage() {
- return " !!SPEED TOO BIG!! ";
- }
- @Override
- public String getSummary() {
- return "SpeedEvent: " + getCommon() + ", speed: " + speed;
- }
- }
- interface NotifChannel {
- void notify(TelemetryEvent event);
- }
- class SMSNotifChannel implements NotifChannel {
- private final String nr;
- public SMSNotifChannel(String nr) {
- this.nr = nr;
- }
- @Override
- public void notify(TelemetryEvent event) {
- IO.println("sending sms to " + nr + ".... " + event.getAlertMessage());
- }
- }
- class EmailNotifChannel implements NotifChannel {
- private final String email;
- public EmailNotifChannel(String email) {
- this.email = email;
- }
- @Override
- public void notify(TelemetryEvent event) {
- IO.println("sending email to " + email + "... " + event.getAlertMessage());
- }
- }
- class EventProcessor {
- private final TelemetryEvent[] events;
- private final List<NotifChannel> channels;
- public EventProcessor(final TelemetryEvent[] events) {
- this.events = events;
- this.channels = new ArrayList<>();
- }
- public void addChannel(final NotifChannel channel) {
- this.channels.add(channel);
- }
- public void showEvents() {
- for (final var event : events) {
- if (event.shouldAlert()) {
- for (final var channel : channels) {
- channel.notify(event);
- }
- }
- IO.println(event.getSummary());
- }
- }
- }
- public class Main {
- static void main(final String[] args) {
- final var e1 = new SpeedEvent(1, 1231, 33);
- final var e2 = new LocationEvent(2, 32, 2, 23);
- final var e3 = new SpeedEvent(3, 322, 212);
- final TelemetryEvent[] events = {e1, e2, e3};
- final var ep = new EventProcessor(events);
- final var smsChannel = new SMSNotifChannel("123456789");
- ep.addChannel(smsChannel);
- ep.showEvents();
- IO.println("----------------------------------------");
- ep.addChannel(emailNotifChannel);
- ep.showEvents();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment