Skip to content

Commit c7d62e1

Browse files
Merge pull request #165 from aliciamichellew/alicia/note-delete
Add note `delete` feature
2 parents 68af6ad + bfe32f3 commit c7d62e1

14 files changed

+156
-12
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class NoteAddCommand extends NoteCommand {
1717

1818
// Also available in abstract class LocalCourseCommand to support polymorphism
1919
public static final String NOTE_ADD_MESSAGE_USAGE = COMMAND_WORD
20-
+ "add [content] [tags]: Adds a local course.";
20+
+ "add [content] [tags]: Adds a note.";
2121
public static final String ACTION_WORD = "add";
2222

2323
public static final String MESSAGE_SUCCESS = "New note added: %1$s";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import seedu.address.commons.util.ToStringBuilder;
6+
import seedu.address.logic.Messages;
7+
import seedu.address.logic.commands.exceptions.CommandException;
8+
import seedu.address.model.Model;
9+
import seedu.address.model.SeplendidModel;
10+
import seedu.address.model.notes.Note;
11+
import seedu.address.seplendidui.UiUtil;
12+
13+
/**
14+
* Deletes a note to the NoteList.
15+
*/
16+
public class NoteDeleteCommand extends NoteCommand {
17+
18+
// Also available in abstract class LocalCourseCommand to support polymorphism
19+
public static final String NOTE_DELETE_MESSAGE_USAGE = COMMAND_WORD
20+
+ " delete [index]: Deletes a note.";
21+
public static final String ACTION_WORD = "delete";
22+
23+
public static final String MESSAGE_SUCCESS = "Note deleted: %1$s";
24+
25+
private final Integer noteIndexToDelete;
26+
27+
/**
28+
* Creates a NoteAddCommand to add the specified {@code Note}
29+
*
30+
* @param note The Note to be added into Storage.
31+
*/
32+
public NoteDeleteCommand(int note) {
33+
super();
34+
requireNonNull(note);
35+
noteIndexToDelete = note;
36+
}
37+
38+
/**
39+
* TBD: This stub is to be removed after morphing is complete.
40+
*
41+
* @param model {@code Model} which the command should operate on.
42+
* @return Nothing.
43+
* @throws CommandException Always.
44+
*/
45+
@Override
46+
public CommandResult execute(Model model) throws CommandException {
47+
throw new CommandException("TBD: this is a stub and should be removed after morph.");
48+
}
49+
50+
@Override
51+
public CommandResult execute(SeplendidModel seplendidModel) throws CommandException {
52+
requireNonNull(seplendidModel);
53+
54+
Note deletedNote = seplendidModel.deleteNote(noteIndexToDelete);
55+
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(deletedNote)),
56+
UiUtil.ListViewModel.NOTE_LIST);
57+
58+
}
59+
60+
@Override
61+
public boolean equals(Object other) {
62+
if (other == this) {
63+
return true;
64+
}
65+
66+
// instanceof handles nulls
67+
if (!(other instanceof NoteDeleteCommand)) {
68+
return false;
69+
}
70+
71+
NoteDeleteCommand otherNoteDeleteCommand = (NoteDeleteCommand) other;
72+
return noteIndexToDelete.equals(otherNoteDeleteCommand.noteIndexToDelete);
73+
}
74+
75+
@Override
76+
public String toString() {
77+
return new ToStringBuilder(this)
78+
.add("noteToDelete", noteIndexToDelete)
79+
.toString();
80+
}
81+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ public class CliSyntax {
3737
public static final SeplendidParameter PARAMETER_MAPPINGATTRIBUTE =
3838
new SeplendidParameter("mappingattribute");
3939
public static final SeplendidParameter PARAMETER_QUERY = new SeplendidParameter("query");
40+
public static final SeplendidParameter PARAMETER_INDEX = new SeplendidParameter("index");
41+
4042

4143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package seedu.address.logic.parser;
2+
3+
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
import static seedu.address.logic.parser.CliSyntax.PARAMETER_INDEX;
5+
import static seedu.address.logic.parser.ParserUtil.areValuesEnclosedAndNonEmpty;
6+
7+
import seedu.address.logic.commands.NoteDeleteCommand;
8+
import seedu.address.logic.parser.exceptions.ParseException;
9+
10+
11+
/**
12+
* Parses input arguments and deletes a Note object.
13+
*/
14+
public class NoteDeleteCommandParser implements Parser<NoteDeleteCommand> {
15+
16+
/**
17+
* Parses the given {@code String} of arguments in the context of the NoteAddCommand
18+
* and returns an NoteAddCommand object for execution.
19+
*
20+
* @throws ParseException if the user input does not conform the expected format
21+
*/
22+
public NoteDeleteCommand parse(String args) throws ParseException {
23+
if (!areValuesEnclosedAndNonEmpty(args)) {
24+
throw new ParseException(
25+
String.format(MESSAGE_INVALID_COMMAND_FORMAT,
26+
NoteDeleteCommand.NOTE_DELETE_MESSAGE_USAGE));
27+
}
28+
29+
SeplendidArgumentMap parameterToArgMap =
30+
SeplendidArgumentTokenizer.tokenize(args, PARAMETER_INDEX);
31+
32+
if (!ParserUtil.areArgumentsPresent(parameterToArgMap, PARAMETER_INDEX)) {
33+
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
34+
NoteDeleteCommand.NOTE_DELETE_MESSAGE_USAGE));
35+
}
36+
37+
// All arguments should be a non-empty {@code Optional}
38+
Integer noteIndex = Integer.valueOf(parameterToArgMap.getValue(PARAMETER_INDEX).get());
39+
40+
return new NoteDeleteCommand(noteIndex);
41+
}
42+
43+
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import seedu.address.logic.commands.MappingSearchCommand;
2323
import seedu.address.logic.commands.NoteAddCommand;
2424
import seedu.address.logic.commands.NoteCommand;
25+
import seedu.address.logic.commands.NoteDeleteCommand;
2526
import seedu.address.logic.commands.NoteListCommand;
2627
import seedu.address.logic.commands.NoteSearchCommand;
2728
import seedu.address.logic.commands.PartnerCourseAddCommand;
@@ -254,6 +255,8 @@ private NoteCommand getNoteCommandWithArg(String userInput, String actionWord, S
254255
return new NoteAddCommandParser().parse(arguments);
255256
case NoteSearchCommand.ACTION_WORD:
256257
return new NoteSearchCommandParser().parse(arguments);
258+
case NoteDeleteCommand.ACTION_WORD:
259+
return new NoteDeleteCommandParser().parse(arguments);
257260
default:
258261
logger.finer("This user input caused a ParseException: " + userInput);
259262
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public void setNote(Note target, Note editedNote) {
9090
* Removes {@code toRemove} from this {@code NoteCatalogue}.
9191
* {@code toRemove} must exist in the note catalogue.
9292
*/
93-
public void removeNote(Note toRemove) {
94-
notes.remove(toRemove);
93+
public Note removeNote(int toRemove) {
94+
return notes.remove(toRemove);
9595
}
9696

9797
//// util methods

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,10 @@ public interface SeplendidModel {
228228
/**
229229
* Deletes the given Note.
230230
* The note must exist in the NoteCatalogue.
231+
*
232+
* @return
231233
*/
232-
void deleteNote(Note note);
234+
Note deleteNote(int noteIndex);
233235

234236
/**
235237
* Adds the given Note.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ public boolean hasNote(Note note) {
449449
}
450450

451451
@Override
452-
public void deleteNote(Note target) {
453-
noteCatalogue.removeNote(target);
452+
public Note deleteNote(int target) {
453+
return noteCatalogue.removeNote(target);
454454
}
455455

456456
@Override

src/main/java/seedu/address/model/notes/UniqueNoteList.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,21 @@ public void setNote(Note target, Note editedNote) {
7373
* Removes the equivalent note from the list.
7474
* The note must exist in the list.
7575
*/
76-
public void remove(Note toRemove) {
76+
public Note remove(int toRemove) {
7777
requireNonNull(toRemove);
78-
if (!internalList.remove(toRemove)) {
78+
79+
for (int i = toRemove; i < internalList.size(); i++) {
80+
internalList.get(i).setIndex(i);
81+
}
82+
83+
Note deletedNote;
84+
try {
85+
deletedNote = internalList.remove(toRemove - 1);
86+
} catch (Exception e) {
7987
throw new NoteNotFoundException();
8088
}
89+
90+
return deletedNote;
8191
}
8292

8393
/**

src/main/java/seedu/address/model/notes/exceptions/NoteNotFoundException.java

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
* Signals that the operation is unable to find the specified note.
55
*/
66
public class NoteNotFoundException extends RuntimeException {
7+
public NoteNotFoundException() {
8+
super("Note is not found. Please check if the index you input is valid.");
9+
}
710
}

src/test/java/seedu/address/logic/commands/LocalCourseAddCommandTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public boolean hasNote(Note note) {
295295
}
296296

297297
@Override
298-
public void deleteNote(Note note) {
298+
public Note deleteNote(int noteIndex) {
299299
throw new AssertionError("This method should not be called.");
300300
}
301301

src/test/java/seedu/address/logic/commands/LocalCourseDeleteCommandTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public boolean hasNote(Note note) {
303303
}
304304

305305
@Override
306-
public void deleteNote(Note note) {
306+
public Note deleteNote(int noteIndex) {
307307
throw new AssertionError("This method should not be called.");
308308
}
309309

src/test/java/seedu/address/logic/commands/PartnerCourseAddCommandTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public boolean hasNote(Note note) {
298298
}
299299

300300
@Override
301-
public void deleteNote(Note note) {
301+
public Note deleteNote(int noteIndex) {
302302
throw new AssertionError("This method should not be called.");
303303
}
304304

src/test/java/seedu/address/logic/commands/PartnerCourseDeleteCommandTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public boolean hasNote(Note note) {
295295
}
296296

297297
@Override
298-
public void deleteNote(Note note) {
298+
public Note deleteNote(int noteIndex) {
299299
throw new AssertionError("This method should not be called.");
300300
}
301301

0 commit comments

Comments
 (0)