forked from AY2425S1-CS2103T-T12-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 pull request AY2425S1-CS2103T-T12-4#209 from JJtan2002
Create working addGameCommand
- Loading branch information
Showing
3 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
220 changes: 220 additions & 0 deletions
220
src/main/java/seedu/address/logic/commands/AddGameCommand.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,220 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROLE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SKILLLEVEL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_USERNAME; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.util.CollectionUtil; | ||
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.game.Game; | ||
import seedu.address.model.game.Role; | ||
import seedu.address.model.game.SkillLevel; | ||
import seedu.address.model.game.Username; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Edits the details of an existing person in the address book. | ||
*/ | ||
public class AddGameCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "addgame"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a game to the person identified " | ||
+ "by the index number used in the displayed person list. " | ||
+ "Existing values will be overwritten by the input values.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "[" + PREFIX_GAME + "GAME] " | ||
+ "[" + PREFIX_USERNAME + "USERNAME] " | ||
+ "[" + PREFIX_ROLE + "ROLE] " | ||
+ "[" + PREFIX_SKILLLEVEL + "SKILL LEVEL]\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_GAME + "Brawl Stars " | ||
+ PREFIX_USERNAME + "johndoe420"; | ||
|
||
public static final String MESSAGE_ADD_GAME_SUCCESS = "Added Game to Person: %1$s"; | ||
public static final String MESSAGE_GAME_EXISTS = "The game provided already exists for that person."; | ||
|
||
private final Index index; | ||
private final AddGameDescriptor addGameDescriptor; | ||
private final String gameName; | ||
|
||
private Person personToEdit; | ||
private Person editedPerson; | ||
|
||
/** | ||
* @param index of the person in the filtered person list to edit | ||
* @param addGameDescriptor details to edit the person with | ||
*/ | ||
public AddGameCommand(Index index, String gameName, AddGameDescriptor addGameDescriptor) { | ||
requireNonNull(index); | ||
requireNonNull(addGameDescriptor); | ||
|
||
this.index = index; | ||
this.gameName = gameName; | ||
this.addGameDescriptor = new AddGameDescriptor(addGameDescriptor); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
assert model != null; | ||
requireNonNull(model); | ||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} | ||
|
||
personToEdit = lastShownList.get(index.getZeroBased()); | ||
Map<String, Game> gameMap = personToEdit.getGames(); | ||
|
||
if (gameMap.containsKey(gameName)) { | ||
throw new CommandException(MESSAGE_GAME_EXISTS); | ||
} | ||
|
||
Game editedGame = createNewGame(gameName, addGameDescriptor); | ||
gameMap.put(gameName, editedGame); | ||
model.setPerson(personToEdit, personToEdit); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
model.addCommandToLog(this); | ||
return new CommandResult(String.format(MESSAGE_ADD_GAME_SUCCESS, Messages.format(editedGame))); | ||
} | ||
|
||
@Override | ||
public void undo(Model model) { | ||
requireNonNull(model); | ||
|
||
Map<String, Game> gameMap = personToEdit.getGames(); | ||
gameMap.remove(gameName); | ||
|
||
model.setPerson(personToEdit, personToEdit); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Game} with the details of {@code addGameDescriptor} | ||
*/ | ||
private static Game createNewGame(String gameName, AddGameCommand.AddGameDescriptor addGameDescriptor) { | ||
Username updatedUsername = addGameDescriptor.getUsername().orElse(new Username("")); | ||
SkillLevel updatedSkillLevel = addGameDescriptor.getSkillLevel().orElse(new SkillLevel("")); | ||
Role updatedRole = addGameDescriptor.getRole().orElse(new Role("")); | ||
return new Game(gameName, updatedUsername, updatedSkillLevel, updatedRole, false); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddGameCommand)) { | ||
return false; | ||
} | ||
|
||
AddGameCommand otherEditCommand = (AddGameCommand) other; | ||
return index.equals(otherEditCommand.index) | ||
&& addGameDescriptor.equals(otherEditCommand.addGameDescriptor); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("index", index) | ||
.add("addGameDescriptor", addGameDescriptor) | ||
.toString(); | ||
} | ||
|
||
|
||
/** | ||
* Stores the details for the game to be added. | ||
*/ | ||
public static class AddGameDescriptor { | ||
private Username username; | ||
private SkillLevel skillLevel; | ||
private Role role; | ||
private boolean isFavourite; | ||
|
||
public AddGameDescriptor() {} | ||
|
||
/** | ||
* Copy constructor. | ||
*/ | ||
public AddGameDescriptor(AddGameCommand.AddGameDescriptor toCopy) { | ||
setUsername(toCopy.username); | ||
setSkillLevel(toCopy.skillLevel); | ||
setRole(toCopy.role); | ||
} | ||
|
||
/** | ||
* Returns true if at least one field is edited. | ||
*/ | ||
public boolean isAnyFieldEdited() { | ||
return CollectionUtil.isAnyNonNull(username, skillLevel, role); | ||
} | ||
public void setUsername(Username username) { | ||
this.username = username; | ||
} | ||
|
||
public Optional<Username> getUsername() { | ||
return Optional.ofNullable(username); | ||
} | ||
|
||
public void setSkillLevel(SkillLevel skillLevel) { | ||
this.skillLevel = skillLevel; | ||
} | ||
|
||
public Optional<SkillLevel> getSkillLevel() { | ||
return Optional.ofNullable(skillLevel); | ||
} | ||
|
||
public void setRole(Role role) { | ||
this.role = role; | ||
} | ||
|
||
public Optional<Role> getRole() { | ||
return Optional.ofNullable(role); | ||
} | ||
|
||
public boolean getFavouriteStatus() { | ||
return this.isFavourite; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AddGameCommand.AddGameDescriptor)) { | ||
return false; | ||
} | ||
|
||
AddGameCommand.AddGameDescriptor otherAddGameDescriptor = (AddGameCommand.AddGameDescriptor) other; | ||
return Objects.equals(username, otherAddGameDescriptor.username) | ||
&& Objects.equals(skillLevel, otherAddGameDescriptor.skillLevel) | ||
&& Objects.equals(role, otherAddGameDescriptor.role); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("username", username) | ||
.add("skillLevel", skillLevel) | ||
.add("role", role) | ||
.toString(); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/seedu/address/logic/parser/AddGameCommandParser.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,58 @@ | ||
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_GAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROLE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SKILLLEVEL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_USERNAME; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.AddGameCommand; | ||
import seedu.address.logic.commands.AddGameCommand.AddGameDescriptor; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
|
||
|
||
/** | ||
* Parses input arguments and creates a new AddGameCommand object. | ||
*/ | ||
public class AddGameCommandParser implements Parser<AddGameCommand> { | ||
|
||
@Override | ||
public AddGameCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_GAME, PREFIX_USERNAME, | ||
PREFIX_SKILLLEVEL, PREFIX_ROLE); | ||
Index index; | ||
String gameName; | ||
if (!argMultimap.getValue(PREFIX_GAME).isPresent()) { | ||
throw new ParseException("Please specify a game!"); | ||
} | ||
|
||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
gameName = argMultimap.getValue(PREFIX_GAME).get(); | ||
} catch (ParseException pe) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddGameCommand.MESSAGE_USAGE), pe); | ||
} | ||
|
||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_GAME, PREFIX_USERNAME, PREFIX_SKILLLEVEL, PREFIX_ROLE); | ||
|
||
AddGameDescriptor addGameDescriptor = new AddGameDescriptor(); | ||
|
||
if (argMultimap.getValue(PREFIX_USERNAME).isPresent()) { | ||
addGameDescriptor.setUsername(ParserUtil.parseUsername(argMultimap.getValue(PREFIX_USERNAME).get())); | ||
} | ||
if (argMultimap.getValue(PREFIX_SKILLLEVEL).isPresent()) { | ||
addGameDescriptor.setSkillLevel(ParserUtil.parseSkillLevel(argMultimap.getValue(PREFIX_SKILLLEVEL).get())); | ||
} | ||
if (argMultimap.getValue(PREFIX_ROLE).isPresent()) { | ||
addGameDescriptor.setRole(ParserUtil.parseRole(argMultimap.getValue(PREFIX_ROLE).get())); | ||
} | ||
|
||
|
||
return new AddGameCommand(index, gameName, addGameDescriptor); | ||
} | ||
} |
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