Skip to content

Commit

Permalink
Clean up code and fix several bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
zbz-lvlv committed Sep 28, 2022
1 parent 8e2bff5 commit 118c73f
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 182 deletions.
8 changes: 4 additions & 4 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_MINECRAFT_NAME;
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_SOCIALS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOCIAL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.logic.commands.exceptions.CommandException;
Expand All @@ -27,15 +27,15 @@ public class AddCommand extends Command {
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_ADDRESS + "ADDRESS "
+ "[" + PREFIX_SOCIALS + "SOCIAL MEDIA "
+ "[" + PREFIX_SOCIAL + "SOCIAL MEDIA "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_SOCIALS + "Tele: @zbzlvlv"
+ PREFIX_SOCIALS + "Insta: sdfsdf__2"
+ PREFIX_SOCIAL + "Tele: @zbzlvlv"
+ PREFIX_SOCIAL + "Insta: sdfsdf__2"
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";

Expand Down
25 changes: 12 additions & 13 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,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());
Set<Social> updatedSocials = editPersonDescriptor.getSocial().orElse(personToEdit.getSocials());
Set<Social> updatedSocials = editPersonDescriptor.getSocials().orElse(personToEdit.getSocials());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Set<Server> updatedServers = editPersonDescriptor.getServers().orElse(personToEdit.getServers());
TimeZone updatedTimeZone = editPersonDescriptor.getTimeZone().orElse(personToEdit.getTimeZone());
Expand Down Expand Up @@ -144,7 +144,7 @@ public static class EditPersonDescriptor {
private Phone phone;
private Email email;
private Address address;
private Set<Social> social;
private Set<Social> socials;
private Set<Tag> tags;
private Set<Server> servers;
private TimeZone timeZone;
Expand All @@ -161,7 +161,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setPhone(toCopy.phone);
setEmail(toCopy.email);
setAddress(toCopy.address);
setSocial(toCopy.social);
setSocials(toCopy.socials);
setTags(toCopy.tags);
setServers(toCopy.servers);
setTimeZone(toCopy.timeZone);
Expand All @@ -171,7 +171,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, minecraftName, phone, email, address, social, tags, servers, timeZone);
return CollectionUtil.isAnyNonNull(name, minecraftName, phone, email, address, socials, tags, servers, timeZone);
}

