Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2122S1#90 from elroygohjy/elroy-noteR…
Browse files Browse the repository at this point in the history
…efactor-v1.2

Feat: Add Note Command, Add Note UI & Add Note Date Saving
  • Loading branch information
EltonGohJH authored Oct 12, 2021
2 parents 1b0249f + feedcab commit 513fb40
Show file tree
Hide file tree
Showing 31 changed files with 424 additions and 231 deletions.
23 changes: 23 additions & 0 deletions src/main/java/seedu/address/commons/util/DateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package seedu.address.commons.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;



/**
* A class for getting current Date time in (E, MMM dd yyyy HH:mm) format.
*/
public class DateUtil {
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm");

/**
* Returns the String representation of current date and time.
*
* @return String representation of current date and time.
*/
public static String getCurrentDateTime() {
return TIME_FORMATTER.format(LocalDateTime.now());
}

}
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public interface Logic {
*/
CommandResult execute(String commandText) throws CommandException, ParseException, ExecuteException;


/**
* Executes instruction to save note of the person.
*
* @param person The person with yet to edit notes.
* @param editedPerson The person with newly edited notes.
* @throws CommandException
*/
void executeSaveNote(Person person, Person editedPerson) throws CommandException;
/**
* Returns the AddressBook.
*
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
return commandResult;
}

/**
* Executes command to save Note of Person.
* @param person The person with yet to edit notes.
* @param editedPerson The person with newly edited notes.
* @throws CommandException If an error occurs during command execution.
*/
public void executeSaveNote(Person person, Person editedPerson) throws CommandException {
model.setPerson(person, editedPerson);
try {
storage.saveAddressBook(model.getAddressBook());
} catch (IOException ioe) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
}
}


