Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
There were some errors in my code which I could not add the edit

Changed some files to fix the code
  • Loading branch information
Darren-Tung committed Oct 9, 2024
1 parent 188a6e7 commit 8b2048c
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 18 deletions.
18 changes: 16 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
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_NOTE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

Expand Down Expand Up @@ -44,6 +45,7 @@ public class EditCommand extends Command {
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_NOTE + "NOTE] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
Expand Down Expand Up @@ -100,7 +102,7 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Note updatedNote = personToEdit.getNote(); // edit command does not allow editing remarks
Note updatedNote = editPersonDescriptor.getNote().orElse(personToEdit.getNote());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedNote, updatedTags);
Expand Down Expand Up @@ -139,6 +141,7 @@ public static class EditPersonDescriptor {
private Phone phone;
private Email email;
private Address address;
private Note note;
private Set<Tag> tags;

public EditPersonDescriptor() {}
Expand All @@ -152,14 +155,15 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setPhone(toCopy.phone);
setEmail(toCopy.email);
setAddress(toCopy.address);
setNote(toCopy.note);
setTags(toCopy.tags);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags);
return CollectionUtil.isAnyNonNull(name, phone, email, address, note, tags);
}

public void setName(Name name) {
Expand Down Expand Up @@ -194,6 +198,14 @@ public Optional<Address> getAddress() {
return Optional.ofNullable(address);
}

public void setNote(Note note) {
this.note = note;
}

public Optional<Note> getNote() {
return Optional.ofNullable(note);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
Expand Down Expand Up @@ -227,6 +239,7 @@ public boolean equals(Object other) {
&& Objects.equals(phone, otherEditPersonDescriptor.phone)
&& Objects.equals(email, otherEditPersonDescriptor.email)
&& Objects.equals(address, otherEditPersonDescriptor.address)
&& Objects.equals(note, otherEditPersonDescriptor.note)
&& Objects.equals(tags, otherEditPersonDescriptor.tags);
}

Expand All @@ -237,6 +250,7 @@ public String toString() {
.add("phone", phone)
.add("email", email)
.add("address", address)
.add("note", note)
.add("tags", tags)
.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
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_NOTE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Set;
Expand All @@ -31,15 +32,15 @@ public class AddCommandParser implements Parser<AddCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME,
PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_NOTE, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_NOTE)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_NOTE, PREFIX_ADDRESS);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
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_NOTE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Collection;
Expand All @@ -25,14 +26,16 @@
public class EditCommandParser implements Parser<EditCommand> {

/**
* Parses the given {@code String} of arguments in the context of the EditCommand
* 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 {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_NOTE, PREFIX_TAG);

Index index;

Expand All @@ -42,7 +45,7 @@ public EditCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_NOTE);

EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();

Expand All @@ -58,6 +61,9 @@ 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_NOTE).isPresent()) {
editPersonDescriptor.setNote(ParserUtil.parseNote(argMultimap.getValue(PREFIX_NOTE).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);

if (!editPersonDescriptor.isAnyFieldEdited()) {
Expand All @@ -68,8 +74,10 @@ public EditCommand parse(String args) throws ParseException {
}

/**
* 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
* 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<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws ParseException {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Note;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -95,6 +96,21 @@ public static Email parseEmail(String email) throws ParseException {
return new Email(trimmedEmail);
}

/**
* Parses a {@code String email} into an {@code Email}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code email} is invalid.
*/
public static Note parseNote(String note) throws ParseException {
requireNonNull(note);
String trimmedNote = note.trim();
if (!Note.isValidNote(trimmedNote)) {
throw new ParseException(Note.MESSAGE_CONSTRAINTS);
}
return new Note(trimmedNote);
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/person/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
public class Note {
public final String value;

public static final String MESSAGE_CONSTRAINTS =
"Notes should not be more than 200 characters long and it should not be blank";

/**
* Constructs a {@code Note}.
*
Expand All @@ -15,6 +18,13 @@ public Note(String note) {
value = note;
}

/**
* Returns true if a given string is less than 200 characters long
*/
public static boolean isValidNote(String test) {
return (test.length() <= 200) && (!test.isEmpty());
}

@Override
public String toString() {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"name": "Valid Person",
"phone": "9482424",
"email": "[email protected]",
"address": "4th street"
"address": "4th street",
"note": ""
}, {
"name": "Person With Invalid Phone Field",
"phone": "948asdf2424",
"email": "[email protected]",
"address": "4th street"
"address": "4th street",
"note": ""
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"name": "Person with invalid name field: Ha!ns Mu@ster",
"phone": "9482424",
"email": "[email protected]",
"address": "4th street"
"address": "4th street",
"note": ""
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"name": "Hans Muster",
"phone": "9482424",
"email": "invalid@email!3e",
"address": "4th street"
"address": "4th street",
"note": ""
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import seedu.address.logic.commands.NoteCommand;
import seedu.address.model.person.Note;

public class RemarkCommandParserTest {
public class NoteCommandParserTest {
private NoteCommandParser parser = new NoteCommandParser();
private final String nonEmptyRemark = "Some remark.";

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/address/testutil/PersonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public PersonBuilder withNote(String note) {
}

public Person build() {
return new Person(name, phone, email, address, tags);
return new Person(name, phone, email, address, note, tags);
}

}

0 comments on commit 8b2048c

Please sign in to comment.