Skip to content

Commit

Permalink
Merge 458866b into a8c111f
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuXuanIan committed Apr 2, 2019
2 parents a8c111f + 458866b commit 2b10d0c
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 7 deletions.
62 changes: 62 additions & 0 deletions src/main/java/seedu/equipment/logic/commands/PutCommand.java
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.equipment.logic.commands.ListClientCommand;
import seedu.equipment.logic.commands.ListEquipmentCommand;
import seedu.equipment.logic.commands.ListWorkListCommand;
import seedu.equipment.logic.commands.PutCommand;
import seedu.equipment.logic.commands.RedoCommand;
import seedu.equipment.logic.commands.SelectCommand;
import seedu.equipment.logic.commands.SortCommand;
Expand Down Expand Up @@ -97,6 +98,9 @@ public Command parseCommand(String userInput) throws ParseException {
case UndoCommand.COMMAND_WORD:
return new UndoCommand();

case PutCommand.COMMAND_WORD:
return new PutCommandParser().parse(arguments);

case RedoCommand.COMMAND_WORD:
return new RedoCommand();

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/seedu/equipment/logic/parser/PutCommandParser.java
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());
}

}
12 changes: 12 additions & 0 deletions src/main/java/seedu/equipment/model/EquipmentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.collections.ObservableList;
import seedu.equipment.commons.util.InvalidationListenerManager;
import seedu.equipment.model.equipment.Equipment;
import seedu.equipment.model.equipment.SerialNumber;
import seedu.equipment.model.equipment.UniqueEquipmentList;
import seedu.equipment.model.tag.Tag;

Expand Down Expand Up @@ -101,6 +102,17 @@ public void addWorkList(WorkList w) {
indicateModified();
}

/**
* Put the equipment with the serialNumber into the WorkList with workListId.
* The workListId and the serialNumber must exist in the Equipment Manager.
*/
void putEquipment(WorkListId workListId, SerialNumber serialNumber) {
requireNonNull(workListId);
requireNonNull(serialNumber);
Equipment target = equipment.getEquipment(serialNumber);
worklist.addEquipment(target, workListId);
}

/**
* Replaces the given equipment {@code target} in the list with {@code editedEquipment}.
* {@code target} must exist in the equipment book.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/equipment/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javafx.collections.ObservableList;
import seedu.equipment.commons.core.GuiSettings;
import seedu.equipment.model.equipment.Equipment;
import seedu.equipment.model.equipment.SerialNumber;
import seedu.equipment.model.tag.Tag;

/**
Expand Down Expand Up @@ -73,6 +74,12 @@ public interface Model {
*/
boolean hasWorkList(WorkList workList);

/**
* Put the equipment with the serialNumber into the worklist with workListId.
* The workListId and the serialNumber must exist.
*/
void putEquipment(WorkListId workListId, SerialNumber serialNumber);

/**
* Deletes the given equipment.
* The equipment must exist in the equipment book.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/equipment/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.equipment.commons.core.LogsCenter;
import seedu.equipment.commons.util.CollectionUtil;
import seedu.equipment.model.equipment.Equipment;
import seedu.equipment.model.equipment.SerialNumber;
import seedu.equipment.model.equipment.exceptions.EquipmentNotFoundException;
import seedu.equipment.model.tag.Tag;

Expand Down Expand Up @@ -136,6 +137,11 @@ public void addWorkList(WorkList workList) {
updateFilteredWorkListList(PREDICATE_SHOW_ALL_WORKLISTS);
}

@Override
public void putEquipment(WorkListId workListId, SerialNumber serialNumber) {
versionedEquipmentManager.putEquipment(workListId, serialNumber);
}

@Override
public void resetData(ReadOnlyEquipmentManager newData) {
versionedEquipmentManager.resetData(newData);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/seedu/equipment/model/UniqueWorkListList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.equipment.model.equipment.Equipment;
import seedu.equipment.model.equipment.exceptions.DuplicateEquipmentException;
import seedu.equipment.model.equipment.exceptions.EquipmentNotFoundException;

Expand Down Expand Up @@ -50,6 +51,27 @@ public void add(WorkList toAdd) {
internalList.add(toAdd);
}

/**
* Put a certain Equipment into a worklist with the id given.
*/
public void addEquipment(Equipment e, WorkListId id) {
requireNonNull(e);
requireNonNull(id);
WorkList sampleWorkList = new WorkList("01 May 2019", "SampleName", id);
if (!contains(sampleWorkList)) {
throw new EquipmentNotFoundException();
} else {
Iterator<WorkList> ir = iterator();
int size = internalList.size();
for (int i = 0; i < size; i++) {
WorkList thisWorkList = ir.next();
if (thisWorkList.isSameWorkList(sampleWorkList)) {
thisWorkList.addEquipment(e);
}
}
}
}

