Skip to content

Commit bc1efc3

Browse files
committed
Implements add note command for user to add note
1 parent 39b2f7f commit bc1efc3

26 files changed

Lines changed: 778 additions & 34 deletions

src/main/java/seedu/address/MainApp.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@
2121
import seedu.address.model.UserPrefs;
2222
import seedu.address.model.meeting.MeetingBook;
2323
import seedu.address.model.meeting.ReadOnlyMeetingBook;
24+
import seedu.address.model.note.NoteBook;
25+
import seedu.address.model.note.ReadOnlyNoteBook;
2426
import seedu.address.model.person.AddressBook;
2527
import seedu.address.model.person.ReadOnlyAddressBook;
2628
import seedu.address.model.util.SampleDataUtil;
2729
import seedu.address.storage.AddressBookStorage;
2830
import seedu.address.storage.JsonAddressBookStorage;
2931
import seedu.address.storage.JsonMeetingBookStorage;
32+
import seedu.address.storage.JsonNoteBookStorage;
3033
import seedu.address.storage.JsonUserPrefsStorage;
3134
import seedu.address.storage.MeetingBookStorage;
35+
import seedu.address.storage.NoteBookStorage;
3236
import seedu.address.storage.Storage;
3337
import seedu.address.storage.StorageManager;
3438
import seedu.address.storage.UserPrefsStorage;
@@ -63,7 +67,8 @@ public void init() throws Exception {
6367
UserPrefs userPrefs = initPrefs(userPrefsStorage);
6468
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
6569
MeetingBookStorage meetingBookStorage = new JsonMeetingBookStorage(userPrefs.getMeetingBookFilePath());
66-
storage = new StorageManager(addressBookStorage, meetingBookStorage, userPrefsStorage);
70+
NoteBookStorage noteBookStorage = new JsonNoteBookStorage(userPrefs.getNoteBookFilePath());
71+
storage = new StorageManager(addressBookStorage, meetingBookStorage, noteBookStorage, userPrefsStorage);
6772

6873
initLogging(config);
6974

@@ -82,8 +87,10 @@ public void init() throws Exception {
8287
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
8388
Optional<ReadOnlyAddressBook> addressBookOptional;
8489
Optional<ReadOnlyMeetingBook> meetingBookOptional;
90+
Optional<ReadOnlyNoteBook> noteBookOptional;
8591
ReadOnlyAddressBook initialDataAddressBook;
8692
ReadOnlyMeetingBook initialDataMeetingBook;
93+
ReadOnlyNoteBook initialDataNoteBook;
8794

8895
try {
8996
addressBookOptional = storage.readAddressBook();
@@ -115,8 +122,23 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
115122
initialDataMeetingBook = new MeetingBook();
116123
}
117124

125+
//--============= NOTE ==================================================================================
118126

119-
return new ModelManager(initialDataAddressBook, initialDataMeetingBook, userPrefs);
127+
try {
128+
noteBookOptional = storage.readNoteBook();
129+
if (!noteBookOptional.isPresent()) {
130+
logger.info("Data file not found. Will be starting with a sample NoteBook");
131+
}
132+
initialDataNoteBook = noteBookOptional.orElseGet(SampleDataUtil::getSampleNoteBook);
133+
} catch (DataConversionException e) {
134+
logger.warning("Data file not in the correct format. Will be starting with an empty NoteBook");
135+
initialDataNoteBook = new NoteBook();
136+
} catch (IOException e) {
137+
logger.warning("Problem while reading from the file. Will be starting with an empty NoteBook");
138+
initialDataNoteBook = new NoteBook();
139+
}
140+
141+
return new ModelManager(initialDataAddressBook, initialDataMeetingBook, initialDataNoteBook, userPrefs);
120142
}
121143

122144
private void initLogging(Config config) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import seedu.address.model.Model;
1313
import seedu.address.model.meeting.Meeting;
1414
import seedu.address.model.meeting.ReadOnlyMeetingBook;
15+
import seedu.address.model.note.Note;
16+
import seedu.address.model.note.ReadOnlyNoteBook;
1517
import seedu.address.model.person.Person;
1618
import seedu.address.model.person.ReadOnlyAddressBook;
1719

@@ -53,6 +55,12 @@ public interface Logic {
5355
/** Returns an unmodifiable view of the filtered list of meetings */
5456
ObservableList<Meeting> getFilteredMeetingList();
5557

58+
ReadOnlyNoteBook getNoteBook();
59+
60+
ObservableList<Note> getFilteredNoteList();
61+
62+
Path getNoteBookFilePath();
63+
5664
/**
5765
* Returns the user prefs' GUI settings.
5866
*/

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import seedu.address.model.Model;
1818
import seedu.address.model.meeting.Meeting;
1919
import seedu.address.model.meeting.ReadOnlyMeetingBook;
20+
import seedu.address.model.note.Note;
21+
import seedu.address.model.note.ReadOnlyNoteBook;
2022
import seedu.address.model.person.Person;
2123
import seedu.address.model.person.ReadOnlyAddressBook;
2224
import seedu.address.storage.Storage;
@@ -52,6 +54,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE
5254
try {
5355
storage.saveAddressBook(model.getAddressBook());
5456
storage.saveMeetingBook(model.getMeetingBook());
57+
storage.saveNoteBook(model.getNoteBook());
5558
} catch (IOException ioe) {
5659
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
5760
}
@@ -93,6 +96,24 @@ public GuiSettings getGuiSettings() {
9396
public void setGuiSettings(GuiSettings guiSettings) {
9497
model.setGuiSettings(guiSettings);
9598
}
99+
100+
//======================================Notebook methods ============================================
101+
@Override
102+
public ReadOnlyNoteBook getNoteBook() {
103+
return model.getNoteBook();
104+
}
105+
106+
@Override
107+
public ObservableList<Note> getFilteredNoteList() {
108+
return model.getFilteredNoteList();
109+
}
110+
111+
@Override
112+
public Path getNoteBookFilePath() {
113+
return model.getNoteBookFilePath();
114+
}
115+
116+
96117
//======================================Timetable UI methods ============================================
97118

98119
@Override

src/main/java/seedu/address/logic/commands/notes/AddNoteCommand.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package seedu.address.logic.commands.notes;
22

33
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
6+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUP;
47
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
8+
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
9+
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRIORITY;
510

611
import seedu.address.logic.commands.Command;
712
import seedu.address.logic.commands.CommandResult;
@@ -15,7 +20,11 @@ public class AddNoteCommand extends Command {
1520

1621
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a note to the note book. "
1722
+ "Parameters: "
18-
+ PREFIX_NAME + "NAME ";
23+
+ PREFIX_NAME + "NAME "
24+
+ "[" + PREFIX_PRIORITY + "PRIORITY]...\n"
25+
+ "Example: " + COMMAND_WORD + " "
26+
+ PREFIX_NAME + "Complete CS2103 Tutorial "
27+
+ PREFIX_PRIORITY + "5 ";
1928

2029
public static final String MESSAGE_SUCCESS = "New note added: %1$s";
2130
public static final String MESSAGE_DUPLICATE_NOTE = "This note already exists in the note book";

src/main/java/seedu/address/logic/parser/MeetBuddyParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public Command parseCommand(String userInput) throws ParseException {
109109

110110
case UnsortMeetingCommand.COMMAND_WORD:
111111
return new UnsortMeetingCommand();
112+
113+
//======================= Note =====================================
114+
case AddNoteCommand.COMMAND_WORD:
115+
return new AddNoteCommandParser().parse(arguments);
116+
112117
//======================= Timetable =====================================
113118

114119
case SetTimetableCommand.COMMAND_WORD:

src/main/java/seedu/address/logic/parser/ParserUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import seedu.address.model.meeting.Description;
99
import seedu.address.model.meeting.MeetingName;
1010
import seedu.address.model.meeting.Priority;
11+
import seedu.address.model.note.Content;
1112
import seedu.address.model.person.Address;
1213
import seedu.address.model.person.Email;
1314
import seedu.address.model.person.PersonName;
@@ -196,4 +197,18 @@ public static Set<Index> parsePersonsConnection(Collection<String> oneBasedIndex
196197
}
197198
return indexSet;
198199
}
200+
201+
// =========================== ParserUtil for Note ==============================
202+
203+
/**
204+
* Parses a {@code String content} into a {@code Content}.
205+
* Leading and trailing whitespaces will be trimmed.
206+
*
207+
*/
208+
public static Content parseNoteContent(String content) {
209+
requireNonNull(content);
210+
String trimmedContent = content.trim();
211+
return new Content(trimmedContent);
212+
}
213+
199214
}
Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,61 @@
11
package seedu.address.logic.parser.notes;
22

3-
public class AddNoteCommandParser {
4-
}
3+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRIORITY;
6+
7+
import java.util.stream.Stream;
8+
9+
import seedu.address.logic.commands.notes.AddNoteCommand;
10+
import seedu.address.logic.parser.ArgumentMultimap;
11+
import seedu.address.logic.parser.ArgumentTokenizer;
12+
import seedu.address.logic.parser.Parser;
13+
import seedu.address.logic.parser.ParserUtil;
14+
import seedu.address.logic.parser.Prefix;
15+
import seedu.address.logic.parser.exceptions.ParseException;
16+
import seedu.address.model.meeting.Priority;
17+
import seedu.address.model.note.Content;
18+
import seedu.address.model.note.Note;
19+
20+
/**
21+
* Parses input arguments and creates a new AddNoteCommand object
22+
*/
23+
public class AddNoteCommandParser implements Parser<AddNoteCommand> {
24+
25+
/**
26+
* Parses the given {@code String} of arguments in the context of the AddNoteCommand
27+
* and returns an AddNoteCommand object for execution.
28+
* @throws ParseException if the user input does not conform the expected format
29+
*/
30+
public AddNoteCommand parse(String args) throws ParseException {
31+
ArgumentMultimap argMultimap =
32+
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PRIORITY);
33+
34+
if (!arePrefixesPresent(argMultimap, PREFIX_NAME)
35+
|| !argMultimap.getPreamble().isEmpty()) {
36+
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddNoteCommand.MESSAGE_USAGE));
37+
}
38+
39+
Content content = ParserUtil.parseNoteContent(argMultimap.getValue(PREFIX_NAME).get());
40+
Priority priority;
41+
42+
if (!arePrefixesPresent(argMultimap, PREFIX_PRIORITY)) {
43+
priority = new Priority("1");
44+
} else {
45+
priority = ParserUtil.parseMeetingPriority(argMultimap.getValue(PREFIX_PRIORITY).get());
46+
}
47+
48+
Note note = new Note(content, priority);
49+
50+
return new AddNoteCommand(note);
51+
}
52+
53+
/**
54+
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
55+
* {@code ArgumentMultimap}.
56+
*/
57+
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
58+
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
59+
}
60+
61+
}

