Skip to content

Commit feedcab

Browse files
committed
Refactor: Refactor Note Command for Group Command
2 parents 7dc3b04 + 1b0249f commit feedcab

40 files changed

+1271
-204
lines changed

src/main/java/seedu/address/MainApp.java

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class MainApp extends Application {
4848
protected Model model;
4949
protected Config config;
5050

51+
5152
@Override
5253
public void init() throws Exception {
5354
logger.info("=============================[ Initializing AddressBook ]===========================");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
5+
6+
import seedu.address.logic.commands.exceptions.CommandException;
7+
import seedu.address.model.Model;
8+
import seedu.address.model.group.SuperGroup;
9+
10+
public class GroupCreateCommand extends Command {
11+
12+
public static final String COMMAND_WORD = "group_create";
13+
14+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Creates a group "
15+
+ "Parameters: "
16+
+ COMMAND_WORD
17+
+ PREFIX_GROUP + "GROUP\n"
18+
+ "Example: " + COMMAND_WORD
19+
+ PREFIX_GROUP + "Orbital";
20+
21+
public static final String MESSAGE_SUCCESS = "Group %s created";
22+
public static final String MESSAGE_DUPLICATE_GROUP = "Group already exists";
23+
24+
public final SuperGroup toAdd;
25+
/**
26+
* Creates a new GroupCommand that add the specified group.
27+
* @param toAdd the SuperGroup that will be created and added to the list.
28+
*/
29+
public GroupCreateCommand(SuperGroup toAdd) {
30+
this.toAdd = toAdd;
31+
}
32+
33+
@Override
34+
public CommandResult execute(Model model) throws CommandException {
35+
requireNonNull(model);
36+
37+
if (model.hasSuperGroup(toAdd)) {
38+
throw new CommandException(MESSAGE_DUPLICATE_GROUP);
39+
}
40+
41+
model.addSuperGroup(toAdd);
42+
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
6+
7+
import seedu.address.logic.commands.exceptions.CommandException;
8+
import seedu.address.model.Model;
9+
import seedu.address.model.group.SuperGroup;
10+
import seedu.address.model.person.Person;
11+
12+
public class PersonAddGroupCommand extends Command {
13+
14+
public static final String COMMAND_WORD = "person_add_group";
15+
16+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to a group"
17+
+ "Parameters: "
18+
+ PREFIX_NAME + "NAME "
19+
+ PREFIX_GROUP + "GROUP\n"
20+
+ "Example: " + COMMAND_WORD + " "
21+
+ PREFIX_NAME + "John Doe "
22+
+ PREFIX_GROUP + "Team";
23+
24+
public static final String MESSAGE_SUCCESS = "Added person to %s";
25+
26+
public static final String MESSAGE_DUPLICATE_GROUP = "This person is already in the group";
27+
28+
protected String personName;
29+
30+
protected String groupName;
31+
32+
/**
33+
* Creates an PersonAddGroupCommand to add the specified {@code Person}
34+
*/
35+
public PersonAddGroupCommand(String personName, String groupName) {
36+
this.personName = personName;
37+
this.groupName = groupName;
38+
}
39+
40+
@Override
41+
public CommandResult execute(Model model) throws CommandException {
42+
requireNonNull(model);
43+
Person personToEdit = model.findPerson(personName);
44+
if (personToEdit.getSuperGroups().contains(groupName)) {
45+
throw new CommandException(MESSAGE_DUPLICATE_GROUP);
46+
}
47+
SuperGroup superGroup = model.findSuperGroup(groupName);
48+
if (superGroup == null) {
49+
superGroup = new SuperGroup(groupName);
50+
model.addSuperGroup(superGroup);
51+
}
52+
superGroup.addPerson(personToEdit);
53+
personToEdit.addSuperGroup(superGroup);
54+
model.setPerson(personToEdit, personToEdit);
55+
return new CommandResult(String.format(MESSAGE_SUCCESS, groupName));
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
6+
import static seedu.address.logic.parser.CliSyntax.PREFIX_SUBGROUP;
7+
8+
import seedu.address.logic.commands.exceptions.CommandException;
9+
import seedu.address.model.Model;
10+
import seedu.address.model.group.SubGroup;
11+
import seedu.address.model.group.SuperGroup;
12+
import seedu.address.model.person.Person;
13+
14+
public class PersonAddSubGroupCommand extends Command {
15+
16+
public static final String COMMAND_WORD = "person_add_subgroup";
17+
18+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to a subgroup"
19+
+ "Parameters: "
20+
+ PREFIX_NAME + "NAME "
21+
+ PREFIX_GROUP + "GROUP"
22+
+ PREFIX_SUBGROUP + "SUBGROUP\n"
23+
+ "Example: " + COMMAND_WORD + " "
24+
+ PREFIX_NAME + "John Doe "
25+
+ PREFIX_GROUP + "Orbital "
26+
+ PREFIX_SUBGROUP + "Artemis";
27+
28+
public static final String MESSAGE_SUCCESS = "Added person to the %s";
29+
public static final String MESSAGE_DUPLICATE_GROUP = "This person is already in the subgroup";
30+
31+
protected String personName;
32+
33+
protected String groupName;
34+
35+
protected String subGroupName;
36+
37+
/**
38+
* Creates an PersonAddSubGroupCommand to add the specified {@code SubGroup}
39+
*
40+
* @param personName the name of the person to be added into the subgroup.
41+
* @param groupName the name of the group where the subgroup belongs to.
42+
* @param subGroupName the name of the subGroup.
43+
*/
44+
public PersonAddSubGroupCommand(String personName, String groupName, String subGroupName) {
45+
this.personName = personName;
46+
this.groupName = groupName;
47+
this.subGroupName = subGroupName;
48+
}
49+
50+
51+
52+
@Override
53+
public CommandResult execute(Model model) throws CommandException {
54+
requireNonNull(model);
55+
Person personToEdit = model.findPerson(personName);
56+
if (personToEdit.getSubGroups().contains(groupName + "_" + subGroupName)) {
57+
throw new CommandException(MESSAGE_DUPLICATE_GROUP);
58+
}
59+
60+
SuperGroup superGroup = model.findSuperGroup(groupName);
61+
if (superGroup == null) {
62+
superGroup = new SuperGroup(groupName);
63+
model.addSuperGroup(superGroup);
64+
}
65+
66+
SubGroup subGroup = model.findSubGroup(groupName + "_" + subGroupName);
67+
if (subGroup == null) {
68+
subGroup = new SubGroup(subGroupName, superGroup.getName());
69+
model.addSubGroup(subGroup);
70+
}
71+
superGroup.addSubGroup(subGroup);
72+
superGroup.addPerson(personToEdit);
73+
subGroup.addPerson(personToEdit);
74+
personToEdit.addSuperGroup(superGroup);
75+
personToEdit.addSubGroup(subGroup);
76+
model.setPerson(personToEdit, personToEdit);
77+
return new CommandResult(String.format(MESSAGE_SUCCESS, groupName + "_" + subGroupName));
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
6+
7+
import java.util.Arrays;
8+
9+
import seedu.address.logic.commands.exceptions.CommandException;
10+
import seedu.address.model.Model;
11+
import seedu.address.model.person.Person;
12+
13+
public class PersonRemoveGroupCommand extends Command {
14+
15+
public static final String COMMAND_WORD = "person_rm_group";
16+
17+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Removes a person from a group"
18+
+ "Parameters: "
19+
+ PREFIX_NAME + "NAME "
20+
+ PREFIX_GROUP + "GROUP\n"
21+
+ "Example: " + COMMAND_WORD + " "
22+
+ PREFIX_NAME + "John Doe "
23+
+ PREFIX_GROUP + "Team";
24+
25+
public static final String MESSAGE_SUCCESS = "Removed person from %s";
26+
27+
public static final String MESSAGE_NOT_IN_GROUP = "This person is not in the group";
28+
29+
protected String personName;
30+
31+
protected String groupName;
32+
33+
/**
34+
* Creates an PersonRemoveGroupCommand to add the specified {@code Person}
35+
*/
36+
public PersonRemoveGroupCommand(String personName, String groupName) {
37+
this.personName = personName;
38+
this.groupName = groupName;
39+
}
40+
41+
@Override
42+
public CommandResult execute(Model model) throws CommandException {
43+
requireNonNull(model);
44+
Person personToEdit = model.findPerson(personName);
45+
if (!personToEdit.getSuperGroups().contains(groupName)) {
46+
throw new CommandException(MESSAGE_NOT_IN_GROUP);
47+
}
48+
personToEdit.getSuperGroups().remove(groupName);
49+
System.out.println(Arrays.toString(personToEdit.getSubGroups().toArray()));
50+
personToEdit.getSubGroups().removeIf(subGroup -> subGroup.split("_")[0].equals(groupName));
51+
model.setPerson(personToEdit, personToEdit);
52+
return new CommandResult(String.format(MESSAGE_SUCCESS, groupName));
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
6+
import static seedu.address.logic.parser.CliSyntax.PREFIX_SUBGROUP;
7+
8+
import seedu.address.logic.commands.exceptions.CommandException;
9+
import seedu.address.model.Model;
10+
import seedu.address.model.group.SubGroup;
11+
import seedu.address.model.person.Person;
12+
13+
public class PersonRemoveSubGroupCommand extends Command {
14+
15+
public static final String COMMAND_WORD = "person_rm_subgroup";
16+
17+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Removes a person from a subgroup"
18+
+ "Parameters: "
19+
+ PREFIX_NAME + "NAME "
20+
+ PREFIX_GROUP + "GROUP"
21+
+ PREFIX_SUBGROUP + "SUBGROUP\n"
22+
+ "Example: " + COMMAND_WORD + " "
23+
+ PREFIX_NAME + "John Doe "
24+
+ PREFIX_GROUP + "Orbital "
25+
+ PREFIX_SUBGROUP + "Artemis";
26+
27+
public static final String MESSAGE_SUCCESS = "Removed person from %s";
28+
public static final String MESSAGE_NOT_IN_SUBGROUP = "This person is not in the subgroup";
29+
30+
protected String personName;
31+
32+
protected String groupName;
33+
34+
protected String subGroupName;
35+
36+
/**
37+
* Creates an PersonRemoveSubGroupCommand to add the specified {@code Person}
38+
*/
39+
public PersonRemoveSubGroupCommand(String personName, String groupName, String subGroupName) {
40+
this.personName = personName;
41+
this.groupName = groupName;
42+
this.subGroupName = subGroupName;
43+
}
44+
45+
46+
47+
@Override
48+
public CommandResult execute(Model model) throws CommandException {
49+
requireNonNull(model);
50+
Person personToEdit = model.findPerson(personName);
51+
if (!personToEdit.getSubGroups().contains(groupName + "_" + subGroupName)) {
52+
throw new CommandException(MESSAGE_NOT_IN_SUBGROUP);
53+
}
54+
personToEdit.getSubGroups().remove(groupName + "_" + subGroupName);
55+
SubGroup subGroup = model.findSubGroup(groupName + "_" + subGroupName);
56+
subGroup.getPeople().remove(personName);
57+
model.setPerson(personToEdit, personToEdit);
58+
return new CommandResult(String.format(MESSAGE_SUCCESS, groupName + "_" + subGroupName));
59+
}
60+
}

src/main/java/seedu/address/logic/parser/AddressBookParser.java

+20
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010
import seedu.address.logic.commands.Command;
1111
import seedu.address.logic.commands.ExitCommand;
1212
import seedu.address.logic.commands.FindCommand;
13+
import seedu.address.logic.commands.GroupCreateCommand;
1314
import seedu.address.logic.commands.HelpCommand;
1415
import seedu.address.logic.commands.ListCommand;
16+
import seedu.address.logic.commands.PersonAddGroupCommand;
17+
import seedu.address.logic.commands.PersonAddSubGroupCommand;
18+
import seedu.address.logic.commands.PersonRemoveGroupCommand;
19+
import seedu.address.logic.commands.PersonRemoveSubGroupCommand;
1520
import seedu.address.logic.commands.person.PersonCreateCommand;
1621
import seedu.address.logic.commands.person.PersonDeleteCommand;
1722
import seedu.address.logic.commands.person.PersonEditCommand;
@@ -76,6 +81,21 @@ public Command parseCommand(String userInput) throws ParseException {
7681
case HelpCommand.COMMAND_WORD:
7782
return new HelpCommand();
7883

84+
case GroupCreateCommand.COMMAND_WORD:
85+
return new GroupCreateCommandParser().parse(arguments);
86+
87+
case PersonAddGroupCommand.COMMAND_WORD:
88+
return new PersonAddGroupCommandParser().parse(arguments);
89+
90+
case PersonAddSubGroupCommand.COMMAND_WORD:
91+
return new PersonAddSubGroupCommandParser().parse(arguments);
92+
93+
case PersonRemoveGroupCommand.COMMAND_WORD:
94+
return new PersonRemoveGroupCommandParser().parse(arguments);
95+
96+
case PersonRemoveSubGroupCommand.COMMAND_WORD:
97+
return new PersonRemoveSubGroupCommandParser().parse(arguments);
98+
7999
default:
80100
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
81101
}

src/main/java/seedu/address/logic/parser/CliSyntax.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public class CliSyntax {
1111
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
1212
public static final Prefix PREFIX_NOTE = new Prefix("n/");
1313
public static final Prefix PREFIX_TAG = new Prefix("t/");
14-
14+
public static final Prefix PREFIX_GROUP = new Prefix("g:");
15+
public static final Prefix PREFIX_SUBGROUP = new Prefix("sg:");
1516
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package seedu.address.logic.parser;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
6+
7+
import java.util.stream.Stream;
8+
9+
import seedu.address.logic.commands.GroupCreateCommand;
10+
import seedu.address.logic.parser.exceptions.ParseException;
11+
import seedu.address.model.group.SuperGroup;
12+
13+
/**
14+
* Parses input arguments to create a group command.
15+
*/
16+
public class GroupCreateCommandParser extends Parser<GroupCreateCommand> {
17+
18+
/**
19+
* Parses the given {@code String} of arguments in the context of the {@code GroupCommand} and
20+
* returns a {@code GroupCommand} object for execution.
21+
*
22+
* @throws ParseException if the user input does not conform the expected format
23+
*/
24+
public GroupCreateCommand parse(String args) throws ParseException {
25+
requireNonNull(args);
26+
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GROUP);
27+
28+
if (!arePrefixesPresent(argMultimap, PREFIX_GROUP)
29+
|| !argMultimap.getPreamble().isEmpty()) {
30+
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
31+
GroupCreateCommand.MESSAGE_USAGE));
32+
}
33+
34+
SuperGroup superGroup = ParserUtil.parseSuperGroup(argMultimap.getValue(PREFIX_GROUP).get());
35+
return new GroupCreateCommand(superGroup);
36+
}
37+
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
38+
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
39+
}
40+
}

0 commit comments

Comments
 (0)