Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2425S1#92 from kaikquah/branch-Update…
Browse files Browse the repository at this point in the history
…-OptionalFields

Update Email and Address command fields to be optional when adding contacts
  • Loading branch information
luileng authored Oct 22, 2024
2 parents 8044620 + e01af03 commit 7a21426
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/main/java/tuteez/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static String format(Person person) {
.append("; Address: ")
.append(person.getAddress())
.append("; Telegram: ")
.append(person.getTelegramUsername())
.append(person.getTelegramUsername().toString())
.append("; Tags: ");
person.getTags().forEach(builder::append);
builder.append("; Lessons: ");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tuteez/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class AddCommand extends Command {
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_TELEGRAM + "TELEGRAM USERNAME"
+ "[" + PREFIX_TAG + "TAG]...\n"
+ " [" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/tuteez/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AddCommand parse(String args) throws ParseException {
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_TELEGRAM, PREFIX_TAG, PREFIX_LESSON);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}
Expand All @@ -47,8 +47,8 @@ public AddCommand parse(String args) throws ParseException {
PREFIX_TELEGRAM);
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());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).orElse(null));
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).orElse(null));
TelegramUsername telegramUsername = ParserUtil.parseTelegramUsername(
argMultimap.getValue(PREFIX_TELEGRAM).orElse(null));

Expand Down
38 changes: 34 additions & 4 deletions src/main/java/tuteez/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import tuteez.logic.commands.EditCommand;
import tuteez.logic.commands.EditCommand.EditPersonDescriptor;
import tuteez.logic.parser.exceptions.ParseException;
import tuteez.model.person.Address;
import tuteez.model.person.Email;
import tuteez.model.person.TelegramUsername;
import tuteez.model.person.lesson.Lesson;
import tuteez.model.tag.Tag;

Expand Down Expand Up @@ -58,14 +61,16 @@ public EditCommand parse(String args) throws ParseException {
editPersonDescriptor.setPhone(ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()));
}
if (argMultimap.getValue(PREFIX_EMAIL).isPresent()) {
editPersonDescriptor.setEmail(ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()));
String email = argMultimap.getValue(PREFIX_EMAIL).get();
setEditedEmail(editPersonDescriptor, email);
}
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
String address = argMultimap.getValue(PREFIX_ADDRESS).get();
setEditedAddress(editPersonDescriptor, address);
}
if (argMultimap.getValue(PREFIX_TELEGRAM).isPresent()) {
editPersonDescriptor.setTelegramUsername(ParserUtil.parseTelegramUsername(
argMultimap.getValue(PREFIX_TELEGRAM).get()));
String telegramUsername = argMultimap.getValue(PREFIX_TELEGRAM).get();
setEditedTelegramUsername(editPersonDescriptor, telegramUsername);
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);
parseLessonsForEdit(argMultimap.getAllValues(PREFIX_LESSON)).ifPresent(editPersonDescriptor::setLessons);
Expand All @@ -77,6 +82,31 @@ public EditCommand parse(String args) throws ParseException {
return new EditCommand(index, editPersonDescriptor);
}

private void setEditedEmail(EditPersonDescriptor editPersonDescriptor, String email) throws ParseException {
if (email.isEmpty()) {
editPersonDescriptor.setEmail(new Email(null));
} else {
editPersonDescriptor.setEmail(ParserUtil.parseEmail(email));
}
}

private void setEditedAddress(EditPersonDescriptor editPersonDescriptor, String address) throws ParseException {
if (address.isEmpty()) {
editPersonDescriptor.setAddress(new Address(null));
} else {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(address));
}
}

private void setEditedTelegramUsername(EditPersonDescriptor editPersonDescriptor, String telegramUsername)
throws ParseException {
if (telegramUsername.isEmpty()) {
editPersonDescriptor.setTelegramUsername(TelegramUsername.empty());
} else {
editPersonDescriptor.setTelegramUsername(ParserUtil.parseTelegramUsername(telegramUsername));
}
}

