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#70 from successs404/branch-del…
…ete-grp Branch delete grp
- Loading branch information
Showing
9 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/seedu/address/logic/commands/DelGrpCommand.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,58 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.group.Group; | ||
import seedu.address.model.group.GrpContainsKeywordPredicate; | ||
|
||
public class DelGrpCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "delgrp"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes an existing tutorial group. " | ||
+ "Parameter: " | ||
+ PREFIX_GRP + "GRP \n" | ||
+ "Example: " + COMMAND_WORD + " " + PREFIX_GRP + "G04"; | ||
|
||
public static final String MESSAGE_DELETE_GROUP_SUCCESS = "Tutorial group deleted: %1$s"; | ||
|
||
private final GrpContainsKeywordPredicate grpPredicate; | ||
|
||
/** | ||
* Creates a DelGrpCommand to add the specified {@code Group} | ||
*/ | ||
public DelGrpCommand(GrpContainsKeywordPredicate grpPredicate) { | ||
this.grpPredicate = grpPredicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
Group toDel = new Group(); | ||
|
||
boolean hasGroup = false; | ||
|
||
if (!model.getSerenity().getGroupList().isEmpty()) { | ||
for (Group group : model.getSerenity().getGroupList()) { | ||
if (group.getName().equals(grpPredicate.getKeyword())) { | ||
toDel = group; | ||
hasGroup = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!hasGroup) { | ||
throw new CommandException(Messages.MESSAGE_GROUP_EMPTY); | ||
} | ||
|
||
model.deleteGroup(toDel); | ||
model.updateFilteredGroupList(grpPredicate); | ||
return new CommandResult(String.format(MESSAGE_DELETE_GROUP_SUCCESS, toDel)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/seedu/address/logic/parser/DelGrpCommandParser.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,45 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.DelGrpCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.group.GrpContainsKeywordPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddGrpCommand object | ||
*/ | ||
public class DelGrpCommandParser implements Parser<DelGrpCommand> { | ||
|
||
private final ParseException delGrpCommandParserException = new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DelGrpCommand.MESSAGE_USAGE)); | ||
|
||
@Override | ||
public DelGrpCommand parse(String args) throws ParseException { | ||
|
||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GRP); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_GRP) || !argMultimap.getPreamble().isEmpty()) { | ||
throw delGrpCommandParserException; | ||
} | ||
|
||
String[] grpKeyword = argMultimap.getValue(PREFIX_GRP).get().split("\\s+"); | ||
|
||
if (grpKeyword.length > 1) { | ||
throw delGrpCommandParserException; | ||
} | ||
|
||
return new DelGrpCommand(new GrpContainsKeywordPredicate(grpKeyword[0])); | ||
} | ||
|
||
/** | ||
* 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