Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bug - Delivery details not updated #482

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

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.CommandResult;
import seedu.address.logic.commands.delivery.DeliveryEditCommand.DeliveryEditDescriptor;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.customer.Address;
import seedu.address.model.customer.Customer;
import seedu.address.model.customer.Email;
import seedu.address.model.customer.Name;
import seedu.address.model.customer.Phone;
import seedu.address.model.delivery.Delivery;
import seedu.address.model.delivery.DeliveryDate;
import seedu.address.model.delivery.DeliveryName;
import seedu.address.model.delivery.DeliveryStatus;
import seedu.address.model.delivery.Note;
import seedu.address.model.delivery.OrderDate;

/**
* Edits the details of an existing customer in the address book.
Expand Down Expand Up @@ -100,7 +108,11 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
} else {
model.setCustomer(customerToEdit, editedCustomer);

// update delivery associated with the edited customer
updateDelivery(model, editedCustomer);
model.updateFilteredCustomerList(PREDICATE_SHOW_ALL_CUSTOMERS);
zhonghan721 marked this conversation as resolved.
Show resolved Hide resolved

return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS,
Messages.format(editedCustomer)), true);
}
Expand All @@ -125,6 +137,51 @@ private static Customer createEditedCustomer(Customer customerToEdit,
updatedEmail, updatedAddress);
}

/**
* Updates all the deliveries associated with the customer with new customer details.
*/
protected static void updateDelivery(Model model, Customer editedCustomer) {
juliusgambe marked this conversation as resolved.
Show resolved Hide resolved
int customerId = editedCustomer.getCustomerId();
Stream<Delivery> deliveries = model.getDeliveryByCustomerId(customerId);
deliveries.forEach(d -> {
DeliveryEditDescriptor descriptor = new DeliveryEditDescriptor();
descriptor.setCustomerId(customerId);
Delivery editedDelivery = createEditedDelivery(d, descriptor, editedCustomer);
model.setDelivery(d, editedDelivery);
});
}