public void setName(Name name) {
Expand Down Expand Up @@ -218,20 +218,18 @@ public Optional<Address> getAddress() {
* Sets {@code socials} to this object's {@code socials}.
* A defensive copy of {@code socials} is used internally.
*/

public void setSocial(Set<Social> socials) {
this.social = (socials != null) ? new HashSet<>(social) : null;
public void setSocials(Set<Social> socials) {
this.socials = (socials != null) ? new HashSet<>(socials) : null;
}

/**
* Returns an unmodifiable socials set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code socials} is null.
*/

public Optional<Set<Social>> getSocial() {
return (social != null)
? Optional.of(Collections.unmodifiableSet(social))
public Optional<Set<Social>> getSocials() {
return (socials != null)
? Optional.of(Collections.unmodifiableSet(socials))
: Optional.empty();
}

Expand Down Expand Up @@ -270,6 +268,7 @@ public Optional<TimeZone> getTimeZone() {

@Override
public boolean equals(Object other) {

// short circuit if same object
if (other == this) {
return true;
Expand All @@ -284,11 +283,11 @@ public boolean equals(Object other) {
EditPersonDescriptor e = (EditPersonDescriptor) other;

return getName().equals(e.getName())
&& getMinecraftName().equals(e.getMinecraftName())
&& getPhone().equals(e.getPhone())
&& getEmail().equals(e.getEmail())
&& getAddress().equals(e.getAddress())
&& getSocial().equals(e.getSocial())
&& getTags().equals(e.getTags())
&& getSocials().equals(e.getSocials())
&& getTags().equals(e.getTags())
&& getServers().equals(e.getServers())
&& getTimeZone().equals(e.getTimeZone());
Expand Down
42 changes: 31 additions & 11 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.logic.parser.utils.CheckedFunction;
import seedu.address.model.person.*;
import seedu.address.model.server.Server;
import seedu.address.model.tag.Tag;
Expand All @@ -23,30 +24,49 @@ 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_MINECRAFT_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_SOCIALS, PREFIX_TAG);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_MINECRAFT_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_SOCIAL, PREFIX_TAG, PREFIX_MINECRAFT_SERVER, PREFIX_TIMEZONE);

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

Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
MinecraftName mcName = ParserUtil.parseMinecraftName(argMultimap.getValue(PREFIX_MINECRAFT_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());
Set<Social> socialList = ParserUtil.parseSocials(argMultimap.getAllValues(PREFIX_SOCIALS));
Name name = (Name) parseMandatoryArgument(PREFIX_NAME, argMultimap, ParserUtil::parseName);
MinecraftName mcName = (MinecraftName) parseMandatoryArgument(PREFIX_MINECRAFT_NAME, argMultimap, ParserUtil::parseMinecraftName);
Phone phone = (Phone) parseOptionalArgument(PREFIX_PHONE, argMultimap, ParserUtil::parsePhone);
Email email = (Email) parseOptionalArgument(PREFIX_EMAIL, argMultimap, ParserUtil::parseEmail);
Address address = (Address) parseOptionalArgument(PREFIX_ADDRESS, argMultimap, ParserUtil::parseAddress);
Set<Social> socialList = ParserUtil.parseSocials(argMultimap.getAllValues(PREFIX_SOCIAL));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Set<Server> serverList = ParserUtil.parseServers(argMultimap.getAllValues(PREFIX_MINECRAFT_SERVER));
TimeZone timeZone = ParserUtil.parseTimeZone(argMultimap.getValue(PREFIX_TIMEZONE).get());
TimeZone timeZone = (TimeZone) parseOptionalArgument(PREFIX_TIMEZONE, argMultimap, ParserUtil::parseTimeZone);

Person person = new Person(name, mcName, phone, email, address, socialList, tagList, serverList, timeZone);

return new AddCommand(person);
}

private Object parseOptionalArgument(Prefix prefix, ArgumentMultimap argMultimap, CheckedFunction<String, ?> parserFn) {
if (argMultimap.getValue(prefix).isPresent()) {
try {
return parserFn.apply(argMultimap.getValue(prefix).get());
} catch (Exception e) {
return null;
}
}
return null;
}

private Object parseMandatoryArgument(Prefix prefix, ArgumentMultimap argMultimap, CheckedFunction<String, ?> parserFn) {
try {
return parserFn.apply(argMultimap.getValue(prefix).get());
} catch (Exception e) {
return null;
}
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_SOCIALS = new Prefix("s/");
public static final Prefix PREFIX_SOCIAL = new Prefix("s/");
public static final Prefix PREFIX_MINECRAFT_SERVER = new Prefix("ms/");

public static final Prefix PREFIX_TIMEZONE = new Prefix("tz/");
Expand Down
33 changes: 28 additions & 5 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MINECRAFT_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MINECRAFT_SERVER;
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_SOCIALS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOCIAL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIMEZONE;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;

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.logic.parser.utils.CheckedFunction;
import seedu.address.model.person.Social;
import seedu.address.model.server.Server;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -33,10 +38,11 @@ public class EditCommandParser implements Parser<EditCommand> {
* @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_MINECRAFT_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_SOCIALS, PREFIX_TAG);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_MINECRAFT_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_SOCIAL, PREFIX_TAG, PREFIX_MINECRAFT_SERVER, PREFIX_TIMEZONE);

Index index;

Expand Down Expand Up @@ -65,8 +71,16 @@ public EditCommand parse(String args) throws ParseException {
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);
parseSocialsForEdit(argMultimap.getAllValues(PREFIX_SOCIALS)).ifPresent(editPersonDescriptor::setSocial);
if (argMultimap.getValue(PREFIX_TIMEZONE).isPresent()) {
editPersonDescriptor.setTimeZone(ParserUtil.parseTimeZone(argMultimap.getValue(PREFIX_TIMEZONE).get()));
}

parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG))
.ifPresent(editPersonDescriptor::setTags);
parseSocialsForEdit(argMultimap.getAllValues(PREFIX_SOCIAL))
.ifPresent(editPersonDescriptor::setSocials);
parseMinecraftServersForEdit(argMultimap.getAllValues(PREFIX_MINECRAFT_SERVER))
.ifPresent(editPersonDescriptor::setServers);

if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand Down Expand Up @@ -99,4 +113,13 @@ private Optional<Set<Social>> parseSocialsForEdit(Collection<String> socials) th
return Optional.of(ParserUtil.parseSocials(socials));
}

private Optional<Set<Server>> parseMinecraftServersForEdit(Collection<String> servers) throws ParseException {
assert servers != null;

if (servers.isEmpty()) {
return Optional.empty();
}
return Optional.of(ParserUtil.parseServers(servers));
}

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static TimeZone parseTimeZone(String offset) throws ParseException {
requireNonNull(offset);
String trimmedOffset = offset.trim();
if (!TimeZone.isValidTimeZone(trimmedOffset)) {
throw new ParseException(TimeZone.getTimezoneConstraints());
throw new ParseException(TimeZone.getMessageConstraints());
}
return new TimeZone(trimmedOffset);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package seedu.address.logic.parser.utils;

@FunctionalInterface
public interface CheckedFunction<T, R> {
R apply(T t) throws Exception;
}
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
*/
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 = "Any string can be accepted";

/*
* The first character of the address must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "[^\\s].*";
public static final String VALIDATION_REGEX = ".*";

public final String value;

Expand Down
22 changes: 2 additions & 20 deletions src/main/java/seedu/address/model/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,8 @@
public class Email {

private static final String SPECIAL_CHARACTERS = "+_.-";
public static final String MESSAGE_CONSTRAINTS = "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 "
+ "characters.\n"
+ "2. This is followed by a '@' and then a domain name. The domain name is made up of domain labels "
+ "separated by periods.\n"
+ "The domain name must:\n"
+ " - end with a domain label at least 2 characters long\n"
+ " - have each domain label start and end with alphanumeric characters\n"
+ " - have each domain label consist of alphanumeric characters, separated only by hyphens, if any.";
// alphanumeric and special characters
private static final String ALPHANUMERIC_NO_UNDERSCORE = "[^\\W_]+"; // alphanumeric characters except underscore
private static final String LOCAL_PART_REGEX = "^" + ALPHANUMERIC_NO_UNDERSCORE + "([" + SPECIAL_CHARACTERS + "]"
+ ALPHANUMERIC_NO_UNDERSCORE + ")*";
private static final String DOMAIN_PART_REGEX = ALPHANUMERIC_NO_UNDERSCORE
+ "(-" + ALPHANUMERIC_NO_UNDERSCORE + ")*";
private static final String DOMAIN_LAST_PART_REGEX = "(" + DOMAIN_PART_REGEX + "){2,}$"; // At least two chars
private static final String DOMAIN_REGEX = "(" + DOMAIN_PART_REGEX + "\\.)*" + DOMAIN_LAST_PART_REGEX;
public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@" + DOMAIN_REGEX;
public static final String MESSAGE_CONSTRAINTS = "Any string can be accepted";
public static final String VALIDATION_REGEX = ".*";

public final String value;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
public class Name {

public static final String MESSAGE_CONSTRAINTS =
"Names should only contain alphanumeric characters and spaces, and it should not be blank";
"Any string can be accepted";

/*
* The first character of the address must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";
public static final String VALIDATION_REGEX = ".*";

public final String fullName;

Expand Down
Loading

0 comments on commit 118c73f

Please sign in to comment.