From 91a4d7ee304ea9316ef07bb9dfa64150a2bbaebf Mon Sep 17 00:00:00 2001 From: tiif Date: Sun, 22 Oct 2023 17:26:48 +0800 Subject: [PATCH 01/13] fixing lead related instantiation --- .../logic/commands/AddLeadCommand.java | 7 +- .../address/logic/commands/EditCommand.java | 45 +++++--- .../logic/commands/EditLeadCommand.java | 109 ++++++++++++++++++ .../logic/parser/AddLeadCommandParser.java | 21 ++-- .../seedu/address/logic/parser/CliSyntax.java | 2 + .../logic/parser/EditCommandParser.java | 25 ++-- .../address/logic/parser/ParserUtil.java | 13 ++- .../address/model/person/KeyMilestone.java | 51 ++++++++ .../java/seedu/address/model/person/Lead.java | 8 +- .../address/model/util/SampleDataUtil.java | 11 +- .../address/storage/JsonAdaptedPerson.java | 1 + .../seedu/address/testutil/PersonBuilder.java | 10 +- 12 files changed, 241 insertions(+), 62 deletions(-) create mode 100644 src/main/java/seedu/address/logic/commands/EditLeadCommand.java create mode 100644 src/main/java/seedu/address/model/person/KeyMilestone.java diff --git a/src/main/java/seedu/address/logic/commands/AddLeadCommand.java b/src/main/java/seedu/address/logic/commands/AddLeadCommand.java index cc34c54bec6..42cef11464c 100644 --- a/src/main/java/seedu/address/logic/commands/AddLeadCommand.java +++ b/src/main/java/seedu/address/logic/commands/AddLeadCommand.java @@ -1,11 +1,7 @@ package seedu.address.logic.commands; import static java.util.Objects.requireNonNull; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.*; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; @@ -32,6 +28,7 @@ public class AddLeadCommand extends Command { + PREFIX_PHONE + "98765432 " + PREFIX_EMAIL + "johnd@example.com " + PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 " + + PREFIX_KEYMILESTONE + "2023-10-20" + PREFIX_TAG + "classmate"; public static final String MESSAGE_SUCCESS = "New lead added: %1$s"; diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index c7dc9a432d8..ef8727ae637 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -8,6 +8,7 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; +import java.security.Key; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -21,13 +22,7 @@ import seedu.address.logic.Messages; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; -import seedu.address.model.person.Address; -import seedu.address.model.person.Client; -import seedu.address.model.person.Email; -import seedu.address.model.person.Lead; -import seedu.address.model.person.Name; -import seedu.address.model.person.Person; -import seedu.address.model.person.Phone; +import seedu.address.model.person.*; import seedu.address.model.tag.Tag; /** @@ -36,7 +31,6 @@ public class EditCommand extends Command { public static final String COMMAND_WORD = "edit"; - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified " + "by the index number used in the displayed person list. " + "Existing values will be overwritten by the input values.\n" @@ -81,14 +75,11 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail()); Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress()); Set updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags()); - - if (personToEdit.isClient()) { + //todo: change this return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags); - } else { - return new Lead(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags); - } } + //todo: make createeditedclient in future @Override public CommandResult execute(Model model) throws CommandException { requireNonNull(model); @@ -159,7 +150,6 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) { setAddress(toCopy.address); setTags(toCopy.tags); } - /** * Returns true if at least one field is edited. */ @@ -238,6 +228,7 @@ public boolean equals(Object other) { @Override public String toString() { return new ToStringBuilder(this) + //todo how to specify the toString for leads .add("name", name) .add("phone", phone) .add("email", email) @@ -246,4 +237,30 @@ public String toString() { .toString(); } } + + public static class EditLeadDescriptor extends EditPersonDescriptor { + //put here because need access to editPersonDescriptor, can put in editeadcommand? + KeyMilestone keyMilestone; + public EditLeadDescriptor(EditLeadDescriptor toCopy) { + super(toCopy); + setKeyMilestone(toCopy.keyMilestone); + } + + public EditLeadDescriptor() { + + } + + + public Optional getKeyMilestone() { + return Optional.ofNullable(keyMilestone); + } + + public void setKeyMilestone (KeyMilestone keyMilestone) { + this.keyMilestone = keyMilestone; + } + @Override + public String toString() { + return super.toString() + new ToStringBuilder(this).add("key milestone", keyMilestone); + } + } } diff --git a/src/main/java/seedu/address/logic/commands/EditLeadCommand.java b/src/main/java/seedu/address/logic/commands/EditLeadCommand.java new file mode 100644 index 00000000000..52759e56973 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/EditLeadCommand.java @@ -0,0 +1,109 @@ +package seedu.address.logic.commands; + +import seedu.address.commons.util.ToStringBuilder; +import seedu.address.logic.Messages; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.person.*; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import seedu.address.commons.core.index.Index; +import seedu.address.model.tag.Tag; + +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.parser.CliSyntax.*; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; + +public class EditLeadCommand extends EditCommand { + private final Index index; + private final EditLeadDescriptor editLeadDescriptor; + + + public static final String COMMAND_WORD = "edit"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified " + + "by the index number used in the displayed person list. " + + "Existing values will be overwritten by the input values.\n" + + "Parameters: INDEX (must be a positive integer) " + + "[" + PREFIX_NAME + "NAME] " + + "[" + PREFIX_PHONE + "PHONE] " + + "[" + PREFIX_EMAIL + "EMAIL] " + + "[" + PREFIX_ADDRESS + "ADDRESS] " + + "[" + PREFIX_KEYMILESTONE+ "KEYMILESTONE] " + + "[" + PREFIX_TAG + "TAG]...\n" + + "Example: " + COMMAND_WORD + " 1 " + + PREFIX_PHONE + "91234567 " + + PREFIX_EMAIL + "johndoe@example.com"; + public EditLeadCommand(Index index, EditLeadDescriptor editLeadDescriptor) { + super(index, editLeadDescriptor); + requireNonNull(index); + requireNonNull(editLeadDescriptor); + + this.index = index; + this.editLeadDescriptor = new EditLeadDescriptor(editLeadDescriptor); + } + + + private static Lead createEditedLead (Lead leadToEdit, EditLeadDescriptor editLeadDescriptor) { + assert leadToEdit != null; + + Name updatedName = editLeadDescriptor.getName().orElse(leadToEdit.getName()); + Phone updatedPhone = editLeadDescriptor.getPhone().orElse(leadToEdit.getPhone()); + Email updatedEmail = editLeadDescriptor.getEmail().orElse(leadToEdit.getEmail()); + Address updatedAddress = editLeadDescriptor.getAddress().orElse(leadToEdit.getAddress()); + Set updatedTags = editLeadDescriptor.getTags().orElse(leadToEdit.getTags()); + KeyMilestone updatedKeyMilestone = editLeadDescriptor.getKeyMilestone().orElse(leadToEdit.getKeyMilestone()); + + return new Lead(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedKeyMilestone, updatedTags); + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredPersonList(); + + if (index.getZeroBased() >= lastShownList.size()) { + //todo: fix the message so it is for lead + throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + Person personToEdit = lastShownList.get(index.getZeroBased()); + Person editedPerson = createEditedLead((Lead) personToEdit, editLeadDescriptor); + + if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) { + throw new CommandException(MESSAGE_DUPLICATE_PERSON); + } + + model.setPerson(personToEdit, editedPerson); + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson))); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof EditLeadCommand)) { + return false; + } + + EditLeadCommand otherEditCommand = (EditLeadCommand) other; + return index.equals(otherEditCommand.index) + && editLeadDescriptor.equals(otherEditCommand.editLeadDescriptor); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("index", index) + .add("editLeadDescriptor", editLeadDescriptor) + .toString(); + } + + +} diff --git a/src/main/java/seedu/address/logic/parser/AddLeadCommandParser.java b/src/main/java/seedu/address/logic/parser/AddLeadCommandParser.java index d7a04e3fc7e..bf93c4cd88a 100644 --- a/src/main/java/seedu/address/logic/parser/AddLeadCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AddLeadCommandParser.java @@ -1,22 +1,14 @@ package seedu.address.logic.parser; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.*; import java.util.Set; import java.util.stream.Stream; import seedu.address.logic.commands.AddLeadCommand; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.person.Address; -import seedu.address.model.person.Email; -import seedu.address.model.person.Lead; -import seedu.address.model.person.Name; -import seedu.address.model.person.Phone; +import seedu.address.model.person.*; import seedu.address.model.tag.Tag; /** @@ -31,22 +23,23 @@ public class AddLeadCommandParser implements Parser { */ public AddLeadCommand parse(String args) throws ParseException { ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG); + ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_KEYMILESTONE, PREFIX_TAG); - if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL) + if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_KEYMILESTONE, PREFIX_EMAIL) || !argMultimap.getPreamble().isEmpty()) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddLeadCommand.MESSAGE_USAGE)); } - argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS); + argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_KEYMILESTONE); 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()); + KeyMilestone keyMilestone = ParserUtil.parseKeyMilestone(argMultimap.getValue(PREFIX_KEYMILESTONE).get()); Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); // TODO: temporary fix, implement add Client and Lead commands - Lead lead = new Lead(name, phone, email, address, tagList); + Lead lead = new Lead(name, phone, email, address, keyMilestone, tagList); return new AddLeadCommand(lead); } diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 80fe792becc..afc9142b2e9 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -11,11 +11,13 @@ 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_KEYMILESTONE = new Prefix("k/"); public static final Prefix PREFIX_NAME_TP = new Prefix("--name"); public static final Prefix PREFIX_PHONE_TP = new Prefix("--phone"); public static final Prefix PREFIX_EMAIL_TP = new Prefix("--email"); public static final Prefix PREFIX_ADDRESS_TP = new Prefix("--address"); public static final Prefix PREFIX_TAG_TP = new Prefix("--tag"); + public static final Prefix PREFIX_KEYMILESTONE_TP = new Prefix("--key"); } diff --git a/src/main/java/seedu/address/logic/parser/EditCommandParser.java b/src/main/java/seedu/address/logic/parser/EditCommandParser.java index 46b3309a78b..58ffd97244c 100644 --- a/src/main/java/seedu/address/logic/parser/EditCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/EditCommandParser.java @@ -2,11 +2,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.*; import java.util.Collection; import java.util.Collections; @@ -16,6 +12,8 @@ import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; +import seedu.address.logic.commands.EditCommand.EditLeadDescriptor; +import seedu.address.logic.commands.EditLeadCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.tag.Tag; @@ -31,8 +29,9 @@ public class EditCommandParser implements Parser { */ public EditCommand parse(String args) throws ParseException { requireNonNull(args); + boolean isLead = false; ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG); + ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_KEYMILESTONE, PREFIX_TAG); Index index; @@ -42,9 +41,15 @@ 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); - - EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor(); + argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_KEYMILESTONE, PREFIX_ADDRESS); + //todo: add another field for lead in future + EditPersonDescriptor editPersonDescriptor; + if (argMultimap.getValue(PREFIX_KEYMILESTONE).isPresent()) { + isLead = true; + editPersonDescriptor = new EditLeadDescriptor(); + } else { + editPersonDescriptor = new EditPersonDescriptor(); + } if (argMultimap.getValue(PREFIX_NAME).isPresent()) { editPersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get())); @@ -64,7 +69,7 @@ public EditCommand parse(String args) throws ParseException { throw new ParseException(EditCommand.MESSAGE_NOT_EDITED); } - return new EditCommand(index, editPersonDescriptor); + return isLead ? new EditLeadCommand(index, (EditLeadDescriptor) editPersonDescriptor) : new EditCommand(index, editPersonDescriptor); } /** diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index b117acb9c55..8bbd39efbbf 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -9,10 +9,7 @@ import seedu.address.commons.core.index.Index; import seedu.address.commons.util.StringUtil; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.person.Address; -import seedu.address.model.person.Email; -import seedu.address.model.person.Name; -import seedu.address.model.person.Phone; +import seedu.address.model.person.*; import seedu.address.model.tag.Tag; /** @@ -80,6 +77,14 @@ public static Address parseAddress(String address) throws ParseException { return new Address(trimmedAddress); } + public static KeyMilestone parseKeyMilestone(String keyMilestone) throws ParseException { + requireNonNull(keyMilestone); + String trimmedKeyMilestone = keyMilestone.trim(); + if (!Address.isValidAddress(trimmedKeyMilestone)) { + throw new ParseException(Address.MESSAGE_CONSTRAINTS); + } + return new KeyMilestone(keyMilestone); + } /** * Parses a {@code String email} into an {@code Email}. * Leading and trailing whitespaces will be trimmed. diff --git a/src/main/java/seedu/address/model/person/KeyMilestone.java b/src/main/java/seedu/address/model/person/KeyMilestone.java new file mode 100644 index 00000000000..014678a622b --- /dev/null +++ b/src/main/java/seedu/address/model/person/KeyMilestone.java @@ -0,0 +1,51 @@ +package seedu.address.model.person; + +import static java.util.Objects.requireNonNull; +import static seedu.address.commons.util.AppUtil.checkArgument; + +public class KeyMilestone { + public static final String MESSAGE_CONSTRAINTS = + "Key milestone is the date of Lead's milestone moment"; + public static final String VALIDATION_REGEX = "^\\d{4}-\\d{2}-\\d{2}$"; + public final String keyMilestone; + //todo: add comment + public KeyMilestone(String keyMilestone) { + requireNonNull(keyMilestone); + checkArgument(isValidKeyMilestone(keyMilestone), MESSAGE_CONSTRAINTS); + //todo: use SimpleDateFormat if needed + // https://stackoverflow.com/questions/2149680/regex-date-format-validation-on-java + this.keyMilestone = keyMilestone; + } + + //todo: add comment + public static boolean isValidKeyMilestone(String test) { + return test.matches(VALIDATION_REGEX); + } + + @Override + public String toString() { + return keyMilestone; + } + + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof KeyMilestone)) { + return false; + } + + KeyMilestone otherName = (KeyMilestone) other; + return keyMilestone.equals(otherName.keyMilestone); + } + + @Override + public int hashCode() { + return keyMilestone.hashCode(); + } + +} diff --git a/src/main/java/seedu/address/model/person/Lead.java b/src/main/java/seedu/address/model/person/Lead.java index 96448f64615..93790154a33 100644 --- a/src/main/java/seedu/address/model/person/Lead.java +++ b/src/main/java/seedu/address/model/person/Lead.java @@ -9,12 +9,18 @@ */ public class Lead extends Person { public static final String TYPE_LEAD = "lead"; + public KeyMilestone keyMilestone; /** * Every field must be present and not null. */ - public Lead(Name name, Phone phone, Email email, Address address, Set tags) { + public Lead(Name name, Phone phone, Email email, Address address, KeyMilestone keyMilestone, Set tags) { super(name, phone, email, new Type(TYPE_LEAD), address, tags); + this.keyMilestone = keyMilestone; + } + + public KeyMilestone getKeyMilestone() { + return this.keyMilestone; } @Override diff --git a/src/main/java/seedu/address/model/util/SampleDataUtil.java b/src/main/java/seedu/address/model/util/SampleDataUtil.java index 2465a4de27e..d5c5041b9dc 100644 --- a/src/main/java/seedu/address/model/util/SampleDataUtil.java +++ b/src/main/java/seedu/address/model/util/SampleDataUtil.java @@ -6,13 +6,7 @@ import seedu.address.model.AddressBook; import seedu.address.model.ReadOnlyAddressBook; -import seedu.address.model.person.Address; -import seedu.address.model.person.Client; -import seedu.address.model.person.Email; -import seedu.address.model.person.Lead; -import seedu.address.model.person.Name; -import seedu.address.model.person.Person; -import seedu.address.model.person.Phone; +import seedu.address.model.person.*; import seedu.address.model.tag.Tag; /** @@ -32,12 +26,15 @@ public static Person[] getSamplePersons() { getTagSet("neighbours")), new Lead(new Name("David Li"), new Phone("91031282"), new Email("lidavid@example.com"), new Address("Blk 436 Serangoon Gardens Street 26, #16-43"), + new KeyMilestone("2022-12-01"), getTagSet("family")), new Lead(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("irfan@example.com"), new Address("Blk 47 Tampines Street 20, #17-35"), + new KeyMilestone("2022-12-01"), getTagSet("classmates")), new Lead(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("royb@example.com"), new Address("Blk 45 Aljunied Street 85, #11-31"), + new KeyMilestone("2022-12-01"), getTagSet("colleagues")) }; } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index cd7953c7c24..805419b23e0 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -27,6 +27,7 @@ * Jackson-friendly version of {@link Person}. */ class JsonAdaptedPerson { + //todo: fix this, might need json adapted lead or client if needed public static final String MISSING_FIELD_MESSAGE_FORMAT = "Person's %s field is missing!"; diff --git a/src/test/java/seedu/address/testutil/PersonBuilder.java b/src/test/java/seedu/address/testutil/PersonBuilder.java index e4439707d95..9b7dcf33f95 100644 --- a/src/test/java/seedu/address/testutil/PersonBuilder.java +++ b/src/test/java/seedu/address/testutil/PersonBuilder.java @@ -3,13 +3,7 @@ import java.util.HashSet; import java.util.Set; -import seedu.address.model.person.Address; -import seedu.address.model.person.Client; -import seedu.address.model.person.Email; -import seedu.address.model.person.Lead; -import seedu.address.model.person.Name; -import seedu.address.model.person.Person; -import seedu.address.model.person.Phone; +import seedu.address.model.person.*; import seedu.address.model.tag.Tag; import seedu.address.model.util.SampleDataUtil; @@ -22,12 +16,14 @@ public class PersonBuilder { public static final String DEFAULT_PHONE = "85355255"; public static final String DEFAULT_EMAIL = "amy@gmail.com"; public static final String DEFAULT_ADDRESS = "123, Jurong West Ave 6, #08-111"; + public static final String DEFAULT_KEYMILESTONE= "2022-12-01"; private Name name; private Phone phone; private Email email; private Address address; private Set tags; + private KeyMilestone keyMilestone; /** * Creates a {@code PersonBuilder} with the default details. From 26cc9dbfdb3403b0645b451f1883f08aee95c5c6 Mon Sep 17 00:00:00 2001 From: tiif Date: Wed, 25 Oct 2023 20:04:04 +0800 Subject: [PATCH 02/13] Add KeyMilestone field to Lead class --- .../java/seedu/address/logic/Messages.java | 18 +++ .../address/model/person/KeyMilestone.java | 10 +- .../address/storage/JsonAdaptedLead.java | 122 ++++++++++++++++++ .../address/storage/JsonAdaptedPerson.java | 33 +++-- .../duplicatePersonAddressBook.json | 2 + .../invalidPersonAddressBook.json | 1 + .../typicalPersonsAddressBook.json | 7 + .../logic/commands/CommandTestUtil.java | 9 +- .../logic/commands/EditCommandTest.java | 16 +-- .../parser/AddLeadCommandParserTest.java | 69 +++------- .../storage/JsonAdaptedPersonTest.java | 49 +++++-- .../testutil/EditLeadDescriptorBuilder.java | 85 ++++++++++++ .../testutil/EditPersonDescriptorBuilder.java | 10 +- .../java/seedu/address/testutil/LeadUtil.java | 9 +- .../seedu/address/testutil/PersonBuilder.java | 19 ++- .../seedu/address/testutil/TypicalLeads.java | 2 +- .../address/testutil/TypicalPersons.java | 20 +-- 17 files changed, 357 insertions(+), 124 deletions(-) create mode 100644 src/main/java/seedu/address/storage/JsonAdaptedLead.java create mode 100644 src/test/java/seedu/address/testutil/EditLeadDescriptorBuilder.java diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index ecd32c31b53..1faadff1cfe 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -5,6 +5,7 @@ import java.util.stream.Stream; import seedu.address.logic.parser.Prefix; +import seedu.address.model.person.Lead; import seedu.address.model.person.Person; /** @@ -48,4 +49,21 @@ public static String format(Person person) { return builder.toString(); } + + public static String format(Lead lead) { + final StringBuilder builder = new StringBuilder(); + builder.append(lead.getName()) + .append("; Phone: ") + .append(lead.getPhone()) + .append("; Email: ") + .append(lead.getEmail()) + .append("; Address: ") + .append(lead.getAddress()) + .append("; Key Milestone: ") + .append(lead.getKeyMilestone()) + .append("; Tags: "); + lead.getTags().forEach(builder::append); + return builder.toString(); + } + } diff --git a/src/main/java/seedu/address/model/person/KeyMilestone.java b/src/main/java/seedu/address/model/person/KeyMilestone.java index 014678a622b..292d15b3db3 100644 --- a/src/main/java/seedu/address/model/person/KeyMilestone.java +++ b/src/main/java/seedu/address/model/person/KeyMilestone.java @@ -7,14 +7,14 @@ public class KeyMilestone { public static final String MESSAGE_CONSTRAINTS = "Key milestone is the date of Lead's milestone moment"; public static final String VALIDATION_REGEX = "^\\d{4}-\\d{2}-\\d{2}$"; - public final String keyMilestone; + public final String value; //todo: add comment public KeyMilestone(String keyMilestone) { requireNonNull(keyMilestone); checkArgument(isValidKeyMilestone(keyMilestone), MESSAGE_CONSTRAINTS); //todo: use SimpleDateFormat if needed // https://stackoverflow.com/questions/2149680/regex-date-format-validation-on-java - this.keyMilestone = keyMilestone; + this.value = keyMilestone; } //todo: add comment @@ -24,7 +24,7 @@ public static boolean isValidKeyMilestone(String test) { @Override public String toString() { - return keyMilestone; + return value; } @@ -40,12 +40,12 @@ public boolean equals(Object other) { } KeyMilestone otherName = (KeyMilestone) other; - return keyMilestone.equals(otherName.keyMilestone); + return value.equals(otherName.value); } @Override public int hashCode() { - return keyMilestone.hashCode(); + return value.hashCode(); } } diff --git a/src/main/java/seedu/address/storage/JsonAdaptedLead.java b/src/main/java/seedu/address/storage/JsonAdaptedLead.java new file mode 100644 index 00000000000..66fcbc8af1b --- /dev/null +++ b/src/main/java/seedu/address/storage/JsonAdaptedLead.java @@ -0,0 +1,122 @@ +package seedu.address.storage; + +import static seedu.address.model.person.Client.TYPE_CLIENT; +import static seedu.address.model.person.Lead.TYPE_LEAD; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.model.person.*; +import seedu.address.model.tag.Tag; +public class JsonAdaptedLead{ + public static final String MISSING_FIELD_MESSAGE_FORMAT = "Lead's %s field is missing!"; + + private final String name; + private final String phone; + private final String email; + private final String type; + private final String address; + private final String keyMilestone; + private final List tags = new ArrayList<>(); + + /** + * Constructs a {@code JsonAdaptedLead} with the given person details. + */ + @JsonCreator + public JsonAdaptedLead(@JsonProperty("name") String name, @JsonProperty("phone") String phone, + @JsonProperty("email") String email, @JsonProperty("type") String type, + @JsonProperty("address") String address, + @JsonProperty("key milestone") String keyMilestone, + @JsonProperty("tags") List tags) { + this.name = name; + this.phone = phone; + this.email = email; + this.type = type; + this.address = address; + this.keyMilestone = keyMilestone; + if (tags != null) { + this.tags.addAll(tags); + } + } + + /** + * Converts a given {@code Lead} into this class for Jackson use. + */ + public JsonAdaptedLead(Lead source) { + name = source.getName().fullName; + phone = source.getPhone().value; + email = source.getEmail().value; + address = source.getAddress().value; + type = source.getType().value; + keyMilestone = source.getKeyMilestone().value; + tags.addAll(source.getTags().stream() + .map(JsonAdaptedTag::new) + .collect(Collectors.toList())); + } + + /** + * Converts this Jackson-friendly adapted person object into the model's {@code Lead} object. + * + * @throws IllegalValueException if there were any data constraints violated in the adapted person. + */ + public Lead toModelType() throws IllegalValueException { + final List personTags = new ArrayList<>(); + for (JsonAdaptedTag tag : tags) { + personTags.add(tag.toModelType()); + } + + if (name == null) { + throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName())); + } + if (!Name.isValidName(name)) { + throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS); + } + final Name modelName = new Name(name); + + if (phone == null) { + throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Phone.class.getSimpleName())); + } + if (!Phone.isValidPhone(phone)) { + throw new IllegalValueException(Phone.MESSAGE_CONSTRAINTS); + } + final Phone modelPhone = new Phone(phone); + + 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); + } + final Email modelEmail = new Email(email); + + 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); + } + if (!KeyMilestone.isValidKeyMilestone(keyMilestone)) { + throw new IllegalValueException(KeyMilestone.MESSAGE_CONSTRAINTS); + } + final KeyMilestone modelKeyMilestone = new KeyMilestone(keyMilestone); + final Address modelAddress = new Address(address); + + final Set modelTags = new HashSet<>(personTags); + + if (type == null) { + throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, "Type")); + } + if (type.equals(TYPE_LEAD)) { + return new Lead(modelName, modelPhone, modelEmail, modelAddress, modelKeyMilestone, modelTags); + } else { + throw new IllegalValueException(Type.MESSAGE_CONSTRAINTS); + } + } +} diff --git a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java index 805419b23e0..006a2ea2a6e 100644 --- a/src/main/java/seedu/address/storage/JsonAdaptedPerson.java +++ b/src/main/java/seedu/address/storage/JsonAdaptedPerson.java @@ -13,22 +13,13 @@ import com.fasterxml.jackson.annotation.JsonProperty; import seedu.address.commons.exceptions.IllegalValueException; -import seedu.address.model.person.Address; -import seedu.address.model.person.Client; -import seedu.address.model.person.Email; -import seedu.address.model.person.Lead; -import seedu.address.model.person.Name; -import seedu.address.model.person.Person; -import seedu.address.model.person.Phone; -import seedu.address.model.person.Type; +import seedu.address.model.person.*; import seedu.address.model.tag.Tag; /** * Jackson-friendly version of {@link Person}. */ class JsonAdaptedPerson { - //todo: fix this, might need json adapted lead or client if needed - public static final String MISSING_FIELD_MESSAGE_FORMAT = "Person's %s field is missing!"; private final String name; @@ -36,20 +27,25 @@ class JsonAdaptedPerson { private final String email; private final String type; private final String address; + private String keyMilestone; private final List tags = new ArrayList<>(); /** * Constructs a {@code JsonAdaptedPerson} with the given person details. */ + @JsonCreator public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("phone") String phone, - @JsonProperty("email") String email, @JsonProperty("type") String type, - @JsonProperty("address") String address, @JsonProperty("tags") List tags) { + @JsonProperty("email") String email, @JsonProperty("type") String type, + @JsonProperty("address") String address, + @JsonProperty("keyMilestone") String keyMilestone, + @JsonProperty("tags") List tags) { this.name = name; this.phone = phone; this.email = email; this.type = type; this.address = address; + this.keyMilestone = keyMilestone; if (tags != null) { this.tags.addAll(tags); } @@ -64,6 +60,10 @@ public JsonAdaptedPerson(Person source) { email = source.getEmail().value; address = source.getAddress().value; type = source.getType().value; + if (type == TYPE_LEAD) { + Lead sourceLead = (Lead) source; + keyMilestone = sourceLead.getKeyMilestone().value; + } tags.addAll(source.getTags().stream() .map(JsonAdaptedTag::new) .collect(Collectors.toList())); @@ -120,8 +120,13 @@ public Person toModelType() throws IllegalValueException { if (type.equals(TYPE_CLIENT)) { return new Client(modelName, modelPhone, modelEmail, modelAddress, modelTags); } else if (type.equals(TYPE_LEAD)) { - return new Lead(modelName, modelPhone, modelEmail, modelAddress, modelTags); - } else { + if (!KeyMilestone.isValidKeyMilestone(keyMilestone)) { + throw new IllegalValueException(KeyMilestone.MESSAGE_CONSTRAINTS); + } + final KeyMilestone modelKeyMilestone = new KeyMilestone(keyMilestone); + return new Lead(modelName, modelPhone, modelEmail, modelAddress, modelKeyMilestone, modelTags); + } + else { throw new IllegalValueException(Type.MESSAGE_CONSTRAINTS); } } diff --git a/src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json index 18414edc343..84d5e9872b0 100644 --- a/src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json @@ -5,6 +5,7 @@ "email": "alice@example.com", "type": "lead", "address": "123, Jurong West Ave 6, #08-111", + "keyMilestone": "2022-12-01", "tags": [ "Client", "friends" ] }, { "name": "Alice Pauline", @@ -12,6 +13,7 @@ "email": "pauline@example.com", "type": "lead", "address": "4th street", + "keyMilestone": "2022-12-01", "tags": [ "Client" ] } ] } diff --git a/src/test/data/JsonSerializableAddressBookTest/invalidPersonAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/invalidPersonAddressBook.json index 5ef7e989c31..b289a9bc6f9 100644 --- a/src/test/data/JsonSerializableAddressBookTest/invalidPersonAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/invalidPersonAddressBook.json @@ -4,6 +4,7 @@ "phone": "9482424", "type": "invalidtype", "email": "invalid@email!3e", + "keyMilestone": null, "address": "4th street" } ] } diff --git a/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json b/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json index 60b94d48170..a7989d7d914 100644 --- a/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json +++ b/src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json @@ -6,6 +6,7 @@ "email" : "alice@example.com", "type": "client", "address" : "123, Jurong West Ave 6, #08-111", + "keyMilestone": null, "tags" : [ "friends" ] }, { "name" : "Benson Meier", @@ -13,12 +14,14 @@ "email" : "johnd@example.com", "type": "client", "address" : "311, Clementi Ave 2, #02-25", + "keyMilestone": null, "tags" : [ "owesMoney", "friends" ] }, { "name" : "Carl Kurz", "phone" : "95352563", "email" : "heinz@example.com", "type": "client", + "keyMilestone": null, "address" : "wall street", "tags" : [] }, { @@ -27,12 +30,14 @@ "email" : "cornelia@example.com", "type": "client", "address" : "10th street", + "keyMilestone": null, "tags" : [ "friends" ] }, { "name" : "Elle Meyer", "phone" : "9482224", "email" : "werner@example.com", "type": "lead", + "keyMilestone": "2022-12-01", "address" : "michegan ave", "tags" : [] }, { @@ -40,6 +45,7 @@ "phone" : "9482427", "email" : "lydia@example.com", "type": "lead", + "keyMilestone": "2022-12-01", "address" : "little tokyo", "tags" : [] }, { @@ -47,6 +53,7 @@ "phone" : "9482442", "email" : "anna@example.com", "type": "lead", + "keyMilestone": "2022-12-01", "address" : "4th street", "tags" : [] } ] diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index 643a1d08069..51410bc14fa 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -2,11 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.*; import static seedu.address.testutil.Assert.assertThrows; import java.util.ArrayList; @@ -35,6 +31,8 @@ public class CommandTestUtil { public static final String VALID_ADDRESS_AMY = "Block 312, Amy Street 1"; public static final String VALID_ADDRESS_BOB = "Block 123, Bobby Street 3"; public static final String VALID_TAG_HUSBAND = "husband"; + + public static final String VALID_KEYMILESTONE_BOB= "2022-12-01"; public static final String VALID_TAG_FRIEND = "friend"; public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY; @@ -45,6 +43,7 @@ public class CommandTestUtil { public static final String EMAIL_DESC_BOB = " " + PREFIX_EMAIL + VALID_EMAIL_BOB; public static final String ADDRESS_DESC_AMY = " " + PREFIX_ADDRESS + VALID_ADDRESS_AMY; public static final String ADDRESS_DESC_BOB = " " + PREFIX_ADDRESS + VALID_ADDRESS_BOB; + public static final String KEYMILESTONE_DESC_BOB = " " + PREFIX_KEYMILESTONE + VALID_KEYMILESTONE_BOB; public static final String TAG_DESC_FRIEND = " " + PREFIX_TAG + VALID_TAG_FRIEND; public static final String TAG_DESC_HUSBAND = " " + PREFIX_TAG + VALID_TAG_HUSBAND; diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index 3b6860107e6..da3da6c5440 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -3,14 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import static seedu.address.logic.commands.CommandTestUtil.DESC_AMY; -import static seedu.address.logic.commands.CommandTestUtil.DESC_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; -import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; -import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; -import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; +import static seedu.address.logic.commands.CommandTestUtil.*; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; @@ -20,11 +13,13 @@ import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; +import seedu.address.logic.commands.EditCommand.EditLeadDescriptor; import seedu.address.model.AddressBook; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; import seedu.address.model.person.Person; +import seedu.address.testutil.EditLeadDescriptorBuilder; import seedu.address.testutil.EditPersonDescriptorBuilder; import seedu.address.testutil.PersonBuilder; @@ -48,7 +43,6 @@ public void execute_allFieldsSpecifiedUnfilteredList_success() { assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); } - @Test public void execute_someFieldsSpecifiedUnfilteredList_success() { Index indexLastPerson = Index.fromOneBased(model.getFilteredPersonList().size()); @@ -58,9 +52,9 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() { Person editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) .withTags(VALID_TAG_HUSBAND).buildLead(); - EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB) + EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB) .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).build(); - EditCommand editCommand = new EditCommand(indexLastPerson, descriptor); + EditCommand editCommand = new EditLeadCommand(indexLastPerson, descriptor); String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); diff --git a/src/test/java/seedu/address/logic/parser/AddLeadCommandParserTest.java b/src/test/java/seedu/address/logic/parser/AddLeadCommandParserTest.java index 34c06fe6a4f..a58fb73a86a 100644 --- a/src/test/java/seedu/address/logic/parser/AddLeadCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddLeadCommandParserTest.java @@ -1,33 +1,8 @@ package seedu.address.logic.parser; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -import static seedu.address.logic.commands.CommandTestUtil.ADDRESS_DESC_AMY; -import static seedu.address.logic.commands.CommandTestUtil.ADDRESS_DESC_BOB; -import static seedu.address.logic.commands.CommandTestUtil.EMAIL_DESC_AMY; -import static seedu.address.logic.commands.CommandTestUtil.EMAIL_DESC_BOB; -import static seedu.address.logic.commands.CommandTestUtil.INVALID_ADDRESS_DESC; -import static seedu.address.logic.commands.CommandTestUtil.INVALID_EMAIL_DESC; -import static seedu.address.logic.commands.CommandTestUtil.INVALID_NAME_DESC; -import static seedu.address.logic.commands.CommandTestUtil.INVALID_PHONE_DESC; -import static seedu.address.logic.commands.CommandTestUtil.INVALID_TAG_DESC; -import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_AMY; -import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_BOB; -import static seedu.address.logic.commands.CommandTestUtil.PHONE_DESC_AMY; -import static seedu.address.logic.commands.CommandTestUtil.PHONE_DESC_BOB; -import static seedu.address.logic.commands.CommandTestUtil.PREAMBLE_NON_EMPTY; -import static seedu.address.logic.commands.CommandTestUtil.PREAMBLE_WHITESPACE; -import static seedu.address.logic.commands.CommandTestUtil.TAG_DESC_FRIEND; -import static seedu.address.logic.commands.CommandTestUtil.TAG_DESC_HUSBAND; -import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_FRIEND; -import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; +import static seedu.address.logic.commands.CommandTestUtil.*; +import static seedu.address.logic.parser.CliSyntax.*; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalPersons.AMY; @@ -37,11 +12,7 @@ import seedu.address.logic.Messages; import seedu.address.logic.commands.AddLeadCommand; -import seedu.address.model.person.Address; -import seedu.address.model.person.Email; -import seedu.address.model.person.Lead; -import seedu.address.model.person.Name; -import seedu.address.model.person.Phone; +import seedu.address.model.person.*; import seedu.address.model.tag.Tag; import seedu.address.testutil.PersonBuilder; @@ -50,25 +21,26 @@ public class AddLeadCommandParserTest { @Test public void parse_allFieldsPresent_success() { - Lead expectedLead = new PersonBuilder(BOB).withTags(VALID_TAG_FRIEND).buildLead(); + Lead expectedLead = new PersonBuilder(BOB).withTags(VALID_TAG_FRIEND).withKeyMilestone(VALID_KEYMILESTONE_BOB).buildLead(); // whitespace only preamble assertParseSuccess(parser, PREAMBLE_WHITESPACE + NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB - + ADDRESS_DESC_BOB + TAG_DESC_FRIEND, new AddLeadCommand(expectedLead)); + + ADDRESS_DESC_BOB + TAG_DESC_FRIEND + KEYMILESTONE_DESC_BOB, new AddLeadCommand(expectedLead)); // multiple tags - all accepted Lead expectedLeadMultipleTags = new PersonBuilder(BOB).withTags(VALID_TAG_FRIEND, VALID_TAG_HUSBAND) .buildLead(); assertParseSuccess(parser, - NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, + NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, new AddLeadCommand(expectedLeadMultipleTags)); } - + //todo: fix the message error @Test public void parse_repeatedNonTagValue_failure() { + //todo: need another test for clients String validExpectedPersonString = NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB - + ADDRESS_DESC_BOB + TAG_DESC_FRIEND; + + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + TAG_DESC_FRIEND; // multiple names assertParseFailure(parser, NAME_DESC_AMY + validExpectedPersonString, @@ -87,10 +59,11 @@ public void parse_repeatedNonTagValue_failure() { Messages.getErrorMessageForDuplicatePrefixes(PREFIX_ADDRESS)); // multiple fields repeated + //todo: fix this test, it failed assertParseFailure(parser, - validExpectedPersonString + PHONE_DESC_AMY + EMAIL_DESC_AMY + NAME_DESC_AMY + ADDRESS_DESC_AMY + validExpectedPersonString + PHONE_DESC_AMY + EMAIL_DESC_AMY + NAME_DESC_AMY + ADDRESS_DESC_AMY + KEYMILESTONE_DESC_BOB + validExpectedPersonString, - Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NAME, PREFIX_ADDRESS, PREFIX_EMAIL, PREFIX_PHONE)); + Messages.getErrorMessageForDuplicatePrefixes(PREFIX_NAME, PREFIX_ADDRESS, PREFIX_EMAIL, PREFIX_PHONE, PREFIX_KEYMILESTONE)); // invalid value followed by valid value @@ -132,8 +105,8 @@ public void parse_repeatedNonTagValue_failure() { @Test public void parse_optionalFieldsMissing_success() { // zero tags - Lead expectedLead = new PersonBuilder(AMY).withTags().buildLead(); - assertParseSuccess(parser, NAME_DESC_AMY + PHONE_DESC_AMY + EMAIL_DESC_AMY + ADDRESS_DESC_AMY, + Lead expectedLead = new PersonBuilder(BOB).withTags().buildLead(); + assertParseSuccess(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB, new AddLeadCommand(expectedLead)); } @@ -165,32 +138,32 @@ public void parse_compulsoryFieldMissing_failure() { @Test public void parse_invalidValue_failure() { // invalid name - assertParseFailure(parser, INVALID_NAME_DESC + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + assertParseFailure(parser, INVALID_NAME_DESC + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, Name.MESSAGE_CONSTRAINTS); // invalid phone - assertParseFailure(parser, NAME_DESC_BOB + INVALID_PHONE_DESC + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + assertParseFailure(parser, NAME_DESC_BOB + INVALID_PHONE_DESC + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, Phone.MESSAGE_CONSTRAINTS); // invalid email - assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + INVALID_EMAIL_DESC + ADDRESS_DESC_BOB + assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + INVALID_EMAIL_DESC + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, Email.MESSAGE_CONSTRAINTS); // invalid address - assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + INVALID_ADDRESS_DESC + assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + INVALID_ADDRESS_DESC + KEYMILESTONE_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, Address.MESSAGE_CONSTRAINTS); // invalid tag - assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + assertParseFailure(parser, NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + INVALID_TAG_DESC + VALID_TAG_FRIEND, Tag.MESSAGE_CONSTRAINTS); // two invalid values, only first invalid value reported - assertParseFailure(parser, INVALID_NAME_DESC + PHONE_DESC_BOB + EMAIL_DESC_BOB + INVALID_ADDRESS_DESC, + assertParseFailure(parser, INVALID_NAME_DESC + PHONE_DESC_BOB + EMAIL_DESC_BOB + INVALID_ADDRESS_DESC + KEYMILESTONE_DESC_BOB, Name.MESSAGE_CONSTRAINTS); // non-empty preamble assertParseFailure(parser, PREAMBLE_NON_EMPTY + NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB - + ADDRESS_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, + + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddLeadCommand.MESSAGE_USAGE)); } } diff --git a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java index 2e5966fa9b7..627744d76ba 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java @@ -4,6 +4,7 @@ import static seedu.address.storage.JsonAdaptedPerson.MISSING_FIELD_MESSAGE_FORMAT; import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.BENSON; +import static seedu.address.testutil.TypicalPersons.FIONA; import java.util.ArrayList; import java.util.List; @@ -25,6 +26,9 @@ public class JsonAdaptedPersonTest { private static final String INVALID_TYPE = "friend"; private static final String INVALID_TAG = "#friend"; + private static final String VALID_KEYMILESTONE = "2022-12-01"; + private static final String CLIENT_KEYMILESTONE_NULL = null; + private static final String VALID_NAME = BENSON.getName().toString(); private static final String VALID_PHONE = BENSON.getPhone().toString(); private static final String VALID_EMAIL = BENSON.getEmail().toString(); @@ -33,24 +37,28 @@ public class JsonAdaptedPersonTest { private static final List VALID_TAGS = BENSON.getTags().stream() .map(JsonAdaptedTag::new) .collect(Collectors.toList()); - @Test public void toModelType_validPersonDetails_returnsPerson() throws Exception { JsonAdaptedPerson person = new JsonAdaptedPerson(BENSON); assertEquals(BENSON, person.toModelType()); } + @Test + public void toModelType_validLeadDetails_returnsPerson() throws Exception { + JsonAdaptedPerson person = new JsonAdaptedPerson(FIONA); + assertEquals(FIONA, person.toModelType()); + } @Test public void toModelType_invalidName_throwsIllegalValueException() { JsonAdaptedPerson person = - new JsonAdaptedPerson(INVALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_TYPE, VALID_ADDRESS, VALID_TAGS); + new JsonAdaptedPerson(INVALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_TYPE, VALID_ADDRESS, CLIENT_KEYMILESTONE_NULL, VALID_TAGS); String expectedMessage = Name.MESSAGE_CONSTRAINTS; assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } @Test public void toModelType_nullName_throwsIllegalValueException() { - JsonAdaptedPerson person = new JsonAdaptedPerson(null, VALID_PHONE, VALID_EMAIL, VALID_TYPE, + JsonAdaptedPerson person = new JsonAdaptedPerson(null, VALID_PHONE, VALID_EMAIL, CLIENT_KEYMILESTONE_NULL, VALID_TYPE, VALID_ADDRESS, VALID_TAGS); String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName()); assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); @@ -59,23 +67,30 @@ public void toModelType_nullName_throwsIllegalValueException() { @Test public void toModelType_invalidPhone_throwsIllegalValueException() { JsonAdaptedPerson person = - new JsonAdaptedPerson(VALID_NAME, INVALID_PHONE, VALID_EMAIL, VALID_TYPE, VALID_ADDRESS, VALID_TAGS); + new JsonAdaptedPerson(VALID_NAME, INVALID_PHONE, VALID_EMAIL, VALID_TYPE, VALID_ADDRESS, CLIENT_KEYMILESTONE_NULL, VALID_TAGS); String expectedMessage = Phone.MESSAGE_CONSTRAINTS; assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } @Test public void toModelType_nullPhone_throwsIllegalValueException() { - JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, null, VALID_EMAIL, VALID_TYPE, + JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, null, VALID_EMAIL, CLIENT_KEYMILESTONE_NULL, VALID_TYPE, VALID_ADDRESS, VALID_TAGS); String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, Phone.class.getSimpleName()); assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } - + //uncomment later + @Test + public void toModelType_nullPhone_withValidKeyMilestone_throwsIllegalValueException() { + JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, null, VALID_EMAIL, VALID_TYPE, + VALID_ADDRESS, VALID_KEYMILESTONE, VALID_TAGS); + String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, Phone.class.getSimpleName()); + assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); + } @Test public void toModelType_invalidEmail_throwsIllegalValueException() { JsonAdaptedPerson person = - new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, INVALID_EMAIL, VALID_TYPE, VALID_ADDRESS, VALID_TAGS); + new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, INVALID_EMAIL, VALID_TYPE, VALID_ADDRESS, CLIENT_KEYMILESTONE_NULL, VALID_TAGS); String expectedMessage = Email.MESSAGE_CONSTRAINTS; assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } @@ -83,7 +98,7 @@ public void toModelType_invalidEmail_throwsIllegalValueException() { @Test public void toModelType_nullEmail_throwsIllegalValueException() { JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, null, VALID_TYPE, - VALID_ADDRESS, VALID_TAGS); + VALID_ADDRESS, CLIENT_KEYMILESTONE_NULL, VALID_TAGS); String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, Email.class.getSimpleName()); assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } @@ -91,7 +106,7 @@ public void toModelType_nullEmail_throwsIllegalValueException() { @Test public void toModelType_invalidAddress_throwsIllegalValueException() { JsonAdaptedPerson person = - new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_TYPE, INVALID_ADDRESS, VALID_TAGS); + new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_TYPE, INVALID_ADDRESS, CLIENT_KEYMILESTONE_NULL, VALID_TAGS); String expectedMessage = Address.MESSAGE_CONSTRAINTS; assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } @@ -99,7 +114,7 @@ public void toModelType_invalidAddress_throwsIllegalValueException() { @Test public void toModelType_nullAddress_throwsIllegalValueException() { JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_TYPE, null, - VALID_TAGS); + CLIENT_KEYMILESTONE_NULL, VALID_TAGS); String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT, Address.class.getSimpleName()); assertThrows(IllegalValueException.class, expectedMessage, person::toModelType); } @@ -107,14 +122,14 @@ public void toModelType_nullAddress_throwsIllegalValueException() { @Test public void toModelType_invalidType_throwsIllegalValueException() { JsonAdaptedPerson person = - new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, INVALID_TYPE, VALID_ADDRESS, VALID_TAGS); + new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, INVALID_TYPE, VALID_ADDRESS, CLIENT_KEYMILESTONE_NULL, VALID_TAGS); assertThrows(IllegalValueException.class, person::toModelType); } @Test public void toModelType_nullType_throwsIllegalValueException() { JsonAdaptedPerson person = - new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, null, VALID_ADDRESS, VALID_TAGS); + new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, null, VALID_ADDRESS, CLIENT_KEYMILESTONE_NULL, VALID_TAGS); assertThrows(IllegalValueException.class, person::toModelType); } @@ -123,7 +138,15 @@ public void toModelType_invalidTags_throwsIllegalValueException() { List invalidTags = new ArrayList<>(VALID_TAGS); invalidTags.add(new JsonAdaptedTag(INVALID_TAG)); JsonAdaptedPerson person = - new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_TYPE, VALID_ADDRESS, invalidTags); + new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_TYPE, VALID_ADDRESS, CLIENT_KEYMILESTONE_NULL, invalidTags); + assertThrows(IllegalValueException.class, person::toModelType); + } + @Test + public void toModelType_invalidTags_withKeyMilestone_throwsIllegalValueException() { + List invalidTags = new ArrayList<>(VALID_TAGS); + invalidTags.add(new JsonAdaptedTag(INVALID_TAG)); + JsonAdaptedPerson person = + new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_TYPE, VALID_ADDRESS, VALID_KEYMILESTONE, invalidTags); assertThrows(IllegalValueException.class, person::toModelType); } diff --git a/src/test/java/seedu/address/testutil/EditLeadDescriptorBuilder.java b/src/test/java/seedu/address/testutil/EditLeadDescriptorBuilder.java new file mode 100644 index 00000000000..1f6cd60085a --- /dev/null +++ b/src/test/java/seedu/address/testutil/EditLeadDescriptorBuilder.java @@ -0,0 +1,85 @@ +package seedu.address.testutil; + +import seedu.address.logic.commands.EditCommand; +import seedu.address.logic.commands.EditCommand.EditLeadDescriptor; + +import seedu.address.model.person.*; +import seedu.address.model.tag.Tag; + +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class EditLeadDescriptorBuilder { + + private EditLeadDescriptor leadDescriptor; + + public EditLeadDescriptorBuilder() { + this.leadDescriptor = new EditLeadDescriptor(); + } + + + public EditLeadDescriptorBuilder(EditLeadDescriptor descriptor) { + this.leadDescriptor = new EditLeadDescriptor(descriptor); + } + + public EditLeadDescriptorBuilder(Lead lead) { + leadDescriptor = new EditLeadDescriptor(); + leadDescriptor.setName(lead.getName()); + leadDescriptor.setPhone(lead.getPhone()); + leadDescriptor.setEmail(lead.getEmail()); + leadDescriptor.setAddress(lead.getAddress()); + leadDescriptor.setKeyMilestone(lead.getKeyMilestone()); + leadDescriptor.setTags(lead.getTags()); + } + + + public EditLeadDescriptorBuilder withName(String name) { + leadDescriptor.setName(new Name(name)); + return this; + } + + /** + * Sets the {@code Phone} of the {@code EditPersonDescriptor} that we are building. + */ + public EditLeadDescriptorBuilder withPhone(String phone) { + leadDescriptor.setPhone(new Phone(phone)); + return this; + } + + /** + * Sets the {@code Email} of the {@code EditPersonDescriptor} that we are building. + */ + public EditLeadDescriptorBuilder withEmail(String email) { + leadDescriptor.setEmail(new Email(email)); + return this; + } + + /** + * Sets the {@code Address} of the {@code EditPersonDescriptor} that we are building. + */ + public EditLeadDescriptorBuilder withAddress(String address) { + leadDescriptor.setAddress(new Address(address)); + return this; + } + + public EditLeadDescriptorBuilder withKeyMilestone(String keyMilestone) { + leadDescriptor.setKeyMilestone(new KeyMilestone(keyMilestone)); + return this; + } + + + /** + * Parses the {@code tags} into a {@code Set} and set it to the {@code EditPersonDescriptor} + * that we are building. + */ + public EditLeadDescriptorBuilder withTags(String... tags) { + Set tagSet = Stream.of(tags).map(Tag::new).collect(Collectors.toSet()); + leadDescriptor.setTags(tagSet); + return this; + } + + public EditLeadDescriptor build() { + return leadDescriptor; + } +} diff --git a/src/test/java/seedu/address/testutil/EditPersonDescriptorBuilder.java b/src/test/java/seedu/address/testutil/EditPersonDescriptorBuilder.java index 4584bd5044e..b32152055d9 100644 --- a/src/test/java/seedu/address/testutil/EditPersonDescriptorBuilder.java +++ b/src/test/java/seedu/address/testutil/EditPersonDescriptorBuilder.java @@ -4,12 +4,10 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.EditCommand.EditPersonDescriptor; -import seedu.address.model.person.Address; -import seedu.address.model.person.Email; -import seedu.address.model.person.Name; -import seedu.address.model.person.Person; -import seedu.address.model.person.Phone; +import seedu.address.logic.commands.EditCommand.EditLeadDescriptor; +import seedu.address.model.person.*; import seedu.address.model.tag.Tag; /** @@ -27,6 +25,7 @@ public EditPersonDescriptorBuilder(EditPersonDescriptor descriptor) { this.descriptor = new EditPersonDescriptor(descriptor); } + /** * Returns an {@code EditPersonDescriptor} with fields containing {@code person}'s details */ @@ -84,4 +83,5 @@ public EditPersonDescriptorBuilder withTags(String... tags) { public EditPersonDescriptor build() { return descriptor; } + } diff --git a/src/test/java/seedu/address/testutil/LeadUtil.java b/src/test/java/seedu/address/testutil/LeadUtil.java index 0175c7d4b24..cf80f45c67f 100644 --- a/src/test/java/seedu/address/testutil/LeadUtil.java +++ b/src/test/java/seedu/address/testutil/LeadUtil.java @@ -1,14 +1,10 @@ package seedu.address.testutil; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; - import seedu.address.logic.commands.AddLeadCommand; import seedu.address.model.person.Lead; +import static seedu.address.logic.parser.CliSyntax.*; + /** * A utility class for Lead. */ @@ -30,6 +26,7 @@ private static String getLeadDetails(Lead lead) { sb.append(PREFIX_PHONE + lead.getPhone().value + " "); sb.append(PREFIX_EMAIL + lead.getEmail().value + " "); sb.append(PREFIX_ADDRESS + lead.getAddress().value + " "); + sb.append(PREFIX_KEYMILESTONE + lead.getKeyMilestone().value + " "); lead.getTags().stream().forEach( s -> sb.append(PREFIX_TAG + s.tagName + " ") ); diff --git a/src/test/java/seedu/address/testutil/PersonBuilder.java b/src/test/java/seedu/address/testutil/PersonBuilder.java index 9b7dcf33f95..421c1707689 100644 --- a/src/test/java/seedu/address/testutil/PersonBuilder.java +++ b/src/test/java/seedu/address/testutil/PersonBuilder.java @@ -24,7 +24,6 @@ public class PersonBuilder { private Address address; private Set tags; private KeyMilestone keyMilestone; - /** * Creates a {@code PersonBuilder} with the default details. */ @@ -33,6 +32,7 @@ public PersonBuilder() { phone = new Phone(DEFAULT_PHONE); email = new Email(DEFAULT_EMAIL); address = new Address(DEFAULT_ADDRESS); + keyMilestone = new KeyMilestone(DEFAULT_KEYMILESTONE); tags = new HashSet<>(); } @@ -47,6 +47,15 @@ public PersonBuilder(Person personToCopy) { tags = new HashSet<>(personToCopy.getTags()); } + public PersonBuilder(Lead leadToCopy) { + name = leadToCopy.getName(); + phone = leadToCopy.getPhone(); + email = leadToCopy.getEmail(); + address = leadToCopy.getAddress(); + keyMilestone = leadToCopy.getKeyMilestone(); + tags = new HashSet<>(leadToCopy.getTags()); + } + /** * Sets the {@code Name} of the {@code Person} that we are building. */ @@ -87,11 +96,17 @@ public PersonBuilder withEmail(String email) { return this; } + //temporary fix: buildLead must call keyMilestone + public PersonBuilder withKeyMilestone(String keyMilestone) { + this.keyMilestone = new KeyMilestone(keyMilestone); + return this; + } + public Client buildClient() { return new Client(name, phone, email, address, tags); } public Lead buildLead() { - return new Lead(name, phone, email, address, tags); + return new Lead(name, phone, email, address, keyMilestone, tags); } } diff --git a/src/test/java/seedu/address/testutil/TypicalLeads.java b/src/test/java/seedu/address/testutil/TypicalLeads.java index d8329a9efd8..0b6766e4f7e 100644 --- a/src/test/java/seedu/address/testutil/TypicalLeads.java +++ b/src/test/java/seedu/address/testutil/TypicalLeads.java @@ -35,7 +35,7 @@ public class TypicalLeads { public static final Lead DANIEL = new PersonBuilder().withName("Daniel Meier").withPhone("87652533") .withEmail("cornelia@example.com").withAddress("10th street").withTags("friends").buildLead(); public static final Lead ELLE = new PersonBuilder().withName("Elle Meyer").withPhone("9482224") - .withEmail("werner@example.com").withAddress("michegan ave").buildLead(); + .withEmail("werner@example.com").withAddress("michegan ave").withKeyMilestone("2022-12-01").buildLead(); public static final Lead FIONA = new PersonBuilder().withName("Fiona Kunz").withPhone("9482427") .withEmail("lydia@example.com").withAddress("little tokyo").buildLead(); public static final Lead GEORGE = new PersonBuilder().withName("George Best").withPhone("9482442") diff --git a/src/test/java/seedu/address/testutil/TypicalPersons.java b/src/test/java/seedu/address/testutil/TypicalPersons.java index 70ee0efe345..8de05c1f394 100644 --- a/src/test/java/seedu/address/testutil/TypicalPersons.java +++ b/src/test/java/seedu/address/testutil/TypicalPersons.java @@ -1,16 +1,5 @@ package seedu.address.testutil; -import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_FRIEND; -import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -20,6 +9,8 @@ import seedu.address.model.person.Lead; import seedu.address.model.person.Person; +import static seedu.address.logic.commands.CommandTestUtil.*; + /** * A utility class containing a list of {@code Person} objects to be used in tests. */ @@ -38,11 +29,11 @@ public class TypicalPersons { public static final Client DANIEL = new PersonBuilder().withName("Daniel Meier").withPhone("87652533") .withEmail("cornelia@example.com").withAddress("10th street").withTags("friends").buildClient(); public static final Lead ELLE = new PersonBuilder().withName("Elle Meyer").withPhone("9482224") - .withEmail("werner@example.com").withAddress("michegan ave").buildLead(); + .withEmail("werner@example.com").withAddress("michegan ave").withKeyMilestone("2022-12-01").buildLead(); public static final Lead FIONA = new PersonBuilder().withName("Fiona Kunz").withPhone("9482427") - .withEmail("lydia@example.com").withAddress("little tokyo").buildLead(); + .withEmail("lydia@example.com").withAddress("little tokyo").withKeyMilestone("2022-12-01").buildLead(); public static final Lead GEORGE = new PersonBuilder().withName("George Best").withPhone("9482442") - .withEmail("anna@example.com").withAddress("4th street").buildLead(); + .withEmail("anna@example.com").withAddress("4th street").withKeyMilestone("2022-12-01").buildLead(); // Manually added public static final Client HOON = new PersonBuilder().withName("Hoon Meier").withPhone("8482424") @@ -55,6 +46,7 @@ public class TypicalPersons { .withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY).withTags(VALID_TAG_FRIEND).buildClient(); public static final Lead BOB = new PersonBuilder().withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) .withEmail(VALID_EMAIL_BOB).withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_HUSBAND, VALID_TAG_FRIEND) + .withKeyMilestone(VALID_KEYMILESTONE_BOB) .buildLead(); public static final String KEYWORD_MATCHING_MEIER = "Meier"; // A keyword that matches MEIER From dec98faeab1c68d8cb052f62f0ea3db3f67b3396 Mon Sep 17 00:00:00 2001 From: tiif Date: Thu, 26 Oct 2023 13:33:28 +0800 Subject: [PATCH 03/13] Add tests --- .../address/logic/commands/EditCommand.java | 30 +++- .../logic/commands/EditLeadCommand.java | 2 +- .../logic/parser/AddClientCommandParser.java | 1 - .../logic/parser/AddLeadCommandParser.java | 1 - .../logic/parser/EditCommandParser.java | 1 - .../address/model/person/KeyMilestone.java | 10 +- .../java/seedu/address/ui/PersonCard.java | 9 + .../logic/commands/AddCommandTest.java | 13 +- .../logic/commands/EditLeadCommandTest.java | 169 ++++++++++++++++++ .../parser/AddLeadCommandParserTest.java | 6 +- .../model/person/KeyMilestoneTest.java | 47 +++++ .../address/testutil/TypicalClients.java | 1 - 12 files changed, 267 insertions(+), 23 deletions(-) create mode 100644 src/test/java/seedu/address/logic/commands/EditLeadCommandTest.java create mode 100644 src/test/java/seedu/address/model/person/KeyMilestoneTest.java diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index ef8727ae637..63263394a8c 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -1,11 +1,7 @@ package seedu.address.logic.commands; import static java.util.Objects.requireNonNull; -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_NAME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.logic.parser.CliSyntax.*; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; import java.security.Key; @@ -75,11 +71,9 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail()); Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress()); Set updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags()); - //todo: change this - return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags); + return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags); } - //todo: make createeditedclient in future @Override public CommandResult execute(Model model) throws CommandException { requireNonNull(model); @@ -228,7 +222,6 @@ public boolean equals(Object other) { @Override public String toString() { return new ToStringBuilder(this) - //todo how to specify the toString for leads .add("name", name) .add("phone", phone) .add("email", email) @@ -250,6 +243,10 @@ public EditLeadDescriptor() { } + @Override + public boolean isAnyFieldEdited() { + return super.isAnyFieldEdited() && CollectionUtil.isAnyNonNull(keyMilestone); + } public Optional getKeyMilestone() { return Optional.ofNullable(keyMilestone); @@ -258,6 +255,21 @@ public Optional getKeyMilestone() { public void setKeyMilestone (KeyMilestone keyMilestone) { this.keyMilestone = keyMilestone; } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof EditLeadDescriptor)) { + return false; + } + EditLeadDescriptor otherEditLeadDescriptor = (EditLeadDescriptor) other; + return super.equals(other) && Objects.equals(keyMilestone, otherEditLeadDescriptor.keyMilestone); + } + @Override public String toString() { return super.toString() + new ToStringBuilder(this).add("key milestone", keyMilestone); diff --git a/src/main/java/seedu/address/logic/commands/EditLeadCommand.java b/src/main/java/seedu/address/logic/commands/EditLeadCommand.java index 52759e56973..f46829240e5 100644 --- a/src/main/java/seedu/address/logic/commands/EditLeadCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditLeadCommand.java @@ -31,7 +31,7 @@ public class EditLeadCommand extends EditCommand { + "[" + PREFIX_PHONE + "PHONE] " + "[" + PREFIX_EMAIL + "EMAIL] " + "[" + PREFIX_ADDRESS + "ADDRESS] " - + "[" + PREFIX_KEYMILESTONE+ "KEYMILESTONE] " + + "[" + PREFIX_KEYMILESTONE+ "KEY MILESTONE] " + "[" + PREFIX_TAG + "TAG]...\n" + "Example: " + COMMAND_WORD + " 1 " + PREFIX_PHONE + "91234567 " diff --git a/src/main/java/seedu/address/logic/parser/AddClientCommandParser.java b/src/main/java/seedu/address/logic/parser/AddClientCommandParser.java index fed52c69460..fc251a7ea1a 100644 --- a/src/main/java/seedu/address/logic/parser/AddClientCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AddClientCommandParser.java @@ -45,7 +45,6 @@ public AddClientCommand parse(String args) throws ParseException { Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()); Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); - // TODO: temporary fix, implement add Client and Lead commands Client client = new Client(name, phone, email, address, tagList); return new AddClientCommand(client); diff --git a/src/main/java/seedu/address/logic/parser/AddLeadCommandParser.java b/src/main/java/seedu/address/logic/parser/AddLeadCommandParser.java index bf93c4cd88a..3754907c48e 100644 --- a/src/main/java/seedu/address/logic/parser/AddLeadCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AddLeadCommandParser.java @@ -38,7 +38,6 @@ public AddLeadCommand parse(String args) throws ParseException { KeyMilestone keyMilestone = ParserUtil.parseKeyMilestone(argMultimap.getValue(PREFIX_KEYMILESTONE).get()); Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); - // TODO: temporary fix, implement add Client and Lead commands Lead lead = new Lead(name, phone, email, address, keyMilestone, tagList); return new AddLeadCommand(lead); diff --git a/src/main/java/seedu/address/logic/parser/EditCommandParser.java b/src/main/java/seedu/address/logic/parser/EditCommandParser.java index 58ffd97244c..2fe5cf71332 100644 --- a/src/main/java/seedu/address/logic/parser/EditCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/EditCommandParser.java @@ -42,7 +42,6 @@ public EditCommand parse(String args) throws ParseException { } argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_KEYMILESTONE, PREFIX_ADDRESS); - //todo: add another field for lead in future EditPersonDescriptor editPersonDescriptor; if (argMultimap.getValue(PREFIX_KEYMILESTONE).isPresent()) { isLead = true; diff --git a/src/main/java/seedu/address/model/person/KeyMilestone.java b/src/main/java/seedu/address/model/person/KeyMilestone.java index 292d15b3db3..7c36c05975f 100644 --- a/src/main/java/seedu/address/model/person/KeyMilestone.java +++ b/src/main/java/seedu/address/model/person/KeyMilestone.java @@ -8,7 +8,11 @@ public class KeyMilestone { "Key milestone is the date of Lead's milestone moment"; public static final String VALIDATION_REGEX = "^\\d{4}-\\d{2}-\\d{2}$"; public final String value; - //todo: add comment + /** + * Constructs an {@code KeyMilestone}. + * + * @param keyMilestone A valid date. + */ public KeyMilestone(String keyMilestone) { requireNonNull(keyMilestone); checkArgument(isValidKeyMilestone(keyMilestone), MESSAGE_CONSTRAINTS); @@ -17,7 +21,9 @@ public KeyMilestone(String keyMilestone) { this.value = keyMilestone; } - //todo: add comment + /** + * Returns if a given string is a valid date. + */ public static boolean isValidKeyMilestone(String test) { return test.matches(VALIDATION_REGEX); } diff --git a/src/main/java/seedu/address/ui/PersonCard.java b/src/main/java/seedu/address/ui/PersonCard.java index 2b2c63be26d..15120d3b3bb 100644 --- a/src/main/java/seedu/address/ui/PersonCard.java +++ b/src/main/java/seedu/address/ui/PersonCard.java @@ -7,6 +7,7 @@ import javafx.scene.layout.FlowPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Region; +import seedu.address.model.person.Lead; import seedu.address.model.person.Person; /** @@ -36,6 +37,10 @@ public class PersonCard extends UiPart { private Label phone; @FXML private Label address; + + @FXML + private Label keyMilestone; + @FXML private Label email; @FXML @@ -52,6 +57,10 @@ public PersonCard(Person person, int displayedIndex) { phone.setText(person.getPhone().value); address.setText(person.getAddress().value); email.setText(person.getEmail().value); + if (person.isLead()) { + Lead lead = (Lead) person; + keyMilestone.setText(lead.getKeyMilestone().value); + } Label label = new Label(person.getType().value); if (person.isClient()) { diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index 16b3ef6914d..5e8d99fbd48 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -5,7 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.testutil.Assert.assertThrows; -import static seedu.address.testutil.TypicalPersons.ALICE; +import static seedu.address.testutil.TypicalClients.ALICE; +import static seedu.address.testutil.TypicalLeads.BENSON; import java.nio.file.Path; import java.util.ArrayList; @@ -106,14 +107,20 @@ public void equalsForClientandLeads() { // different person -> returns false assertFalse(addClientCommand.equals(addLeadCommand)); } - //todo: test to string method for client and lead @Test - public void toStringMethod() { + public void clientToStringMethod() { AddCommand addCommand = new AddCommand(ALICE); String expected = AddCommand.class.getCanonicalName() + "{toAdd=" + ALICE + "}"; assertEquals(expected, addCommand.toString()); } + //put a lead here + @Test + public void leadToStringMethod() { + AddCommand addCommand = new AddCommand(BENSON); + String expected = AddCommand.class.getCanonicalName() + "{toAdd=" + BENSON + "}"; + assertEquals(expected, addCommand.toString()); + } /** * A default model stub that have all of the methods failing. */ diff --git a/src/test/java/seedu/address/logic/commands/EditLeadCommandTest.java b/src/test/java/seedu/address/logic/commands/EditLeadCommandTest.java new file mode 100644 index 00000000000..c9b8f956e14 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/EditLeadCommandTest.java @@ -0,0 +1,169 @@ +package seedu.address.logic.commands; + +import org.junit.jupiter.api.Test; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; +import seedu.address.model.AddressBook; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.testutil.EditLeadDescriptorBuilder; +import seedu.address.testutil.EditPersonDescriptorBuilder; + +import static org.junit.jupiter.api.Assertions.*; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import seedu.address.testutil.PersonBuilder; + +import seedu.address.model.person.Lead; + +class EditLeadCommandTest { + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + + @Test + public void execute_allFieldsSpecifiedUnfilteredList_success() { + Lead editedPerson = new PersonBuilder().buildLead(); + EditCommand.EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder(editedPerson).build(); + EditLeadCommand editCommand = new EditLeadCommand(INDEX_FIRST_PERSON, descriptor); + + String expectedMessage = String.format(EditLeadCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + + assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); + } + @Test + public void execute_someFieldsSpecifiedUnfilteredList_success() { + Index indexLastPerson = Index.fromOneBased(model.getFilteredPersonList().size()); + Person lastPerson = model.getFilteredPersonList().get(indexLastPerson.getZeroBased()); + + PersonBuilder personInList = new PersonBuilder(lastPerson); + Person editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) + .withTags(VALID_TAG_HUSBAND).buildLead(); + + EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB) + .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).build(); + EditCommand editCommand = new EditLeadCommand(indexLastPerson, descriptor); + + String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + expectedModel.setPerson(lastPerson, editedPerson); + + assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); + } + + @Test + public void execute_noFieldSpecifiedUnfilteredList_success() { + EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, new EditPersonDescriptor()); + Person editedPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + + String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + + assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); + } + + @Test + public void execute_filteredList_success() { + showPersonAtIndex(model, INDEX_FIRST_PERSON); + + Person personInFilteredList = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + Person editedPerson = new PersonBuilder(personInFilteredList).withName(VALID_NAME_BOB).buildClient(); + EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, + new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); + + String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + + assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); + } + + @Test + public void execute_duplicatePersonUnfilteredList_failure() { + Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstPerson).build(); + EditCommand editCommand = new EditCommand(INDEX_SECOND_PERSON, descriptor); + + assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON); + } + + @Test + public void execute_duplicatePersonFilteredList_failure() { + showPersonAtIndex(model, INDEX_FIRST_PERSON); + + // edit person in filtered list into a duplicate in address book + Person personInList = model.getAddressBook().getPersonList().get(INDEX_SECOND_PERSON.getZeroBased()); + EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, + new EditPersonDescriptorBuilder(personInList).build()); + + assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON); + } + + @Test + public void execute_invalidPersonIndexUnfilteredList_failure() { + Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); + EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build(); + EditCommand editCommand = new EditCommand(outOfBoundIndex, descriptor); + + assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + /** + * Edit filtered list where index is larger than size of filtered list, + * but smaller than size of address book + */ + @Test + public void execute_invalidPersonIndexFilteredList_failure() { + showPersonAtIndex(model, INDEX_FIRST_PERSON); + Index outOfBoundIndex = INDEX_SECOND_PERSON; + // ensures that outOfBoundIndex is still in bounds of address book list + assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size()); + + EditCommand editCommand = new EditCommand(outOfBoundIndex, + new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); + + assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + @Test + public void equals() { + final EditCommand standardCommand = new EditCommand(INDEX_FIRST_PERSON, DESC_AMY); + + // same values -> returns true + EditPersonDescriptor copyDescriptor = new EditPersonDescriptor(DESC_AMY); + EditCommand commandWithSameValues = new EditCommand(INDEX_FIRST_PERSON, copyDescriptor); + assertTrue(standardCommand.equals(commandWithSameValues)); + + // same object -> returns true + assertTrue(standardCommand.equals(standardCommand)); + + // null -> returns false + assertFalse(standardCommand.equals(null)); + + // different types -> returns false + assertFalse(standardCommand.equals(new ClearCommand())); + + // different index -> returns false + assertFalse(standardCommand.equals(new EditCommand(INDEX_SECOND_PERSON, DESC_AMY))); + + // different descriptor -> returns false + assertFalse(standardCommand.equals(new EditCommand(INDEX_FIRST_PERSON, DESC_BOB))); + } + + @Test + public void toStringMethod() { + Index index = Index.fromOneBased(1); + EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor(); + EditCommand editCommand = new EditCommand(index, editPersonDescriptor); + String expected = EditCommand.class.getCanonicalName() + "{index=" + index + ", editPersonDescriptor=" + + editPersonDescriptor + "}"; + assertEquals(expected, editCommand.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/seedu/address/logic/parser/AddLeadCommandParserTest.java b/src/test/java/seedu/address/logic/parser/AddLeadCommandParserTest.java index a58fb73a86a..e4b04b7eba1 100644 --- a/src/test/java/seedu/address/logic/parser/AddLeadCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddLeadCommandParserTest.java @@ -35,10 +35,9 @@ public void parse_allFieldsPresent_success() { NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + TAG_DESC_HUSBAND + TAG_DESC_FRIEND, new AddLeadCommand(expectedLeadMultipleTags)); } - //todo: fix the message error @Test - public void parse_repeatedNonTagValue_failure() { - //todo: need another test for clients + public void parseLead_repeatedNonTagValue_failure() { + //todo: need another test for clients in future String validExpectedPersonString = NAME_DESC_BOB + PHONE_DESC_BOB + EMAIL_DESC_BOB + ADDRESS_DESC_BOB + KEYMILESTONE_DESC_BOB + TAG_DESC_FRIEND; @@ -59,7 +58,6 @@ public void parse_repeatedNonTagValue_failure() { Messages.getErrorMessageForDuplicatePrefixes(PREFIX_ADDRESS)); // multiple fields repeated - //todo: fix this test, it failed assertParseFailure(parser, validExpectedPersonString + PHONE_DESC_AMY + EMAIL_DESC_AMY + NAME_DESC_AMY + ADDRESS_DESC_AMY + KEYMILESTONE_DESC_BOB + validExpectedPersonString, diff --git a/src/test/java/seedu/address/model/person/KeyMilestoneTest.java b/src/test/java/seedu/address/model/person/KeyMilestoneTest.java new file mode 100644 index 00000000000..bd8bf744fb1 --- /dev/null +++ b/src/test/java/seedu/address/model/person/KeyMilestoneTest.java @@ -0,0 +1,47 @@ +package seedu.address.model.person; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class KeyMilestoneTest { + + @Test + void isValidKeyMilestone() { + assertFalse(KeyMilestone.isValidKeyMilestone("01-01-2022")); + assertFalse(KeyMilestone.isValidKeyMilestone("01/01/2022")); + assertFalse(KeyMilestone.isValidKeyMilestone("01.01.2022")); + //assertFalse(KeyMilestone.isValidKeyMilestone("2022-02-31")); + assertTrue(KeyMilestone.isValidKeyMilestone("2022-01-29")); + assertTrue(KeyMilestone.isValidKeyMilestone("2022-03-31")); + } + + @Test + void equals() { + KeyMilestone keyMilestone = new KeyMilestone("2022-12-01"); + + // same values -> returns true + assertTrue(keyMilestone.equals(new KeyMilestone("2022-12-01"))); + + // same object -> returns true + assertTrue(keyMilestone.equals(keyMilestone)); + + // null -> returns false + assertFalse(keyMilestone.equals(null)); + + // different types -> returns false + assertFalse(keyMilestone.equals(5.0f)); + + // different values -> returns false + assertFalse(keyMilestone.equals(new KeyMilestone("2022-01-01"))); + } + + @Test + void hashCode_equal() { + assertEquals(new KeyMilestone("2022-12-01").hashCode(), + new KeyMilestone("2022-12-01").hashCode()); + } +} \ No newline at end of file diff --git a/src/test/java/seedu/address/testutil/TypicalClients.java b/src/test/java/seedu/address/testutil/TypicalClients.java index 5a3f40459f8..6ba40c56fd6 100644 --- a/src/test/java/seedu/address/testutil/TypicalClients.java +++ b/src/test/java/seedu/address/testutil/TypicalClients.java @@ -72,7 +72,6 @@ public static AddressBook getTypicalClientsAddressBook() { } public static List getTypicalClients() { - //todo: change here return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE)); } } From d5a522d2525765d64c21c40c4ab25e26472426cb Mon Sep 17 00:00:00 2001 From: tiif Date: Sat, 28 Oct 2023 01:13:46 +0800 Subject: [PATCH 04/13] Add testcases --- build.gradle | 4 + .../java/seedu/address/logic/Messages.java | 5 + .../address/logic/commands/EditCommand.java | 10 +- .../logic/commands/EditLeadCommand.java | 13 +- .../address/storage/JsonAdaptedLead.java | 122 ------------------ .../logic/commands/CommandTestUtil.java | 3 +- .../logic/commands/EditCommandTest.java | 6 +- .../logic/commands/EditLeadCommandTest.java | 114 +++++++++------- .../storage/JsonAdaptedPersonTest.java | 7 + .../seedu/address/testutil/TypicalLeads.java | 42 +++--- 10 files changed, 124 insertions(+), 202 deletions(-) delete mode 100644 src/main/java/seedu/address/storage/JsonAdaptedLead.java diff --git a/build.gradle b/build.gradle index a2951cc709e..9a21fdccd3d 100644 --- a/build.gradle +++ b/build.gradle @@ -69,4 +69,8 @@ shadowJar { archiveFileName = 'addressbook.jar' } +run { + enableAssertions = true +} + defaultTasks 'clean', 'test' diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index 1faadff1cfe..f553c674ced 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -36,6 +36,10 @@ public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePref * Formats the {@code person} for display to the user. */ public static String format(Person person) { + if (person.isLead()) { + return format((Lead) person); + } + assert(!person.isLead()); final StringBuilder builder = new StringBuilder(); builder.append(person.getName()) .append("; Phone: ") @@ -51,6 +55,7 @@ public static String format(Person person) { public static String format(Lead lead) { + assert(lead.isLead()); final StringBuilder builder = new StringBuilder(); builder.append(lead.getName()) .append("; Phone: ") diff --git a/src/main/java/seedu/address/logic/commands/EditCommand.java b/src/main/java/seedu/address/logic/commands/EditCommand.java index 63263394a8c..c2bf30d9069 100644 --- a/src/main/java/seedu/address/logic/commands/EditCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditCommand.java @@ -41,6 +41,7 @@ public class EditCommand extends Command { + PREFIX_EMAIL + "johndoe@example.com"; public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s"; + public static final String MESSAGE_EDIT_LEAD_SUCCESS = "Edited Lead: %1$s"; public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book."; @@ -272,7 +273,14 @@ public boolean equals(Object other) { @Override public String toString() { - return super.toString() + new ToStringBuilder(this).add("key milestone", keyMilestone); + return new ToStringBuilder(this) + .add("name", super.name) + .add("phone", super.phone) + .add("email", super.email) + .add("address", super.address) + .add("key milestone", keyMilestone) + .add("tags", super.tags) + .toString(); } } } diff --git a/src/main/java/seedu/address/logic/commands/EditLeadCommand.java b/src/main/java/seedu/address/logic/commands/EditLeadCommand.java index f46829240e5..4c025e83d2e 100644 --- a/src/main/java/seedu/address/logic/commands/EditLeadCommand.java +++ b/src/main/java/seedu/address/logic/commands/EditLeadCommand.java @@ -7,7 +7,6 @@ import seedu.address.model.person.*; import java.util.List; -import java.util.Optional; import java.util.Set; import seedu.address.commons.core.index.Index; @@ -65,20 +64,20 @@ public CommandResult execute(Model model) throws CommandException { List lastShownList = model.getFilteredPersonList(); if (index.getZeroBased() >= lastShownList.size()) { - //todo: fix the message so it is for lead throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } - Person personToEdit = lastShownList.get(index.getZeroBased()); - Person editedPerson = createEditedLead((Lead) personToEdit, editLeadDescriptor); + Lead leadToEdit = (Lead) lastShownList.get(index.getZeroBased()); + assert(leadToEdit.isLead()); + Lead editedLead = createEditedLead((Lead) leadToEdit, editLeadDescriptor); - if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) { + if (!leadToEdit.isSamePerson(editedLead) && model.hasPerson(editedLead)) { throw new CommandException(MESSAGE_DUPLICATE_PERSON); } - model.setPerson(personToEdit, editedPerson); + model.setPerson(leadToEdit, editedLead); model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); - return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson))); + return new CommandResult(String.format(MESSAGE_EDIT_LEAD_SUCCESS, Messages.format(editedLead))); } @Override diff --git a/src/main/java/seedu/address/storage/JsonAdaptedLead.java b/src/main/java/seedu/address/storage/JsonAdaptedLead.java deleted file mode 100644 index 66fcbc8af1b..00000000000 --- a/src/main/java/seedu/address/storage/JsonAdaptedLead.java +++ /dev/null @@ -1,122 +0,0 @@ -package seedu.address.storage; - -import static seedu.address.model.person.Client.TYPE_CLIENT; -import static seedu.address.model.person.Lead.TYPE_LEAD; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -import seedu.address.commons.exceptions.IllegalValueException; -import seedu.address.model.person.*; -import seedu.address.model.tag.Tag; -public class JsonAdaptedLead{ - public static final String MISSING_FIELD_MESSAGE_FORMAT = "Lead's %s field is missing!"; - - private final String name; - private final String phone; - private final String email; - private final String type; - private final String address; - private final String keyMilestone; - private final List tags = new ArrayList<>(); - - /** - * Constructs a {@code JsonAdaptedLead} with the given person details. - */ - @JsonCreator - public JsonAdaptedLead(@JsonProperty("name") String name, @JsonProperty("phone") String phone, - @JsonProperty("email") String email, @JsonProperty("type") String type, - @JsonProperty("address") String address, - @JsonProperty("key milestone") String keyMilestone, - @JsonProperty("tags") List tags) { - this.name = name; - this.phone = phone; - this.email = email; - this.type = type; - this.address = address; - this.keyMilestone = keyMilestone; - if (tags != null) { - this.tags.addAll(tags); - } - } - - /** - * Converts a given {@code Lead} into this class for Jackson use. - */ - public JsonAdaptedLead(Lead source) { - name = source.getName().fullName; - phone = source.getPhone().value; - email = source.getEmail().value; - address = source.getAddress().value; - type = source.getType().value; - keyMilestone = source.getKeyMilestone().value; - tags.addAll(source.getTags().stream() - .map(JsonAdaptedTag::new) - .collect(Collectors.toList())); - } - - /** - * Converts this Jackson-friendly adapted person object into the model's {@code Lead} object. - * - * @throws IllegalValueException if there were any data constraints violated in the adapted person. - */ - public Lead toModelType() throws IllegalValueException { - final List personTags = new ArrayList<>(); - for (JsonAdaptedTag tag : tags) { - personTags.add(tag.toModelType()); - } - - if (name == null) { - throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName())); - } - if (!Name.isValidName(name)) { - throw new IllegalValueException(Name.MESSAGE_CONSTRAINTS); - } - final Name modelName = new Name(name); - - if (phone == null) { - throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Phone.class.getSimpleName())); - } - if (!Phone.isValidPhone(phone)) { - throw new IllegalValueException(Phone.MESSAGE_CONSTRAINTS); - } - final Phone modelPhone = new Phone(phone); - - 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); - } - final Email modelEmail = new Email(email); - - 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); - } - if (!KeyMilestone.isValidKeyMilestone(keyMilestone)) { - throw new IllegalValueException(KeyMilestone.MESSAGE_CONSTRAINTS); - } - final KeyMilestone modelKeyMilestone = new KeyMilestone(keyMilestone); - final Address modelAddress = new Address(address); - - final Set modelTags = new HashSet<>(personTags); - - if (type == null) { - throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, "Type")); - } - if (type.equals(TYPE_LEAD)) { - return new Lead(modelName, modelPhone, modelEmail, modelAddress, modelKeyMilestone, modelTags); - } else { - throw new IllegalValueException(Type.MESSAGE_CONSTRAINTS); - } - } -} diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index 51410bc14fa..7f429896702 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -32,7 +32,8 @@ public class CommandTestUtil { public static final String VALID_ADDRESS_BOB = "Block 123, Bobby Street 3"; public static final String VALID_TAG_HUSBAND = "husband"; - public static final String VALID_KEYMILESTONE_BOB= "2022-12-01"; + public static final String VALID_KEYMILESTONE_AMY = "2022-12-01"; + public static final String VALID_KEYMILESTONE_BOB = "2022-12-01"; public static final String VALID_TAG_FRIEND = "friend"; public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY; diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index da3da6c5440..43607887d11 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -50,11 +50,11 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() { PersonBuilder personInList = new PersonBuilder(lastPerson); Person editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) - .withTags(VALID_TAG_HUSBAND).buildLead(); + .withTags(VALID_TAG_HUSBAND).buildClient(); - EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB) + EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB) .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).build(); - EditCommand editCommand = new EditLeadCommand(indexLastPerson, descriptor); + EditCommand editCommand = new EditCommand(indexLastPerson, descriptor); String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); diff --git a/src/test/java/seedu/address/logic/commands/EditLeadCommandTest.java b/src/test/java/seedu/address/logic/commands/EditLeadCommandTest.java index c9b8f956e14..95d5261a4f1 100644 --- a/src/test/java/seedu/address/logic/commands/EditLeadCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditLeadCommandTest.java @@ -1,54 +1,64 @@ package seedu.address.logic.commands; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.logic.commands.CommandTestUtil.*; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; +import static seedu.address.testutil.TypicalLeads.getTypicalLeadsAddressBook; + import org.junit.jupiter.api.Test; + import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; +import seedu.address.logic.commands.EditCommand.EditLeadDescriptor; import seedu.address.model.AddressBook; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; +import seedu.address.model.person.Lead; +import seedu.address.model.person.Person; import seedu.address.testutil.EditLeadDescriptorBuilder; -import seedu.address.testutil.EditPersonDescriptorBuilder; - -import static org.junit.jupiter.api.Assertions.*; -import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; -import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; -import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; - import seedu.address.testutil.PersonBuilder; -import seedu.address.model.person.Lead; - +//todo: fix this class EditLeadCommandTest { - private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); - + private Model model = new ModelManager(getTypicalLeadsAddressBook(), new UserPrefs()); + EditLeadDescriptor DESC_AMY_LEAD = new EditLeadDescriptorBuilder().withName(VALID_NAME_AMY) + .withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY) + .withTags(VALID_TAG_FRIEND).build(); + //fix this @Test public void execute_allFieldsSpecifiedUnfilteredList_success() { - Lead editedPerson = new PersonBuilder().buildLead(); - EditCommand.EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder(editedPerson).build(); - EditLeadCommand editCommand = new EditLeadCommand(INDEX_FIRST_PERSON, descriptor); + Lead editedLead = new PersonBuilder().withKeyMilestone("2022-12-01").buildLead(); + EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder(editedLead).build(); + EditLeadCommand editLeadCommand = new EditLeadCommand(INDEX_FIRST_PERSON, descriptor); - String expectedMessage = String.format(EditLeadCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + String expectedMessage = String.format(EditLeadCommand.MESSAGE_EDIT_LEAD_SUCCESS, Messages.format(editedLead)); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); - expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + expectedModel.setPerson(model.getFilteredPersonList().get(0), editedLead); - assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); + assertCommandSuccess(editLeadCommand, model, expectedMessage, expectedModel); } @Test public void execute_someFieldsSpecifiedUnfilteredList_success() { Index indexLastPerson = Index.fromOneBased(model.getFilteredPersonList().size()); - Person lastPerson = model.getFilteredPersonList().get(indexLastPerson.getZeroBased()); + Lead lastPerson = (Lead) model.getFilteredPersonList().get(indexLastPerson.getZeroBased()); + assert (lastPerson.isLead()); PersonBuilder personInList = new PersonBuilder(lastPerson); - Person editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) - .withTags(VALID_TAG_HUSBAND).buildLead(); + Lead editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) + .withTags(VALID_TAG_HUSBAND).withKeyMilestone(VALID_KEYMILESTONE_BOB).buildLead(); EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB) - .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).build(); - EditCommand editCommand = new EditLeadCommand(indexLastPerson, descriptor); + .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).withKeyMilestone(VALID_KEYMILESTONE_BOB) + .build(); + + EditLeadCommand editCommand = new EditLeadCommand(indexLastPerson, descriptor); - String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_LEAD_SUCCESS, Messages.format(editedPerson)); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); expectedModel.setPerson(lastPerson, editedPerson); @@ -58,10 +68,11 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() { @Test public void execute_noFieldSpecifiedUnfilteredList_success() { - EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, new EditPersonDescriptor()); - Person editedPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + EditLeadCommand editCommand = new EditLeadCommand(INDEX_FIRST_PERSON, new EditLeadDescriptor()); + Lead editedPerson = (Lead) model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + assert (editedPerson.isLead()); - String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_LEAD_SUCCESS, Messages.format(editedPerson)); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); @@ -72,24 +83,26 @@ public void execute_noFieldSpecifiedUnfilteredList_success() { public void execute_filteredList_success() { showPersonAtIndex(model, INDEX_FIRST_PERSON); - Person personInFilteredList = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); - Person editedPerson = new PersonBuilder(personInFilteredList).withName(VALID_NAME_BOB).buildClient(); - EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, - new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); + Lead personInFilteredList = (Lead) model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + assert(personInFilteredList.isLead()); + Lead editedLead = new PersonBuilder(personInFilteredList).withName(VALID_NAME_BOB).buildLead(); + EditLeadCommand editCommand = new EditLeadCommand(INDEX_FIRST_PERSON, + new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB).withKeyMilestone(VALID_KEYMILESTONE_BOB).build()); - String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); + String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_LEAD_SUCCESS, Messages.format(editedLead)); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); - expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); + expectedModel.setPerson(model.getFilteredPersonList().get(0), editedLead); assertCommandSuccess(editCommand, model, expectedMessage, expectedModel); } @Test public void execute_duplicatePersonUnfilteredList_failure() { - Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); - EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstPerson).build(); - EditCommand editCommand = new EditCommand(INDEX_SECOND_PERSON, descriptor); + Lead firstPerson = (Lead) model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + assert(firstPerson.isLead()); + EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder(firstPerson).build(); + EditLeadCommand editCommand = new EditLeadCommand(INDEX_SECOND_PERSON, descriptor); assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON); } @@ -100,8 +113,9 @@ public void execute_duplicatePersonFilteredList_failure() { // edit person in filtered list into a duplicate in address book Person personInList = model.getAddressBook().getPersonList().get(INDEX_SECOND_PERSON.getZeroBased()); - EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, - new EditPersonDescriptorBuilder(personInList).build()); + assert(personInList.isLead()); + EditLeadCommand editCommand = new EditLeadCommand(INDEX_FIRST_PERSON, + new EditLeadDescriptorBuilder((Lead) personInList).build()); assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON); } @@ -109,8 +123,8 @@ public void execute_duplicatePersonFilteredList_failure() { @Test public void execute_invalidPersonIndexUnfilteredList_failure() { Index outOfBoundIndex = Index.fromOneBased(model.getFilteredPersonList().size() + 1); - EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build(); - EditCommand editCommand = new EditCommand(outOfBoundIndex, descriptor); + EditLeadDescriptor descriptor = new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB).build(); + EditLeadCommand editCommand = new EditLeadCommand(outOfBoundIndex, descriptor); assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } @@ -126,19 +140,19 @@ public void execute_invalidPersonIndexFilteredList_failure() { // ensures that outOfBoundIndex is still in bounds of address book list assertTrue(outOfBoundIndex.getZeroBased() < model.getAddressBook().getPersonList().size()); - EditCommand editCommand = new EditCommand(outOfBoundIndex, - new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); + EditLeadCommand editCommand = new EditLeadCommand(outOfBoundIndex, + new EditLeadDescriptorBuilder().withName(VALID_NAME_BOB).build()); assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } @Test public void equals() { - final EditCommand standardCommand = new EditCommand(INDEX_FIRST_PERSON, DESC_AMY); + final EditLeadCommand standardCommand = new EditLeadCommand(INDEX_FIRST_PERSON, DESC_AMY_LEAD); // same values -> returns true - EditPersonDescriptor copyDescriptor = new EditPersonDescriptor(DESC_AMY); - EditCommand commandWithSameValues = new EditCommand(INDEX_FIRST_PERSON, copyDescriptor); + EditLeadDescriptor copyDescriptor = new EditLeadDescriptor(DESC_AMY_LEAD); + EditLeadCommand commandWithSameValues = new EditLeadCommand(INDEX_FIRST_PERSON, copyDescriptor); assertTrue(standardCommand.equals(commandWithSameValues)); // same object -> returns true @@ -151,7 +165,7 @@ public void equals() { assertFalse(standardCommand.equals(new ClearCommand())); // different index -> returns false - assertFalse(standardCommand.equals(new EditCommand(INDEX_SECOND_PERSON, DESC_AMY))); + assertFalse(standardCommand.equals(new EditCommand(INDEX_SECOND_PERSON, DESC_AMY_LEAD))); // different descriptor -> returns false assertFalse(standardCommand.equals(new EditCommand(INDEX_FIRST_PERSON, DESC_BOB))); @@ -160,10 +174,10 @@ public void equals() { @Test public void toStringMethod() { Index index = Index.fromOneBased(1); - EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor(); - EditCommand editCommand = new EditCommand(index, editPersonDescriptor); - String expected = EditCommand.class.getCanonicalName() + "{index=" + index + ", editPersonDescriptor=" - + editPersonDescriptor + "}"; - assertEquals(expected, editCommand.toString()); + EditLeadDescriptor editLeadDescriptor = new EditLeadDescriptor(); + EditLeadCommand editLeadCommand = new EditLeadCommand(index, editLeadDescriptor); + String expected = EditLeadCommand.class.getCanonicalName() + "{index=" + index + ", editLeadDescriptor=" + + editLeadDescriptor + "}"; + assertEquals(expected, editLeadCommand.toString()); } } \ No newline at end of file diff --git a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java index 627744d76ba..ac3223bfd78 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java @@ -142,6 +142,13 @@ public void toModelType_invalidTags_throwsIllegalValueException() { assertThrows(IllegalValueException.class, person::toModelType); } @Test + public void toModelType_leadWithInvalidKeyMilestone_throwsIllegalValueException() { + String invalidKeyMilestone = "12345"; + JsonAdaptedPerson person = + new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, "lead", VALID_ADDRESS, "12345", VALID_TAGS); + assertThrows(IllegalValueException.class, person::toModelType); + } + @Test public void toModelType_invalidTags_withKeyMilestone_throwsIllegalValueException() { List invalidTags = new ArrayList<>(VALID_TAGS); invalidTags.add(new JsonAdaptedTag(INVALID_TAG)); diff --git a/src/test/java/seedu/address/testutil/TypicalLeads.java b/src/test/java/seedu/address/testutil/TypicalLeads.java index 0b6766e4f7e..3d6747297b3 100644 --- a/src/test/java/seedu/address/testutil/TypicalLeads.java +++ b/src/test/java/seedu/address/testutil/TypicalLeads.java @@ -1,16 +1,5 @@ package seedu.address.testutil; -import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_AMY; -import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; -import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_FRIEND; -import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -18,6 +7,8 @@ import seedu.address.model.AddressBook; import seedu.address.model.person.Lead; +import static seedu.address.logic.commands.CommandTestUtil.*; + /** * A utility class containing a list of {@code Lead} objects to be used in tests. */ @@ -25,33 +16,48 @@ public class TypicalLeads { public static final Lead ALICE = new PersonBuilder().withName("Alice Pauline") .withAddress("123, Jurong West Ave 6, #08-111").withEmail("alice@example.com") .withPhone("94351253") + .withKeyMilestone("2022-12-01") .withTags("friends").buildLead(); public static final Lead BENSON = new PersonBuilder().withName("Benson Meier") .withAddress("311, Clementi Ave 2, #02-25") .withEmail("johnd@example.com").withPhone("98765432") + .withKeyMilestone("2022-12-01") .withTags("owesMoney", "friends").buildLead(); public static final Lead CARL = new PersonBuilder().withName("Carl Kurz").withPhone("95352563") - .withEmail("heinz@example.com").withAddress("wall street").buildLead(); + .withEmail("heinz@example.com").withAddress("wall street") + .withKeyMilestone("2022-12-01") + .buildLead(); public static final Lead DANIEL = new PersonBuilder().withName("Daniel Meier").withPhone("87652533") - .withEmail("cornelia@example.com").withAddress("10th street").withTags("friends").buildLead(); + .withEmail("cornelia@example.com").withAddress("10th street").withTags("friends") + .withKeyMilestone("2022-12-01") + .buildLead(); public static final Lead ELLE = new PersonBuilder().withName("Elle Meyer").withPhone("9482224") .withEmail("werner@example.com").withAddress("michegan ave").withKeyMilestone("2022-12-01").buildLead(); public static final Lead FIONA = new PersonBuilder().withName("Fiona Kunz").withPhone("9482427") - .withEmail("lydia@example.com").withAddress("little tokyo").buildLead(); + .withEmail("lydia@example.com").withAddress("little tokyo") + .withKeyMilestone("2022-12-01") + .buildLead(); public static final Lead GEORGE = new PersonBuilder().withName("George Best").withPhone("9482442") - .withEmail("anna@example.com").withAddress("4th street").buildLead(); + .withEmail("anna@example.com").withAddress("4th street") + .withKeyMilestone("2022-12-01") + .buildLead(); // Manually added public static final Lead HOON = new PersonBuilder().withName("Hoon Meier").withPhone("8482424") - .withEmail("stefan@example.com").withAddress("little india").buildLead(); + .withEmail("stefan@example.com").withAddress("little india") + .withKeyMilestone("2022-12-01") + .buildLead(); public static final Lead IDA = new PersonBuilder().withName("Ida Mueller").withPhone("8482131") - .withEmail("hans@example.com").withAddress("chicago ave").buildLead(); + .withEmail("hans@example.com").withAddress("chicago ave") + .withKeyMilestone("2022-12-01") + .buildLead(); // Manually added - Lead's details found in {@code CommandTestUtil} public static final Lead AMY = new PersonBuilder().withName(VALID_NAME_AMY).withPhone(VALID_PHONE_AMY) - .withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY).withTags(VALID_TAG_FRIEND).buildLead(); + .withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY).withTags(VALID_TAG_FRIEND).withKeyMilestone(VALID_KEYMILESTONE_AMY).buildLead(); public static final Lead BOB = new PersonBuilder().withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) .withEmail(VALID_EMAIL_BOB).withAddress(VALID_ADDRESS_BOB).withTags(VALID_TAG_HUSBAND, VALID_TAG_FRIEND) + .withKeyMilestone(VALID_KEYMILESTONE_BOB) .buildLead(); public static final String KEYWORD_MATCHING_MEIER = "Meier"; // A keyword that matches MEIER From 568462b44f168032ae560274483994172c357017 Mon Sep 17 00:00:00 2001 From: tiif Date: Sat, 28 Oct 2023 02:13:55 +0800 Subject: [PATCH 05/13] Use LocalDateTime in KeyMilestone validation --- .../seedu/address/model/person/KeyMilestone.java | 16 +++++++++++----- .../logic/parser/AddressBookParserTest.java | 2 +- .../address/model/person/KeyMilestoneTest.java | 1 + 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/seedu/address/model/person/KeyMilestone.java b/src/main/java/seedu/address/model/person/KeyMilestone.java index 7c36c05975f..ba98a84ae87 100644 --- a/src/main/java/seedu/address/model/person/KeyMilestone.java +++ b/src/main/java/seedu/address/model/person/KeyMilestone.java @@ -1,12 +1,15 @@ package seedu.address.model.person; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; public class KeyMilestone { public static final String MESSAGE_CONSTRAINTS = - "Key milestone is the date of Lead's milestone moment"; - public static final String VALIDATION_REGEX = "^\\d{4}-\\d{2}-\\d{2}$"; + "Key milestone is the date of Lead's milestone moment, should be in the format of yyyy-MM-dd"; public final String value; /** * Constructs an {@code KeyMilestone}. @@ -16,8 +19,6 @@ public class KeyMilestone { public KeyMilestone(String keyMilestone) { requireNonNull(keyMilestone); checkArgument(isValidKeyMilestone(keyMilestone), MESSAGE_CONSTRAINTS); - //todo: use SimpleDateFormat if needed - // https://stackoverflow.com/questions/2149680/regex-date-format-validation-on-java this.value = keyMilestone; } @@ -25,7 +26,12 @@ public KeyMilestone(String keyMilestone) { * Returns if a given string is a valid date. */ public static boolean isValidKeyMilestone(String test) { - return test.matches(VALIDATION_REGEX); + try { + LocalDate.parse(test, DateTimeFormatter.ofPattern("yyyy-MM-dd")); + return true; + } catch (DateTimeParseException e) { + return false; + } } @Override diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index a89cb239f61..c11ac73c11f 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -48,7 +48,7 @@ public void parseCommand_addClient() throws Exception { @Test public void parseCommand_addLead() throws Exception { - Lead lead = new PersonBuilder(ELLE).buildLead(); + Lead lead = new PersonBuilder(ELLE).withKeyMilestone("2022-12-01").buildLead(); AddLeadCommand command = (AddLeadCommand) parser.parseCommand(LeadUtil.getAddLeadCommand(lead)); assertEquals(new AddLeadCommand(lead), command); } diff --git a/src/test/java/seedu/address/model/person/KeyMilestoneTest.java b/src/test/java/seedu/address/model/person/KeyMilestoneTest.java index bd8bf744fb1..67d0f9082b4 100644 --- a/src/test/java/seedu/address/model/person/KeyMilestoneTest.java +++ b/src/test/java/seedu/address/model/person/KeyMilestoneTest.java @@ -11,6 +11,7 @@ class KeyMilestoneTest { @Test void isValidKeyMilestone() { + //todo: fix needed for local date time assertFalse(KeyMilestone.isValidKeyMilestone("01-01-2022")); assertFalse(KeyMilestone.isValidKeyMilestone("01/01/2022")); assertFalse(KeyMilestone.isValidKeyMilestone("01.01.2022")); From f187ce1b8490f3558bf1740711b8c26026673969 Mon Sep 17 00:00:00 2001 From: tiif Date: Sun, 29 Oct 2023 20:45:36 +0800 Subject: [PATCH 06/13] Update the UI --- src/main/java/seedu/address/ui/PersonCard.java | 8 -------- src/main/java/seedu/address/ui/ViewWindow.java | 12 +++++++++++- src/main/resources/view/ViewWindow.fxml | 5 +++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/seedu/address/ui/PersonCard.java b/src/main/java/seedu/address/ui/PersonCard.java index 1809749321b..2dd729158bf 100644 --- a/src/main/java/seedu/address/ui/PersonCard.java +++ b/src/main/java/seedu/address/ui/PersonCard.java @@ -37,10 +37,6 @@ public class PersonCard extends UiPart { private Label phone; @FXML private Label address; - - @FXML - private Label keyMilestone; - @FXML private Label meetingTime; @FXML @@ -60,10 +56,6 @@ public PersonCard(Person person, int displayedIndex) { address.setText(person.getAddress().value); meetingTime.setText("Meeting on: " + person.getMeetingTime().toString()); email.setText(person.getEmail().value); - if (person.isLead()) { - Lead lead = (Lead) person; - keyMilestone.setText(lead.getKeyMilestone().value); - } Label label = new Label(person.getType().value); if (person.isClient()) { diff --git a/src/main/java/seedu/address/ui/ViewWindow.java b/src/main/java/seedu/address/ui/ViewWindow.java index 21e8f6317d7..4ad562cc973 100644 --- a/src/main/java/seedu/address/ui/ViewWindow.java +++ b/src/main/java/seedu/address/ui/ViewWindow.java @@ -9,6 +9,7 @@ import javafx.scene.layout.HBox; import javafx.scene.layout.Region; import seedu.address.commons.core.LogsCenter; +import seedu.address.model.person.Lead; import seedu.address.model.person.Person; /** @@ -42,6 +43,9 @@ public class ViewWindow extends UiPart { private Label address; @FXML private Label email; + + @FXML + private Label keyMilestone; @FXML private FlowPane tags; @@ -59,7 +63,13 @@ public ViewWindow(Person person, int displayedIndex) { phone.setText(person.getPhone().value); address.setText(person.getAddress().value); email.setText(person.getEmail().value); - + if (person.isLead()) { + Lead convertedPerson = (Lead) person; + keyMilestone.setText("Key Milestone: " + convertedPerson.getKeyMilestone().value); + } else { + //keyMilestone should be at the last line of the ui, if not the empty line will be obvious + keyMilestone.setText(""); + } Label label = new Label(person.getType().value); if (person.isClient()) { label.getStyleClass().add("client-label"); diff --git a/src/main/resources/view/ViewWindow.fxml b/src/main/resources/view/ViewWindow.fxml index b078982cfca..a53fc74ad1c 100644 --- a/src/main/resources/view/ViewWindow.fxml +++ b/src/main/resources/view/ViewWindow.fxml @@ -80,6 +80,11 @@ Additional Information: + + + From 0b79201ba1d90f38389134e51930a59d28bf8c4b Mon Sep 17 00:00:00 2001 From: tiif Date: Mon, 30 Oct 2023 13:14:31 +0800 Subject: [PATCH 07/13] Update UI --- .../java/seedu/address/ui/ViewWindow.java | 20 ++++++++++++++++--- src/main/resources/view/ViewWindow.fxml | 10 ++++++++-- .../logic/parser/AddressBookParserTest.java | 2 -- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/address/ui/ViewWindow.java b/src/main/java/seedu/address/ui/ViewWindow.java index 4ad562cc973..46753bba4ab 100644 --- a/src/main/java/seedu/address/ui/ViewWindow.java +++ b/src/main/java/seedu/address/ui/ViewWindow.java @@ -5,6 +5,7 @@ import javafx.fxml.FXML; import javafx.scene.control.Label; +import javafx.scene.image.ImageView; import javafx.scene.layout.FlowPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Region; @@ -46,6 +47,13 @@ public class ViewWindow extends UiPart { @FXML private Label keyMilestone; + + @FXML + private ImageView keyMilestoneIcon; + + @FXML + private Label keyMilestoneTitle; + @FXML private FlowPane tags; @@ -58,17 +66,23 @@ public class ViewWindow extends UiPart { public ViewWindow(Person person, int displayedIndex) { super(FXML); this.person = person; - //id.setText(displayedIndex + "."); + //id.setText(displayedIndex + ".") name.setText(person.getName().fullName); phone.setText(person.getPhone().value); address.setText(person.getAddress().value); email.setText(person.getEmail().value); if (person.isLead()) { Lead convertedPerson = (Lead) person; - keyMilestone.setText("Key Milestone: " + convertedPerson.getKeyMilestone().value); + keyMilestoneIcon.setVisible(true); + keyMilestone.setManaged(true); + keyMilestoneTitle.setManaged(true); + //the key milestone title is added here to make it disappear entirely + keyMilestone.setText(convertedPerson.getKeyMilestone().value); } else { //keyMilestone should be at the last line of the ui, if not the empty line will be obvious - keyMilestone.setText(""); + keyMilestoneIcon.setVisible(false); + keyMilestone.setManaged(false); + keyMilestoneTitle.setManaged(false); } Label label = new Label(person.getType().value); if (person.isClient()) { diff --git a/src/main/resources/view/ViewWindow.fxml b/src/main/resources/view/ViewWindow.fxml index a53fc74ad1c..dac67f544e1 100644 --- a/src/main/resources/view/ViewWindow.fxml +++ b/src/main/resources/view/ViewWindow.fxml @@ -81,9 +81,15 @@ -