forked from nus-cs2103-AY1819S2/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
328 additions
and
7 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/seedu/equipment/logic/commands/PutCommand.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,62 @@ | ||
package seedu.equipment.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.equipment.logic.CommandHistory; | ||
import seedu.equipment.logic.commands.exceptions.CommandException; | ||
import seedu.equipment.logic.parser.CliSyntax; | ||
import seedu.equipment.model.Model; | ||
import seedu.equipment.model.WorkListId; | ||
import seedu.equipment.model.equipment.SerialNumber; | ||
|
||
/** | ||
* Put an equipment into a WorkList in the Equipment Manager. | ||
*/ | ||
public class PutCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "put"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Put an equipment into a WorkList in the Equipment Manager. " | ||
+ "Parameters: " | ||
+ CliSyntax.PREFIX_ID + "WORKLISTID " | ||
+ CliSyntax.PREFIX_SERIALNUMBER + "EQUIPMENT \n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ CliSyntax.PREFIX_ID + "1 " | ||
+ CliSyntax.PREFIX_SERIALNUMBER + "A008866X "; | ||
|
||
public static final String MESSAGE_SUCCESS = "The equipment added into the worklist: %1$s"; | ||
public static final String MESSAGE_EQUIPMENT_NOT_FOUND = "This equipment serial number is not found."; | ||
public static final String MESSAGE_WORKLIST_NOT_FOUND = "This worklist id is not found."; | ||
|
||
private final WorkListId id; | ||
private final SerialNumber sr; | ||
|
||
/** | ||
* Creates a PutCommand. | ||
*/ | ||
public PutCommand(WorkListId id, SerialNumber sr) { | ||
requireNonNull(id); | ||
requireNonNull(sr); | ||
this.id = id; | ||
this.sr = sr; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model, CommandHistory history) throws CommandException { | ||
requireNonNull(model); | ||
|
||
model.putEquipment(id, sr); | ||
|
||
model.commitEquipmentManager(); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, id, sr)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof PutCommand // instanceof handles nulls | ||
&& id.equals(((PutCommand) other).id) | ||
&& sr.equals(((PutCommand) other).sr)); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/main/java/seedu/equipment/logic/parser/PutCommandParser.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,46 @@ | ||
package seedu.equipment.logic.parser; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.equipment.commons.core.Messages; | ||
import seedu.equipment.logic.commands.PutCommand; | ||
import seedu.equipment.logic.parser.exceptions.ParseException; | ||
import seedu.equipment.model.WorkListId; | ||
import seedu.equipment.model.equipment.SerialNumber; | ||
|
||
/** | ||
* Parses input arguments and creates a new PutCommand object | ||
*/ | ||
public class PutCommandParser implements Parser<PutCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the PutCommand | ||
* and returns a PutCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public PutCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, CliSyntax.PREFIX_ID, CliSyntax.PREFIX_SERIALNUMBER); | ||
|
||
if (!arePrefixesPresent(argMultimap, CliSyntax.PREFIX_ID, CliSyntax.PREFIX_SERIALNUMBER) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format( | ||
Messages.MESSAGE_INVALID_COMMAND_FORMAT, PutCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
SerialNumber serialNumber = ParserUtil.parseSerialNumber(argMultimap.getValue( | ||
CliSyntax.PREFIX_SERIALNUMBER).get()); | ||
WorkListId id = ParserUtil.parseWorkListId(argMultimap.getValue(CliSyntax.PREFIX_ID).get()); | ||
|
||
return new PutCommand(id, serialNumber); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values 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
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
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
24 changes: 24 additions & 0 deletions
24
src/test/java/seedu/equipment/logic/commands/PutCommandTest.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,24 @@ | ||
package seedu.equipment.logic.commands; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import seedu.equipment.logic.CommandHistory; | ||
|
||
public class PutCommandTest { | ||
|
||
private static final CommandHistory EMPTY_COMMAND_HISTORY = new CommandHistory(); | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
private CommandHistory commandHistory = new CommandHistory(); | ||
|
||
@Test | ||
public void constructor_nullWorkListIdNullSerialNumber_throwsNullPointerException() { | ||
thrown.expect(NullPointerException.class); | ||
new PutCommand(null, null); | ||
} | ||
|
||
} |
Oops, something went wrong.