forked from nus-cs2103-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
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 nus-cs2103-AY2021S1#68 from nicholasyeo/branch-tim…
…etable-delete Add delete feature to Timetable
- Loading branch information
Showing
12 changed files
with
206 additions
and
9 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/main/java/seedu/address/logic/commands/TimetableDeleteSlotCommand.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,60 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DAY; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIME; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Slot; | ||
|
||
/** | ||
* Deletes a Slot from the Timetable in fitNUS. | ||
*/ | ||
public class TimetableDeleteSlotCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "timetable_delete"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes the slot identified by its day and time.\n" | ||
+ "Parameters: " | ||
+ PREFIX_DAY + "DAY" | ||
+ PREFIX_TIME + "TIME" | ||
+ "\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_DAY + "Monday " | ||
+ PREFIX_TIME + "1600-1800"; | ||
|
||
public static final String MESSAGE_DELETE_SLOT_SUCCESS = "Deleted Slot: %1$s"; | ||
public static final String MESSAGE_MISSING_SLOT = "This slot does not exist in your timetable."; | ||
|
||
private final Slot slotToFind; | ||
|
||
public TimetableDeleteSlotCommand(Slot slot) { | ||
slotToFind = slot; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
List<Slot> lastShownList = model.getFilteredSlotList(); | ||
boolean hasSlot = lastShownList.stream().anyMatch(slotToFind::isSameSlot); | ||
if (!hasSlot) { | ||
throw new CommandException(MESSAGE_MISSING_SLOT); | ||
} | ||
|
||
Slot slotToDelete = lastShownList.stream().filter(slotToFind::isSameSlot).findFirst().get(); | ||
|
||
model.deleteSlotFromTimetable(slotToDelete); | ||
return new CommandResult(String.format(MESSAGE_DELETE_SLOT_SUCCESS, slotToDelete)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof TimetableDeleteSlotCommand // instanceof handles nulls | ||
&& slotToFind.equals(((TimetableDeleteSlotCommand) other).slotToFind)); | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/main/java/seedu/address/logic/parser/TimetableDeleteSlotCommandParser.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,50 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DAY; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIME; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.TimetableAddRoutineCommand; | ||
import seedu.address.logic.commands.TimetableDeleteSlotCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.Day; | ||
import seedu.address.model.person.Duration; | ||
import seedu.address.model.person.Slot; | ||
|
||
/** | ||
* Parses input arguments and creates a new TimetableDeleteSlotCommand object | ||
*/ | ||
public class TimetableDeleteSlotCommandParser implements Parser<TimetableDeleteSlotCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the RoutineAddExerciseCommand | ||
* and returns an RoutineAddExerciseCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public TimetableDeleteSlotCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_DAY, PREFIX_TIME); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_DAY, PREFIX_TIME) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
TimetableAddRoutineCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
Day day = ParserUtil.parseDay(argMultimap.getValue(PREFIX_DAY).get()); | ||
Duration duration = ParserUtil.parseDuration(argMultimap.getValue(PREFIX_TIME).get()); | ||
|
||
Slot slotToFind = new Slot(day, duration); | ||
return new TimetableDeleteSlotCommand(slotToFind); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
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
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