Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2324S1#108 from tiif/branch-add-field
Browse files Browse the repository at this point in the history
Branch add field for Lead's KeyMilestone
  • Loading branch information
LicongHuang authored Oct 30, 2023
2 parents 28a13d7 + bb957a0 commit 69d9c4f
Show file tree
Hide file tree
Showing 37 changed files with 1,031 additions and 116 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ shadowJar {
archiveFileName = 'addressbook.jar'
}

run {
enableAssertions = true
}

defaultTasks 'clean', 'test'

run {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -35,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: ")
Expand All @@ -50,4 +55,26 @@ public static String format(Person person) {
return builder.toString();
}

/**
* Formats the {@code lead} for display to the user.
*/
public static String format(Lead lead) {
assert(lead.isLead());
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("; Meeting Time: ")
.append(lead.getKeyMilestone())
.append("; Tags: ");
lead.getTags().forEach(builder::append);
return builder.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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_KEYMILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEETING_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class AddLeadCommand extends Command {
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "[email protected] "
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 "
+ PREFIX_KEYMILESTONE + "2023-10-20 "
+ PREFIX_MEETING_TIME + "10/10/2023 14:30 "
+ PREFIX_TAG + "classmate";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import seedu.address.model.person.Address;
import seedu.address.model.person.Client;
import seedu.address.model.person.Email;
import seedu.address.model.person.KeyMilestone;
import seedu.address.model.person.Lead;
import seedu.address.model.person.MeetingTime;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;


/**
* Converts a Client to a Lead in the address book.
*/
Expand Down Expand Up @@ -65,13 +67,15 @@ public CommandResult execute(Model model) throws CommandException {
Phone phone = personToConvert.getPhone();
Email email = personToConvert.getEmail();
Address address = personToConvert.getAddress();
//todo: temporary fix for keyMilestone
KeyMilestone keyMilestone = null;
Set<Tag> tags = new HashSet<>(personToConvert.getTags());
Optional<MeetingTime> meetingTime = personToConvert.getMeetingTime();

// TODO: Add more fields from client to lead


Lead convertedLead = new Lead(name, phone, email, address, meetingTime, tags);
Lead convertedLead = new Lead(name, phone, email, address, keyMilestone, meetingTime, tags);

model.setPerson(personToConvert, convertedLead);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
Expand Down
82 changes: 75 additions & 7 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@
import seedu.address.model.person.Address;
import seedu.address.model.person.Client;
import seedu.address.model.person.Email;
import seedu.address.model.person.KeyMilestone;
import seedu.address.model.person.Lead;
import seedu.address.model.person.MeetingTime;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;



/**
* Edits the details of an existing person in the address book.
*/
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"
Expand All @@ -54,6 +56,7 @@ public class EditCommand extends Command {
+ PREFIX_EMAIL + "[email protected]";

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.";

Expand Down Expand Up @@ -86,14 +89,18 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Optional<MeetingTime> updatedMeetingTime = editPersonDescriptor.getMeetingTime()
.or(personToEdit::getMeetingTime);
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());

if (personToEdit.isClient()) {
return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedMeetingTime, updatedTags);
} else {
return new Lead(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedMeetingTime, updatedTags);
//todo: this temporary fix related to the one in editcommandparser
if (personToEdit.isLead()) {
//keyMilestone will not be updated if editLeadCommand is not used
Lead leadToEdit = (Lead) personToEdit;
KeyMilestone keyMilestone = leadToEdit.getKeyMilestone();
return new Lead(updatedName, updatedPhone, updatedEmail, updatedAddress, keyMilestone,
updatedMeetingTime, updatedTags);
}
return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedMeetingTime, updatedTags);
}


@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Expand Down Expand Up @@ -166,7 +173,6 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setMeetingTime(toCopy.meetingTime);
setTags(toCopy.tags);
}

/**
* Returns true if at least one field is edited.
*/
Expand Down Expand Up @@ -263,4 +269,66 @@ public String toString() {
.toString();
}
}

/**
* Stores the details to edit the lead with. Each non-empty field value will replace the
* corresponding field value of the lead.
*/
public static class EditLeadDescriptor extends EditPersonDescriptor {
//This class only used for edit KeyMilestone, for other field of leads like name, editPersonDescriptor
// will be used
private KeyMilestone keyMilestone;
/**
* Copy constructor only for leads.
* A defensive copy of {@code tags} is used internally.
*/
public EditLeadDescriptor(EditLeadDescriptor toCopy) {
super(toCopy);
setKeyMilestone(toCopy.keyMilestone);
}

public EditLeadDescriptor() {

}

@Override
public boolean isAnyFieldEdited() {
return super.isAnyFieldEdited() || CollectionUtil.isAnyNonNull(keyMilestone);
}

public Optional<KeyMilestone> getKeyMilestone() {
return Optional.ofNullable(keyMilestone);
}

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 new ToStringBuilder(this)
.add("name", super.name)
.add("phone", super.phone)
.add("email", super.email)
.add("address", super.address)
.add("key milestone", keyMilestone)
.add("meeting time", super.meetingTime)
.add("tags", super.tags)
.toString();
}
}
}
135 changes: 135 additions & 0 deletions src/main/java/seedu/address/logic/commands/EditLeadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
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_KEYMILESTONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEETING_TIME;
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.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
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.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.KeyMilestone;
import seedu.address.model.person.Lead;
import seedu.address.model.person.MeetingTime;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;

/**
* Edits the details of an existing lead in the address book.
*/
public class EditLeadCommand extends EditCommand {

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 + "KEY MILESTONE] "
+ "[" + PREFIX_MEETING_TIME + "MEETING TIME] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
+ PREFIX_EMAIL + "[email protected]";


private final Index index;
private final EditLeadDescriptor editLeadDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param editLeadDescriptor details to edit the lead with
*/
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<Tag> updatedTags = editLeadDescriptor.getTags().orElse(leadToEdit.getTags());
KeyMilestone updatedKeyMilestone = editLeadDescriptor.getKeyMilestone()
.orElse(leadToEdit.getKeyMilestone());
Optional<MeetingTime> updatedMeetingTime = editLeadDescriptor.getMeetingTime()
.or(leadToEdit::getMeetingTime);

return new Lead(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedKeyMilestone,
updatedMeetingTime, updatedTags);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Lead leadToEdit = (Lead) lastShownList.get(index.getZeroBased());
assert(leadToEdit.isLead());
Lead editedLead = createEditedLead((Lead) leadToEdit, editLeadDescriptor);

if (!leadToEdit.isSamePerson(editedLead) && model.hasPerson(editedLead)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.setPerson(leadToEdit, editedLead);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_LEAD_SUCCESS, Messages.format(editedLead)));
}

@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();
}


}
Loading

0 comments on commit 69d9c4f

Please sign in to comment.