Skip to content

Commit

Permalink
Merge pull request #22 from Lin-Shuang-Shuang/branch-AddProjectCommand
Browse files Browse the repository at this point in the history
Update AddCommand to AddProjectCommand
  • Loading branch information
modembcc authored Mar 13, 2024
2 parents 4cdc52f + 5cecfc6 commit f80ac43
Show file tree
Hide file tree
Showing 25 changed files with 246 additions and 688 deletions.
10 changes: 1 addition & 9 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,7 @@ public static String getErrorMessageForDuplicatePrefixes(Prefix... duplicatePref
*/
public static String format(Person person) {
final StringBuilder builder = new StringBuilder();
builder.append(person.getName())
.append("; Phone: ")
.append(person.getPhone())
.append("; Email: ")
.append(person.getEmail())
.append("; Address: ")
.append(person.getAddress())
.append("; Tags: ");
person.getTags().forEach(builder::append);
builder.append(person.getName());
return builder.toString();
}

Expand Down
84 changes: 0 additions & 84 deletions src/main/java/seedu/address/logic/commands/AddCommand.java

This file was deleted.

68 changes: 68 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddProjectCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

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

/**
* Adds a person to the address book.
*/
public class AddProjectCommand extends Command {

public static final String COMMAND_WORD = "add project";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
+ "Parameters: "
+ "PROJECT_NAME ";

public static final String MESSAGE_SUCCESS = "%1$s has been added to the project list.";
public static final String MESSAGE_DUPLICATE_PERSON = "Project %1$s already exists";

private final Person toAdd;

/**
* Creates an AddCommand to add the specified {@code Person}
*/
public AddProjectCommand(Person person) {
requireNonNull(person);
toAdd = person;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
throw new CommandException(String.format(MESSAGE_DUPLICATE_PERSON, Messages.format(toAdd)));
}

model.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
}

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

// instanceof handles nulls
if (!(other instanceof AddProjectCommand)) {
return false;
}

AddProjectCommand otherAddProjectCommand = (AddProjectCommand) other;
return toAdd.equals(otherAddProjectCommand.toAdd);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", toAdd)
.toString();
}
}
8 changes: 1 addition & 7 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,8 @@ public CommandResult execute(Model model) throws CommandException {
*/
private static Person createEditedPerson(Person personToEdit, EditPersonDescriptor editPersonDescriptor) {
assert personToEdit != null;

Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName());
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone());
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Person(updatedName);
}

@Override
Expand Down
61 changes: 0 additions & 61 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java

This file was deleted.

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

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

import java.util.stream.Stream;

import seedu.address.logic.commands.AddProjectCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;


/**
* Parses input arguments and creates a new AddCommand object
*/
public class AddProjectCommandParser implements Parser<AddProjectCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddProjectCommand parse(String args) throws ParseException {
String projectName = args;
if (projectName.length() == 0) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddProjectCommand.MESSAGE_USAGE));
}
Name name = ParserUtil.parseName(projectName);
Person person = new Person(name);
return new AddProjectCommand(person);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

}
15 changes: 10 additions & 5 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.regex.Pattern;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddProjectCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand All @@ -27,7 +27,8 @@ public class AddressBookParser {
/**
* Used for initial separation of command word and args.
*/
private static final Pattern BASIC_COMMAND_FORMAT = Pattern.compile("(?<commandWord>\\S+)(?<arguments>.*)");
private static final Pattern BASIC_COMMAND_FORMAT = Pattern.compile(
"(?<commandWord>[^\\s]+\\s[^\\s]+)(?<arguments>.*)");
private static final Logger logger = LogsCenter.getLogger(AddressBookParser.class);

/**
Expand All @@ -38,9 +39,13 @@ public class AddressBookParser {
* @throws ParseException if the user input does not conform the expected format
*/
public Command parseCommand(String userInput) throws ParseException {

if (userInput.length() == 0) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
}
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
throw new ParseException(String.format(MESSAGE_UNKNOWN_COMMAND, HelpCommand.MESSAGE_USAGE));
}

final String commandWord = matcher.group("commandWord");
Expand All @@ -53,8 +58,8 @@ public Command parseCommand(String userInput) throws ParseException {

switch (commandWord) {

case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);
case AddProjectCommand.COMMAND_WORD:
return new AddProjectCommandParser().parse(arguments);

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);
Expand Down
Loading

0 comments on commit f80ac43

Please sign in to comment.