Skip to content

Commit

Permalink
Merge pull request #62 from 0-yibai/edit-order-command
Browse files Browse the repository at this point in the history
Add test cases for edit order parser and re-structure edit order command
  • Loading branch information
nigel27022001 authored Mar 21, 2024
2 parents fc4bba6 + 3bc9029 commit 2623714
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/logic/commands/EditOrderCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public class EditOrderCommand extends EditCommand {

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Edits the details of the order identified "
+ "by the index number used in the displayed person list. "
+ "by the index number used in the displayed order list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: [" + PREFIX_ORDER + "INDEX (must be a positive integer)] "
+ "[" + PREFIX_PRODUCT_NAME + "PRODUCT_NAME] "
+ "[" + PREFIX_PRODUCT_QUANTITY + "PRODUCT_QUANTITY] "
+ "Example: " + COMMAND_WORD + " " + PREFIX_ORDER + "1 "
+ PREFIX_PRODUCT_NAME + "cupcake "
Expand All @@ -44,8 +43,8 @@ public class EditOrderCommand extends EditCommand {
private final EditOrderDescriptor editOrderDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param editOrderDescriptor details to edit the person with
* @param index of the order in the filtered order list to edit
* @param editOrderDescriptor details to edit the order with
*/
public EditOrderCommand(Index index, EditOrderDescriptor editOrderDescriptor) {
requireNonNull(index);
Expand All @@ -64,11 +63,12 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(Messages.MESSAGE_INVALID_ORDER_DISPLAYED_INDEX);
}

Order orderToEdit = lastShownList.get(index.getZeroBased());
assert orderToEdit != null;
Order orderToEdit = new Order(lastShownList.get(index.getZeroBased()));

Order editedOrder = model.editOrder(orderToEdit,
editOrderDescriptor.getProduct(), editOrderDescriptor.getQuantity());
//model.updateFilteredOrderList(PREDICATE_SHOW_ALL_ORDERS);
model.setOrder(orderToEdit, editedOrder);
model.updateFilteredOrderList(PREDICATE_SHOW_ALL_ORDERS);
return new CommandResult(String.format(MESSAGE_EDIT_ORDER_SUCCESS,
Messages.format(editedOrder)));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public ObservableList<Person> getPersonList() {
public ObservableList<Order> getOrderList() {
return orders.asUnmodifiableObservableList();
}

public OrderList getOrderListClass() {
return orders;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public interface Model {
*/
void setPerson(Person target, Person editedPerson);

/**
* Replaces the given order {@code target} with {@code editedPerson}.
* {@code target} must exist in the order list.
*/
void setOrder(Order target, Order editedOrder);

/**
* Adds the given order {@code newOrder} to the given person {@code person} into the orderlist.
* @param newOrder Order object to be added to the orderlist.
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,16 @@ public void addPerson(Person person) {
updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
}

@Override
public void setPerson(Person target, Person editedPerson) {
requireAllNonNull(target, editedPerson);

addressBook.setPerson(target, editedPerson);
}

@Override
public void addOrder(Order newOrder, Person person) {
newOrder.setCustomer(person);
addressBook.addOrder(newOrder);
updateFilteredOrderList(PREDICATE_SHOW_ALL_ORDERS);
}
Expand All @@ -121,11 +129,15 @@ public void addOrder(Order newOrder, Person person) {
public void deleteOrder(int id) {
addressBook.removeOrder(id);
}
@Override
public void setPerson(Person target, Person editedPerson) {
requireAllNonNull(target, editedPerson);

addressBook.setPerson(target, editedPerson);
/**
* Replaces the given order {@code target} with {@code editedPerson}.
* {@code target} must exist in the order list.
*/
public void setOrder(Order target, Order editedOrder) {
requireAllNonNull(target, editedOrder);

addressBook.setOrder(target, editedOrder);
}

@Override
Expand Down Expand Up @@ -199,7 +211,8 @@ public boolean equals(Object other) {
ModelManager otherModelManager = (ModelManager) other;
return addressBook.equals(otherModelManager.addressBook)
&& userPrefs.equals(otherModelManager.userPrefs)
&& filteredPersons.equals(otherModelManager.filteredPersons);
&& filteredPersons.equals(otherModelManager.filteredPersons)
&& filteredOrders.equals(otherModelManager.filteredOrders);
}

}
29 changes: 25 additions & 4 deletions src/main/java/seedu/address/model/order/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ public Order(Map<Product, Quantity> map) {
productMap = map;
}

/**
* Contructs an {@code Order} object with {@code map}
* @param map Mappings of Products and Quantity
*/
public Order(int id, Person customer, Map<Product, Quantity> map) {
this.id = id;
this.customer = customer;
productMap = map;
}

/**
* Contructs an {@code Order} object copied from another {@code order}
* @param order The other order to copy from
*/
public Order(Order order) {
this.id = order.getId();
this.productMap = new HashMap<>(order.getProductMap());
this.customer = order.getCustomer();
}

/**
* Sets the Order ID.
* @param id ID of the Order.
Expand Down Expand Up @@ -100,13 +120,14 @@ public int getQuantityValue(Product product) {
/**
* Sets the quantity values of the product in the order.
*
* @param currProduct Product of which quantity to be editted.
* @param currProduct Product of which quantity to be edited.
* @param newQuantity New Quantity of the specified product.
* @return Updated order.
*/
public Order changeQuantity(Product currProduct, Quantity newQuantity) {
productMap.put(currProduct, newQuantity);
return new Order(productMap);
Map<Product, Quantity> newMap = new HashMap<>(productMap);
newMap.put(currProduct, newQuantity);
return new Order(this.id, this.customer, newMap);
}

/**
Expand All @@ -116,7 +137,7 @@ public Order changeQuantity(Product currProduct, Quantity newQuantity) {
*/
public Order deleteProduct(Product product) {
productMap.remove(product);
return new Order(productMap);
return new Order(this.id, this.customer, productMap);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/model/order/OrderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import javafx.collections.ObservableList;
import seedu.address.model.exceptions.OrderNotFoundException;
import seedu.address.model.person.Person;
import seedu.address.model.person.exceptions.PersonNotFoundException;

/**
* Represents the list of active orders in the addressbook.
Expand Down Expand Up @@ -89,7 +88,7 @@ public void setOrder(Order target, Order editedOrder) {

int index = internalList.indexOf(target);
if (index == -1) {
throw new PersonNotFoundException();
throw new OrderNotFoundException();
}

internalList.set(index, editedOrder);
Expand Down Expand Up @@ -231,7 +230,9 @@ public boolean equals(Object other) {
}

OrderList otherOrderList = (OrderList) other;
return orderList.equals(otherOrderList.orderList);
//return orderList.equals(otherOrderList.orderList);
return orderList.equals(otherOrderList.orderList)
&& internalList.equals(otherOrderList.internalList);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/ui/OrderListPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class OrderListPanel extends UiPart<Region> {
/**
* Creates a {@code OrderListPanel} with the given {@code ObservableList}.
*/
public OrderListPanel(ObservableList<Order> personList) {
public OrderListPanel(ObservableList<Order> orderList) {
super(FXML);
orderListView.setItems(personList);
orderListView.setItems(orderList);
orderListView.setCellFactory(listView -> new OrderListPanel.OrderListViewCell());
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,5 @@ public PersonCard(Person person, int displayedIndex) {
.forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
person.getOrders().stream()
.forEach(order -> orders.getChildren().add(new Label("Order " + order.getId())));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public void addOrder(Order newOrder, Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setOrder(Order target, Order editedOrder) {
throw new AssertionError("This method should not be called.");
}

@Override
public Order editOrder(Order order, Product product, Quantity quantity) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_PRODUCT_DESC;
import static seedu.address.logic.commands.CommandTestUtil.INVALID_QUANTITY_DESC;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PRODUCT_DOUGHNUT;
import static seedu.address.logic.commands.CommandTestUtil.VALID_QUANTITY_ONE;
import static seedu.address.logic.commands.CommandTestUtil.VALID_QUANTITY_THREE;
import static seedu.address.logic.commands.CommandTestUtil.VALID_QUANTITY_TWO;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ORDER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRODUCT_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRODUCT_QUANTITY;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON;

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditOrderCommand;
import seedu.address.model.order.Product;
import seedu.address.model.order.Quantity;
import seedu.address.testutil.EditOrderDescriptorBuilder;

public class EditOrderCommandParserTest {
private static final String MESSAGE_INVALID_FORMAT =
String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditOrderCommand.MESSAGE_USAGE);

private EditOrderCommandParser parser = new EditOrderCommandParser();

@Test
public void parse_missingParts_failure() {
// no field specified
assertParseFailure(parser, " " + PREFIX_ORDER + "1", EditOrderCommand.MESSAGE_NOT_EDITED);

// no index and no field specified
assertParseFailure(parser, "", MESSAGE_INVALID_FORMAT);
}

@Test
public void parse_invalidValue_failure() {
assertParseFailure(parser, " " + PREFIX_ORDER + "1" + INVALID_PRODUCT_DESC,
Product.MESSAGE_CONSTRAINTS); // invalid name
assertParseFailure(parser, " " + PREFIX_ORDER + "1" + INVALID_QUANTITY_DESC,
Quantity.MESSAGE_CONSTRAINTS); // invalid phone

// invalid product followed by valid quantity
assertParseFailure(parser, " " + PREFIX_ORDER + "1" + INVALID_PRODUCT_DESC
+ PREFIX_PRODUCT_QUANTITY + " " + VALID_QUANTITY_ONE, Product.MESSAGE_CONSTRAINTS);

// multiple invalid values, but only the first invalid value is captured
assertParseFailure(parser, " " + PREFIX_ORDER + "1" + INVALID_PRODUCT_DESC
+ INVALID_QUANTITY_DESC, Product.MESSAGE_CONSTRAINTS);
}

@Test
public void parse_bothFieldsSpecified_success() {
Index targetIndex = INDEX_SECOND_PERSON;
String userInput = " " + PREFIX_ORDER + targetIndex.getOneBased() + " " + PREFIX_PRODUCT_NAME + " "
+ VALID_PRODUCT_DOUGHNUT + " " + PREFIX_PRODUCT_QUANTITY + " " + VALID_QUANTITY_TWO;

EditOrderCommand.EditOrderDescriptor descriptor = new EditOrderDescriptorBuilder()
.withProduct(VALID_PRODUCT_DOUGHNUT)
.withQuantity(Integer.parseInt(VALID_QUANTITY_TWO))
.build();

EditOrderCommand expectedCommand = new EditOrderCommand(targetIndex, descriptor);

assertParseSuccess(parser, userInput, expectedCommand);
}

@Test
public void parse_oneFieldSpecified_failure() {
// product only
Index targetIndex = INDEX_THIRD_PERSON;
String userInput = " " + PREFIX_ORDER + targetIndex.getOneBased()
+ " " + PREFIX_PRODUCT_NAME + VALID_PRODUCT_DOUGHNUT;
EditOrderCommand.EditOrderDescriptor descriptor = new EditOrderDescriptorBuilder()
.withProduct(VALID_PRODUCT_DOUGHNUT).build();
assertParseFailure(parser, userInput, EditOrderCommand.MESSAGE_NOT_EDITED);

// quantity only
userInput = " " + PREFIX_ORDER + targetIndex.getOneBased()
+ " " + PREFIX_PRODUCT_QUANTITY + VALID_QUANTITY_THREE;
descriptor = new EditOrderDescriptorBuilder()
.withQuantity(Integer.parseInt(VALID_QUANTITY_THREE)).build();
assertParseFailure(parser, userInput, EditOrderCommand.MESSAGE_NOT_EDITED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package seedu.address.testutil;

import seedu.address.logic.commands.EditOrderCommand;
import seedu.address.model.order.Product;
import seedu.address.model.order.Quantity;

/**
* A utility class to help with building EditOrderDescriptor objects.
*/
public class EditOrderDescriptorBuilder {
private EditOrderCommand.EditOrderDescriptor descriptor;

public EditOrderDescriptorBuilder() {
descriptor = new EditOrderCommand.EditOrderDescriptor();
}

public EditOrderDescriptorBuilder(EditOrderCommand.EditOrderDescriptor descriptor) {
this.descriptor = new EditOrderCommand.EditOrderDescriptor(descriptor);
}

/**
* Returns an {@code EditPersonDescriptor} with fields containing {@code person}'s details
*/
public EditOrderDescriptorBuilder(Product product, Quantity quantity) {
descriptor = new EditOrderCommand.EditOrderDescriptor();
descriptor.setProduct(product);
descriptor.setQuantity(quantity);
}

/**
* Sets the {@code Name} of the {@code EditPersonDescriptor} that we are building.
*/
public EditOrderDescriptorBuilder withProduct(String product) {
descriptor.setProduct(new Product(product));
return this;
}

/**
* Sets the {@code Name} of the {@code EditPersonDescriptor} that we are building.
*/
public EditOrderDescriptorBuilder withQuantity(int quantity) {
descriptor.setQuantity(new Quantity(quantity));
return this;
}

public EditOrderCommand.EditOrderDescriptor build() {
return descriptor;
}
}

0 comments on commit 2623714

Please sign in to comment.