Skip to content

Commit

Permalink
Fix remaining testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
aureliony committed Feb 27, 2024
1 parent 64030dd commit 9772321
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
9 changes: 0 additions & 9 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,10 @@ public Optional<Address> getAddress() {
return Optional.ofNullable(address);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setTags(Tags tags) {
this.tags = tags;
}

/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Tags> getTags() {
return Optional.ofNullable(tags);
}
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.fields.optional.Tags;

/**
* Parses input arguments and creates a new EditCommand object
Expand Down Expand Up @@ -52,9 +57,7 @@ public EditCommand parse(String args) throws ParseException {
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
if (argMultimap.getValue(PREFIX_TAG).isPresent()) {
editPersonDescriptor.setTags(ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);

if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand All @@ -63,4 +66,19 @@ public EditCommand parse(String args) throws ParseException {
return new EditCommand(index, editPersonDescriptor);
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty.
* If {@code tags} contain only one element which is an empty string, it will be parsed into a
* {@code Set<Tag>} containing zero tags.
*/
private Optional<Tags> parseTagsForEdit(Collection<String> tags) throws ParseException {
assert tags != null;

if (tags.isEmpty()) {
return Optional.empty();
}
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
return Optional.of(ParserUtil.parseTags(tagSet));
}

}
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Person {
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Tags tags) {
requireAllNonNull(name, phone, email, address);
requireAllNonNull(name, phone, email, address, tags);
this.name = name;
this.phone = phone;
this.email = email;
Expand All @@ -45,12 +45,12 @@ public Person(Name name, Phone phone, Email email, Address address, Tags tags) {
private Person(@JsonProperty("name") String name, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") String[] tagNames) {
requireAllNonNull(name, phone, email, address);
requireAllNonNull(name, phone, email, address, tagNames);
this.name = new Name(name);
this.phone = new Phone(phone);
this.email = new Email(email);
this.address = new Address(address);
this.tags = tagNames == null ? null : new Tags(tagNames);
this.tags = new Tags(tagNames);
}

public Name getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public Tags(Tag... tags) {
* @param tagNames A list of strings, or null.
*/
public Tags(String... tagNames) {
for (String name : tagNames) {
if (name.isEmpty()) System.out.println(tagNames.length);
}
this.tags = Stream.of(tagNames)
.map(Tag::new)
.collect(Collectors.toUnmodifiableSet());
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/seedu/address/testutil/PersonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class PersonBuilder {
public static final String DEFAULT_PHONE = "85355255";
public static final String DEFAULT_EMAIL = "[email protected]";
public static final String DEFAULT_ADDRESS = "123, Jurong West Ave 6, #08-111";
public static final String[] DEFAULT_TAGS = { };

private Name name;
private Phone phone;
Expand All @@ -32,7 +33,7 @@ public PersonBuilder() {
phone = new Phone(DEFAULT_PHONE);
email = new Email(DEFAULT_EMAIL);
address = new Address(DEFAULT_ADDRESS);
// tags = new Tags(null);
tags = new Tags(DEFAULT_TAGS);
}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/seedu/address/testutil/PersonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public static String getAddCommand(Person person) {
*/
public static String getPersonDetails(Person person) {
StringBuilder sb = new StringBuilder();
sb.append(PREFIX_NAME + person.getName().toString() + " ");
sb.append(PREFIX_PHONE + person.getPhone().toString() + " ");
sb.append(PREFIX_EMAIL + person.getEmail().toString() + " ");
sb.append(PREFIX_ADDRESS + person.getAddress().toString() + " ");
sb.append(PREFIX_NAME).append(person.getName().toString()).append(" ");
sb.append(PREFIX_PHONE).append(person.getPhone().toString()).append(" ");
sb.append(PREFIX_EMAIL).append(person.getEmail().toString()).append(" ");
sb.append(PREFIX_ADDRESS).append(person.getAddress().toString()).append(" ");
person.getTags().stream().forEach(
s -> sb.append(PREFIX_TAG + s.tagName + " ")
s -> sb.append(PREFIX_TAG).append(s.tagName).append(" ")
);
return sb.toString();
}
Expand All @@ -46,7 +46,10 @@ public static String getEditPersonDescriptorDetails(EditCommand.EditPersonDescri
descriptor.getPhone().ifPresent(phone -> sb.append(PREFIX_PHONE).append(phone).append(" "));
descriptor.getEmail().ifPresent(email -> sb.append(PREFIX_EMAIL).append(email).append(" "));
descriptor.getAddress().ifPresent(address -> sb.append(PREFIX_ADDRESS).append(address).append(" "));
descriptor.getTags().ifPresent(tags -> tags.stream().forEach(s -> sb.append(PREFIX_TAG).append(s.tagName).append(" ")));
if (descriptor.getTags().isPresent()) {
sb.append(PREFIX_TAG);
descriptor.getTags().get().stream().forEach(s -> sb.append(PREFIX_TAG).append(s.tagName).append(" "));
}
return sb.toString();
}
}

0 comments on commit 9772321

Please sign in to comment.