forked from AY2324S2-CS2103T-T09-4/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 branch 'Edit-UI' of https://github.com/qinxutan/tp into Edit-UI
* 'Edit-UI' of https://github.com/qinxutan/tp: Create tutorial teams (AY2324S2-CS2103T-T09-4#108)
- Loading branch information
Showing
19 changed files
with
993 additions
and
19 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
src/main/java/seedu/address/logic/commands/AddTeamCommand.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,158 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TEAM_SIZE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TUTORIALCLASS; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.logic.messages.ModuleMessages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.ModuleTutorialPair; | ||
import seedu.address.model.module.TutorialClass; | ||
import seedu.address.model.module.TutorialTeam; | ||
|
||
/** | ||
* A class that handles the /add_team command execution. | ||
*/ | ||
public class AddTeamCommand extends Command { | ||
public static final String MESSAGE_ADD_TEAM_SUCCESS_WITHOUT_SIZE = "Added %1$s to %2$s %3$s"; | ||
public static final String MESSAGE_ADD_TEAM_SUCCESS_WITH_SIZE = "Added %1$s of size %2$s to %3$s %4$s"; | ||
public static final String MESSAGE_DUPLICATE_TEAM = "%1$s already added to %2$s %3$s!"; | ||
public static final String COMMAND_WORD = "/add_team"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Create a team with a name and an optional size, assigned to a particular tutorial class\n" | ||
+ "Parameters: " + PREFIX_MODULECODE + "MODULE_CODE " | ||
+ PREFIX_TUTORIALCLASS + "TUTORIAL_CLASS " + PREFIX_NAME + "TEAM_NAME " + PREFIX_TEAM_SIZE + "TEAM_SIZE\n" | ||
+ "Example: " + COMMAND_WORD + " " + PREFIX_MODULECODE + "CS2103T " | ||
+ PREFIX_TUTORIALCLASS + "T09 " + PREFIX_NAME + "Team 1 " + PREFIX_TEAM_SIZE + "5"; | ||
|
||
private final ModuleCode module; | ||
private final TutorialClass tutorialClass; | ||
private final String teamName; | ||
private final int teamSize; | ||
|
||
/** | ||
* Constructs an AddTeamCommand to add the specified {@code TutorialTeam} to the | ||
* specified {@code TutorialClass}, without the specified team size. | ||
* @param module The module code of the tutorial class to be added. | ||
* @param tutorialClass The tutorial class to be added. | ||
* @param teamName The name of the team to be added. | ||
*/ | ||
public AddTeamCommand(ModuleCode module, TutorialClass tutorialClass, String teamName) { | ||
requireAllNonNull(module); | ||
this.module = module; | ||
this.tutorialClass = tutorialClass; | ||
this.teamName = teamName; | ||
this.teamSize = Integer.MAX_VALUE; | ||
} | ||
|
||
/** | ||
* Constructs an AddTeamCommand to add the specified {@code TutorialTeam} to the | ||
* specified {@code TutorialClass}, with the specified team size. | ||
* @param module The module code of the tutorial class to be added. | ||
* @param tutorialClass The tutorial class to be added. | ||
* @param teamName The name of the team to be added. | ||
* @param teamSize The size of the team to be added. | ||
*/ | ||
public AddTeamCommand(ModuleCode module, TutorialClass tutorialClass, String teamName, int teamSize) { | ||
requireAllNonNull(module); | ||
this.module = module; | ||
this.tutorialClass = tutorialClass; | ||
this.teamName = teamName; | ||
this.teamSize = teamSize; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
ModuleTutorialPair moduleAndTutorialClass = getModuleAndTutorialClass(model); | ||
ModuleCode module = moduleAndTutorialClass.getModule(); | ||
TutorialClass tutorialClass = moduleAndTutorialClass.getTutorialClass(); | ||
|
||
TutorialTeam newTeam = new TutorialTeam(teamName, teamSize); | ||
|
||
if (tutorialClass.hasTeam(newTeam)) { | ||
throw new CommandException(String.format(MESSAGE_DUPLICATE_TEAM, teamName, module, tutorialClass)); | ||
} else { | ||
tutorialClass.addTeam(newTeam); | ||
} | ||
|
||
if (teamSize != Integer.MAX_VALUE) { | ||
return new CommandResult(generateSuccessMessage(module, tutorialClass, teamName, teamSize)); | ||
} else { | ||
return new CommandResult(generateSuccessMessage(module, tutorialClass, teamName)); | ||
} | ||
} | ||
|
||
protected ModuleTutorialPair getModuleAndTutorialClass(Model model) throws CommandException { | ||
requireNonNull(model); | ||
ModuleCode module = getModule(); | ||
TutorialClass tutorialClass = getTutorialClass(); | ||
ModuleCode existingModule = model.findModuleFromList(module); | ||
TutorialClass existingTutorialClass = model.findTutorialClassFromList(tutorialClass, existingModule); | ||
if (existingModule == null) { | ||
throw new CommandException(String.format(ModuleMessages.MESSAGE_MODULE_NOT_FOUND, module)); | ||
} | ||
if (existingTutorialClass == null) { | ||
throw new CommandException( | ||
String.format(ModuleMessages.MESSAGE_TUTORIAL_DOES_NOT_BELONG_TO_MODULE, tutorialClass, module)); | ||
} | ||
return new ModuleTutorialPair(existingModule, existingTutorialClass); | ||
} | ||
|
||
protected ModuleCode getModule() { | ||
return module; | ||
} | ||
|
||
protected TutorialClass getTutorialClass() { | ||
return tutorialClass; | ||
} | ||
|
||
/** | ||
* Generates a command execution success message based on whether the tutorial | ||
* team is added successfully and if the team size is not specified. | ||
* @param module The module code of the tutorial class. | ||
* @param tutorialString The tutorial class. | ||
* @param teamName The name of the team. | ||
* @return The success message. | ||
*/ | ||
private String generateSuccessMessage(ModuleCode module, TutorialClass tutorialString, String teamName) { | ||
return String.format(MESSAGE_ADD_TEAM_SUCCESS_WITHOUT_SIZE, teamName, module, | ||
tutorialString); | ||
} | ||
|
||
/** | ||
* Generates a command execution success message based on whether the tutorial | ||
* team is added successfully and if the team size is specified. | ||
* @param module The module code of the tutorial class. | ||
* @param tutorialString The tutorial class. | ||
* @param teamName The name of the team. | ||
* @param teamSize The size of the team. | ||
* @return The success message. | ||
*/ | ||
private String generateSuccessMessage(ModuleCode module, TutorialClass tutorialString, String teamName, | ||
int teamSize) { | ||
return String.format(MESSAGE_ADD_TEAM_SUCCESS_WITH_SIZE, teamName, teamSize, module, tutorialString); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddTeamCommand)) { | ||
return false; | ||
} | ||
|
||
AddTeamCommand e = (AddTeamCommand) other; | ||
return module.equals(e.module) && tutorialClass.equals(e.tutorialClass) && teamName.equals(e.teamName) | ||
&& teamSize == e.teamSize; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/seedu/address/logic/parser/AddTeamCommandParser.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,70 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TEAM_SIZE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TUTORIALCLASS; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.AddTeamCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.TutorialClass; | ||
import seedu.address.model.module.TutorialTeam; | ||
|
||
/** | ||
* Parses input arguments and creates a new {@code AddClassCommandParser} object | ||
*/ | ||
public class AddTeamCommandParser implements Parser<AddTeamCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the | ||
* {@code AddClassCommandParser} | ||
* and returns a {@code AddClassCommandParser} object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AddTeamCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_MODULECODE, PREFIX_TUTORIALCLASS, | ||
PREFIX_NAME, PREFIX_TEAM_SIZE); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_MODULECODE, PREFIX_TUTORIALCLASS, PREFIX_NAME) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTeamCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
boolean isTeamSizePresent = argMultimap.getValue(PREFIX_TEAM_SIZE).isPresent(); | ||
String moduleCode = argMultimap.getValue(PREFIX_MODULECODE).orElse(""); | ||
String tutorialClass = argMultimap.getValue(PREFIX_TUTORIALCLASS).orElse(""); | ||
String teamName = argMultimap.getValue(PREFIX_NAME).orElse(""); | ||
if (!(ModuleCode.isValidModuleCode(moduleCode))) { | ||
throw new ParseException(ModuleCode.MESSAGE_CONSTRAINTS); | ||
} | ||
if (!(TutorialClass.isValidTutorialClass(tutorialClass))) { | ||
throw new ParseException(TutorialClass.MESSAGE_CONSTRAINTS); | ||
} | ||
|
||
if (!TutorialTeam.isValidTeamName(teamName)) { | ||
throw new ParseException(TutorialTeam.MESSAGE_NAME_CONSTRAINTS); | ||
} | ||
if (isTeamSizePresent) { | ||
int teamSize = Integer.parseInt(argMultimap.getValue(PREFIX_TEAM_SIZE).get()); | ||
if (teamSize <= 0) { | ||
throw new ParseException(TutorialTeam.MESSAGE_SIZE_CONSTRAINTS); | ||
} | ||
return new AddTeamCommand(new ModuleCode(moduleCode), new TutorialClass(tutorialClass), teamName, teamSize); | ||
} else { | ||
return new AddTeamCommand(new ModuleCode(moduleCode), new TutorialClass(tutorialClass), teamName); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if all the prefixes are present in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
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
Oops, something went wrong.