@Override
public ReadOnlyAddressBook getAddressBook() {
return model.getAddressBook();
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/seedu/address/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import java.util.Objects;

import seedu.address.model.person.Person;



/**
* Represents the result of a command execution.
*/
Expand All @@ -21,21 +25,29 @@ public class CommandResult {
*/
private final boolean exit;

/** The note should be shown for the respective user. */
private final boolean showNote;

private final Person person;


/**
* Constructs a {@code CommandResult} with the specified fields.
*/
public CommandResult(String feedbackToUser, boolean showHelp, boolean exit) {
public CommandResult(String feedbackToUser, boolean showHelp, boolean showNote, Person person, boolean exit) {
this.feedbackToUser = requireNonNull(feedbackToUser);
this.showHelp = showHelp;
this.showNote = showNote;
this.exit = exit;
this.person = person;
}

/**
* Constructs a {@code CommandResult} with the specified {@code feedbackToUser},
* and other fields set to their default value.
*/
public CommandResult(String feedbackToUser) {
this(feedbackToUser, false, false);
this(feedbackToUser, false, false, null, false);
}

public String getFeedbackToUser() {
Expand All @@ -46,10 +58,18 @@ public boolean isShowHelp() {
return showHelp;
}

public boolean isShowNote() {
return showNote;
}

public boolean isExit() {
return exit;
}

public Person person() {
return person;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -64,6 +84,7 @@ public boolean equals(Object other) {
CommandResult otherCommandResult = (CommandResult) other;
return feedbackToUser.equals(otherCommandResult.feedbackToUser)
&& showHelp == otherCommandResult.showHelp
&& showNote == otherCommandResult.showNote
&& exit == otherCommandResult.exit;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class ExitCommand extends Command {

@Override
public CommandResult execute(Model model) {
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true);
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, false,
null, true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class HelpCommand extends Command {

@Override
public CommandResult execute(Model model) {
return new CommandResult(SHOWING_HELP_MESSAGE, true, false);
return new CommandResult(SHOWING_HELP_MESSAGE, true, false, null, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package seedu.address.logic.commands.person;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.executors.exceptions.ExecuteException;
import seedu.address.logic.executors.person.PersonNoteExecutor;
import seedu.address.model.person.Note;

/**
*
Expand All @@ -17,22 +15,19 @@ public class PersonNoteCommand extends PersonCommand {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the notes of the person identified "
+ "by the index number used in the last person listing. "
+ "Existing notes will be overwritten by the input.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ PREFIX_NOTE + "[NOTE]\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_NOTE + "Likes to swim.";
+ "Example: " + COMMAND_WORD + " 1 ";

private final PersonNoteExecutor executor;

/**
* @param index Index of the person to edit note for.
* @param note New note to be updated to.
*
*/
public PersonNoteCommand(Index index, Note note) {
public PersonNoteCommand(Index index) {
super(index);
requireAllNonNull(index, note);
this.executor = new PersonNoteExecutor(index, note);
requireAllNonNull(index);
this.executor = new PersonNoteExecutor(index);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public Optional<Email> getEmail() {
public void setNote(Note note) {
this.note = note;
}

public Optional<Note> getNote() {
return Optional.ofNullable(note);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,46 @@
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.executors.exceptions.ExecuteException;
import seedu.address.model.person.Note;
import seedu.address.model.person.Person;

/**
* Executor for a PersonNoteCommand.
*/
public class PersonNoteExecutor extends PersonExecutor {
public static final String MESSAGE_ADD_NOTE_SUCCESS = "Added note to Person: %1$s";
public static final String MESSAGE_DELETE_NOTE_SUCCESS = "Removed note from Person: %1$s";
public static final String MESSAGE_OPEN_NOTE_SUCCESS = "Opened note to Person: %1$s";

private final Note note;

/**
* Constructor for a PersonNoteExecutor instance.
*
* @param index Index of the person to add a note to.
* @param note Note to be added to the person.
*
*/
public PersonNoteExecutor(Index index, Note note) {
public PersonNoteExecutor(Index index) {
super(index);
requireAllNonNull(index, note);
this.note = note;
requireAllNonNull(index);
}

@Override
public CommandResult execute() throws ExecuteException {
Person storedPerson = super.getPerson();
Person editedPerson = new Person(
storedPerson.getName(), storedPerson.getPhone(), storedPerson.getEmail(), note, storedPerson.getTags());
storedPerson.getName(), storedPerson.getPhone(), storedPerson.getEmail(),
storedPerson.getNote(), storedPerson.getTags());

model.setPerson(storedPerson, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(generateSuccessMessage(editedPerson));
return new CommandResult(generateSuccessMessage(editedPerson), false, true, editedPerson, false);
}

/**
* Generates a command execution success message based on whether
* the note is added to or removed from
* the note is added.
* {@code personToEdit}.
*/
private String generateSuccessMessage(Person personToEdit) {
String message = !note.value.isEmpty() ? MESSAGE_ADD_NOTE_SUCCESS : MESSAGE_DELETE_NOTE_SUCCESS;
return String.format(message, personToEdit);
return String.format(MESSAGE_OPEN_NOTE_SUCCESS, personToEdit);
}

@Override
Expand All @@ -67,6 +63,6 @@ public boolean equals(Object other) {
PersonNoteExecutor e = (PersonNoteExecutor) other;

// state check
return super.equals(other) && note.equals(e.note);
return super.equals(other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public PersonCreateCommand parse(String args) throws ParseException {
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());
Note note = new Note("");
Note note = new Note("", "");
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Person person = new Person(name, phone, email, note, tagList);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package seedu.address.logic.parser.person;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
Expand All @@ -10,7 +9,6 @@
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Note;

public class PersonNoteCommandParser extends PersonCommandParser {
/**
Expand All @@ -21,7 +19,7 @@ public class PersonNoteCommandParser extends PersonCommandParser {
*/
public PersonNoteCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NOTE);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args);

Index index;

Expand All @@ -32,8 +30,6 @@ public PersonNoteCommand parse(String args) throws ParseException {
PersonNoteCommand.MESSAGE_USAGE), ive);
}

String note = argMultimap.getValue(PREFIX_NOTE).orElse("");

return new PersonNoteCommand(index, new Note(note));
return new PersonNoteCommand(index);
}
}
23 changes: 21 additions & 2 deletions src/main/java/seedu/address/model/person/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

import static java.util.Objects.requireNonNull;

/**
* Represents a Person's note that is opened upon note command.
*/
public class Note {

/** content of note **/
public final String value;
public final String savedDate;


/**
* Constructor for a Note instance.
*
* @param value Value of the note.
*/
public Note(String value) {
public Note(String value, String savedDate) {
requireNonNull(value);
this.value = value;
this.savedDate = savedDate;
}

/**
* Returns string representation of saved date.
*
* @return String representation of saved date
*/
public String getSavedDate() {
return savedDate;
}

@Override
Expand All @@ -24,7 +41,7 @@ public boolean equals(Object other) {
return false;
}
Note noteOther = (Note) other;
return value.equals(noteOther.value); // state check
return value.equals(noteOther.value) && savedDate.equals(noteOther.savedDate); // state check
}

@Override
Expand All @@ -36,4 +53,6 @@ public int hashCode() {
public String toString() {
return value;
}


}
15 changes: 12 additions & 3 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public Email getEmail() {
public Note getNote() {
return note;
}
public String getNoteSavedDate() {
return note.getSavedDate();
}


public void addSuperGroup(SuperGroup superGroup) {
superGroups.add(superGroup.toString());
Expand Down Expand Up @@ -144,15 +148,20 @@ public String toString() {
.append("; Phone: ")
.append(getPhone())
.append("; Email: ")
.append(getEmail())
.append("; Note: ")
.append(getNote());
.append(getEmail());

String noteSavedDate = note.getSavedDate();
if (!noteSavedDate.isEmpty()) {
builder.append("; Last Edited: ")
.append(getNoteSavedDate());
}
Set<Tag> tags = getTags();

if (!tags.isEmpty()) {
builder.append("; Tags: ");
tags.forEach(builder::append);
}

return builder.toString();
}

Expand Down
Loading

0 comments on commit 513fb40

Please sign in to comment.