Skip to content

Commit

Permalink
Merge pull request #212 from 0-yibai/refresh-order-list
Browse files Browse the repository at this point in the history
Fix incorrect outcome of edit customer command
  • Loading branch information
0-yibai authored Apr 12, 2024
2 parents a604d53 + 8716c95 commit eae570d
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ORDERS;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.ArrayList;
Expand Down Expand Up @@ -88,7 +89,9 @@ public CommandResult execute(Model model) throws CommandException {
ArrayList<Order> personToEditOrder = personToEdit.getOrders();
editedPerson.setOrders(personToEditOrder);
model.setPerson(personToEdit, editedPerson);
model.refreshCustomer(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
model.updateFilteredOrderList(PREDICATE_SHOW_ALL_ORDERS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public CommandResult execute(Model model) throws CommandException {
return new CommandResult(String.format(MESSAGE_EDIT_ORDER_SUCCESS,
Messages.format(editedOrder)));
}

/**
* Creates or updates an order based on the specified edit descriptors.
* This method decides how to edit an existing order based on the presence of product,
Expand All @@ -102,7 +103,6 @@ public CommandResult execute(Model model) throws CommandException {
* @param orderToEdit The order to be edited.
* @return The edited order with the updates applied.
*/

public Order createEditOrder(Model model, Order orderToEdit) {
Order editedOrder;
if (editOrderDescriptor.getProduct() != null && editOrderDescriptor.getQuantity() != null) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,18 @@ public int getOrderListSize() {
return activeOrders.size();
}

/**
* Refreshes the order lists to display up-to-date customer information in orders
* after customer is edited.
*
* @param oldCustomer customer before the change.
* @param newCustomer customer after the change.
*/
public void refreshCustomer(Person oldCustomer, Person newCustomer) {
activeOrders.refreshCustomer(oldCustomer, newCustomer);
completedOrders.refreshCustomer(oldCustomer, newCustomer);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,13 @@ public interface Model {
* @return the product to search for
*/
Product findProductByIndex(int id);

/**
* Refreshes the order list to display up-to-date customer information in orders
* after customer is edited.
*
* @param oldCustomer customer before the change.
* @param newCustomer customer after the change.
*/
void refreshCustomer(Person oldCustomer, Person newCustomer);
}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,7 @@ public Product findProductByIndex(int id) {
return addressBook.findProductByIndex(id);
}

public void refreshCustomer(Person oldCustomer, Person newCustomer) {
addressBook.refreshCustomer(oldCustomer, newCustomer);
}
}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/order/OrderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.model.exceptions.OrderNotFoundException;
import seedu.address.model.person.Person;

/**
* Represents the list of active orders in the addressbook.
Expand Down Expand Up @@ -220,6 +221,21 @@ public ObservableList<Order> asUnmodifiableObservableList() {
return internalUnmodifiableList;
}

/**
* Refreshes the order list to display up-to-date customer information in orders
* after customer is edited.
* @param oldCustomer customer before the change.
* @param newCustomer customer after the change.
*/
public void refreshCustomer(Person oldCustomer, Person newCustomer) {
for (int i = 0; i < internalList.size(); i++) {
if (internalList.get(i).getCustomer().isSamePerson(oldCustomer)) {
internalList.get(i).setCustomer(newCustomer);
internalList.set(i, new Order(internalList.get(i)));
}
}
}

@Override
public Iterator<Order> iterator() {
return internalList.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ public void completeOrder(int id) {
public boolean orderIdExists(int id) {
throw new AssertionError("This method should not be called.");
}

@Override
public void refreshCustomer(Person oldCustomer, Person newCustomer) {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ public void completeOrder(int id) {
public boolean orderIdExists(int id) {
throw new AssertionError("This method should not be called.");
}

@Override
public void refreshCustomer(Person oldCustomer, Person newCustomer) {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ public void completeOrder(int id) {
public boolean orderIdExists(int id) {
throw new AssertionError("This method should not be called.");
}

@Override
public void refreshCustomer(Person oldCustomer, Person newCustomer) {
throw new AssertionError("This method should not be called.");
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/seedu/address/model/order/OrderListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalOrders.CUPCAKES_ONLY;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BENSON;

import org.junit.jupiter.api.Test;

import seedu.address.model.exceptions.OrderNotFoundException;
import seedu.address.model.person.Person;
import seedu.address.testutil.OrderBuilder;
import seedu.address.testutil.PersonBuilder;

public class OrderListTest {

Expand Down Expand Up @@ -150,4 +154,18 @@ public void getOrder_nullOrder_throwsNullPointerException() {
public void getOrder_orderDoesNotExist_throwsOrderNotFoundException() {
assertThrows(OrderNotFoundException.class, () -> orderList.getOrder(1));
}

@Test
public void refreshCustomer_updatesInternalList() {
Person oldCustomer = new PersonBuilder(ALICE).build();
Person newCustomer = new PersonBuilder(BENSON).build();

Order order = new OrderBuilder(CUPCAKES_ONLY).build();
order.setCustomer(oldCustomer);

orderList.addOrder(order);
orderList.refreshCustomer(oldCustomer, newCustomer);

assertEquals(newCustomer, orderList.getOrder(CUPCAKES_ONLY.getId()).getCustomer());
}
}

0 comments on commit eae570d

Please sign in to comment.