Skip to content

Commit

Permalink
Merge pull request #42 from McNaBry/add-fields-optional
Browse files Browse the repository at this point in the history
Make fields for `AddCommand` optional
  • Loading branch information
CJ-Lee01 authored Oct 19, 2023
2 parents 14ba23c + 327ab8f commit 4bfdba6
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 255 deletions.
8 changes: 4 additions & 4 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public static String format(Contact contact) {
.append("; Id: ")
.append(contact.getId())
.append("; Phone: ")
.append(contact.getPhone())
.append(contact.getPhone().map(p -> p.value).orElse("(none)"))
.append("; Email: ")
.append(contact.getEmail())
.append(contact.getEmail().map(e -> e.value).orElse("(none)"))
.append("; Url: ")
.append(contact.getUrl())
.append(contact.getUrl().map(u -> u.value).orElse("(none)"))
.append("; Address: ")
.append(contact.getAddress())
.append(contact.getAddress().map(a -> a.value).orElse("(none)"))
.append("; Tags: ");
contact.getTags().forEach(builder::append);
return builder.toString();
Expand Down
60 changes: 51 additions & 9 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import static seedu.address.logic.parser.CliSyntax.FLAG_EMAIL;
import static seedu.address.logic.parser.CliSyntax.FLAG_ID;
import static seedu.address.logic.parser.CliSyntax.FLAG_NAME;
import static seedu.address.logic.parser.CliSyntax.FLAG_ORGANIZATION;
import static seedu.address.logic.parser.CliSyntax.FLAG_ORGANIZATION_ID;
import static seedu.address.logic.parser.CliSyntax.FLAG_PHONE;
import static seedu.address.logic.parser.CliSyntax.FLAG_POSITION;
import static seedu.address.logic.parser.CliSyntax.FLAG_RECRUITER;
import static seedu.address.logic.parser.CliSyntax.FLAG_STATUS;
import static seedu.address.logic.parser.CliSyntax.FLAG_TAG;
import static seedu.address.logic.parser.CliSyntax.FLAG_URL;

import java.util.logging.Logger;

Expand All @@ -24,21 +30,57 @@ public class AddCommand extends Command {

public static final String COMMAND_WORD = "add";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a contact to the address book. "
public static final String MESSAGE_ORGANIZATION_USAGE = "Adds an organization. "
+ "Parameters: "
+ FLAG_ORGANIZATION + " "
+ FLAG_NAME + " NAME "
+ FLAG_ID + " ID "
+ FLAG_PHONE + " PHONE "
+ FLAG_EMAIL + " EMAIL "
+ FLAG_ADDRESS + " ADDRESS "
+ "[" + FLAG_ID + " ID] "
+ "[" + FLAG_PHONE + " PHONE] "
+ "[" + FLAG_EMAIL + " EMAIL] "
+ "[" + FLAG_URL + " URL] "
+ "[" + FLAG_ADDRESS + " ADDRESS] "
+ "[" + FLAG_STATUS + " STATUS] "
+ "[" + FLAG_POSITION + " POSITION] "
+ "[" + FLAG_TAG + " TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ FLAG_NAME + " John Doe "
+ FLAG_ORGANIZATION + " "
+ FLAG_NAME + " JobsInc "
+ FLAG_ID + " id_12345-1 "
+ FLAG_PHONE + " 98765432 "
+ FLAG_EMAIL + " [email protected] "
+ FLAG_EMAIL + " [email protected] "
+ FLAG_URL + " www.jobsinc.com "
+ FLAG_ADDRESS + " 311, Clementi Ave 2, #02-25 "
+ FLAG_TAG + " friends "
+ FLAG_TAG + " owesMoney";
+ FLAG_STATUS + " applied "
+ FLAG_POSITION + " Junior Software Engineer "
+ FLAG_TAG + " softwareEngineering "
+ FLAG_TAG + " competitive ";

public static final String MESSAGE_RECRUITER_USAGE = "Adds a recruiter. "
+ "Parameters: "
+ FLAG_RECRUITER + " "
+ FLAG_NAME + " NAME "
+ "[" + FLAG_ID + " ID] "
+ "[" + FLAG_ORGANIZATION_ID + " ORG_ID] "
+ "[" + FLAG_PHONE + " PHONE] "
+ "[" + FLAG_EMAIL + " EMAIL] "
+ "[" + FLAG_URL + " URL] "
+ "[" + FLAG_ADDRESS + " ADDRESS] "
+ "[" + FLAG_TAG + " TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ FLAG_RECRUITER + " "
+ FLAG_NAME + " Steve "
+ FLAG_ID + " id_98765-1 "
+ FLAG_PHONE + " 83452145 "
+ FLAG_EMAIL + " [email protected] "
+ FLAG_URL + " www.linkedin.com/in/steve/ "
+ FLAG_ADDRESS + " 311 W Coast Walk, #02-30 "
+ FLAG_TAG + " friendly ";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds a contact to the address book of the class type Organization or Recruiter."
+ " The input format varies depending on the class:\n\n"
+ MESSAGE_ORGANIZATION_USAGE + "\n\n"
+ MESSAGE_RECRUITER_USAGE;

public static final String MESSAGE_SUCCESS = "New contact added: %1$s";
public static final String MESSAGE_DUPLICATE_CONTACT = "This contact already exists in the address book";
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,20 @@ public CommandResult execute(Model model) throws CommandException {
private static Contact createEditedContact(Contact contactToEdit, EditContactDescriptor editContactDescriptor) {
assert contactToEdit != null;

Name updatedName = editContactDescriptor.getName().orElse(contactToEdit.getName());
Id updatedId = editContactDescriptor.getId().orElse(contactToEdit.getId());
Phone updatedPhone = editContactDescriptor.getPhone().orElse(contactToEdit.getPhone());
Email updatedEmail = editContactDescriptor.getEmail().orElse(contactToEdit.getEmail());
Url updatedUrl = editContactDescriptor.getUrl().orElse(contactToEdit.getUrl());
Address updatedAddress = editContactDescriptor.getAddress().orElse(contactToEdit.getAddress());
Set<Tag> updatedTags = editContactDescriptor.getTags().orElse(contactToEdit.getTags());
Name updatedName = editContactDescriptor.getName()
.orElse(contactToEdit.getName());
Id updatedId = editContactDescriptor.getId()
.orElse(contactToEdit.getId());
Phone updatedPhone = editContactDescriptor.getPhone()
.orElse(contactToEdit.getPhone().orElse(null));
Email updatedEmail = editContactDescriptor.getEmail()
.orElse(contactToEdit.getEmail().orElse(null));
Url updatedUrl = editContactDescriptor.getUrl()
.orElse(contactToEdit.getUrl().orElse(null));
Address updatedAddress = editContactDescriptor.getAddress()
.orElse(contactToEdit.getAddress().orElse(null));
Set<Tag> updatedTags = editContactDescriptor.getTags()
.orElse(contactToEdit.getTags());

return new Contact(updatedName, updatedId, updatedPhone, updatedEmail, updatedUrl, updatedAddress, updatedTags);
}
Expand Down
143 changes: 61 additions & 82 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static seedu.address.logic.parser.CliSyntax.FLAG_TAG;
import static seedu.address.logic.parser.CliSyntax.FLAG_URL;

import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;

import seedu.address.logic.commands.AddCommand;
Expand All @@ -39,21 +39,23 @@
public class AddCommandParser implements Parser<AddCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
* Parses the given {@code String} of arguments in the context of the AddCommand and returns an AddCommand object
* for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args,
FLAG_NAME, FLAG_PHONE, FLAG_EMAIL,
FLAG_ADDRESS, FLAG_TAG, FLAG_URL,
FLAG_ID, FLAG_STATUS, FLAG_POSITION,
FLAG_ORGANIZATION_ID,
FLAG_ORGANIZATION, FLAG_RECRUITER);

if (!argMultimap.hasAllOfFlags(FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS)
|| !argMultimap.getPreamble().isEmpty()) {
ArgumentTokenizer.tokenize(args,
FLAG_NAME, FLAG_PHONE, FLAG_EMAIL,
FLAG_ADDRESS, FLAG_TAG, FLAG_URL,
FLAG_ID, FLAG_STATUS, FLAG_POSITION,
FLAG_ORGANIZATION_ID,
FLAG_ORGANIZATION, FLAG_RECRUITER
);

if (!argMultimap.hasAllOfFlags(FLAG_NAME)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

Expand All @@ -69,21 +71,18 @@ public AddCommand parse(String args) throws ParseException {

// Deprecated contact format. Will be removed in future versions.
Name name = ParserUtil.parseName(argMultimap.getValue(FLAG_NAME).get());
Id id;
try {
id = ParserUtil.parseId(argMultimap.getValue(FLAG_ID).get());
} catch (NoSuchElementException e) {
id = new Id();
}
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(FLAG_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(FLAG_EMAIL).get());
Url url;
try {
url = ParserUtil.parseUrl(argMultimap.getValue(FLAG_URL).get());
} catch (NoSuchElementException e) {
url = new Url();
}
Address address = ParserUtil.parseAddress(argMultimap.getValue(FLAG_ADDRESS).get());
Optional<String> idString = argMultimap.getValue(FLAG_ID);
Id id = idString.isPresent()
? ParserUtil.parseId(idString.get())
: new Id();
Phone phone = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_PHONE), ParserUtil::parsePhone);
Email email = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_EMAIL), ParserUtil::parseEmail);
Address address = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_ADDRESS), ParserUtil::parseAddress);
Url url = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_URL), ParserUtil::parseUrl);
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(FLAG_TAG));