src/main/java/seedu/address/model/Model.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package seedu.address.model;
22

3+
import static java.util.Objects.requireNonNull;
4+
35
import java.nio.file.Path;
46
import java.time.LocalDate;
57
import java.time.LocalDateTime;
@@ -241,7 +243,6 @@ public interface Model {
241243
*/
242244
boolean hasNote(Note note);
243245

244-
245246
/**
246247
* Deletes the given note.
247248
* The note must exist in the note book.
@@ -270,6 +271,9 @@ public interface Model {
270271
*/
271272
void updateFilteredNoteList(Predicate<Note> predicate);
272273

274+
Path getNoteBookFilePath();
275+
276+
void setNoteBookFilePath(Path noteBookFilePath);
273277

274278
//============================= Timetable settings =====================================
275279

src/main/java/seedu/address/model/ModelManager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,17 @@ public void updateFilteredNoteList(Predicate<Note> predicate) {
497497
filteredNotes.setPredicate(predicate);
498498
}
499499

500+
@Override
501+
public Path getNoteBookFilePath() {
502+
return userPrefs.getNoteBookFilePath();
503+
}
504+
505+
@Override
506+
public void setNoteBookFilePath(Path noteBookFilePath) {
507+
requireNonNull(noteBookFilePath);
508+
userPrefs.setAddressBookFilePath(noteBookFilePath);
509+
}
510+
500511
//================= Get timetable prefs methods ================================================
501512

502513
@Override

src/main/java/seedu/address/model/UserPrefs.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class UserPrefs implements ReadOnlyUserPrefs {
1616
private GuiSettings guiSettings = new GuiSettings();
1717
private Path addressBookFilePath = Paths.get("data" , "addressbook.json");
1818
private Path meetingBookFilePath = Paths.get("data", "meetingbook.json");
19+
private Path noteBookFilePath = Paths.get("data", "notebook.json");
1920

2021
/**
2122
* Creates a {@code UserPrefs} with default values.
@@ -56,15 +57,25 @@ public Path getMeetingBookFilePath() {
5657
return meetingBookFilePath;
5758
}
5859

60+
public Path getNoteBookFilePath() {
61+
return noteBookFilePath;
62+
}
63+
5964
public void setAddressBookFilePath(Path addressBookFilePath) {
6065
requireNonNull(addressBookFilePath);
6166
this.addressBookFilePath = addressBookFilePath;
6267
}
68+
6369
public void setMeetingBookFilePath(Path meetingBookFilePath) {
6470
requireNonNull(meetingBookFilePath);
6571
this.meetingBookFilePath = meetingBookFilePath;
6672
}
6773

74+
public void setNoteBookFilePath(Path noteBookFilePath) {
75+
requireNonNull(noteBookFilePath);
76+
this.noteBookFilePath = noteBookFilePath;
77+
}
78+
6879
@Override
6980
public boolean equals(Object other) {
7081
if (other == this) {

0 commit comments

Comments
 (0)