Skip to content

Commit

Permalink
Merge pull request #213 from raneeng/fix-view-search
Browse files Browse the repository at this point in the history
Fix view and search for non existent modules
  • Loading branch information
angelinawong1210 authored Nov 12, 2024
2 parents 2e9d5dd + 3f6f23a commit 470bdff
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 additions & 22 deletions src/main/java/seedu/duke/flashutils/utils/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,28 @@ public static Command createEditCommand(String input) {
}

public static Command createViewCommand(String input) {
Pattern viewModulePattern = Pattern.compile("--m\\s+(.+)");
Matcher matcher = viewModulePattern.matcher(input);
Pattern viewAllModulePattern = Pattern.compile("--all");
Matcher matcherAll = viewAllModulePattern.matcher(input);
if (matcher.find()) {
String moduleName = matcher.group(1);
FlashCardSet module = FlashBook.getInstance().getFlashCardSet(moduleName);
return new ViewCommand(module);
} else if (matcherAll.find()) {
return new ViewAllCommand();
} else {
return new InvalidCommand();
try {
Pattern viewModulePattern = Pattern.compile("--m\\s+(.+)");
Matcher matcher = viewModulePattern.matcher(input);
Pattern viewAllModulePattern = Pattern.compile("--all");
Matcher matcherAll = viewAllModulePattern.matcher(input);
if (matcher.find()) {
String moduleName = matcher.group(1);

if (!FlashBook.getInstance().flashCardSetExists(moduleName)) {
throw new FlashCardSetDoesNotExistException();
}

FlashCardSet module = FlashBook.getInstance().getFlashCardSet(moduleName);
return new ViewCommand(module);
} else if (matcherAll.find()) {
return new ViewAllCommand();
} else {
return new InvalidCommand();
}

} catch (FlashCardSetDoesNotExistException e) {
return new InvalidCommand("No such module exists");
}
}

Expand All @@ -212,16 +222,25 @@ public static Command createFlashbangCommand(String input) {
}
}
public static Command createSearchCommand(String input) {
Pattern searchPattern = Pattern.compile("--m\\s+(.+?)(?:\\s+(/t))?\\s+--s\\s+(.+)");
Matcher searchMatcher = searchPattern.matcher(input);
if (searchMatcher.find()) {
String module = searchMatcher.group(1);
boolean byTopic = searchMatcher.group(2) != null && searchMatcher.group(2).equals("/t");
String searchTerm = searchMatcher.group(3);
assert (!(module == null || searchTerm == null));
return new SearchCommand(searchTerm, byTopic, FlashBook.getInstance().getFlashCardSet(module));
} else {
return new InvalidCommand("Invalid format for search =.=");
try {
Pattern searchPattern = Pattern.compile("--m\\s+(.+?)(?:\\s+(/t))?\\s+--s\\s+(.+)");
Matcher searchMatcher = searchPattern.matcher(input);
if (searchMatcher.find()) {
String module = searchMatcher.group(1);

if (!FlashBook.getInstance().flashCardSetExists(module)) {
throw new FlashCardSetDoesNotExistException();
}

boolean byTopic = searchMatcher.group(2) != null && searchMatcher.group(2).equals("/t");
String searchTerm = searchMatcher.group(3);
assert (!(module == null || searchTerm == null));
return new SearchCommand(searchTerm, byTopic, FlashBook.getInstance().getFlashCardSet(module));
} else {
return new InvalidCommand("Invalid format for search =.=");
}
} catch (FlashCardSetDoesNotExistException e) {
return new InvalidCommand("No such module exists");
}
}

Expand Down

0 comments on commit 470bdff

Please sign in to comment.