Skip to content

Commit

Permalink
Merge branch 'master' into sort-by-priority
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyiheng12 authored Mar 29, 2022
2 parents cc983e9 + 3ed70f1 commit 82b2343
Show file tree
Hide file tree
Showing 15 changed files with 514 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class AddTagCommand extends Command {
+ "Example: " + COMMAND_WORD + "1 "
+ "owesMoney :p2";

public static final String MESSAGE_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_SUCCESS = "Added tag to Person: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";

private final Index index;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Objects.requireNonNull;

import seedu.address.model.AddressBook;
import seedu.address.model.Model;

/**
Expand All @@ -17,7 +16,7 @@ public class ClearCommand extends Command {
@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.setAddressBook(new AddressBook());
model.resetAddressBook();
return new CommandResult(MESSAGE_SUCCESS);
}
}
82 changes: 82 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteTagCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package seedu.address.logic.commands;

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

import java.util.ArrayList;
import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

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

public static final String COMMAND_WORD = "deleteTag";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Delete a tag "
+ "(identified by the tag's index number) "
+ "of the person identified "
+ "by the index number used in the displayed person list. "
+ "Only one tag can be deleted at a time. "
+ "Parameters: PERSON_INDEX (must be a positive integer) "
+ "TAG_INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + "3 " + "2";

public static final String MESSAGE_SUCCESS = "Deleted tag in Person: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";

private final Index index;
private final int tagNumber;

/**
* Creates an AddTagCommand to add the specified {@code Tag}
*
* @param index of the person in the filtered person list to edit
* @param tagNumber of the tag to be deleted
*/
public DeleteTagCommand(Index index, int tagNumber) {
requireNonNull(index);
this.index = index;
this.tagNumber = tagNumber;
}

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

Person personToEdit = lastShownList.get(index.getZeroBased());
Person tagAddedPerson = deleteTagFromPerson(personToEdit, tagNumber);

model.setPerson(personToEdit, tagAddedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, tagAddedPerson.getName()));
}

private Person deleteTagFromPerson(Person personToEdit, int tagNumber) {
Person newPerson = Person.copyPerson(personToEdit);
ArrayList<Tag> tagList = newPerson.getTags();
tagList.remove(tagNumber - 1);

newPerson.setTags(tagList);
return newPerson;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteTagCommand // instanceof handles nulls
&& tagNumber == ((DeleteTagCommand) other).tagNumber);
}
}
93 changes: 93 additions & 0 deletions src/main/java/seedu/address/logic/commands/EditTagCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package seedu.address.logic.commands;

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

import java.util.ArrayList;
import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

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

public static final String COMMAND_WORD = "editTag";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edit a tag "
+ "(identified by the tag's index number) "
+ "of the person identified "
+ "by the index number used in the displayed person list. "
+ "Only one tag can edited at a time. "
+ "Parameters: PERSON_INDEX (must be a positive integer) "
+ "TAG_INDEX (must be a positive integer) "
+ "TAG\n"
+ "Example: " + COMMAND_WORD + "1 " + "2 "
+ "owesMoney :p2";

public static final String MESSAGE_SUCCESS = "Edited tag in Person: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";

private final Index index;
private final int tagNumber;
private final Tag toAdd;

/**
* Creates an EditTagCommand to replace the tag in the index with the specified {@code Tag}
*
* @param index of the person in the filtered person list to edit
* @param tagNumber of the person's tag list to edit
* @param tag to be added to the person identified
*/
public EditTagCommand(Index index, int tagNumber, Tag tag) {
requireNonNull(index);
requireNonNull(tag);
this.index = index;
this.tagNumber = tagNumber;
toAdd = tag;
}

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

Person personToEdit = lastShownList.get(index.getZeroBased());
Person tagAddedPerson = editTagOfPerson(personToEdit, tagNumber, toAdd);

model.setPerson(personToEdit, tagAddedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, tagAddedPerson.getName()));
}

private Person editTagOfPerson(Person personToEdit, int tagNumber, Tag tag) {
Person newPerson = Person.copyPerson(personToEdit);
ArrayList<Tag> tagList = newPerson.getTags();
tagList.set(tagNumber - 1, tag); // add exception later

newPerson.setTags(tagList);
return newPerson;
}

