Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #68 from lhy-hoyin/branch-SortEvent
Browse files Browse the repository at this point in the history
New feature: Sort Events
  • Loading branch information
lhy-hoyin authored Mar 24, 2023
2 parents a5c8e4e + c0fa930 commit b93bca9
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/main/java/ezschedule/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ezschedule.logic.commands;

import static java.util.Objects.requireNonNull;

import ezschedule.logic.commands.exceptions.CommandException;
import ezschedule.model.Model;

/**
* Sort all events in the scheduler in chronological order.
*/
public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_SUCCESS = "Events sorted in order.";


@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.sortEvents();
return new CommandResult(MESSAGE_SUCCESS);
}
}
4 changes: 4 additions & 0 deletions src/main/java/ezschedule/logic/parser/SchedulerParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ezschedule.logic.commands.FindCommand;
import ezschedule.logic.commands.HelpCommand;
import ezschedule.logic.commands.ListCommand;
import ezschedule.logic.commands.SortCommand;
import ezschedule.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -60,6 +61,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ListCommand.COMMAND_WORD:
return new ListCommand();

case SortCommand.COMMAND_WORD:
return new SortCommand();

case ExitCommand.COMMAND_WORD:
return new ExitCommand();

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ezschedule/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public interface Model {
*/
void setEvent(Event target, Event editedEvent);

/**
* Sorts all the events in the event list in chronological order.
*/
void sortEvents();

/**
* Returns an unmodifiable view of the filtered event list
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ezschedule/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public void setEvent(Event target, Event editedEvent) {
scheduler.setEvent(target, editedEvent);
}

@Override
public void sortEvents() {
scheduler.sortEvent();
}

//=========== Filtered Event List Accessors =============================================================

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/ezschedule/model/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public void setEvent(Event target, Event editedEvent) {
events.setEvent(target, editedEvent);
}

/**
* Sorts all events in the scheduler in chronological order.
*/
public void sortEvent() {
events.sortByChronologicalOrder();
}

/**
* Removes {@code key} from this {@code Scheduler}.
* {@code key} must exist in the scheduler
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/ezschedule/model/event/UniqueEventList.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public void setEvents(List<Event> events) {
internalList.setAll(events);
}

/**
* Sorts all events in chronological order.
*/
public void sortByChronologicalOrder() {
FXCollections.sort(internalList);
}

/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ezschedule/ui/UiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public UiManager(Logic logic) {
public void start(Stage primaryStage) {
logger.info("Starting UI...");

//Set the application icon.
// Set the application icon
primaryStage.getIcons().add(getImage(ICON_APPLICATION));

try {
mainWindow = new MainWindow(primaryStage, logic);
mainWindow.show(); //This should be called before creating other UI parts
mainWindow.show(); // This should be called before creating other UI parts
mainWindow.fillInnerParts();

} catch (Throwable e) {
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/ezschedule/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public void setEvent(Event target, Event editedEvent) {
throw new AssertionError("This method should not be called.");
}

@Override
public void sortEvents() {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Event> getFilteredEventList() {
throw new AssertionError("This method should not be called.");
Expand Down
124 changes: 124 additions & 0 deletions src/test/java/ezschedule/logic/commands/SortCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package ezschedule.logic.commands;

import static ezschedule.logic.commands.CommandTestUtil.assertCommandSuccess;
import static ezschedule.testutil.TypicalEvents.getTypicalScheduler;

import org.junit.jupiter.api.Test;

import ezschedule.model.Model;
import ezschedule.model.ModelManager;
import ezschedule.model.Scheduler;
import ezschedule.model.UserPrefs;
import ezschedule.model.event.Event;
import ezschedule.testutil.EventBuilder;
import ezschedule.testutil.SchedulerBuilder;
import ezschedule.testutil.TypicalEvents;

public class SortCommandTest {

private Model model = new ModelManager(getTypicalScheduler(), new UserPrefs());

@Test
public void execute_typicalEventsAbcd_success() {
String expectedMessage = SortCommand.MESSAGE_SUCCESS;

Scheduler scheduler = new SchedulerBuilder()
.withEvent(TypicalEvents.CARNIVAL)
.withEvent(TypicalEvents.ART)
.withEvent(TypicalEvents.DRAG)
.withEvent(TypicalEvents.BOAT)
.build();
Model givenModel = new ModelManager(scheduler, new UserPrefs());

assertCommandSuccess(new SortCommand(), givenModel, expectedMessage, model);
}

@Test
public void execute_typicalEventAEventB_success() {
String expectedMessage = SortCommand.MESSAGE_SUCCESS;

Model givenModel = new ModelManager(
new SchedulerBuilder()
.withEvent(TypicalEvents.EVENT_B)
.withEvent(TypicalEvents.EVENT_A)
.build(),
new UserPrefs());

Model expectedModel = new ModelManager(
new SchedulerBuilder()
.withEvent(TypicalEvents.EVENT_A)
.withEvent(TypicalEvents.EVENT_B)
.build(),
new UserPrefs());

assertCommandSuccess(new SortCommand(), givenModel, expectedMessage, expectedModel);
}

@Test
public void execute_customEventsSameDate_success() {
String expectedMessage = SortCommand.MESSAGE_SUCCESS;

// Events are build with default date, end time
Event e1 = new EventBuilder().withName("e1").withStartTime("12:34").build();
Event e2 = new EventBuilder().withName("e2").withStartTime("21:43").build();

Model givenModel = new ModelManager(
new SchedulerBuilder().withEvent(e2).withEvent(e1).build(),
new UserPrefs());

Model expectedModel = new ModelManager(
new SchedulerBuilder().withEvent(e1).withEvent(e2).build(),
new UserPrefs());

assertCommandSuccess(new SortCommand(), givenModel, expectedMessage, expectedModel);
}

@Test
public void execute_customEventsSameTime_success() {
String expectedMessage = SortCommand.MESSAGE_SUCCESS;

// Events are build with default date, end time
Event e1 = new EventBuilder().withName("e1").withDate("2023-05-01").build();
Event e2 = new EventBuilder().withName("e2").withDate("2023-05-02").build();

Model givenModel = new ModelManager(
new SchedulerBuilder().withEvent(e2).withEvent(e1).build(),
new UserPrefs());

Model expectedModel = new ModelManager(
new SchedulerBuilder().withEvent(e1).withEvent(e2).build(),
new UserPrefs());

assertCommandSuccess(new SortCommand(), givenModel, expectedMessage, expectedModel);
}

@Test
public void execute_customEvents_success() {
String expectedMessage = SortCommand.MESSAGE_SUCCESS;

// Events are build with default date, end time
// e1 have earlier date, later time
// e2 have later date, earlier time
// e1 is expected to come before e2
Event e1 = new EventBuilder()
.withName("e1")
.withDate("2023-05-01")
.withStartTime("21:43")
.build();
Event e2 = new EventBuilder()
.withName("e2")
.withDate("2023-05-02")
.withStartTime("12:34")
.build();

Model givenModel = new ModelManager(
new SchedulerBuilder().withEvent(e2).withEvent(e1).build(),
new UserPrefs());

Model expectedModel = new ModelManager(
new SchedulerBuilder().withEvent(e1).withEvent(e2).build(),
new UserPrefs());

assertCommandSuccess(new SortCommand(), givenModel, expectedMessage, expectedModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import ezschedule.logic.commands.FindCommand;
import ezschedule.logic.commands.HelpCommand;
import ezschedule.logic.commands.ListCommand;
import ezschedule.logic.commands.SortCommand;
import ezschedule.logic.parser.exceptions.ParseException;
import ezschedule.model.event.Event;
import ezschedule.model.event.EventContainsKeywordsPredicate;
Expand Down Expand Up @@ -53,6 +54,11 @@ public void parseCommand_delete() throws Exception {
assertEquals(new DeleteCommand(INDEX_FIRST_EVENT), command);
}

@Test
public void parseCommand_sort() throws Exception {
assertTrue(parser.parseCommand(SortCommand.COMMAND_WORD) instanceof SortCommand);
}

@Test
public void parseCommand_edit() throws Exception {
Event event = new EventBuilder().build();
Expand Down

0 comments on commit b93bca9

Please sign in to comment.