Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2324S1#103 from LicongHuang/convertlead
Browse files Browse the repository at this point in the history
Add command to convert lead to client
  • Loading branch information
yuxunn authored Oct 29, 2023
2 parents b5e2fe5 + 3c80ed9 commit 97f54b0
Show file tree
Hide file tree
Showing 10 changed files with 833 additions and 11 deletions.
38 changes: 27 additions & 11 deletions docs/DeveloperGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,13 @@ Use case ends.
Use case ends.

**Extensions**

3a. The list of leads is empty.
3a1. AddressBooks display a message indicating all clients are displayed.
Use case ends.
Use case ends.

**Extensions
**Extensions**

*{More to be added}*

Expand Down Expand Up @@ -655,33 +655,33 @@ testers are expected to do more *exploratory* testing.
1. Add a client with a necessary fields

1. Prerequisites: List all clients/leads using the `list` command. The information about the lead to be added should not already exist in the list.

1. Test case: `addclient n/John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/classmate`
Expected: A client with the information above will be added.

1. Test case: `addclient p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/classmate`
Expected: No client will be added. The following error message will be shown.
Expected: No client will be added. The following error message will be shown.
```
Invalid command format!
Invalid command format!
addclient: Adds a client to the address book. Parameters: n/NAME p/PHONE e/EMAIL a/ADDRESS [t/TAG]...
Example: addclient n/John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/classmate
```
1. _{ more test cases …​ }_
### Add a lead
### Add a lead
1. Add a lead with a necessary fields
1. Prerequisites: List all clients/leads using the `list` command. The information about the lead to be added should not already exist in the list.
1. Test case: `addlead n/John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/classmate`
Expected: A lead with the information above will be added.
1. Test case: `addlead p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/classmate`
Expected: No lead will be added. The following error message will be shown.
Expected: No lead will be added. The following error message will be shown.
```
Invalid command format!
Invalid command format!
addlead: Adds a lead to the address book. Parameters: n/NAME p/PHONE e/EMAIL a/ADDRESS [t/TAG]...
Example: addlead n/John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/classmate
```
Expand All @@ -697,10 +697,10 @@ testers are expected to do more *exploratory* testing.
Expected: The name of first contact is edited to ``Alan``. Details of the edited contact shown in the status message.
1. Test case: `edit 1`<br>
Expected: No client/lead is edited. Error details shown in the status message.
Expected: No client/lead is edited. Error details shown in the status message.
1. Test case: `edit 0`<br>
Expected: No client/lead is edited. Error details shown in the status message.
Expected: No client/lead is edited. Error details shown in the status message.
1. _{ more test cases …​ }_
Expand Down Expand Up @@ -734,6 +734,22 @@ testers are expected to do more *exploratory* testing.
1. Test case: `view 1`, `view x` (where x is an integer within the size of the list) <br>
Expected: The full details of the first person is displayed. Success message: `Viewed Person Successfully`
### Converting a lead to client
1. Converting a lead to a client
1. Prerequisites: At least one client shown on the interface.
2. Test case: `converttoclient 1`<br>
Expected: Person changes from a lead to a client
3. Test case: `converttoclient 0`<br>
Expected: No change to any information. Error details shown in status message. Status bar remains the same.
4. Other incorrect convert commands to try: `converttoclient`, `converttoclient x` (where x is larger than the list size)<br>
Expected: Similar to previous
### Converting a client to lead
1. Similar to converting a lead to client, but for client to lead
### Saving data
1. Dealing with missing/corrupted data files
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.index.Index;
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.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.
*/
public class ConvertClientToLeadCommand extends Command {
public static final String COMMAND_WORD = "converttolead";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Converts a client to a lead by the index "
+ "number used in the displayed person list. \n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_CONVERT_SUCCESS = "Converted Client to Lead: %1$s";
public static final String MESSAGE_CONVERT_FAILURE = "The person at the specified index is not a Client.";

private final Index index;

/**
* Creates an ConvertClientToLeadCommand to convert the specified {@code Client} to {@code Lead}
*/
public ConvertClientToLeadCommand(Index index) {
requireNonNull(index);
this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();
System.out.println(lastShownList);
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToConvert = lastShownList.get(index.getZeroBased());

if (!(personToConvert instanceof Client)) {
throw new CommandException("The person at the specified index is not a Client.");
}

Name name = personToConvert.getName();
Phone phone = personToConvert.getPhone();
Email email = personToConvert.getEmail();
Address address = personToConvert.getAddress();
Set<Tag> tags = new HashSet<>(personToConvert.getTags());
MeetingTime meetingTime = new MeetingTime(personToConvert.getMeetingTime().toString());

// TODO: Add more fields from client to lead


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

model.setPerson(personToConvert, convertedLead);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_CONVERT_SUCCESS, Messages.format(convertedLead)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof ConvertClientToLeadCommand)) {
return false;
}

