Skip to content

Commit 5c38987

Browse files
authored
Merge pull request nus-cs2113-AY2324S1#15 from AY2324S1-CS2113-F11-3/master
Update
2 parents a8f0822 + 643dff1 commit 5c38987

13 files changed

+184
-33
lines changed

data/flashcards/flashcard.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
11 | f | dfdf | 5
77
12 | dfdf | | 5
88
13 | 1 | j | 5
9-
14 | Hello | Duke | 5
9+
14 | Hello | Duke | 5

src/main/java/seedu/duke/Duke.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public static void main(String[] args) {
1616
new Duke().run();
1717
}
1818

19+
/**
20+
* Starts a REPL session where commands are inputted and then processed.
21+
*/
1922
private void run() {
2023
Calendar calendar = new Calendar();
2124
FlashcardComponent fc = new FlashcardComponent(calendar);
@@ -31,9 +34,13 @@ private void run() {
3134

3235
if (fc.isResponsible(input)) {
3336
fc.processInput(input);
37+
continue;
3438
} else if (cm.isResponsible(input)) {
3539
cm.processInput(input);
36-
} else if (input.toLowerCase().strip().equals("exit")) {
40+
continue;
41+
}
42+
43+
if (input.toLowerCase().strip().equals("exit")) {
3744
System.out.println(" You are exiting TaskLinker! Bye!");
3845
break;
3946
} else if (input.toLowerCase().strip().equals("help")) {
@@ -57,6 +64,8 @@ private void printHelp() {
5764
"list flashcards",
5865
"create flashcard",
5966
"review flashcards",
67+
"delete flashcard",
68+
"delete all flashcards",
6069
"help",
6170
"add event",
6271
"add goal event",

src/main/java/seedu/duke/flashcard/Flashcard.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public Flashcard(String frontText, String backText) {
2828

2929
globalMaxId += 1;
3030
id = globalMaxId;
31+
32+
assert globalMaxId >= id : "No id must be bigger than globalMaxId";
3133
}
3234

3335
/**

src/main/java/seedu/duke/flashcard/FlashcardCommandParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public class FlashcardCommandParser {
2222
* @param input The text inputted by the user.
2323
* @return The FlashcardCommand corresponding to input
2424
*/
25-
public FlashcardCommand parseInput(String input, Calendar calendar) {
26-
assert input != null : "input is null";
25+
public FlashcardCommand parseInput(String input) {
26+
assert input != null : "input must not be null";
2727

2828
input = input.toLowerCase().strip();
2929

src/main/java/seedu/duke/flashcard/FlashcardComponent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ public FlashcardUi getUi(){
6666
* @return Whether FlashcardComponent is responsible for handling the input.
6767
*/
6868
public boolean isResponsible(String input) {
69+
assert input != null : "input must not be null";
70+
6971
FlashcardCommand command = parser.parseInput(input, calendar);
7072

73+
7174
if (command instanceof UnknownCommand) {
7275
return false;
7376
} else {
@@ -81,6 +84,8 @@ public boolean isResponsible(String input) {
8184
* @param input The text inputted by the user.
8285
*/
8386
public void processInput(String input) {
87+
assert input != null : "input must not be null";
88+
8489
FlashcardCommand command = parser.parseInput(input, calendar);
8590
assert !(command instanceof UnknownCommand) : "Command cannot be " +
8691
"unknown";

src/main/java/seedu/duke/flashcard/FlashcardList.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public void add(Flashcard flashcard) {
4747
*/
4848
public void deleteAllFlashcards() {
4949
flashcards.clear();
50+
51+
assert flashcards.size() == 0 : "flashcardList should be empty now";
5052
}
5153

5254
/**

src/main/java/seedu/duke/flashcard/FlashcardUi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class FlashcardUi {
2121
*/
2222
public FlashcardUi(FlashcardList flashcardList) {
2323
scanner = new Scanner(System.in);
24+
25+
assert flashcardList != null : "flashcardList cannot be null";
2426
this.flashcardList = flashcardList;
2527
}
2628

src/main/java/seedu/duke/flashcard/command/CreateFlashcardCommand.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,12 @@ public class CreateFlashcardCommand extends FlashcardCommand {
2121
* @param flashcardList Which flashcards to perform actions on.
2222
*/
2323
public void execute(Scanner scanner, FlashcardList flashcardList) {
24-
System.out.print(" Enter the front page text: ");
25-
String frontPageText = scanner.nextLine();
24+
assert scanner != null : "Must be a valid Scanner instance";
25+
assert flashcardList != null : "Must be a valid FlashcardList " +
26+
"instance";
2627

27-
while (frontPageText.strip().equals("")) {
28-
System.out.println(" Invalid input! The front text must " +
29-
"contain at least one letter or digit!");
30-
31-
System.out.print(" Enter the front page text: ");
32-
frontPageText = scanner.nextLine();
33-
}
34-
35-
System.out.print(" Enter the back page text: ");
36-
String backPageText = scanner.nextLine();
37-
38-
while (backPageText.strip().equals("")) {
39-
System.out.println(" Invalid input! The back text must " +
40-
"contain at least one letter or digit!");
41-
42-
System.out.print(" Enter the back page text: ");
43-
backPageText = scanner.nextLine();
44-
}
28+
String frontPageText = getInputUntilNonEmptyString(scanner, "front");
29+
String backPageText = getInputUntilNonEmptyString(scanner, "back");
4530

4631
Flashcard flashcard = new Flashcard(frontPageText, backPageText);
4732

@@ -51,4 +36,31 @@ public void execute(Scanner scanner, FlashcardList flashcardList) {
5136
System.out.println(" Success! Flashcard has been added to your " +
5237
"collection.");
5338
}
39+
40+
/**
41+
* Gets a user input for a flashcard, making sure that it is non-empty.
42+
*
43+
* @param scanner To get user input.
44+
* @param flashcardSide Which side of the flashcard (front or back) is
45+
* targeted.
46+
* @return The user input for the specified flashcardSide.
47+
*/
48+
private String getInputUntilNonEmptyString(Scanner scanner,
49+
String flashcardSide) {
50+
System.out.print(" Enter the " + flashcardSide + " page text: ");
51+
String text = scanner.nextLine();
52+
53+
while (text.strip().equals("")) {
54+
System.out.println(" Invalid input! The " + flashcardSide +
55+
" text must contain at least one letter or digit!");
56+
57+
System.out.print(" Enter the " + flashcardSide
58+
+ " page text: ");
59+
text = scanner.nextLine();
60+
}
61+
62+
assert text != null : "Must be a non-null string";
63+
assert (!text.strip().equals("")) : "Must be a non-empty string";
64+
return text;
65+
}
5466
}

src/main/java/seedu/duke/flashcard/command/DeleteAllFlashcardsCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ public void execute(Scanner scanner, FlashcardList flashcardList) {
1919

2020
System.out.println(" All your flashcards have been successfully " +
2121
"deleted .");
22+
23+
assert flashcardList.isEmpty() : "flashcardList must be empty";
2224
}
2325
}

src/main/java/seedu/duke/flashcard/command/DeleteFlashcardCommand.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,31 @@
66

77
import java.util.Scanner;
88

9+
/**
10+
* This command allows deleting a specific flashcard (by its id).
11+
*/
912
public class DeleteFlashcardCommand extends DualFlashcardCommand {
10-
1113
public DeleteFlashcardCommand(String input) {
1214
this.input = input;
1315
beginnerCommandLength = 2;
1416
expertCommandLength = 3;
1517
syntaxString = "delete flashcard FLASHCARD_ID";
1618
}
1719

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+
*/
1827
protected void executeBeginnerMode(Scanner scanner,
1928
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: ");
2234

2335
String input = scanner.nextLine();
2436
int flashcardId;
@@ -33,9 +45,19 @@ protected void executeBeginnerMode(Scanner scanner,
3345
deleteFlashcardById(flashcardId, flashcardList);
3446
}
3547

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+
*/
3654
protected void executeExpertMode(Scanner scanner,
3755
FlashcardList flashcardList) {
56+
assert flashcardList != null : "Must be a valid flashcardList " +
57+
"instance";
58+
3859
String[] commandParts = input.split(" ");
60+
assert commandParts.length != 0 : "must contain command parts";
3961

4062
try {
4163
int flashcardId = Integer.parseInt(commandParts[2]);
@@ -45,15 +67,22 @@ protected void executeExpertMode(Scanner scanner,
4567
}
4668
}
4769

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+
*/
4876
private void deleteFlashcardById(int flashcardId, FlashcardList flashcardList) {
4977
boolean deletionWasSuccessful =
5078
flashcardList.deleteFlashcardById(flashcardId);
5179

5280
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.");
5583
} 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);
5786
System.out.println(" No deletion has been performed. Please " +
5887
"try again with a valid id.");
5988
}

0 commit comments

Comments
 (0)