Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2324S1#110 from jianrong7/feat/editcust
Browse files Browse the repository at this point in the history
Add edit customer feature
  • Loading branch information
jianrong7 authored Oct 30, 2023
2 parents 49b31f3 + 60bbc09 commit a512a15
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
/**
* Edits the details of an existing customer in the budget book.
*/
public class EditCommand extends Command {
public class EditCustomerCommand extends Command {

public static final String COMMAND_WORD = "edit";
public static final String COMMAND_WORD = "editcust";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the customer identified "
+ "by the index number used in the displayed customer list. "
Expand All @@ -59,7 +59,7 @@ public class EditCommand extends Command {
* @param index of the customer in the filtered customer list to edit
* @param editCustomerDescriptor details to edit the customer with
*/
public EditCommand(Index index, EditCustomerDescriptor editCustomerDescriptor) {
public EditCustomerCommand(Index index, EditCustomerDescriptor editCustomerDescriptor) {
requireNonNull(index);
requireNonNull(editCustomerDescriptor);

Expand Down Expand Up @@ -112,13 +112,13 @@ public boolean equals(Object other) {
}

// instanceof handles nulls
if (!(other instanceof EditCommand)) {
if (!(other instanceof EditCustomerCommand)) {
return false;
}

EditCommand otherEditCommand = (EditCommand) other;
return index.equals(otherEditCommand.index)
&& editCustomerDescriptor.equals(otherEditCommand.editCustomerDescriptor);
EditCustomerCommand otherEditCustomerCommand = (EditCustomerCommand) other;
return index.equals(otherEditCustomerCommand.index)
&& editCustomerDescriptor.equals(otherEditCustomerCommand.editCustomerDescriptor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCustomerCommand;
import seedu.address.logic.commands.DeletePropertyCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCustomerCommand;
import seedu.address.logic.commands.EditPropertyCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FilterCustomerCommand;
Expand Down Expand Up @@ -64,8 +64,8 @@ public Command parseCommand(String userInput) throws ParseException {
case AddPropertyCommand.COMMAND_WORD:
return new AddPropertyCommandParser().parse(arguments);

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);
case EditCustomerCommand.COMMAND_WORD:
return new EditCustomerCommandParser().parse(arguments);

case EditPropertyCommand.COMMAND_WORD:
return new EditPropertyCommandParser().parse(arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditCustomerDescriptor;
import seedu.address.logic.commands.EditCustomerCommand;
import seedu.address.logic.commands.EditCustomerCommand.EditCustomerDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new EditCommand object
*/
public class EditCommandParser implements Parser<EditCommand> {
public class EditCustomerCommandParser implements Parser<EditCustomerCommand> {

/**
* Parses the given {@code String} of arguments in the context of the EditCommand
* and returns an EditCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public EditCommand parse(String args) throws ParseException {
public EditCustomerCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_BUDGET, PREFIX_TAG);
Expand All @@ -39,7 +39,8 @@ public EditCommand parse(String args) throws ParseException {
try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCustomerCommand.MESSAGE_USAGE),
pe);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_BUDGET);
Expand All @@ -61,10 +62,10 @@ public EditCommand parse(String args) throws ParseException {
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editCustomerDescriptor::setTags);

if (!editCustomerDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
throw new ParseException(EditCustomerCommand.MESSAGE_NOT_EDITED);
}

return new EditCommand(index, editCustomerDescriptor);
return new EditCustomerCommand(index, editCustomerDescriptor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public class CommandTestUtil {
public static final String PREAMBLE_WHITESPACE = "\t \r \n";
public static final String PREAMBLE_NON_EMPTY = "NonEmptyPreamble";

public static final EditCommand.EditCustomerDescriptor DESC_AMY;
public static final EditCommand.EditCustomerDescriptor DESC_BOB;
public static final EditCustomerCommand.EditCustomerDescriptor DESC_AMY;
public static final EditCustomerCommand.EditCustomerDescriptor DESC_BOB;

static {
DESC_AMY = new EditCustomerDescriptorBuilder().withName(VALID_NAME_AMY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.EditCommand.EditCustomerDescriptor;
import seedu.address.logic.commands.EditCustomerCommand.EditCustomerDescriptor;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
Expand All @@ -33,24 +33,24 @@
/**
* Contains integration tests (interaction with the Model) and unit tests for EditCommand.
*/
public class EditCommandTest {
public class EditCustomerCommandTest {

private Model model = new ModelManager(getTypicalAddressBook(), getTypicalPropertyBook(), new UserPrefs());

@Test
public void execute_allFieldsSpecifiedUnfilteredList_success() {
Customer editedCustomer = new CustomerBuilder().build();
EditCustomerDescriptor descriptor = new EditCustomerDescriptorBuilder(editedCustomer).build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_CUSTOMER, descriptor);
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(INDEX_FIRST_CUSTOMER, descriptor);

String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_CUSTOMER_SUCCESS,
String expectedMessage = String.format(EditCustomerCommand.MESSAGE_EDIT_CUSTOMER_SUCCESS,
Messages.format(editedCustomer));

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()),
model.getPropertyBook(), new UserPrefs());
expectedModel.setCustomer(model.getFilteredCustomerList().get(0), editedCustomer);

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
assertCommandSuccess(editCustomerCommand, model, expectedMessage, expectedModel);
}

@Test
Expand All @@ -64,30 +64,31 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() {

EditCustomerDescriptor descriptor = new EditCustomerDescriptorBuilder().withName(VALID_NAME_BOB)
.withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_BIG).build();
EditCommand editCommand = new EditCommand(indexLastCustomer, descriptor);
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(indexLastCustomer, descriptor);

String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_CUSTOMER_SUCCESS,
String expectedMessage = String.format(EditCustomerCommand.MESSAGE_EDIT_CUSTOMER_SUCCESS,
Messages.format(editedCustomer));

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()),
new PropertyBook(model.getPropertyBook()), new UserPrefs());
expectedModel.setCustomer(lastCustomer, editedCustomer);

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
assertCommandSuccess(editCustomerCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_noFieldSpecifiedUnfilteredList_success() {
EditCommand editCommand = new EditCommand(INDEX_FIRST_CUSTOMER, new EditCustomerDescriptor());
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(INDEX_FIRST_CUSTOMER,
new EditCustomerDescriptor());
Customer editedCustomer = model.getFilteredCustomerList().get(INDEX_FIRST_CUSTOMER.getZeroBased());

String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_CUSTOMER_SUCCESS,
String expectedMessage = String.format(EditCustomerCommand.MESSAGE_EDIT_CUSTOMER_SUCCESS,
Messages.format(editedCustomer));

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()),
new PropertyBook(model.getPropertyBook()), new UserPrefs());

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
assertCommandSuccess(editCustomerCommand, model, expectedMessage, expectedModel);
}

@Test
Expand All @@ -96,26 +97,26 @@ public void execute_filteredList_success() {

Customer customerInFilteredList = model.getFilteredCustomerList().get(INDEX_FIRST_CUSTOMER.getZeroBased());
Customer editedCustomer = new CustomerBuilder(customerInFilteredList).withName(VALID_NAME_BOB).build();
EditCommand editCommand = new EditCommand(INDEX_FIRST_CUSTOMER,
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(INDEX_FIRST_CUSTOMER,
new EditCustomerDescriptorBuilder().withName(VALID_NAME_BOB).build());

String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_CUSTOMER_SUCCESS,
String expectedMessage = String.format(EditCustomerCommand.MESSAGE_EDIT_CUSTOMER_SUCCESS,
Messages.format(editedCustomer));

Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()),
new PropertyBook(model.getPropertyBook()), new UserPrefs());
expectedModel.setCustomer(model.getFilteredCustomerList().get(0), editedCustomer);

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
assertCommandSuccess(editCustomerCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_duplicateCustomerUnfilteredList_failure() {
Customer firstCustomer = model.getFilteredCustomerList().get(INDEX_FIRST_CUSTOMER.getZeroBased());
EditCustomerDescriptor descriptor = new EditCustomerDescriptorBuilder(firstCustomer).build();
EditCommand editCommand = new EditCommand(INDEX_SECOND_CUSTOMER, descriptor);
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(INDEX_SECOND_CUSTOMER, descriptor);

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_CUSTOMER);
assertCommandFailure(editCustomerCommand, model, EditCustomerCommand.MESSAGE_DUPLICATE_CUSTOMER);
}

@Test
Expand All @@ -124,19 +125,19 @@ public void execute_duplicateCustomerFilteredList_failure() {

// edit customer in filtered list into a duplicate in budget book
Customer customerInList = model.getAddressBook().getCustomerList().get(INDEX_SECOND_CUSTOMER.getZeroBased());
EditCommand editCommand = new EditCommand(INDEX_FIRST_CUSTOMER,
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(INDEX_FIRST_CUSTOMER,
new EditCustomerDescriptorBuilder(customerInList).build());

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_CUSTOMER);
assertCommandFailure(editCustomerCommand, model, EditCustomerCommand.MESSAGE_DUPLICATE_CUSTOMER);
}

@Test
public void execute_invalidCustomerIndexUnfilteredList_failure() {
Index outOfBoundIndex = Index.fromOneBased(model.getFilteredCustomerList().size() + 1);
EditCustomerDescriptor descriptor = new EditCustomerDescriptorBuilder().withName(VALID_NAME_BOB).build();
EditCommand editCommand = new EditCommand(outOfBoundIndex, descriptor);
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(outOfBoundIndex, descriptor);

assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_CUSTOMER_DISPLAYED_INDEX);
assertCommandFailure(editCustomerCommand, model, Messages.MESSAGE_INVALID_CUSTOMER_DISPLAYED_INDEX);
}

/**
Expand All @@ -150,19 +151,19 @@ public void execute_invalidCustomerIndexFilteredList_failure() {
// ensures that outOfBoundIndex is still in bounds of budget book list
assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getCustomerList().size());

EditCommand editCommand = new EditCommand(outOfBoundIndex,
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(outOfBoundIndex,
new EditCustomerDescriptorBuilder().withName(VALID_NAME_BOB).build());

assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_CUSTOMER_DISPLAYED_INDEX);
assertCommandFailure(editCustomerCommand, model, Messages.MESSAGE_INVALID_CUSTOMER_DISPLAYED_INDEX);
}

@Test
public void equals() {
final EditCommand standardCommand = new EditCommand(INDEX_FIRST_CUSTOMER, DESC_AMY);
final EditCustomerCommand standardCommand = new EditCustomerCommand(INDEX_FIRST_CUSTOMER, DESC_AMY);

// same values -> returns true
EditCustomerDescriptor copyDescriptor = new EditCustomerDescriptor(DESC_AMY);
EditCommand commandWithSameValues = new EditCommand(INDEX_FIRST_CUSTOMER, copyDescriptor);
EditCustomerCommand commandWithSameValues = new EditCustomerCommand(INDEX_FIRST_CUSTOMER, copyDescriptor);
assertTrue(standardCommand.equals(commandWithSameValues));

// same object -> returns true
Expand All @@ -175,20 +176,20 @@ public void equals() {
assertFalse(standardCommand.equals(new ClearCommand()));

// different index -> returns false
assertFalse(standardCommand.equals(new EditCommand(INDEX_SECOND_CUSTOMER, DESC_AMY)));
assertFalse(standardCommand.equals(new EditCustomerCommand(INDEX_SECOND_CUSTOMER, DESC_AMY)));

// different descriptor -> returns false
assertFalse(standardCommand.equals(new EditCommand(INDEX_FIRST_CUSTOMER, DESC_BOB)));
assertFalse(standardCommand.equals(new EditCustomerCommand(INDEX_FIRST_CUSTOMER, DESC_BOB)));
}

@Test
public void toStringMethod() {
Index index = Index.fromOneBased(1);
EditCustomerDescriptor editCustomerDescriptor = new EditCommand.EditCustomerDescriptor();
EditCommand editCommand = new EditCommand(index, editCustomerDescriptor);
String expected = EditCommand.class.getCanonicalName() + "{index=" + index + ", editCustomerDescriptor="
EditCustomerDescriptor editCustomerDescriptor = new EditCustomerCommand.EditCustomerDescriptor();
EditCustomerCommand editCustomerCommand = new EditCustomerCommand(index, editCustomerDescriptor);
String expected = EditCustomerCommand.class.getCanonicalName() + "{index=" + index + ", editCustomerDescriptor="
+ editCustomerDescriptor + "}";
assertEquals(expected, editCommand.toString());
assertEquals(expected, editCustomerCommand.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.EditCommand.EditCustomerDescriptor;
import seedu.address.logic.commands.EditCustomerCommand.EditCustomerDescriptor;
import seedu.address.testutil.EditCustomerDescriptorBuilder;

public class EditCustomerDescriptorTest {

@Test
public void equals() {
// same values -> returns true
EditCommand.EditCustomerDescriptor descriptorWithSameValues = new EditCustomerDescriptor(DESC_AMY);
EditCustomerCommand.EditCustomerDescriptor descriptorWithSameValues = new EditCustomerDescriptor(DESC_AMY);
assertTrue(DESC_AMY.equals(descriptorWithSameValues));

// same object -> returns true
Expand All @@ -37,7 +37,7 @@ public void equals() {
assertFalse(DESC_AMY.equals(DESC_BOB));

// different name -> returns false
EditCommand.EditCustomerDescriptor editedAmy = new EditCustomerDescriptorBuilder(DESC_AMY)
EditCustomerCommand.EditCustomerDescriptor editedAmy = new EditCustomerDescriptorBuilder(DESC_AMY)
.withName(VALID_NAME_BOB).build();
assertFalse(DESC_AMY.equals(editedAmy));

Expand All @@ -61,7 +61,7 @@ public void equals() {
@Test
public void toStringMethod() {
EditCustomerDescriptor editCustomerDescriptor = new EditCustomerDescriptor();
String expected = EditCommand.EditCustomerDescriptor.class.getCanonicalName() + "{name="
String expected = EditCustomerCommand.EditCustomerDescriptor.class.getCanonicalName() + "{name="
+ editCustomerDescriptor.getName().orElse(null) + ", phone="
+ editCustomerDescriptor.getPhone().orElse(null) + ", email="
+ editCustomerDescriptor.getEmail().orElse(null) + ", budget="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.DeleteCustomerCommand;
import seedu.address.logic.commands.DeletePropertyCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCustomerCommand;
import seedu.address.logic.commands.EditPropertyCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
Expand Down Expand Up @@ -78,10 +78,10 @@ public void parseCommand_delprop() throws Exception {
@Test
public void parseCommand_edit() throws Exception {
Customer customer = new CustomerBuilder().build();
EditCommand.EditCustomerDescriptor descriptor = new EditCustomerDescriptorBuilder(customer).build();
EditCommand command = (EditCommand) parser.parseCommand(EditCommand.COMMAND_WORD + " "
EditCustomerCommand.EditCustomerDescriptor descriptor = new EditCustomerDescriptorBuilder(customer).build();
EditCustomerCommand command = (EditCustomerCommand) parser.parseCommand(EditCustomerCommand.COMMAND_WORD + " "
+ INDEX_FIRST_CUSTOMER.getOneBased() + " " + CustomerUtil.getEditCustomerDescriptorDetails(descriptor));
assertEquals(new EditCommand(INDEX_FIRST_CUSTOMER, descriptor), command);
assertEquals(new EditCustomerCommand(INDEX_FIRST_CUSTOMER, descriptor), command);
}
@Test
public void parseCommand_editprop() throws Exception {
Expand Down
Loading

0 comments on commit a512a15

Please sign in to comment.