Skip to content

Commit 41da9ff

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#77 from claraadora/autosort
Implement Autosort for Meeting
2 parents 0e05660 + c423567 commit 41da9ff

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

src/main/java/seedu/address/model/meeting/MeetingBook.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public MeetingBook() {}
2727
public MeetingBook(ReadOnlyMeetingBook toBeCopied) {
2828
this();
2929
resetData(toBeCopied);
30+
sortMeetings();
31+
}
32+
33+
/**
34+
* Sorts the content of the meeting list by From in ascending order.
35+
*/
36+
public void sortMeetings() {
37+
this.meetings.sortList();
3038
}
3139

3240
/**

src/main/java/seedu/address/model/meeting/meeting/Meeting.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
import java.time.LocalDateTime;
56
import java.util.Objects;
67

78
import seedu.address.model.util.Contacts;
@@ -47,6 +48,10 @@ public From getFrom() {
4748
return from;
4849
}
4950

51+
public LocalDateTime getFromLocalDateTime() {
52+
return from.getLocalDateTime().get();
53+
}
54+
5055
public To getTo() {
5156
return to;
5257
}
@@ -106,18 +111,18 @@ public int hashCode() {
106111
@Override
107112
public String toString() {
108113
final StringBuilder builder = new StringBuilder();
109-
builder.append(getTitle())
110-
.append(" Title: ")
111-
.append(getDescription())
114+
builder.append(" Title: ")
115+
.append(getTitle())
112116
.append(" Description: ")
113-
.append(getFrom())
117+
.append(getDescription())
114118
.append(" From: ")
115-
.append(getTo())
119+
.append(getFrom())
116120
.append(" To: ")
117-
.append(getContacts())
121+
.append(getTo())
118122
.append(" Contacts: ")
119-
.append(getLocation())
120-
.append(" Location: ");
123+
.append(getContacts())
124+
.append(" Location: ")
125+
.append(getLocation());
121126
return builder.toString();
122127
}
123128

src/main/java/seedu/address/model/meeting/meeting/UniqueMeetingList.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
import static java.util.Objects.requireNonNull;
44
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
55

6+
import java.util.Collections;
67
import java.util.Iterator;
78
import java.util.List;
89

910
import javafx.collections.FXCollections;
1011
import javafx.collections.ObservableList;
1112
import seedu.address.model.meeting.meeting.exceptions.DuplicateMeetingException;
1213
import seedu.address.model.meeting.meeting.exceptions.MeetingNotFoundException;
14+
import seedu.address.model.meeting.util.MeetingComparator;
1315

1416
public class UniqueMeetingList implements Iterable<Meeting> {
1517

1618
private final ObservableList<Meeting> internalList = FXCollections.observableArrayList();
1719
private final ObservableList<Meeting> internalUnmodifiableList =
1820
FXCollections.unmodifiableObservableList(internalList);
21+
private final MeetingComparator meetingComparator = new MeetingComparator();
1922

2023
/**
2124
* Returns true if the list contains an equivalent meeting as the given argument.
@@ -35,6 +38,7 @@ public void add(Meeting toAdd) {
3538
throw new DuplicateMeetingException();
3639
}
3740
internalList.add(toAdd);
41+
sortList();
3842
}
3943

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

5862
internalList.set(index, editedMeeting);
63+
sortList();
5964
}
6065

6166
/**
@@ -72,6 +77,7 @@ public void remove(Meeting toRemove) {
7277
public void setMeetings(UniqueMeetingList replacement) {
7378
requireNonNull(replacement);
7479
internalList.setAll(replacement.internalList);
80+
sortList();
7581
}
7682

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

8793
internalList.setAll(meetings);
94+
sortList();
8895
}
8996

9097
/**
@@ -94,8 +101,17 @@ public ObservableList<Meeting> asUnmodifiableObservableList() {
94101
return internalUnmodifiableList;
95102
}
96103

104+
/**
105+
* Sort the list by From's value
106+
*
107+
*/
108+
public void sortList() {
109+
Collections.sort(internalList, meetingComparator);
110+
}
111+
97112
@Override
98113
public Iterator<Meeting> iterator() {
114+
sortList();
99115
return internalList.iterator();
100116
}
101117

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package seedu.address.model.meeting.util;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.Comparator;
5+
6+
import seedu.address.model.meeting.meeting.Meeting;
7+
8+
/**
9+
* Represents a Comparator for Meeting which sorts by From.
10+
*/
11+
public class MeetingComparator implements Comparator<Meeting> {
12+
/**
13+
* Compares Meetings by From in ascending order.
14+
*/
15+
public int compare(Meeting a, Meeting b) {
16+
LocalDateTime aFrom = a.getFromLocalDateTime();
17+
LocalDateTime bFrom = b.getFromLocalDateTime();
18+
19+
return aFrom.compareTo(bFrom);
20+
}
21+
}

src/main/java/seedu/address/model/util/DateTime.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ public DateTime(Optional<String> date) {
8787

8888
}
8989

90+
/**
91+
* Returns the LocalDateTime value object.
92+
*
93+
* @return the value of DateTime.
94+
*/
95+
public Optional<LocalDateTime> getLocalDateTime() {
96+
return value;
97+
}
98+
9099
/**
91100
* Returns true if a given string is a valid DateTime.
92101
*

src/main/resources/view/MeetingListCard.fxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<Label fx:id="title" text="\$title" styleClass="cell_big_label" />
2828
</HBox>
2929
<Label fx:id="description" styleClass="cell_small_label" text="\$description" />
30-
<Label fx:id="to" styleClass="cell_small_label" text="\$to" />
3130
<Label fx:id="from" styleClass="cell_small_label" text="\$from" />
31+
<Label fx:id="to" styleClass="cell_small_label" text="\$to" />
3232
<Label fx:id="contacts" styleClass="cell_small_label" text="\$contacts" />
3333
<Label fx:id="loc" styleClass="cell_small_label" text="\$loc" />
3434
</VBox>

src/test/java/seedu/address/testutil/TypicalMeetings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ public static MeetingBook getTypicalMeetingBook() {
8383
}
8484

8585
public static List<Meeting> getTypicalMeeting() {
86-
return new ArrayList<>(Arrays.asList(MEETING_A, MEETING_B));
86+
return new ArrayList<>(Arrays.asList(MEETING_B, MEETING_A));
8787
}
8888
}

0 commit comments

Comments
 (0)