|
| 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 | +} |
0 commit comments