6
6
7
7
import java .util .Scanner ;
8
8
9
+ /**
10
+ * This command allows deleting a specific flashcard (by its id).
11
+ */
9
12
public class DeleteFlashcardCommand extends DualFlashcardCommand {
10
-
11
13
public DeleteFlashcardCommand (String input ) {
12
14
this .input = input ;
13
15
beginnerCommandLength = 2 ;
14
16
expertCommandLength = 3 ;
15
17
syntaxString = "delete flashcard FLASHCARD_ID" ;
16
18
}
17
19
20
+ /**
21
+ * Starts an interactive process for deleting a flashcard by its id.
22
+ * The user is prompted to enter the id; and then it is deleted.
23
+ *
24
+ * @param scanner Scanner for getting user input.
25
+ * @param flashcardList FlashcardList from which to delete.
26
+ */
18
27
protected void executeBeginnerMode (Scanner scanner ,
19
28
FlashcardList flashcardList ) {
20
- System .out .println (" Enter id of the flashcard you want to delete:" +
21
- " " );
29
+ assert flashcardList != null : "Must be a valid flashcardList " +
30
+ "instance" ;
31
+
32
+ System .out .println (" Enter id of the flashcard you want to " +
33
+ "delete: " );
22
34
23
35
String input = scanner .nextLine ();
24
36
int flashcardId ;
@@ -33,9 +45,19 @@ protected void executeBeginnerMode(Scanner scanner,
33
45
deleteFlashcardById (flashcardId , flashcardList );
34
46
}
35
47
48
+ /**
49
+ * Allows deleting a flashcard whose id is already known.
50
+ *
51
+ * @param scanner Scanner for getting user input.
52
+ * @param flashcardList FlashcardList from which to delete.
53
+ */
36
54
protected void executeExpertMode (Scanner scanner ,
37
55
FlashcardList flashcardList ) {
56
+ assert flashcardList != null : "Must be a valid flashcardList " +
57
+ "instance" ;
58
+
38
59
String [] commandParts = input .split (" " );
60
+ assert commandParts .length != 0 : "must contain command parts" ;
39
61
40
62
try {
41
63
int flashcardId = Integer .parseInt (commandParts [2 ]);
@@ -45,15 +67,22 @@ protected void executeExpertMode(Scanner scanner,
45
67
}
46
68
}
47
69
70
+ /**
71
+ * Tries to delete a flashcard by id and prints whether it succeeded.
72
+ *
73
+ * @param flashcardId The id of the flashcard to delete.
74
+ * @param flashcardList The list of all known flashcards.
75
+ */
48
76
private void deleteFlashcardById (int flashcardId , FlashcardList flashcardList ) {
49
77
boolean deletionWasSuccessful =
50
78
flashcardList .deleteFlashcardById (flashcardId );
51
79
52
80
if (deletionWasSuccessful ) {
53
- System .out .println (" Flashcard with id " + flashcardId + " has been " +
54
- "successfully deleted." );
81
+ System .out .println (" Flashcard with id " + flashcardId +
82
+ " has been successfully deleted." );
55
83
} else {
56
- System .out .println (" Couldn't find a flashcard with id " + flashcardId );
84
+ System .out .println (" Couldn't find a flashcard with id "
85
+ + flashcardId );
57
86
System .out .println (" No deletion has been performed. Please " +
58
87
"try again with a valid id." );
59
88
}
0 commit comments