/**
* Removes the equivalent WorkList from the list.
* The WorkList must exist in the list.
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/seedu/equipment/model/WorkList.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ public boolean isSameWorkList(WorkList otherWorkList) {
return true;
}

return otherWorkList.getAssignee().equals(getAssignee())
&& otherWorkList.getDate().equals(getDate())
&& otherWorkList.getId().equals(getId())
&& otherWorkList.getEquipments().equals(getEquipments());
return otherWorkList != null && otherWorkList.getId().equals(getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.collections.ObservableList;
import seedu.equipment.model.equipment.exceptions.DuplicateEquipmentException;
import seedu.equipment.model.equipment.exceptions.EquipmentNotFoundException;
import seedu.equipment.model.util.SampleDataUtil;

/**
* A list of equipments that enforces uniqueness between its elements and does not allow nulls.
Expand Down Expand Up @@ -98,6 +99,34 @@ public void setEquipments(List<Equipment> equipment) {
internalList.setAll(equipment);
}

/**
* Return the equipment which matches the serialNumber.
*/
public Equipment getEquipment(SerialNumber sr) {
requireNonNull(sr);
Equipment result = null;
Name sampleName = new Name("Anchorvale CC");
Address sampleAddress = new Address("59 Anchorvale Rd, Singapore 544965");
Date sampleDate = new Date("22 April 2019");
Phone samplePhone = new Phone("64894959");
Equipment sampleEquipment = new Equipment(sampleName, samplePhone, sampleDate,
sampleAddress, sr, SampleDataUtil.getTagSet("west"));

if (!contains(sampleEquipment)) {
throw new EquipmentNotFoundException();
} else {
int size = internalList.size();
Iterator<Equipment> ir = iterator();
for (int i = 0; i < size; i++) {
Equipment thisEquip = ir.next();
if (thisEquip.isSameEquipment(sampleEquipment)) {
result = thisEquip;
}
}
}
return result;
}

/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import seedu.equipment.model.ReadOnlyEquipmentManager;
import seedu.equipment.model.ReadOnlyUserPrefs;
import seedu.equipment.model.WorkList;
import seedu.equipment.model.WorkListId;
import seedu.equipment.model.equipment.Equipment;
import seedu.equipment.model.equipment.SerialNumber;
import seedu.equipment.model.tag.Tag;
import seedu.equipment.testutil.EquipmentBuilder;

Expand Down Expand Up @@ -145,6 +147,11 @@ public void addWorkList(WorkList workList) {
throw new AssertionError("This method should not be called.");
}

@Override
public void putEquipment(WorkListId id, SerialNumber sr) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setEquipmentManager(ReadOnlyEquipmentManager newData) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import seedu.equipment.model.ReadOnlyEquipmentManager;
import seedu.equipment.model.ReadOnlyUserPrefs;
import seedu.equipment.model.WorkList;
import seedu.equipment.model.WorkListId;
import seedu.equipment.model.equipment.Equipment;
import seedu.equipment.model.equipment.SerialNumber;
import seedu.equipment.model.tag.Tag;
import seedu.equipment.testutil.WorkListBuilder;

Expand Down Expand Up @@ -168,6 +170,11 @@ public boolean hasEquipment(Equipment equipment) {
throw new AssertionError("This method should not be called.");
}

@Override
public void putEquipment(WorkListId id, SerialNumber sr) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deleteEquipment(Equipment target) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class CommandTestUtil {
public static final String INVALID_TAG_DESC = " " + PREFIX_TAG + "hubby*"; // '*' not allowed in tags
public static final String INVALID_PM_DATE_DESC = " " + PREFIX_DATE + "2019-02";
public static final String INVALID_ASSIGNEE_DESC = " " + PREFIX_ASSIGNEE + " ";
public static final String INVALID_WORKLISTID_DESC = " " + PREFIX_ID + "-1";

public static final String PREAMBLE_WHITESPACE = "\t \r \n";
public static final String PREAMBLE_NON_EMPTY = "NonEmptyPreamble";
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/seedu/equipment/logic/commands/PutCommandTest.java
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);
}

}
Loading

0 comments on commit 2b10d0c

Please sign in to comment.