forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #65 from ananya21/assign-team
Add AssignTeamCommand and AssignTeamCommandParser
- Loading branch information
Showing
8 changed files
with
162 additions
and
3 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
66 changes: 66 additions & 0 deletions
66
src/main/java/seedu/address/logic/commands/AssignTeamCommand.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,66 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.project.Member; | ||
|
||
/** | ||
* Adds a task to a project. | ||
*/ | ||
public class AssignTeamCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "assign team"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + "PERSON_NAME, PERSON_NAME, PERSON_NAME" | ||
+ "/to PROJECT_NAME"; | ||
|
||
public static final String MESSAGE_PROJECT_NOT_FOUND = "Project %1$s not found: " | ||
+ "Please make sure the project exists."; | ||
|
||
public static final String MESSAGE_SUCCESS = "The team %1$s has been assigned to the following project %2$s."; | ||
private final Person project; | ||
|
||
private final List<Member> team; | ||
|
||
/** | ||
* Creates an AddCommand to add the specified {@code Person} | ||
*/ | ||
public AssignTeamCommand(List<String> team, Person project) { | ||
this.project = project; | ||
this.team = new ArrayList<>(); | ||
for (String name : team) { | ||
this.team.add(new Member(name)); | ||
} | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
Person projectAssign = model.findPerson(project.getName()); | ||
|
||
if (projectAssign.equals(null)) { | ||
throw new CommandException(String.format( | ||
MESSAGE_PROJECT_NOT_FOUND, | ||
Messages.format(project))); | ||
} | ||
|
||
projectAssign.assignTeam(this.team); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, team, Messages.format(projectAssign))); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("set Team", team) | ||
.toString(); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/main/java/seedu/address/logic/parser/AssignTeamCommandParser.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.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import seedu.address.logic.commands.AssignTeamCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.Person; | ||
|
||
|
||
/** | ||
* Parses input arguments and creates a new AddCommand object | ||
*/ | ||
public class AssignTeamCommandParser implements Parser<AssignTeamCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddCommand | ||
* and returns an AddCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AssignTeamCommand parse(String args) throws ParseException { | ||
try { | ||
if (!args.contains(" /to ")) { | ||
throw new ParseException("Whoops! When referring to another field like a project," | ||
+ " always remember to put /to instead of just to."); | ||
} | ||
String members = args.split(" /to")[0].trim(); | ||
String projectName = args.split(" /to")[1].trim(); | ||
List<String> team = Arrays.stream(members.split(",")) | ||
.map(String::trim) | ||
.collect(Collectors.toList()); | ||
if ((team.size() == 0) || (projectName.length() == 0)) { | ||
throw new ParseException("Please enter the project and team fields"); | ||
} | ||
Person project = new Person(ParserUtil.parseName(projectName)); | ||
return new AssignTeamCommand(team, project); | ||
} catch (IndexOutOfBoundsException e) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
AssignTeamCommand.MESSAGE_USAGE)); | ||
} | ||
} | ||
} |
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