Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#77 from claraadora/autosort
Browse files Browse the repository at this point in the history
Implement Autosort for Meeting
  • Loading branch information
claraadora authored Oct 18, 2020
2 parents 0e05660 + c423567 commit 41da9ff
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/model/meeting/MeetingBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public MeetingBook() {}
public MeetingBook(ReadOnlyMeetingBook toBeCopied) {
this();
resetData(toBeCopied);
sortMeetings();
}

/**
* Sorts the content of the meeting list by From in ascending order.
*/
public void sortMeetings() {
this.meetings.sortList();
}

/**
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/seedu/address/model/meeting/meeting/Meeting.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import java.time.LocalDateTime;
import java.util.Objects;

import seedu.address.model.util.Contacts;
Expand Down Expand Up @@ -47,6 +48,10 @@ public From getFrom() {
return from;
}

public LocalDateTime getFromLocalDateTime() {
return from.getLocalDateTime().get();
}

public To getTo() {
return to;
}
Expand Down Expand Up @@ -106,18 +111,18 @@ public int hashCode() {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append(getTitle())
.append(" Title: ")
.append(getDescription())
builder.append(" Title: ")
.append(getTitle())
.append(" Description: ")
.append(getFrom())
.append(getDescription())
.append(" From: ")
.append(getTo())
.append(getFrom())
.append(" To: ")
.append(getContacts())
.append(getTo())
.append(" Contacts: ")
.append(getLocation())
.append(" Location: ");
.append(getContacts())
.append(" Location: ")
.append(getLocation());
return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

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

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.model.meeting.meeting.exceptions.DuplicateMeetingException;
import seedu.address.model.meeting.meeting.exceptions.MeetingNotFoundException;
import seedu.address.model.meeting.util.MeetingComparator;

public class UniqueMeetingList implements Iterable<Meeting> {

private final ObservableList<Meeting> internalList = FXCollections.observableArrayList();
private final ObservableList<Meeting> internalUnmodifiableList =
FXCollections.unmodifiableObservableList(internalList);
private final MeetingComparator meetingComparator = new MeetingComparator();

/**
* Returns true if the list contains an equivalent meeting as the given argument.
Expand All @@ -35,6 +38,7 @@ public void add(Meeting toAdd) {
throw new DuplicateMeetingException();
}
internalList.add(toAdd);
sortList();
}

/**
Expand All @@ -56,6 +60,7 @@ public void setMeeting(Meeting target, Meeting editedMeeting) {
}

internalList.set(index, editedMeeting);
sortList();
}

/**
Expand All @@ -72,6 +77,7 @@ public void remove(Meeting toRemove) {
public void setMeetings(UniqueMeetingList replacement) {
requireNonNull(replacement);
internalList.setAll(replacement.internalList);
sortList();
}

/**
Expand All @@ -85,6 +91,7 @@ public void setMeetings(List<Meeting> meetings) {
}

internalList.setAll(meetings);
sortList();
}

/**
Expand All @@ -94,8 +101,17 @@ public ObservableList<Meeting> asUnmodifiableObservableList() {
return internalUnmodifiableList;
}

/**
* Sort the list by From's value
*
*/
public void sortList() {
Collections.sort(internalList, meetingComparator);
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seedu.address.model.meeting.util;

import java.time.LocalDateTime;
import java.util.Comparator;

import seedu.address.model.meeting.meeting.Meeting;

/**
* Represents a Comparator for Meeting which sorts by From.
*/
public class MeetingComparator implements Comparator<Meeting> {
/**
* Compares Meetings by From in ascending order.
*/
public int compare(Meeting a, Meeting b) {
LocalDateTime aFrom = a.getFromLocalDateTime();
LocalDateTime bFrom = b.getFromLocalDateTime();

return aFrom.compareTo(bFrom);
}
}
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/util/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ public DateTime(Optional<String> date) {

}

/**
* Returns the LocalDateTime value object.
*
* @return the value of DateTime.
*/
public Optional<LocalDateTime> getLocalDateTime() {
return value;
}

/**
* Returns true if a given string is a valid DateTime.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/view/MeetingListCard.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<Label fx:id="title" text="\$title" styleClass="cell_big_label" />
</HBox>
<Label fx:id="description" styleClass="cell_small_label" text="\$description" />
<Label fx:id="to" styleClass="cell_small_label" text="\$to" />
<Label fx:id="from" styleClass="cell_small_label" text="\$from" />
<Label fx:id="to" styleClass="cell_small_label" text="\$to" />
<Label fx:id="contacts" styleClass="cell_small_label" text="\$contacts" />
<Label fx:id="loc" styleClass="cell_small_label" text="\$loc" />
</VBox>
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/address/testutil/TypicalMeetings.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ public static MeetingBook getTypicalMeetingBook() {
}

public static List<Meeting> getTypicalMeeting() {
return new ArrayList<>(Arrays.asList(MEETING_A, MEETING_B));
return new ArrayList<>(Arrays.asList(MEETING_B, MEETING_A));
}
}

0 comments on commit 41da9ff

Please sign in to comment.