/**
* 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
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/tuteez/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public static Phone parsePhone(String phone) throws ParseException {
* @throws ParseException if the given {@code address} is invalid.
*/
public static Address parseAddress(String address) throws ParseException {
requireNonNull(address);
if (address == null) {
return new Address(address);
}
String trimmedAddress = address.trim();
if (!Address.isValidAddress(trimmedAddress)) {
throw new ParseException(Address.MESSAGE_CONSTRAINTS);
Expand All @@ -89,7 +91,9 @@ public static Address parseAddress(String address) throws ParseException {
* @throws ParseException if the given {@code email} is invalid.
*/
public static Email parseEmail(String email) throws ParseException {
requireNonNull(email);
if (email == null) {
return new Email(email);
}
String trimmedEmail = email.trim();
if (!Email.isValidEmail(trimmedEmail)) {
throw new ParseException(Email.MESSAGE_CONSTRAINTS);
Expand Down Expand Up @@ -136,12 +140,14 @@ public static TelegramUsername parseTelegramUsername(String username) throws Par
return TelegramUsername.empty();
}
String trimmedUsername = username.trim();

if (!TelegramUsername.isValidTelegramHandle(trimmedUsername)) {
throw new ParseException(TelegramUsername.MESSAGE_CONSTRAINTS);
}
return TelegramUsername.of(trimmedUsername);
}


/**
* Parses a {@code String lesson} into a {@code Lesson}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/tuteez/model/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
*/
public class Address {

public static final String MESSAGE_CONSTRAINTS = "Addresses can take any values, and it should not be blank";
public static final String MESSAGE_CONSTRAINTS = "Addresses can take any values, and it should not be blank"
+ ", unless when editing.";

/*
* The first character of the address must not be a whitespace,
Expand All @@ -25,20 +26,29 @@ public class Address {
* @param address A valid address.
*/
public Address(String address) {
requireNonNull(address);
checkArgument(isValidAddress(address), MESSAGE_CONSTRAINTS);
// address argument can be null to make it optional
if (address != null) {
requireNonNull(address);
checkArgument(isValidAddress(address), MESSAGE_CONSTRAINTS);
}
value = address;
}

/**
* Returns true if a given string is a valid email.
*/
public static boolean isValidAddress(String test) {
if (test == null) {
return true;
}
return test.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
if (value == null) {
return "";
}
return value;
}

Expand All @@ -54,12 +64,15 @@ public boolean equals(Object other) {
}

Address otherAddress = (Address) other;
if (value == null) {
return otherAddress.value == null;
}
return value.equals(otherAddress.value);
}

@Override
public int hashCode() {
return value.hashCode();
return value == null ? 0 : value.hashCode();
}

}
20 changes: 16 additions & 4 deletions src/main/java/tuteez/model/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class Email {

private static final String SPECIAL_CHARACTERS = "+_.-";
public static final String MESSAGE_CONSTRAINTS = "Emails should be of the format local-part@domain "
public static final String MESSAGE_CONSTRAINTS = "If provided, Emails should be of the format local-part@domain "
+ "and adhere to the following constraints:\n"
+ "1. The local-part should only contain alphanumeric characters and these special characters, excluding "
+ "the parentheses, (" + SPECIAL_CHARACTERS + "). The local-part may not start or end with any special "
Expand Down Expand Up @@ -39,20 +39,29 @@ public class Email {
* @param email A valid email address.
*/
public Email(String email) {
requireNonNull(email);
checkArgument(isValidEmail(email), MESSAGE_CONSTRAINTS);
// email argument can be null to make it optional
if (email != null) {
requireNonNull(email);
checkArgument(isValidEmail(email), MESSAGE_CONSTRAINTS);
}
value = email;
}

/**
* Returns if a given string is a valid email.
*/
public static boolean isValidEmail(String test) {
if (test == null) {
return true;
}
return test.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
if (value == null) {
return "";
}
return value;
}

Expand All @@ -68,12 +77,15 @@ public boolean equals(Object other) {
}

Email otherEmail = (Email) other;
if (value == null) {
return otherEmail.value == null;
}
return value.equals(otherEmail.value);
}

@Override
public int hashCode() {
return value.hashCode();
return value == null ? 0 : value.hashCode();
}

}
2 changes: 1 addition & 1 deletion src/main/java/tuteez/model/person/TelegramUsername.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TelegramUsername {
public static final String MESSAGE_CONSTRAINTS =
"Telegram usernames must be at least 5 characters long, and can only contain a-z/A-Z, 0-9, and underscores."
+ " Telegram usernames are case insensitive, and will be in lowercase. It must start with a letter,"
+ " and should not be blank.";
+ " and should not be blank unless the user is editing.";

public static final String VALIDATION_REGEX = "^[a-zA-Z][a-zA-Z0-9_]{4,31}$";

Expand Down
6 changes: 0 additions & 6 deletions src/main/java/tuteez/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,13 @@ private Phone getModelPhone(String phone) throws IllegalValueException {
}

private Email getModelEmail(String email) throws IllegalValueException {
if (email == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Email.class.getSimpleName()));
}
if (!Email.isValidEmail(email)) {
throw new IllegalValueException(Email.MESSAGE_CONSTRAINTS);
}
return new Email(email);
}

private Address getModelAddress(String address) throws IllegalValueException {
if (address == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Address.class.getSimpleName()));
}
if (!Address.isValidAddress(address)) {
throw new IllegalValueException(Address.MESSAGE_CONSTRAINTS);
}
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/tuteez/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import tuteez.model.person.TelegramUsername;

