Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2324S1#58 from wendelinwemhoener/add-…
Browse files Browse the repository at this point in the history
…new-command-syntax

Add expert mode for deleting and reviewing
  • Loading branch information
wendelinwemhoener authored Oct 26, 2023
2 parents 281140a + f9dbd3e commit 7931262
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 10 deletions.
3 changes: 2 additions & 1 deletion data/flashcards/flashcard.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dfdf | f | - | - | -
ddd | dfdf | - | - | -
ddff | a | - | - | -
3 changes: 0 additions & 3 deletions src/main/java/seedu/duke/flashcard/Flashcard.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class Flashcard {
private static int globalMaxId = 1;
private String frontText;
private String backText;
private ArrayList<String> tags;
private ArrayList<FlashcardReview> reviews;
private LocalDateTime lastReviewOn;
private int id;
Expand All @@ -20,7 +19,6 @@ public Flashcard(String frontText, String backText) {
this.frontText = frontText;
this.backText = backText;

tags = new ArrayList<>();
reviews = new ArrayList<>();

lastReviewOn = null;
Expand Down Expand Up @@ -60,7 +58,6 @@ public String getBackText() {
public String toString() {
return "front text: " + frontText + System.lineSeparator()
+ "back text: " + backText + System.lineSeparator()
+ "tags: " + tags.toString() + System.lineSeparator()
+ "next review due on: " + lastReviewOn + System.lineSeparator()
+ "id: " + id + System.lineSeparator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public FlashcardCommand parseInput(String input) {
} else if (input.startsWith("list flashcards")) {
return new ListFlashcardsCommand();
} else if (input.startsWith("review flashcards")) {
return new StartReviewCommand();
return new StartReviewCommand(input);
} else if (input.startsWith("delete all flashcards")) {
return new DeleteAllFlashcardsCommand();
} else if (input.startsWith("delete flashcard")) {
return new DeleteFlashcardCommand();
return new DeleteFlashcardCommand(input);
}

return new UnknownCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

import java.util.Scanner;

public class DeleteFlashcardCommand extends FlashcardCommand {
public void execute(Scanner scanner, FlashcardList flashcardList) {
public class DeleteFlashcardCommand extends DualFlashcardCommand {
private String input = null;

public DeleteFlashcardCommand(String input) {
this.input = input;
beginnerCommandLength = 2;
expertCommandLength = 3;
syntaxString = "delete flashcard FLASHCARD_ID";
}

protected void executeBeginnerMode(Scanner scanner,
FlashcardList flashcardList) {
System.out.println(" Enter id of the flashcard you want to delete:" +
" ");

Expand All @@ -19,6 +29,22 @@ public void execute(Scanner scanner, FlashcardList flashcardList) {
return;
}

deleteFlashcardById(flashcardId, flashcardList);
}

protected void executeExpertMode(Scanner scanner,
FlashcardList flashcardList) {
String[] commandParts = input.split(" ");

try {
int flashcardId = Integer.parseInt(commandParts[2]);
deleteFlashcardById(flashcardId, flashcardList);
} catch (NumberFormatException e) {
System.out.println(" Invalid id! Id must be an integer");
}
}

private void deleteFlashcardById(int flashcardId, FlashcardList flashcardList) {
boolean deletionWasSuccessful =
flashcardList.deleteFlashcardById(flashcardId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.duke.flashcard.command;

import seedu.duke.flashcard.FlashcardList;

import java.util.Scanner;

public abstract class DualFlashcardCommand extends FlashcardCommand {
protected int beginnerCommandLength;
protected int expertCommandLength;
protected String input;
protected String syntaxString;

protected abstract void executeBeginnerMode(Scanner scanner, FlashcardList flashcardList);

protected abstract void executeExpertMode(Scanner scanner,
FlashcardList flashcardList);

public void execute(Scanner scanner, FlashcardList flashcardList) {
String[] commandParts = input.split(" ");
if (commandParts.length == beginnerCommandLength) {
executeBeginnerMode(scanner, flashcardList);
} else if (commandParts.length == expertCommandLength) {
executeExpertMode(scanner, flashcardList);
} else {
System.out.println(" Invalid syntax! The syntax is '" + syntaxString + "'");
System.out.println(" Please try again.");
}
}
}
41 changes: 39 additions & 2 deletions src/main/java/seedu/duke/flashcard/command/StartReviewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@
import java.util.Arrays;
import java.util.Scanner;

public class StartReviewCommand extends FlashcardCommand {
public class StartReviewCommand extends DualFlashcardCommand {
private final ArrayList<String> choices = new ArrayList<>(Arrays.asList(
"a", "b", "c"));

public StartReviewCommand(String input) {
this.input = input;
beginnerCommandLength = 2;
expertCommandLength = 3;
syntaxString = "review flashcards REVIEW_MODE";
}

private String getUserChoiceReviewMode(Scanner scanner) {
System.out.println(" How do you want to review your flashcards?");
System.out.println(" a) random mode");
Expand All @@ -25,7 +32,8 @@ private String getUserChoiceReviewMode(Scanner scanner) {
return scanner.nextLine();
}

public void execute(Scanner scanner, FlashcardList flashcardList) {
protected void executeBeginnerMode(Scanner scanner,
FlashcardList flashcardList) {
String choice = getUserChoiceReviewMode(scanner);

while (!choices.contains(choice.toLowerCase())) {
Expand All @@ -35,6 +43,35 @@ public void execute(Scanner scanner, FlashcardList flashcardList) {
choice = getUserChoiceReviewMode(scanner);
}

startReview(scanner, flashcardList, choice);
}

protected void executeExpertMode(Scanner scanner,
FlashcardList flashcardList) {
String[] commandParts = input.split(" ");

try {
String choice = commandParts[2].toLowerCase();

if (!choices.contains(choice)) {
System.out.println(" Invalid choice! Your choice must be a, b " +
"or c! Please try again.");
return;
}

startReview(scanner, flashcardList, choice);
} catch (IndexOutOfBoundsException e) {
System.out.println(" Invalid syntax! The syntax is 'review " +
"flashcards REVIEW_MODE'");
System.out.println(" Please try again.");
}


}

private void startReview(Scanner scanner,
FlashcardList flashcardList,
String choice) {
ReviewMode reviewMode = createReviewMode(choice.toLowerCase(), flashcardList);

if (reviewMode instanceof RandomReviewMode) {
Expand Down

0 comments on commit 7931262

Please sign in to comment.