From 19775317dd39ca4bdad6de51032b4b841a36d20f Mon Sep 17 00:00:00 2001 From: ForAeons Date: Fri, 15 Mar 2024 18:15:13 +0800 Subject: [PATCH 1/4] Reduce compulsory field for the addstu command --- .../logic/commands/AddPersonCommand.java | 6 +-- .../logic/parser/AddPersonCommandParser.java | 45 +++++++++++++------ .../seedu/address/model/person/Address.java | 4 ++ .../seedu/address/model/person/Email.java | 5 +++ .../seedu/address/model/person/Person.java | 4 +- .../seedu/address/model/person/Phone.java | 5 +++ src/main/resources/view/PersonListCard.fxml | 4 +- 7 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/AddPersonCommand.java b/src/main/java/seedu/address/logic/commands/AddPersonCommand.java index c1cf73bd254..1a4e32ac8c7 100644 --- a/src/main/java/seedu/address/logic/commands/AddPersonCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddPersonCommand.java @@ -26,10 +26,10 @@ public class AddPersonCommand extends Command { COMMAND_WORD, "Adds a person to the address book. ", PARAMETER_NAME, - PARAMETER_PHONE, - PARAMETER_EMAIL, PARAMETER_NUSNET, - PARAMETER_ADDRESS, + PARAMETER_PHONE.asOptional(true), + PARAMETER_EMAIL.asOptional(true), + PARAMETER_ADDRESS.asOptional(true), PARAMETER_TAG.asMultiple(2) ); diff --git a/src/main/java/seedu/address/logic/parser/AddPersonCommandParser.java b/src/main/java/seedu/address/logic/parser/AddPersonCommandParser.java index ade7079eae5..522b90fb485 100644 --- a/src/main/java/seedu/address/logic/parser/AddPersonCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AddPersonCommandParser.java @@ -30,23 +30,42 @@ public class AddPersonCommandParser implements Parser { * @throws ParseException if the user input does not conform the expected format */ public AddPersonCommand parse(String args) throws ParseException { - ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_NUSNET, - PREFIX_ADDRESS, PREFIX_TAG); + ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize( + args, + PREFIX_NAME, + PREFIX_PHONE, + PREFIX_EMAIL, + PREFIX_NUSNET, + PREFIX_ADDRESS, + PREFIX_TAG + ); - if (!ArgumentMultimap.arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, - PREFIX_EMAIL, PREFIX_NUSNET) - || !argMultimap.getPreamble().isEmpty()) { + if (!ArgumentMultimap.arePrefixesPresent( + argMultimap, + PREFIX_NAME, + PREFIX_NUSNET + ) || !argMultimap.getPreamble().isEmpty()) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPersonCommand.MESSAGE_USAGE)); } - argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, - PREFIX_NUSNET, 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()); - NusNet nusNet = ParserUtil.parseNusNet(argMultimap.getValue(PREFIX_NUSNET).get()); - Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()); + argMultimap.verifyNoDuplicatePrefixesFor( + PREFIX_NAME, + PREFIX_PHONE, + PREFIX_EMAIL, + PREFIX_NUSNET, + PREFIX_ADDRESS + ); + + Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME) + .get()); + Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE) + .orElseGet(() -> Phone.PLACEHOLDER)); + Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL) + .orElseGet(() -> Email.PLACEHOLDER)); + NusNet nusNet = ParserUtil.parseNusNet(argMultimap.getValue(PREFIX_NUSNET) + .get()); + Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS) + .orElseGet(() -> Address.PLACEHOLDER)); Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); Person person = new Person(name, phone, email, nusNet, address, tagList); diff --git a/src/main/java/seedu/address/model/person/Address.java b/src/main/java/seedu/address/model/person/Address.java index 469a2cc9a1e..f6937dcac2d 100644 --- a/src/main/java/seedu/address/model/person/Address.java +++ b/src/main/java/seedu/address/model/person/Address.java @@ -9,6 +9,7 @@ */ public class Address { + public static final String PLACEHOLDER = "This person has no address."; public static final String MESSAGE_CONSTRAINTS = "Addresses can take any values, and it should not be blank"; /* @@ -34,6 +35,9 @@ public Address(String address) { * Returns true if a given string is a valid email. */ public static boolean isValidAddress(String test) { + if (test.equals(PLACEHOLDER)) { + return true; + } return test.matches(VALIDATION_REGEX); } diff --git a/src/main/java/seedu/address/model/person/Email.java b/src/main/java/seedu/address/model/person/Email.java index c62e512bc29..c34a008c09b 100644 --- a/src/main/java/seedu/address/model/person/Email.java +++ b/src/main/java/seedu/address/model/person/Email.java @@ -9,6 +9,7 @@ */ public class Email { + public static final String PLACEHOLDER = "This person has no 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" @@ -48,6 +49,10 @@ public Email(String email) { * Returns if a given string is a valid email. */ public static boolean isValidEmail(String test) { + if (test.equals(PLACEHOLDER)) { + return true; + } + return test.matches(VALIDATION_REGEX); } diff --git a/src/main/java/seedu/address/model/person/Person.java b/src/main/java/seedu/address/model/person/Person.java index fe5d0246e90..7848c8a0a92 100644 --- a/src/main/java/seedu/address/model/person/Person.java +++ b/src/main/java/seedu/address/model/person/Person.java @@ -33,7 +33,7 @@ public class Person { */ public Person(Name name, Phone phone, Email email, NusNet nusNet, Address address, Set tags) { - requireAllNonNull(name, phone, email, address, attendance, tags); + requireAllNonNull(name, nusNet, attendance, tags); this.name = name; this.phone = phone; this.email = email; @@ -47,7 +47,7 @@ public Person(Name name, Phone phone, Email email, NusNet nusNet, */ public Person(Name name, Phone phone, Email email, NusNet nusNet, Address address, Set attendance, Set tags) { - requireAllNonNull(name, phone, email, address, attendance, tags); + requireAllNonNull(name, nusNet, attendance, tags); this.name = name; this.phone = phone; this.email = email; diff --git a/src/main/java/seedu/address/model/person/Phone.java b/src/main/java/seedu/address/model/person/Phone.java index d733f63d739..1255e5e2109 100644 --- a/src/main/java/seedu/address/model/person/Phone.java +++ b/src/main/java/seedu/address/model/person/Phone.java @@ -13,6 +13,7 @@ public class Phone { public static final String MESSAGE_CONSTRAINTS = "Phone numbers should only contain numbers, and it should be at least 3 digits long"; public static final String VALIDATION_REGEX = "\\d{3,}"; + public static final String PLACEHOLDER = "This person has no phone number."; public final String value; /** @@ -30,6 +31,10 @@ public Phone(String phone) { * Returns true if a given string is a valid phone number. */ public static boolean isValidPhone(String test) { + if (test.equals(PLACEHOLDER)) { + return true; + } + return test.matches(VALIDATION_REGEX); } diff --git a/src/main/resources/view/PersonListCard.fxml b/src/main/resources/view/PersonListCard.fxml index 7ca807c6268..01cbc272237 100644 --- a/src/main/resources/view/PersonListCard.fxml +++ b/src/main/resources/view/PersonListCard.fxml @@ -28,10 +28,10 @@