/**
* An UI component that displays information of a {@code Person}.
* A UI component that displays information of a {@code Person}.
*/
public class PersonCard extends UiPart<Region> {

Expand Down Expand Up @@ -59,8 +59,8 @@ public PersonCard(Person person, int displayedIndex) {
name.setText(person.getName().fullName);
phone.setText(person.getPhone().value);
setTelegramUsernameText(person);
address.setText(person.getAddress().value);
email.setText(person.getEmail().value);
setAddressText(person);
setEmailText(person);
person.getTags().stream()
.sorted(Comparator.comparing(tag -> tag.tagName))
.forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
Expand All @@ -72,6 +72,24 @@ public PersonCard(Person person, int displayedIndex) {
.forEach(lesson -> lessons.getChildren().add(new Label(lesson.getDayAndTime())));
}

private void setAddressText(Person person) {
if (person.getAddress().value != null) {
address.setText(person.getAddress().value);
address.setVisible(true);
} else {
address.setVisible(false);
}
}

private void setEmailText(Person person) {
if (person.getEmail().value != null) {
email.setText(person.getEmail().value);
email.setVisible(true);
} else {
email.setVisible(false);
}
}

private void setTelegramUsernameText(Person person) {
TelegramUsername username = person.getTelegramUsername();
if (username != null && username.telegramUsername != null && !username.telegramUsername.isEmpty()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/view/PersonListCard.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<FlowPane fx:id="lessons" />
<Label fx:id="phone" styleClass="cell_small_label" text="\$phone" />
<Label fx:id="telegram" styleClass="cell_small_label" text="\$telegram" managed="${telegram.visible}" />
<Label fx:id="address" styleClass="cell_small_label" text="\$address" />
<Label fx:id="email" styleClass="cell_small_label" text="\$email" />
<Label fx:id="address" styleClass="cell_small_label" text="\$address" managed ="${address.visible}" />
<Label fx:id="email" styleClass="cell_small_label" text="\$email" managed ="${email.visible}" />
<VBox fx:id="remarks" spacing="5">
</VBox>
</VBox>
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/tuteez/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public class CommandTestUtil {
public static final String INVALID_NAME_DESC = " " + PREFIX_NAME + "James*"; // '*' not allowed in names
public static final String INVALID_PHONE_DESC = " " + PREFIX_PHONE + "911a"; // 'a' not allowed in phones
public static final String INVALID_EMAIL_DESC = " " + PREFIX_EMAIL + "bob!yahoo"; // missing '@' symbol
public static final String INVALID_ADDRESS_DESC = " " + PREFIX_ADDRESS; // empty string not allowed for addresses
// empty string not allowed for addresses, only while adding, but not editing.
public static final String INVALID_ADDRESS_DESC = " " + PREFIX_ADDRESS;
public static final String INVALID_TELEGRAM_DESC = " " + PREFIX_TELEGRAM + "amybee!"; // '!' not allowed in username
public static final String INVALID_TAG_DESC = " " + PREFIX_TAG + "hubby*"; // '*' not allowed in tags
public static final String INVALID_LESSON_DESC = " " + PREFIX_LESSON + "monday 10:1100";
Expand Down
Loading

0 comments on commit 7a21426

Please sign in to comment.