Contact contact = new Contact(name, id, phone, email, url, address, tagList);
Expand All @@ -94,70 +93,50 @@ public AddCommand parse(String args) throws ParseException {
private Recruiter parseAsRecruiter(ArgumentMultimap argMultimap) throws ParseException {
argMultimap.verifyNoDuplicateFlagsFor(FLAG_ORGANIZATION_ID);
Name name = ParserUtil.parseName(argMultimap.getValue(FLAG_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(FLAG_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(FLAG_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(FLAG_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(FLAG_TAG));
Id id;
try {
id = ParserUtil.parseId(argMultimap.getValue(FLAG_ID).get());
} catch (NoSuchElementException e) {
id = new Id();
}

Url url;
try {
url = ParserUtil.parseUrl(argMultimap.getValue(FLAG_URL).get());
} catch (NoSuchElementException e) {
url = new Url();
}

Id oid;
try {
oid = ParserUtil.parseId(argMultimap.getValue(FLAG_ORGANIZATION_ID).get());
} catch (NoSuchElementException e) {
oid = null;
}
Optional<String> idString = argMultimap.getValue(FLAG_ID);
Id id = idString.isPresent()
? ParserUtil.parseId(idString.get())
: new Id();

Phone phone = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_PHONE), ParserUtil::parsePhone);
Email email = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_EMAIL), ParserUtil::parseEmail);
Address address = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_ADDRESS), ParserUtil::parseAddress);
Url url = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_URL), ParserUtil::parseUrl);
Id oid = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_ORGANIZATION_ID), ParserUtil::parseId);
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(FLAG_TAG));