/**
* Creates and returns a {@code Delivery} with the customer details edited.
*
* @param deliveryToEdit {@code Delivery} which the command edits.
* @param deliveryEditDescriptor {@code editDeliveryDescriptor} details to edit the delivery with.
* @param editedCustomer {@code Customer} which the delivery is associated with.
*/
private static Delivery createEditedDelivery(Delivery deliveryToEdit, DeliveryEditDescriptor
deliveryEditDescriptor, Customer editedCustomer) {

assert deliveryToEdit != null;

DeliveryName updatedDeliveryName =
deliveryEditDescriptor.getDeliveryName().orElse(deliveryToEdit.getName());

OrderDate orderDate = deliveryToEdit.getOrderDate();

DeliveryDate updatedDeliveryDate =
deliveryEditDescriptor.getDeliveryDate().orElse(deliveryToEdit.getDeliveryDate());

DeliveryStatus updatedDeliveryStatus =
deliveryEditDescriptor.getStatus().orElse(deliveryToEdit.getStatus());

Note updatedNote = deliveryEditDescriptor.getNote().orElse(deliveryToEdit.getNote());
zhonghan721 marked this conversation as resolved.
Show resolved Hide resolved

return new Delivery(deliveryToEdit.getDeliveryId(), updatedDeliveryName, editedCustomer, orderDate,
updatedDeliveryDate,
updatedDeliveryStatus,
updatedNote);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/DeliveryBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import javafx.collections.ObservableList;
import seedu.address.commons.util.ToStringBuilder;
Expand Down Expand Up @@ -124,6 +125,10 @@ public Optional<Delivery> getById(int id) {
return deliveries.getById(id);
}

public Stream<Delivery> getByCustomerId(int id) {
return deliveries.getByCustomerId(id);
}

@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 @@ -4,6 +4,7 @@
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
Expand Down Expand Up @@ -168,6 +169,14 @@ public interface Model {
*/
Optional<Delivery> getDelivery(int id);

/**
* Returns an optional containing a delivery with the given customer id.
*
* @param id the id of the customer
* @return the stream containing deliveries with the given customer id
*/
public Stream<Delivery> getDeliveryByCustomerId(int id);

/**
* Returns true if a delivery with the same identity as {@code delivery} exists in the address book.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -385,6 +386,11 @@ public Optional<Delivery> getDelivery(int id) {
return this.deliveryBook.getById(id);
}

@Override
public Stream<Delivery> getDeliveryByCustomerId(int id) {
return this.deliveryBook.getByCustomerId(id);
}

@Override
public boolean hasDelivery(Delivery delivery) {
requireNonNull(delivery);
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/seedu/address/model/delivery/Delivery.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
private OrderDate orderDate;
private DeliveryDate deliveryDate;
private DeliveryStatus status;
private Address address; //The address of the delivery, which is automatically tied to Customer's address.
private Note note;


Expand All @@ -47,7 +46,6 @@
this.deliveryDate = deliveryDate;
this.status = status;
this.note = note;
this.address = customer.getAddress();
}


Expand All @@ -71,7 +69,6 @@
this.orderDate = orderDate;
this.deliveryDate = deliveryDate;
this.status = status;
this.address = customer.getAddress();
}

/**
Expand All @@ -93,7 +90,6 @@
this.orderDate = orderDate;
this.deliveryDate = deliveryDate;
this.status = status;
this.address = customer.getAddress();
}


Expand All @@ -119,7 +115,6 @@
this.deliveryDate = deliveryDate;
this.status = status;
this.note = note;
this.address = customer.getAddress();
}

public void setOrderDate(OrderDate orderDate) {
Expand Down Expand Up @@ -167,7 +162,7 @@
}

public Address getAddress() {
return address;
return customer.getAddress();
}

public Note getNote() {
Expand Down Expand Up @@ -219,12 +214,12 @@
&& Objects.equals(otherDelivery.note, note)
&& otherDelivery.orderDate.equals(orderDate)
&& otherDelivery.status.equals(status)
&& otherDelivery.address.equals(address);
&& otherDelivery.getAddress().equals(customer.getAddress());
}

@Override
public int hashCode() {
return Objects.hash(deliveryId, name, customer, orderDate, deliveryDate, address);
return Objects.hash(deliveryId, name, customer, orderDate, deliveryDate);

Check warning on line 222 in src/main/java/seedu/address/model/delivery/Delivery.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/delivery/Delivery.java#L222

Added line #L222 was not covered by tests
}

@Override
Expand All @@ -234,7 +229,7 @@
.add("customer", customer)
.add("orderedAt", orderDate)
.add("deliveredAt", deliveryDate)
.add("address:", address)
.add("address:", customer.getAddress())
.add("note:", Optional.ofNullable(note)
.map(n -> String.format("\n Note:%s", n)).orElse(""))
.toString();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/model/delivery/UniqueDeliveryList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -116,6 +117,16 @@ public Optional<Delivery> getById(int id) {
return Optional.empty();
}

/**
* Retrieves delivery by its id
*
* @param id The id of the delivery to be retrieved
* @return Optional containing the delivery if it exists
*/
public Stream<Delivery> getByCustomerId(int id) {
return internalList.stream().filter(d -> d.getCustomerId() == id);
}

/**
* Returns the backing list as an unmodifiable {@code ObservableList}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -291,6 +292,11 @@ public Optional<Delivery> getDelivery(int id) {
return Optional.empty();
}

@Override
public Stream<Delivery> getDeliveryByCustomerId(int id) {
throw new AssertionError("This method should not be called.");
};

@Override
public boolean hasDelivery(Delivery delivery) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -233,6 +234,11 @@ public Optional<Delivery> getDelivery(int id) {
throw new AssertionError("This method should not be called.");
}

@Override
public Stream<Delivery> getDeliveryByCustomerId(int id) {
throw new AssertionError("This method should not be called.");
};

@Override
public boolean hasDelivery(Delivery delivery) {
throw new AssertionError("This method should not be called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public void execute_allFieldsSpecifiedUnfilteredList_success() {

System.out.println(editedCustomer);
System.out.println(editCommand);
System.out.println(expectedMessage);

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()),
new DeliveryBook(model.getDeliveryBook()),
new UserPrefs(), model.getUserLoginStatus());
expectedModel.setCustomer(model.getFilteredCustomerList().get(0), editedCustomer);
CustomerEditCommand.updateDelivery(expectedModel, editedCustomer);

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel, true);
}
Expand All @@ -77,6 +79,7 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() {
new DeliveryBook(model.getDeliveryBook()),
new UserPrefs(), model.getUserLoginStatus());
expectedModel.setCustomer(lastCustomer, editedCustomer);
CustomerEditCommand.updateDelivery(expectedModel, editedCustomer);

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel, true);
}
Expand Down Expand Up @@ -117,6 +120,7 @@ public void execute_filteredList_success() {
new DeliveryBook(model.getDeliveryBook()),
new UserPrefs(), model.getUserLoginStatus());
expectedModel.setCustomer(model.getFilteredCustomerList().get(0), editedCustomer);
CustomerEditCommand.updateDelivery(expectedModel, editedCustomer);

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel, true);
}
Expand Down
Loading