Skip to content

Commit df80013

Browse files
Outsource business logic from Ui to Commands
1 parent e71b620 commit df80013

File tree

7 files changed

+53
-29
lines changed

7 files changed

+53
-29
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package seedu.duke.flashcard;
22

3-
import seedu.duke.flashcard.command.CreateFlashcardCommand;
4-
import seedu.duke.flashcard.command.FlashcardCommand;
5-
import seedu.duke.flashcard.command.ListFlashcardsCommand;
6-
import seedu.duke.flashcard.command.UnknownCommand;
3+
import seedu.duke.flashcard.command.*;
74

85
public class FlashcardCommandParser {
96
public FlashcardCommand parseInput(String input) {
107
if (input.startsWith("create flashcard")) {
118
return new CreateFlashcardCommand();
129
} else if (input.startsWith("list flashcards")) {
1310
return new ListFlashcardsCommand();
11+
} else if (input.startsWith("start review")) {
12+
return new StartReviewCommand();
1413
}
1514

1615
return new UnknownCommand();
Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package seedu.duke.flashcard;
22

3-
import seedu.duke.flashcard.command.CreateFlashcardCommand;
43
import seedu.duke.flashcard.command.FlashcardCommand;
5-
import seedu.duke.flashcard.command.ListFlashcardsCommand;
64

75
import java.util.Scanner;
86

@@ -16,27 +14,6 @@ public FlashcardUi(FlashcardList flashcardList) {
1614
}
1715

1816
public void executeCommand(FlashcardCommand command) {
19-
if (command instanceof CreateFlashcardCommand) {
20-
executeCreateFlashcardCommand();
21-
} else if (command instanceof ListFlashcardsCommand) {
22-
listFlashcards();
23-
}
24-
}
25-
26-
private void executeCreateFlashcardCommand() {
27-
System.out.print("Enter the front page text: ");
28-
String frontPageText = scanner.nextLine();
29-
System.out.print("Enter the back page text: ");
30-
String backPageText = scanner.nextLine();
31-
32-
Flashcard flashcard = new Flashcard(frontPageText, backPageText);
33-
34-
flashcardList.add(flashcard);
35-
}
36-
37-
public void listFlashcards() {
38-
for (Flashcard flashcard : flashcardList.getFlashcards()) {
39-
System.out.println(flashcard);
40-
}
17+
command.execute(scanner, flashcardList);
4118
}
4219
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
package seedu.duke.flashcard.command;
22

3+
import seedu.duke.flashcard.Flashcard;
4+
import seedu.duke.flashcard.FlashcardList;
5+
6+
import java.util.Scanner;
7+
38
public class CreateFlashcardCommand extends FlashcardCommand {
9+
public void execute(Scanner scanner, FlashcardList flashcardList) {
10+
System.out.print("Enter the front page text: ");
11+
String frontPageText = scanner.nextLine();
12+
System.out.print("Enter the back page text: ");
13+
String backPageText = scanner.nextLine();
14+
15+
Flashcard flashcard = new Flashcard(frontPageText, backPageText);
16+
17+
flashcardList.add(flashcard);
18+
}
419
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package seedu.duke.flashcard.command;
22

3-
public class FlashcardCommand {
3+
import seedu.duke.flashcard.FlashcardList;
4+
5+
import java.util.Scanner;
6+
7+
public abstract class FlashcardCommand {
8+
public abstract void execute(Scanner scanner, FlashcardList flashcardList);
49
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
package seedu.duke.flashcard.command;
22

3+
import seedu.duke.flashcard.Flashcard;
4+
import seedu.duke.flashcard.FlashcardList;
5+
6+
import java.util.Scanner;
7+
38
public class ListFlashcardsCommand extends FlashcardCommand {
9+
public void execute(Scanner scanner, FlashcardList flashcardList) {
10+
for (Flashcard flashcard : flashcardList.getFlashcards()) {
11+
System.out.println(flashcard);
12+
}
13+
}
414
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package seedu.duke.flashcard.command;
2+
3+
import seedu.duke.flashcard.FlashcardList;
4+
5+
import java.util.Scanner;
6+
7+
public class StartReviewCommand extends FlashcardCommand {
8+
public void execute(Scanner scanner, FlashcardList flashcardList) {
9+
System.out.println("");
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package seedu.duke.flashcard.command;
22

3+
import seedu.duke.flashcard.FlashcardList;
4+
5+
import java.util.Scanner;
6+
37
public class UnknownCommand extends FlashcardCommand {
8+
public void execute(Scanner scanner, FlashcardList flashcardList) {
9+
System.out.println("Unknown command! Please try again.");
10+
}
411
}

0 commit comments

Comments
 (0)