forked from nus-cs2103-AY2122S1/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-AY2122S1#78 from EltonGohJH/Elton-Group…
…-v1.2 Feat: Add Support for Group and SubGroup Commands -- basic version
- Loading branch information
Showing
40 changed files
with
1,316 additions
and
270 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
44 changes: 44 additions & 0 deletions
44
src/main/java/seedu/address/logic/commands/GroupCreateCommand.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,44 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.group.SuperGroup; | ||
|
||
public class GroupCreateCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "group_create"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Creates a group " | ||
+ "Parameters: " | ||
+ COMMAND_WORD | ||
+ PREFIX_GROUP + "GROUP\n" | ||
+ "Example: " + COMMAND_WORD | ||
+ PREFIX_GROUP + "Orbital"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Group %s created"; | ||
public static final String MESSAGE_DUPLICATE_GROUP = "Group already exists"; | ||
|
||
public final SuperGroup toAdd; | ||
/** | ||
* Creates a new GroupCommand that add the specified group. | ||
* @param toAdd the SuperGroup that will be created and added to the list. | ||
*/ | ||
public GroupCreateCommand(SuperGroup toAdd) { | ||
this.toAdd = toAdd; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (model.hasSuperGroup(toAdd)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_GROUP); | ||
} | ||
|
||
model.addSuperGroup(toAdd); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/seedu/address/logic/commands/PersonAddGroupCommand.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,57 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.group.SuperGroup; | ||
import seedu.address.model.person.Person; | ||
|
||
public class PersonAddGroupCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "person_add_group"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to a group" | ||
+ "Parameters: " | ||
+ PREFIX_NAME + "NAME " | ||
+ PREFIX_GROUP + "GROUP\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_NAME + "John Doe " | ||
+ PREFIX_GROUP + "Team"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Added person to %s"; | ||
|
||
public static final String MESSAGE_DUPLICATE_GROUP = "This person is already in the group"; | ||
|
||
protected String personName; | ||
|
||
protected String groupName; | ||
|
||
/** | ||
* Creates an PersonAddGroupCommand to add the specified {@code Person} | ||
*/ | ||
public PersonAddGroupCommand(String personName, String groupName) { | ||
this.personName = personName; | ||
this.groupName = groupName; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
Person personToEdit = model.findPerson(personName); | ||
if (personToEdit.getSuperGroups().contains(groupName)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_GROUP); | ||
} | ||
SuperGroup superGroup = model.findSuperGroup(groupName); | ||
if (superGroup == null) { | ||
superGroup = new SuperGroup(groupName); | ||
model.addSuperGroup(superGroup); | ||
} | ||
superGroup.addPerson(personToEdit); | ||
personToEdit.addSuperGroup(superGroup); | ||
model.setPerson(personToEdit, personToEdit); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, groupName)); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/seedu/address/logic/commands/PersonAddSubGroupCommand.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,79 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SUBGROUP; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.group.SubGroup; | ||
import seedu.address.model.group.SuperGroup; | ||
import seedu.address.model.person.Person; | ||
|
||
public class PersonAddSubGroupCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "person_add_subgroup"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to a subgroup" | ||
+ "Parameters: " | ||
+ PREFIX_NAME + "NAME " | ||
+ PREFIX_GROUP + "GROUP" | ||
+ PREFIX_SUBGROUP + "SUBGROUP\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_NAME + "John Doe " | ||
+ PREFIX_GROUP + "Orbital " | ||
+ PREFIX_SUBGROUP + "Artemis"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Added person to the %s"; | ||
public static final String MESSAGE_DUPLICATE_GROUP = "This person is already in the subgroup"; | ||
|
||
protected String personName; | ||
|
||
protected String groupName; | ||
|
||
protected String subGroupName; | ||
|
||
/** | ||
* Creates an PersonAddSubGroupCommand to add the specified {@code SubGroup} | ||
* | ||
* @param personName the name of the person to be added into the subgroup. | ||
* @param groupName the name of the group where the subgroup belongs to. | ||
* @param subGroupName the name of the subGroup. | ||
*/ | ||
public PersonAddSubGroupCommand(String personName, String groupName, String subGroupName) { | ||
this.personName = personName; | ||
this.groupName = groupName; | ||
this.subGroupName = subGroupName; | ||
} | ||
|
||
|
||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
Person personToEdit = model.findPerson(personName); | ||
if (personToEdit.getSubGroups().contains(groupName + "_" + subGroupName)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_GROUP); | ||
} | ||
|
||
SuperGroup superGroup = model.findSuperGroup(groupName); | ||
if (superGroup == null) { | ||
superGroup = new SuperGroup(groupName); | ||
model.addSuperGroup(superGroup); | ||
} | ||
|
||
SubGroup subGroup = model.findSubGroup(groupName + "_" + subGroupName); | ||
if (subGroup == null) { | ||
subGroup = new SubGroup(subGroupName, superGroup.getName()); | ||
model.addSubGroup(subGroup); | ||
} | ||
superGroup.addSubGroup(subGroup); | ||
superGroup.addPerson(personToEdit); | ||
subGroup.addPerson(personToEdit); | ||
personToEdit.addSuperGroup(superGroup); | ||
personToEdit.addSubGroup(subGroup); | ||
model.setPerson(personToEdit, personToEdit); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, groupName + "_" + subGroupName)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/seedu/address/logic/commands/PersonRemoveGroupCommand.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,54 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import java.util.Arrays; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
|
||
public class PersonRemoveGroupCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "person_rm_group"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Removes a person from a group" | ||
+ "Parameters: " | ||
+ PREFIX_NAME + "NAME " | ||
+ PREFIX_GROUP + "GROUP\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_NAME + "John Doe " | ||
+ PREFIX_GROUP + "Team"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Removed person from %s"; | ||
|
||
public static final String MESSAGE_NOT_IN_GROUP = "This person is not in the group"; | ||
|
||
protected String personName; | ||
|
||
protected String groupName; | ||
|
||
/** | ||
* Creates an PersonRemoveGroupCommand to add the specified {@code Person} | ||
*/ | ||
public PersonRemoveGroupCommand(String personName, String groupName) { | ||
this.personName = personName; | ||
this.groupName = groupName; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
Person personToEdit = model.findPerson(personName); | ||
if (!personToEdit.getSuperGroups().contains(groupName)) { | ||
throw new CommandException(MESSAGE_NOT_IN_GROUP); | ||
} | ||
personToEdit.getSuperGroups().remove(groupName); | ||
System.out.println(Arrays.toString(personToEdit.getSubGroups().toArray())); | ||
personToEdit.getSubGroups().removeIf(subGroup -> subGroup.split("_")[0].equals(groupName)); | ||
model.setPerson(personToEdit, personToEdit); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, groupName)); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/seedu/address/logic/commands/PersonRemoveSubGroupCommand.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_GROUP; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SUBGROUP; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.group.SubGroup; | ||
import seedu.address.model.person.Person; | ||
|
||
public class PersonRemoveSubGroupCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "person_rm_subgroup"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Removes a person from a subgroup" | ||
+ "Parameters: " | ||
+ PREFIX_NAME + "NAME " | ||
+ PREFIX_GROUP + "GROUP" | ||
+ PREFIX_SUBGROUP + "SUBGROUP\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_NAME + "John Doe " | ||
+ PREFIX_GROUP + "Orbital " | ||
+ PREFIX_SUBGROUP + "Artemis"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Removed person from %s"; | ||
public static final String MESSAGE_NOT_IN_SUBGROUP = "This person is not in the subgroup"; | ||
|
||
protected String personName; | ||
|
||
protected String groupName; | ||
|
||
protected String subGroupName; | ||
|
||
/** | ||
* Creates an PersonRemoveSubGroupCommand to add the specified {@code Person} | ||
*/ | ||
public PersonRemoveSubGroupCommand(String personName, String groupName, String subGroupName) { | ||
this.personName = personName; | ||
this.groupName = groupName; | ||
this.subGroupName = subGroupName; | ||
} | ||
|
||
|
||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
Person personToEdit = model.findPerson(personName); | ||
if (!personToEdit.getSubGroups().contains(groupName + "_" + subGroupName)) { | ||
throw new CommandException(MESSAGE_NOT_IN_SUBGROUP); | ||
} | ||
personToEdit.getSubGroups().remove(groupName + "_" + subGroupName); | ||
SubGroup subGroup = model.findSubGroup(groupName + "_" + subGroupName); | ||
subGroup.getPeople().remove(personName); | ||
model.setPerson(personToEdit, personToEdit); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, groupName + "_" + subGroupName)); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/main/java/seedu/address/logic/parser/GroupCreateCommandParser.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,40 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.GroupCreateCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.group.SuperGroup; | ||
|
||
/** | ||
* Parses input arguments to create a group command. | ||
*/ | ||
public class GroupCreateCommandParser extends Parser<GroupCreateCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the {@code GroupCommand} and | ||
* returns a {@code GroupCommand} object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public GroupCreateCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GROUP); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_GROUP) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
GroupCreateCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
SuperGroup superGroup = ParserUtil.parseSuperGroup(argMultimap.getValue(PREFIX_GROUP).get()); | ||
return new GroupCreateCommand(superGroup); | ||
} | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
Oops, something went wrong.