ConvertClientToLeadCommand otherConvertCommand = (ConvertClientToLeadCommand) other;
return index.equals(otherConvertCommand.index);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.index.Index;
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.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 Lead to a Client in the address book.
*/
public class ConvertLeadToClientCommand extends Command {
public static final String COMMAND_WORD = "converttoclient";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Converts a lead to a client by the index "
+ "number used in the displayed person list. \n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_CONVERT_SUCCESS = "Converted Lead to Client: %1$s";
public static final String MESSAGE_NOT_LEAD = "The person at the specified index is not a Lead.";

private final Index index;

/**
* Creates an ConvertLeadToClientCommand to convert the specified {@code Lead} to {@code Client}
*/
public ConvertLeadToClientCommand(Index index) {
requireNonNull(index);
this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();
System.out.println(lastShownList);
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToConvert = lastShownList.get(index.getZeroBased());

if (!(personToConvert instanceof Lead)) {
throw new CommandException("The person at the specified index is not a Lead.");
}

Name name = personToConvert.getName();
Phone phone = personToConvert.getPhone();
Email email = personToConvert.getEmail();
Address address = personToConvert.getAddress();
Set<Tag> tags = new HashSet<>(personToConvert.getTags());
MeetingTime meetingTime = new MeetingTime(personToConvert.getMeetingTime().toString());
// TODO: Add more fields from lead to client

Client convertedClient = new Client(name, phone, email, address, meetingTime, tags);

model.setPerson(personToConvert, convertedClient);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_CONVERT_SUCCESS, Messages.format(convertedClient)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof ConvertLeadToClientCommand)) {
return false;
}

ConvertLeadToClientCommand otherConvertCommand = (ConvertLeadToClientCommand) other;
return index.equals(otherConvertCommand.index);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import seedu.address.logic.commands.AddLeadCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.ConvertClientToLeadCommand;
import seedu.address.logic.commands.ConvertLeadToClientCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
Expand Down Expand Up @@ -93,6 +95,12 @@ public Command parseCommand(String userInput) throws ParseException {
case ViewCommand.COMMAND_WORD:
return new ViewCommandParser().parse(arguments);

case ConvertClientToLeadCommand.COMMAND_WORD:
return new ConvertClientToLeadCommandParser().parse(arguments);

case ConvertLeadToClientCommand.COMMAND_WORD:
return new ConvertLeadToClientCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.ConvertClientToLeadCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new ConvertClientToLeadCommand object
*/
public class ConvertClientToLeadCommandParser implements Parser<ConvertClientToLeadCommand> {

/**
* Parses the given {@code String} of arguments in the context of the ConvertClientToLeadCommand
* and returns a ConvertClientToLeadCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ConvertClientToLeadCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ConvertClientToLeadCommand.MESSAGE_USAGE));
}
Index index = ParserUtil.parseIndex(args);
return new ConvertClientToLeadCommand(index);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.ConvertLeadToClientCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new ConvertLeadToClientCommand object
*/
public class ConvertLeadToClientCommandParser implements Parser<ConvertLeadToClientCommand> {

/**
* Parses the given {@code String} of arguments in the context of the ConvertLeadToClientCommand
* and returns a ConvertLeadToClientCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ConvertLeadToClientCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ConvertLeadToClientCommand.MESSAGE_USAGE));
}

Index index = ParserUtil.parseIndex(args);

return new ConvertLeadToClientCommand(index);
}

}
Loading

0 comments on commit 97f54b0

Please sign in to comment.