forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from yadunut/add-module-search
Add module search
- Loading branch information
Showing
5 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/seedu/address/logic/commands/module/ModuleSearchCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package seedu.address.logic.commands.module; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE_CODE; | ||
|
||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.module.ModuleCode; | ||
|
||
/** | ||
* Searches for students with the specified module code. | ||
*/ | ||
public class ModuleSearchCommand extends Command { | ||
public static final String COMMAND_WORD = "module_search"; | ||
public static final String MESSAGE_USAGE = "Searches for students with the specified module code. " | ||
+ "Parameters: MODULE_CODE\n" | ||
+ "Example: " + COMMAND_WORD + " " + PREFIX_MODULE_CODE + "CS2103T"; | ||
private final ModuleCode moduleCode; | ||
|
||
/** | ||
* Creates a ModuleSearchCommand to search for students with the specified module code. | ||
* @param moduleCode | ||
*/ | ||
public ModuleSearchCommand(ModuleCode moduleCode) { | ||
requireNonNull(moduleCode); | ||
this.moduleCode = moduleCode; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
model.updateFilteredStudentList(student -> student.hasModule(moduleCode)); | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("List of students with module: ").append(moduleCode).append("\n"); | ||
sb.append(String.format(Messages.MESSAGE_STUDENTS_LISTED_OVERVIEW, model.getFilteredStudentList().size())); | ||
return new CommandResult(sb.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/seedu/address/logic/parser/ModuleSearchCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE_CODE; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.module.ModuleSearchCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.module.ModuleCode; | ||
|
||
/** | ||
* Parses input arguments and creates a new ModuleSearchCommand object | ||
*/ | ||
public class ModuleSearchCommandParser implements Parser<ModuleSearchCommand> { | ||
@Override | ||
public ModuleSearchCommand parse(String userInput) throws ParseException { | ||
ArgumentMultimap argumentMultimap = ArgumentTokenizer.tokenize(userInput, PREFIX_MODULE_CODE); | ||
if (!arePrefixesPresent(argumentMultimap, PREFIX_MODULE_CODE) || !argumentMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ModuleSearchCommand.MESSAGE_USAGE)); | ||
} | ||
argumentMultimap.verifyNoDuplicatePrefixesFor(PREFIX_MODULE_CODE); | ||
ModuleCode moduleCode = ParserUtil.parseModule(argumentMultimap.getValue(PREFIX_MODULE_CODE).get()); | ||
return new ModuleSearchCommand(moduleCode); | ||
} | ||
|
||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters