Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify Event class and related classes #69

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ This section describes some noteworthy details on how certain features are imple
* Previous help feature simply opens a page with a link to the website, this is bad because:
* The flow is lengthy
* User may not be able to access website when operating without the internet

Therefore, we want to make this better by simplifying the flow. We do this by adding:
* Making the help command return things in the application console
* Letting users enter an extra argument to specify what command they need guiding on
Expand Down Expand Up @@ -456,7 +456,7 @@ Priorities: High (must have) - `* * *`, Medium (nice to have) - `* *`, Low (unli
* 1c. User inputs a note that does not exist.

* 1c1. KeepInTouch shows a message indicating that the note cannot be found.

Use case ends.

**Use case: UC08 - Add an event**
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import javafx.collections.ObservableList;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.event.Event;
import seedu.address.model.event.UniqueEventList;
import seedu.address.model.note.Note;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniquePersonList;
Expand Down Expand Up @@ -141,6 +143,16 @@ public ObservableList<Person> getPersonList() {
return persons.asUnmodifiableObservableList();
}

public ObservableList<Event> getEventList() {
UniqueEventList out = new UniqueEventList();
persons.forEach(
person -> person.getEvents().forEach(
event -> out.add(event)
)
);
return out.asUnmodifiableObservableList();
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/seedu/address/model/event/Event.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model.event;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

/**
* The class for holding an Event
*/
Expand All @@ -21,6 +23,7 @@ public class Event {
* @param information The information of the event
*/
public Event(EventName name, EventTime start, EventTime end, EventLocation location, EventInformation information) {
requireAllNonNull(name, start);
this.name = name;
this.start = start;
this.end = end;
Expand Down Expand Up @@ -103,4 +106,23 @@ public String getUiText() {
result += " )";
return result;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof Event)) {
return false;
}

Event otherPerson = (Event) other;
return name.equals(otherPerson.name)
&& start.equals(otherPerson.start)
&& end.equals(otherPerson.end)
&& location.equals(otherPerson.location)
&& information.equals(otherPerson.information);
}
}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/event/EventInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ public String toString() {
public static boolean isValidEventInformation(String str) {
return !str.isEmpty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EventInformation)) {
return false;
}

EventInformation otherInformation = (EventInformation) other;
return information.equals(otherInformation.information);
}
}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/event/EventLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ public String toString() {
public static boolean isValidEventLocation(String str) {
return !str.isEmpty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EventLocation)) {
return false;
}

EventLocation otherLocation = (EventLocation) other;
return location.equals(otherLocation.location);
}
}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/event/EventName.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,19 @@ public String toString() {
public static boolean isValidEventName(String str) {
return !str.isEmpty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EventName)) {
return false;
}

EventName otherName = (EventName) other;
return name.equals(otherName.name);
}
}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/event/EventTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,19 @@ public static EventTime fromString(String timeStr) throws DateTimeParseException
public String toString() {
return this.time != null ? DateTimeUtil.toFormattedString(this.time) : "";
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EventTime)) {
return false;
}

EventTime otherName = (EventTime) other;
return time.equals(otherName.time);
}
}
149 changes: 149 additions & 0 deletions src/main/java/seedu/address/model/event/UniqueEventList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package seedu.address.model.event;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Iterator;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.model.event.exceptions.DuplicateEventException;
import seedu.address.model.event.exceptions.EventNotFoundException;

/**
* A list of persons that enforces uniqueness between its elements and does not allow nulls.
* A person is considered unique by comparing using {@code Person#isSamePerson(Person)}. As such, adding and updating of
* persons uses Person#isSamePerson(Person) for equality so as to ensure that the person being added or updated is
* unique in terms of identity in the UniquePersonList. However, the removal of a person uses Person#equals(Object) so
* as to ensure that the person with exactly the same fields will be removed.
*
* Supports a minimal set of list operations.
*
* @see Person#isSamePerson(Person)
*/
public class UniqueEventList implements Iterable<Event> {

private final ObservableList<Event> internalList = FXCollections.observableArrayList();
private final ObservableList<Event> internalUnmodifiableList =
FXCollections.unmodifiableObservableList(internalList);

/**
* Returns true if the list contains an equivalent person as the given argument.
*/
public boolean contains(Event toCheck) {
requireNonNull(toCheck);
return internalList.stream().anyMatch(toCheck::equals);
}

/**
* Adds an event to the list.
*/
public void add(Event toAdd) {
requireNonNull(toAdd);
if (this.contains(toAdd)) {
throw new DuplicateEventException();
}
internalList.add(toAdd);
}

/**
* Replaces the person {@code target} in the list with {@code editedPerson}.
* {@code target} must exist in the list.
* The person identity of {@code editedPerson} must not be the same as another existing person in the list.
*/
public void setPerson(Event target, Event editedEvent) {
requireAllNonNull(target, editedEvent);

int index = internalList.indexOf(target);
if (index == -1) {
throw new EventNotFoundException();
}

if (!target.equals(editedEvent) && contains(editedEvent)) {
throw new DuplicateEventException();
}

internalList.set(index, editedEvent);
}

/**
* Removes the equivalent person from the list.
* The person must exist in the list.
*/
public void remove(Event toRemove) {
requireNonNull(toRemove);
if (!internalList.remove(toRemove)) {
throw new EventNotFoundException();
}
}

public void setPersons(UniqueEventList replacement) {
requireNonNull(replacement);
internalList.setAll(replacement.internalList);
}

/**
* Replaces the contents of this list with {@code persons}.
* {@code persons} must not contain duplicate persons.
*/
public void setPersons(List<Event> events) {
requireAllNonNull(events);
if (!eventsAreUnique(events)) {
throw new DuplicateEventException();
}

internalList.setAll(events);
}

/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
public ObservableList<Event> asUnmodifiableObservableList() {
return internalUnmodifiableList;
}

@Override
public Iterator<Event> iterator() {
return internalList.iterator();
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof UniqueEventList)) {
return false;
}

UniqueEventList otherUniquePersonList = (UniqueEventList) other;
return internalList.equals(otherUniquePersonList.internalList);
}

@Override
public int hashCode() {
return internalList.hashCode();
}

@Override
public String toString() {
return internalList.toString();
}

/**
* Returns true if {@code persons} contains only unique persons.
*/
private boolean eventsAreUnique(List<Event> events) {
for (int i = 0; i < events.size() - 1; i++) {
for (int j = i + 1; j < events.size(); j++) {
if (events.get(i).equals(events.get(j))) {
return false;
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package seedu.address.model.event.exceptions;

/**
* Signals that the operation will result in duplicate Persons (Persons are considered duplicates if they have the same
* identity).
*/
public class DuplicateEventException extends RuntimeException {
public DuplicateEventException() {
super("Operation would result in duplicate events");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package seedu.address.model.event.exceptions;

/**
* Signals that the operation is unable to find the specified person.
*/
public class EventNotFoundException extends RuntimeException {}
Loading