Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2122S1#78 from EltonGohJH/Elton-Group…
Browse files Browse the repository at this point in the history
…-v1.2

Feat: Add Support for Group and SubGroup Commands -- basic version
  • Loading branch information
kaixin-hc authored Oct 12, 2021
2 parents c76ea5d + 1e94ad4 commit 1b0249f
Show file tree
Hide file tree
Showing 40 changed files with 1,316 additions and 270 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class MainApp extends Application {
protected Model model;
protected Config config;


@Override
public void init() throws Exception {
logger.info("=============================[ Initializing AddressBook ]===========================");
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/seedu/address/logic/commands/GroupCreateCommand.java
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));
}
}
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));
}
}
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));
}
}
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));
}
}
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));
}
}
20 changes: 20 additions & 0 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.GroupCreateCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.PersonAddGroupCommand;
import seedu.address.logic.commands.PersonAddSubGroupCommand;
import seedu.address.logic.commands.PersonRemoveGroupCommand;
import seedu.address.logic.commands.PersonRemoveSubGroupCommand;
import seedu.address.logic.commands.person.PersonCreateCommand;
import seedu.address.logic.commands.person.PersonDeleteCommand;
import seedu.address.logic.commands.person.PersonEditCommand;
Expand Down Expand Up @@ -76,6 +81,21 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case GroupCreateCommand.COMMAND_WORD:
return new GroupCreateCommandParser().parse(arguments);

case PersonAddGroupCommand.COMMAND_WORD:
return new PersonAddGroupCommandParser().parse(arguments);

case PersonAddSubGroupCommand.COMMAND_WORD:
return new PersonAddSubGroupCommandParser().parse(arguments);

case PersonRemoveGroupCommand.COMMAND_WORD:
return new PersonRemoveGroupCommandParser().parse(arguments);

case PersonRemoveSubGroupCommand.COMMAND_WORD:
return new PersonRemoveSubGroupCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_NOTE = new Prefix("n/");
public static final Prefix PREFIX_TAG = new Prefix("t/");

public static final Prefix PREFIX_GROUP = new Prefix("g:");
public static final Prefix PREFIX_SUBGROUP = new Prefix("sg:");
}
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());
}
}
Loading

0 comments on commit 1b0249f

Please sign in to comment.