return new Recruiter(name, id, phone, email, url, address, tagList, oid);
}

private Organization parseAsOrganization(ArgumentMultimap argMultimap) throws ParseException {
argMultimap.verifyNoDuplicateFlagsFor(FLAG_NAME, FLAG_PHONE, FLAG_EMAIL, FLAG_ADDRESS);
argMultimap.verifyNoDuplicateFlagsFor(FLAG_POSITION, FLAG_STATUS);
Name name = ParserUtil.parseName(argMultimap.getValue(FLAG_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(FLAG_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(FLAG_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(FLAG_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(FLAG_TAG));
Id id;
try {
id = ParserUtil.parseId(argMultimap.getValue(FLAG_ID).get());
} catch (NoSuchElementException e) {
id = new Id();
}

Url url;
try {
url = ParserUtil.parseUrl(argMultimap.getValue(FLAG_URL).get());
} catch (NoSuchElementException e) {
url = new Url();
}

Position position;
try {
position = ParserUtil.parsePosition(argMultimap.getValue(FLAG_POSITION).get());
} catch (NoSuchElementException e) {
position = new Position();
}

Status status;
try {
status = ParserUtil.parseStatus(argMultimap.getValue(FLAG_STATUS).get());
} catch (NoSuchElementException e) {
status = new Status();
}
Optional<String> idString = argMultimap.getValue(FLAG_ID);
Id id = idString.isPresent()
? ParserUtil.parseId(idString.get())
: new Id();

Phone phone = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_PHONE), ParserUtil::parsePhone);
Email email = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_EMAIL), ParserUtil::parseEmail);
Address address = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_ADDRESS), ParserUtil::parseAddress);
Url url = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_URL), ParserUtil::parseUrl);
Position position = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_POSITION), ParserUtil::parsePosition);
Status status = ParserUtil.parseOptionally(
argMultimap.getValue(FLAG_STATUS), ParserUtil::parseStatus);
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(FLAG_TAG));

