Skip to content

Commit 513fb40

Browse files
authored
Merge pull request nus-cs2103-AY2122S1#90 from elroygohjy/elroy-noteRefactor-v1.2
Feat: Add Note Command, Add Note UI & Add Note Date Saving
2 parents 1b0249f + feedcab commit 513fb40

31 files changed

+424
-231
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package seedu.address.commons.util;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.format.DateTimeFormatter;
5+
6+
7+
8+
/**
9+
* A class for getting current Date time in (E, MMM dd yyyy HH:mm) format.
10+
*/
11+
public class DateUtil {
12+
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm");
13+
14+
/**
15+
* Returns the String representation of current date and time.
16+
*
17+
* @return String representation of current date and time.
18+
*/
19+
public static String getCurrentDateTime() {
20+
return TIME_FORMATTER.format(LocalDateTime.now());
21+
}
22+
23+
}

src/main/java/seedu/address/logic/Logic.java

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public interface Logic {
2525
*/
2626
CommandResult execute(String commandText) throws CommandException, ParseException, ExecuteException;
2727

28+
29+
/**
30+
* Executes instruction to save note of the person.
31+
*
32+
* @param person The person with yet to edit notes.
33+
* @param editedPerson The person with newly edited notes.
34+
* @throws CommandException
35+
*/
36+
void executeSaveNote(Person person, Person editedPerson) throws CommandException;
2837
/**
2938
* Returns the AddressBook.
3039
*

src/main/java/seedu/address/logic/LogicManager.java

+16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
5555
return commandResult;
5656
}
5757

58+
/**
59+
* Executes command to save Note of Person.
60+
* @param person The person with yet to edit notes.
61+
* @param editedPerson The person with newly edited notes.
62+
* @throws CommandException If an error occurs during command execution.
63+
*/
64+
public void executeSaveNote(Person person, Person editedPerson) throws CommandException {
65+
model.setPerson(person, editedPerson);
66+
try {
67+
storage.saveAddressBook(model.getAddressBook());
68+
} catch (IOException ioe) {
69+
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
70+
}
71+
}
72+
73+
5874
@Override
5975
public ReadOnlyAddressBook getAddressBook() {
6076
return model.getAddressBook();

src/main/java/seedu/address/logic/commands/CommandResult.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
import java.util.Objects;
66

7+
import seedu.address.model.person.Person;
8+
9+
10+
711
/**
812
* Represents the result of a command execution.
913
*/
@@ -21,21 +25,29 @@ public class CommandResult {
2125
*/
2226
private final boolean exit;
2327

28+
/** The note should be shown for the respective user. */
29+
private final boolean showNote;
30+
31+
private final Person person;
32+
33+
2434
/**
2535
* Constructs a {@code CommandResult} with the specified fields.
2636
*/
27-
public CommandResult(String feedbackToUser, boolean showHelp, boolean exit) {
37+
public CommandResult(String feedbackToUser, boolean showHelp, boolean showNote, Person person, boolean exit) {
2838
this.feedbackToUser = requireNonNull(feedbackToUser);
2939
this.showHelp = showHelp;
40+
this.showNote = showNote;
3041
this.exit = exit;
42+
this.person = person;
3143
}
3244

3345
/**
3446
* Constructs a {@code CommandResult} with the specified {@code feedbackToUser},
3547
* and other fields set to their default value.
3648
*/
3749
public CommandResult(String feedbackToUser) {
38-
this(feedbackToUser, false, false);
50+
this(feedbackToUser, false, false, null, false);
3951
}
4052

4153
public String getFeedbackToUser() {
@@ -46,10 +58,18 @@ public boolean isShowHelp() {
4658
return showHelp;
4759
}
4860

61+
public boolean isShowNote() {
62+
return showNote;
63+
}
64+
4965
public boolean isExit() {
5066
return exit;
5167
}
5268

69+
public Person person() {
70+
return person;
71+
}
72+
5373
@Override
5474
public boolean equals(Object other) {
5575
if (other == this) {
@@ -64,6 +84,7 @@ public boolean equals(Object other) {
6484
CommandResult otherCommandResult = (CommandResult) other;
6585
return feedbackToUser.equals(otherCommandResult.feedbackToUser)
6686
&& showHelp == otherCommandResult.showHelp
87+
&& showNote == otherCommandResult.showNote
6788
&& exit == otherCommandResult.exit;
6889
}
6990

src/main/java/seedu/address/logic/commands/ExitCommand.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class ExitCommand extends Command {
1313

1414
@Override
1515
public CommandResult execute(Model model) {
16-
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true);
16+
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, false,
17+
null, true);
1718
}
1819

1920
}

src/main/java/seedu/address/logic/commands/HelpCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public class HelpCommand extends Command {
1616

1717
@Override
1818
public CommandResult execute(Model model) {
19-
return new CommandResult(SHOWING_HELP_MESSAGE, true, false);
19+
return new CommandResult(SHOWING_HELP_MESSAGE, true, false, null, false);
2020
}
2121
}

src/main/java/seedu/address/logic/commands/person/PersonNoteCommand.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package seedu.address.logic.commands.person;
22

33
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
4-
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE;
54

65
import seedu.address.commons.core.index.Index;
76
import seedu.address.logic.commands.CommandResult;
87
import seedu.address.logic.executors.exceptions.ExecuteException;
98
import seedu.address.logic.executors.person.PersonNoteExecutor;
10-
import seedu.address.model.person.Note;
119

1210
/**
1311
*
@@ -17,22 +15,19 @@ public class PersonNoteCommand extends PersonCommand {
1715

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

2621
private final PersonNoteExecutor executor;
2722

2823
/**
2924
* @param index Index of the person to edit note for.
30-
* @param note New note to be updated to.
25+
*
3126
*/
32-
public PersonNoteCommand(Index index, Note note) {
27+
public PersonNoteCommand(Index index) {
3328
super(index);
34-
requireAllNonNull(index, note);
35-
this.executor = new PersonNoteExecutor(index, note);
29+
requireAllNonNull(index);
30+
this.executor = new PersonNoteExecutor(index);
3631
}
3732

3833
@Override

src/main/java/seedu/address/logic/executors/person/PersonEditExecutor.java

-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ public Optional<Email> getEmail() {
147147
public void setNote(Note note) {
148148
this.note = note;
149149
}
150-
151150
public Optional<Note> getNote() {
152151
return Optional.ofNullable(note);
153152
}

src/main/java/seedu/address/logic/executors/person/PersonNoteExecutor.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,46 @@
66
import seedu.address.commons.core.index.Index;
77
import seedu.address.logic.commands.CommandResult;
88
import seedu.address.logic.executors.exceptions.ExecuteException;
9-
import seedu.address.model.person.Note;
109
import seedu.address.model.person.Person;
1110

1211
/**
1312
* Executor for a PersonNoteCommand.
1413
*/
1514
public class PersonNoteExecutor extends PersonExecutor {
16-
public static final String MESSAGE_ADD_NOTE_SUCCESS = "Added note to Person: %1$s";
17-
public static final String MESSAGE_DELETE_NOTE_SUCCESS = "Removed note from Person: %1$s";
15+
public static final String MESSAGE_OPEN_NOTE_SUCCESS = "Opened note to Person: %1$s";
1816

19-
private final Note note;
2017

2118
/**
2219
* Constructor for a PersonNoteExecutor instance.
2320
*
2421
* @param index Index of the person to add a note to.
25-
* @param note Note to be added to the person.
22+
*
2623
*/
27-
public PersonNoteExecutor(Index index, Note note) {
24+
public PersonNoteExecutor(Index index) {
2825
super(index);
29-
requireAllNonNull(index, note);
30-
this.note = note;
26+
requireAllNonNull(index);
3127
}
3228

3329
@Override
3430
public CommandResult execute() throws ExecuteException {
3531
Person storedPerson = super.getPerson();
3632
Person editedPerson = new Person(
37-
storedPerson.getName(), storedPerson.getPhone(), storedPerson.getEmail(), note, storedPerson.getTags());
33+
storedPerson.getName(), storedPerson.getPhone(), storedPerson.getEmail(),
34+
storedPerson.getNote(), storedPerson.getTags());
3835

3936
model.setPerson(storedPerson, editedPerson);
4037
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
4138

42-
return new CommandResult(generateSuccessMessage(editedPerson));
39+
return new CommandResult(generateSuccessMessage(editedPerson), false, true, editedPerson, false);
4340
}
4441

4542
/**
4643
* Generates a command execution success message based on whether
47-
* the note is added to or removed from
44+
* the note is added.
4845
* {@code personToEdit}.
4946
*/
5047
private String generateSuccessMessage(Person personToEdit) {
51-
String message = !note.value.isEmpty() ? MESSAGE_ADD_NOTE_SUCCESS : MESSAGE_DELETE_NOTE_SUCCESS;
52-
return String.format(message, personToEdit);
48+
return String.format(MESSAGE_OPEN_NOTE_SUCCESS, personToEdit);
5349
}
5450

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

6965
// state check
70-
return super.equals(other) && note.equals(e.note);
66+
return super.equals(other);
7167
}
7268
}

src/main/java/seedu/address/logic/parser/person/PersonCreateCommandParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public PersonCreateCommand parse(String args) throws ParseException {
4747
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
4848
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
4949
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
50-
Note note = new Note("");
50+
Note note = new Note("", "");
5151
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
5252

5353
Person person = new Person(name, phone, email, note, tagList);

src/main/java/seedu/address/logic/parser/person/PersonNoteCommandParser.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package seedu.address.logic.parser.person;
22

33
import static java.util.Objects.requireNonNull;
4-
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE;
54

65
import seedu.address.commons.core.Messages;
76
import seedu.address.commons.core.index.Index;
@@ -10,7 +9,6 @@
109
import seedu.address.logic.parser.ArgumentMultimap;
1110
import seedu.address.logic.parser.ArgumentTokenizer;
1211
import seedu.address.logic.parser.exceptions.ParseException;
13-
import seedu.address.model.person.Note;
1412

1513
public class PersonNoteCommandParser extends PersonCommandParser {
1614
/**
@@ -21,7 +19,7 @@ public class PersonNoteCommandParser extends PersonCommandParser {
2119
*/
2220
public PersonNoteCommand parse(String args) throws ParseException {
2321
requireNonNull(args);
24-
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NOTE);
22+
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args);
2523

2624
Index index;
2725

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

35-
String note = argMultimap.getValue(PREFIX_NOTE).orElse("");
36-
37-
return new PersonNoteCommand(index, new Note(note));
33+
return new PersonNoteCommand(index);
3834
}
3935
}

src/main/java/seedu/address/model/person/Note.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@
22

33
import static java.util.Objects.requireNonNull;
44

5+
/**
6+
* Represents a Person's note that is opened upon note command.
7+
*/
58
public class Note {
9+
10+
/** content of note **/
611
public final String value;
12+
public final String savedDate;
13+
714

815
/**
916
* Constructor for a Note instance.
1017
*
1118
* @param value Value of the note.
1219
*/
13-
public Note(String value) {
20+
public Note(String value, String savedDate) {
1421
requireNonNull(value);
1522
this.value = value;
23+
this.savedDate = savedDate;
24+
}
25+
26+
/**
27+
* Returns string representation of saved date.
28+
*
29+
* @return String representation of saved date
30+
*/
31+
public String getSavedDate() {
32+
return savedDate;
1633
}
1734

1835
@Override
@@ -24,7 +41,7 @@ public boolean equals(Object other) {
2441
return false;
2542
}
2643
Note noteOther = (Note) other;
27-
return value.equals(noteOther.value); // state check
44+
return value.equals(noteOther.value) && savedDate.equals(noteOther.savedDate); // state check
2845
}
2946

3047
@Override
@@ -36,4 +53,6 @@ public int hashCode() {
3653
public String toString() {
3754
return value;
3855
}
56+
57+
3958
}

src/main/java/seedu/address/model/person/Person.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public Email getEmail() {
7171
public Note getNote() {
7272
return note;
7373
}
74+
public String getNoteSavedDate() {
75+
return note.getSavedDate();
76+
}
77+
7478

7579
public void addSuperGroup(SuperGroup superGroup) {
7680
superGroups.add(superGroup.toString());
@@ -144,15 +148,20 @@ public String toString() {
144148
.append("; Phone: ")
145149
.append(getPhone())
146150
.append("; Email: ")
147-
.append(getEmail())
148-
.append("; Note: ")
149-
.append(getNote());
151+
.append(getEmail());
150152

153+
String noteSavedDate = note.getSavedDate();
154+
if (!noteSavedDate.isEmpty()) {
155+
builder.append("; Last Edited: ")
156+
.append(getNoteSavedDate());
157+
}
151158
Set<Tag> tags = getTags();
159+
152160
if (!tags.isEmpty()) {
153161
builder.append("; Tags: ");
154162
tags.forEach(builder::append);
155163
}
164+
156165
return builder.toString();
157166
}
158167

0 commit comments

Comments
 (0)