public Tag getToAdd() {
return toAdd;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof EditTagCommand // instanceof handles nulls
&& tagNumber == ((EditTagCommand) other).tagNumber
&& toAdd.equals(((EditTagCommand) other).toAdd));
}
}
24 changes: 24 additions & 0 deletions src/main/java/seedu/address/logic/commands/RedoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

/**
* Lists all persons in the address book to the user.
*/
public class RedoCommand extends Command {

public static final String COMMAND_WORD = "redo";

public static final String MESSAGE_SUCCESS = "Command redone";


@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.redoCommand();
return new CommandResult(MESSAGE_SUCCESS);
}
}
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeleteTagCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditTagCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.ExportToCsvCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ImportFromCsvCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.PriorityListCommand;
import seedu.address.logic.commands.RedoCommand;
import seedu.address.logic.commands.UndoCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -59,9 +62,15 @@ public Command parseCommand(String userInput) throws ParseException {
case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);

case EditTagCommand.COMMAND_WORD:
return new EditTagCommandParser().parse(arguments);

case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);

case DeleteTagCommand.COMMAND_WORD:
return new DeleteTagCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
return new ClearCommand();

Expand Down Expand Up @@ -92,6 +101,9 @@ public Command parseCommand(String userInput) throws ParseException {
case UndoCommand.COMMAND_WORD:
return new UndoCommand();

case RedoCommand.COMMAND_WORD:
return new RedoCommand();

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import javafx.util.Pair;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteTagCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.parser.exceptions.ParseException;

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

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns an AddCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteTagCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args);
Pair<Index, String> userInputs;
Pair<Integer, String> trimOutNumber;

try {
userInputs = ParserUtil.parseOutIndex(argMultimap.getPreamble());
trimOutNumber = ParserUtil.parseOutNumber(userInputs.getValue());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}

Index index = userInputs.getKey();
Integer tagNumber = trimOutNumber.getKey();
return new DeleteTagCommand(index, tagNumber);
}

}
41 changes: 41 additions & 0 deletions src/main/java/seedu/address/logic/parser/EditTagCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import javafx.util.Pair;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditTagCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

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

/**
* Parses the given {@code String} of arguments in the context of the EditCommand
* and returns an EditCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public EditTagCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args);
Pair<Index, String> userInputs;
Pair<Integer, String> trimOutNumber;

try {
userInputs = ParserUtil.parseOutIndex(argMultimap.getPreamble());
trimOutNumber = ParserUtil.parseOutNumber(userInputs.getValue());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}

Index index = userInputs.getKey();
Integer tagNumber = trimOutNumber.getKey();
Tag tag = ParserUtil.parseTag(trimOutNumber.getValue());
return new EditTagCommand(index, tagNumber, tag);
}

}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ public static Pair<Index, String> parseOutIndex(String indexAndWords) throws Par
return new Pair<>(index, splitIndexAndRemainingString[1]);
}

/**
* Parses {@code numberAndWords} into an {@code Pair<Integer, String>} and returns it. Leading and trailing
* whitespaces will be trimmed. Must contain a number at the beginning and then words.
*
* @throws ParseException if the specified index is invalid (not non-zero unsigned integer).
*/
public static Pair<Integer, String> parseOutNumber(String numberAndWords) throws ParseException {
String trimmedInput = numberAndWords.trim();
// splitIndexAndRemainingString[0] contains number, splitIndexAndRemainingString[2] contains remaining string
String[] splitIndexAndRemainingString = trimmedInput.split(" ", 2);

// Edit this to handle NumberFormatException
Integer number = Integer.valueOf(splitIndexAndRemainingString[0].trim());
return new Pair<>(number, splitIndexAndRemainingString.length > 1 ? splitIndexAndRemainingString[1] : "");
}

/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public interface Model {
*/
void setAddressBook(ReadOnlyAddressBook addressBook);

/**
* Replaces address book data with an empty {@code addressBook}.
*/
void resetAddressBook();

/** Returns the AddressBook */
ReadOnlyAddressBook getAddressBook();

Expand Down Expand Up @@ -92,4 +97,6 @@ public interface Model {
void sortByPriority();

void undoCommand() throws CommandException;

void redoCommand() throws CommandException;
}
Loading

0 comments on commit 82b2343

Please sign in to comment.