From f9dbd3ebe7f5ced63fa60e2b5853f177bf55d0a2 Mon Sep 17 00:00:00 2001 From: Wendelin Wemhoener Date: Thu, 26 Oct 2023 13:43:35 +0800 Subject: [PATCH] Add expert mode for deleting and reviewing --- data/flashcards/flashcard.txt | 3 +- .../java/seedu/duke/flashcard/Flashcard.java | 3 -- .../flashcard/FlashcardCommandParser.java | 4 +- .../command/DeleteFlashcardCommand.java | 30 +++++++++++++- .../command/DualFlashcardCommand.java | 29 +++++++++++++ .../flashcard/command/StartReviewCommand.java | 41 ++++++++++++++++++- 6 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 src/main/java/seedu/duke/flashcard/command/DualFlashcardCommand.java diff --git a/data/flashcards/flashcard.txt b/data/flashcards/flashcard.txt index 791a74256d..e0e9e10c3e 100644 --- a/data/flashcards/flashcard.txt +++ b/data/flashcards/flashcard.txt @@ -1 +1,2 @@ -dfdf | f | - | - | - +ddd | dfdf | - | - | - +ddff | a | - | - | - diff --git a/src/main/java/seedu/duke/flashcard/Flashcard.java b/src/main/java/seedu/duke/flashcard/Flashcard.java index 191690844c..ed620d2a8d 100644 --- a/src/main/java/seedu/duke/flashcard/Flashcard.java +++ b/src/main/java/seedu/duke/flashcard/Flashcard.java @@ -11,7 +11,6 @@ public class Flashcard { private static int globalMaxId = 1; private String frontText; private String backText; - private ArrayList tags; private ArrayList reviews; private LocalDateTime lastReviewOn; private int id; @@ -20,7 +19,6 @@ public Flashcard(String frontText, String backText) { this.frontText = frontText; this.backText = backText; - tags = new ArrayList<>(); reviews = new ArrayList<>(); lastReviewOn = null; @@ -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(); } diff --git a/src/main/java/seedu/duke/flashcard/FlashcardCommandParser.java b/src/main/java/seedu/duke/flashcard/FlashcardCommandParser.java index c03015926f..e9a7b6f39c 100644 --- a/src/main/java/seedu/duke/flashcard/FlashcardCommandParser.java +++ b/src/main/java/seedu/duke/flashcard/FlashcardCommandParser.java @@ -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(); diff --git a/src/main/java/seedu/duke/flashcard/command/DeleteFlashcardCommand.java b/src/main/java/seedu/duke/flashcard/command/DeleteFlashcardCommand.java index 2432f2ce88..6d12b96cb1 100644 --- a/src/main/java/seedu/duke/flashcard/command/DeleteFlashcardCommand.java +++ b/src/main/java/seedu/duke/flashcard/command/DeleteFlashcardCommand.java @@ -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:" + " "); @@ -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); diff --git a/src/main/java/seedu/duke/flashcard/command/DualFlashcardCommand.java b/src/main/java/seedu/duke/flashcard/command/DualFlashcardCommand.java new file mode 100644 index 0000000000..8d2bb119bd --- /dev/null +++ b/src/main/java/seedu/duke/flashcard/command/DualFlashcardCommand.java @@ -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."); + } + } +} diff --git a/src/main/java/seedu/duke/flashcard/command/StartReviewCommand.java b/src/main/java/seedu/duke/flashcard/command/StartReviewCommand.java index d811969c92..3bea59c6aa 100644 --- a/src/main/java/seedu/duke/flashcard/command/StartReviewCommand.java +++ b/src/main/java/seedu/duke/flashcard/command/StartReviewCommand.java @@ -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 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"); @@ -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())) { @@ -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) {