From ca46721f155da0e0fd4d772cdbe4d590cc2f6913 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Tue, 28 Nov 2023 11:33:45 -0800 Subject: Import Event / EventLog Signed-off-by: Yuuta Liang --- src/main/model/Event.java | 66 ++++++++++++++++++++++++++++++++++++++++++++ src/main/model/EventLog.java | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/main/model/Event.java create mode 100644 src/main/model/EventLog.java (limited to 'src/main') diff --git a/src/main/model/Event.java b/src/main/model/Event.java new file mode 100644 index 0000000..c6a4993 --- /dev/null +++ b/src/main/model/Event.java @@ -0,0 +1,66 @@ +package model; + +import java.util.Calendar; +import java.util.Date; + + +/** + * Represents an alarm system event. + */ +public class Event { + private static final int HASH_CONSTANT = 13; + private Date dateLogged; + private String description; + + /** + * Creates an event with the given description + * and the current date/time stamp. + * @param description a description of the event + */ + public Event(String description) { + dateLogged = Calendar.getInstance().getTime(); + this.description = description; + } + + /** + * Gets the date of this event (includes time). + * @return the date of the event + */ + public Date getDate() { + return dateLogged; + } + + /** + * Gets the description of this event. + * @return the description of the event + */ + public String getDescription() { + return description; + } + + @Override + public boolean equals(Object other) { + if (other == null) { + return false; + } + + if (other.getClass() != this.getClass()) { + return false; + } + + Event otherEvent = (Event) other; + + return (this.dateLogged.equals(otherEvent.dateLogged) + && this.description.equals(otherEvent.description)); + } + + @Override + public int hashCode() { + return (HASH_CONSTANT * dateLogged.hashCode() + description.hashCode()); + } + + @Override + public String toString() { + return dateLogged.toString() + "\n" + description; + } +} diff --git a/src/main/model/EventLog.java b/src/main/model/EventLog.java new file mode 100644 index 0000000..d0a29b5 --- /dev/null +++ b/src/main/model/EventLog.java @@ -0,0 +1,60 @@ +package model; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +/** + * Represents a log of alarm system events. + * We use the Singleton Design Pattern to ensure that there is only + * one EventLog in the system and that the system has global access + * to the single instance of the EventLog. + */ +public class EventLog implements Iterable { + /** the only EventLog in the system (Singleton Design Pattern) */ + private static EventLog theLog; + private Collection events; + + /** + * Prevent external construction. + * (Singleton Design Pattern). + */ + private EventLog() { + events = new ArrayList(); + } + + /** + * Gets instance of EventLog - creates it + * if it doesn't already exist. + * (Singleton Design Pattern) + * @return instance of EventLog + */ + public static EventLog getInstance() { + if (theLog == null) { + theLog = new EventLog(); + } + + return theLog; + } + + /** + * Adds an event to the event log. + * @param e the event to be added + */ + public void logEvent(Event e) { + events.add(e); + } + + /** + * Clears the event log and logs the event. + */ + public void clear() { + events.clear(); + logEvent(new Event("Event log cleared.")); + } + + @Override + public Iterator iterator() { + return events.iterator(); + } +} -- cgit v1.2.3