return new Organization(name, id, phone, email, url, address, tagList, status, position);
}

}
38 changes: 33 additions & 5 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
Expand All @@ -27,8 +28,9 @@ public class ParserUtil {
public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";

/**
* Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be
* trimmed.
* Parses {@code oneBasedIndex} into an {@code Index} and returns it.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the specified index is invalid (not non-zero unsigned integer).
*/
public static Index parseIndex(String oneBasedIndex) throws ParseException {
Expand Down Expand Up @@ -79,7 +81,7 @@ public static Status parseStatus(String status) throws ParseException {
requireNonNull(status);
String trimmedStatus = status.trim();
if (!Status.isValidStatus(status)) {
throw new ParseException(Name.MESSAGE_CONSTRAINTS);
throw new ParseException(Status.MESSAGE_CONSTRAINTS);
}
return new Status(trimmedStatus);
}
Expand All @@ -93,8 +95,8 @@ public static Status parseStatus(String status) throws ParseException {
public static Position parsePosition(String position) throws ParseException {
requireNonNull(position);
String trimmedPosition = position.trim();
if (!Name.isValidName(position)) {
throw new ParseException(Name.MESSAGE_CONSTRAINTS);
if (!Position.isValidPosition(position)) {
throw new ParseException(Position.MESSAGE_CONSTRAINTS);
}
return new Position(trimmedPosition);
}
Expand Down Expand Up @@ -185,4 +187,30 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
}
return tagSet;
}

/**
* References a function that parses a string into an expected output within the {@link ParserUtil} utility class.
* @param <R> The return result.
*/
@FunctionalInterface
public interface StringParserFunction<R> {
R parse(String value) throws ParseException;
}

/**
* Returns an object of type R that is given by passing the given string into {@code parseFunction} if
* {@code optionalString} is non-empty, otherwise returns null.
*
* @param <R> The type of object returned by parsing the optionalString.
*
* @throws ParseException if the given {@code optionalString} is invalid as determined by {@code parseFunction}
*/
public static <R> R parseOptionally(Optional<String> optionalString, StringParserFunction<R> parseFunction)
throws ParseException {

if (optionalString.isPresent()) {
return parseFunction.parse(optionalString.get());
}
return null;
}
}
Loading

0 comments on commit 4bfdba6

Please sign in to comment.