This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
forked from nus-cs2103-AY2223S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from lhy-hoyin/branch-SortEvent
New Feature: ShowNext Command and code for upcoming event (not fully implemented yet)
- Loading branch information
Showing
14 changed files
with
229 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/ezschedule/logic/commands/ShowNextCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ezschedule.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import ezschedule.commons.core.Messages; | ||
import ezschedule.logic.commands.exceptions.CommandException; | ||
import ezschedule.model.Model; | ||
import ezschedule.model.event.UpcomingEventPredicate; | ||
|
||
/** | ||
* List the next (or next few) upcoming events. | ||
* Completed events are not considered upcoming. | ||
*/ | ||
public class ShowNextCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "next"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Display upcoming events." | ||
+ "\nCan be used without any parameters." | ||
+ "\nParameter: COUNT (must be a positive integer)" | ||
+ "\nExample: " + COMMAND_WORD + " 3"; | ||
|
||
private final UpcomingEventPredicate predicate; | ||
|
||
public ShowNextCommand(UpcomingEventPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
model.updateFilteredEventList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_EVENTS_LISTED_OVERVIEW, model.getFilteredEventList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof ShowNextCommand // instanceof handles nulls | ||
&& predicate.equals(((ShowNextCommand) other).predicate)); // state check | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/ezschedule/logic/parser/ShowNextCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ezschedule.logic.parser; | ||
|
||
import ezschedule.commons.core.Messages; | ||
import ezschedule.logic.commands.ShowNextCommand; | ||
import ezschedule.logic.parser.exceptions.ParseException; | ||
import ezschedule.model.event.UpcomingEventPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new ShowNextCommandParser object | ||
*/ | ||
public class ShowNextCommandParser implements Parser<ShowNextCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the ShowNextCommand | ||
* and returns an ShowNextCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
@Override | ||
public ShowNextCommand parse(String userInput) throws ParseException { | ||
// No argument provided, just show the next one | ||
if (userInput.isBlank()) { | ||
return new ShowNextCommand(new UpcomingEventPredicate(1)); | ||
} | ||
|
||
try { | ||
int count = Integer.parseInt(userInput.trim()); | ||
if (count > 0) { | ||
return new ShowNextCommand(new UpcomingEventPredicate(count)); | ||
} | ||
} catch (NumberFormatException e) { | ||
// Empty here; another exception is throw outside. | ||
} | ||
|
||
throw new ParseException( | ||
String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, ShowNextCommand.MESSAGE_USAGE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/ezschedule/model/event/UpcomingEventPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ezschedule.model.event; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Tests that an {@code Event} is one of the upcoming events. | ||
* Only the first {@code count} number of upcoming events are considered. | ||
*/ | ||
public class UpcomingEventPredicate implements Predicate<Event> { | ||
|
||
private int upcomingCount; | ||
|
||
public UpcomingEventPredicate(int count) { | ||
upcomingCount = count; | ||
} | ||
|
||
// Events should be in chronological order | ||
@Override | ||
public boolean test(Event event) { | ||
if (event.isCompleted()) { | ||
return false; | ||
} | ||
|
||
return upcomingCount-- > 0; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof UpcomingEventPredicate // instanceof handles nulls | ||
&& upcomingCount == ((UpcomingEventPredicate) other).upcomingCount); // state check | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters