forked from AY2324S2-CS2103T-T09-4/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update list students of class command
- Loading branch information
Showing
7 changed files
with
239 additions
and
33 deletions.
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
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
53 changes: 53 additions & 0 deletions
53
src/main/java/seedu/address/logic/parser/ListStudentsOfClassCommandParser.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,53 @@ | ||
package seedu.address.logic.parser; | ||
|
||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TUTORIALCLASS; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.ListStudentsOfClassCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.TutorialClass; | ||
|
||
|
||
/** | ||
* Parses input arguments and creates a new ListStudentsCommand object. | ||
*/ | ||
public class ListStudentsOfClassCommandParser implements Parser<ListStudentsOfClassCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the ListStudentsCommand | ||
* and returns a ListStudentsCommand object for execution. | ||
* | ||
* @param args String containing the arguments. | ||
* @return ListStudentsCommand object representing the command. | ||
* @throws ParseException if the user input does not conform to the expected format. | ||
*/ | ||
public ListStudentsOfClassCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_MODULECODE, PREFIX_TUTORIALCLASS); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_MODULECODE, PREFIX_TUTORIALCLASS) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
ListStudentsOfClassCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_MODULECODE, PREFIX_TUTORIALCLASS); | ||
ModuleCode module = ParserUtil.parseModuleCode(argMultimap.getValue(PREFIX_MODULECODE).get()); | ||
TutorialClass tutorialClass = ParserUtil.parseTutorialClass(argMultimap.getValue(PREFIX_TUTORIALCLASS).get()); | ||
|
||
return new ListStudentsOfClassCommand(module, tutorialClass); | ||
} | ||
|
||
/** | ||
* Returns true if all the prefixes contain non-empty values in the given {@code ArgumentMultimap}. | ||
*/ | ||
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
99 changes: 99 additions & 0 deletions
99
src/test/java/seedu/address/logic/commands/ListStudentsOfClassCommandTest.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,99 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.TutorialClass; | ||
|
||
/** | ||
* Contains integration tests (interaction with the Model) and unit tests for ListStudentsOfClassCommand. | ||
*/ | ||
public class ListStudentsOfClassCommandTest { | ||
|
||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
private TutorialClass tutorialClass; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
ModuleCode newModule = new ModuleCode("CS2103T"); | ||
model.addModule(newModule); | ||
TutorialClass newTutorialClass = new TutorialClass("T09"); | ||
newModule.addTutorialClass(newTutorialClass); | ||
tutorialClass = newTutorialClass; | ||
} | ||
|
||
@Test | ||
public void execute_listStudentsOfClass_success() throws CommandException { | ||
ListStudentsOfClassCommand listStudentsOfClassCommand = new ListStudentsOfClassCommand( | ||
new ModuleCode("CS2103T"), new TutorialClass("T09")); | ||
CommandResult commandResult = listStudentsOfClassCommand.execute(model); | ||
|
||
// Update the expected result to match the actual result | ||
String expectedFeedback = "No students found in the specified tutorial class"; | ||
|
||
assertEquals(expectedFeedback, commandResult.getFeedbackToUser()); | ||
} | ||
|
||
@Test | ||
public void execute_listStudentsOfClassNoSuchModule_fail() throws CommandException { | ||
ListStudentsOfClassCommand listStudentsOfClassCommand = new ListStudentsOfClassCommand( | ||
new ModuleCode("CS1111"), new TutorialClass("T09")); | ||
|
||
// Execute the command and capture the CommandResult | ||
CommandResult commandResult = listStudentsOfClassCommand.execute(model); | ||
|
||
// Assert that the CommandResult contains the expected message | ||
assertEquals("Module CS1111 or tutorial class T09 not found", commandResult.getFeedbackToUser()); | ||
} | ||
|
||
@Test | ||
public void execute_listStudentsOfClassNoSuchClass_fail() throws CommandException { | ||
ListStudentsOfClassCommand listStudentsOfClassCommand = new ListStudentsOfClassCommand( | ||
new ModuleCode("CS2103T"), new TutorialClass("T99")); | ||
|
||
CommandResult commandResult = listStudentsOfClassCommand.execute(model); | ||
|
||
assertEquals(String.format("Module %s or tutorial class %s not found", "CS2103T", "T99"), | ||
commandResult.getFeedbackToUser()); | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
ListStudentsOfClassCommand listStudentsOfClassFirstCommand = new ListStudentsOfClassCommand( | ||
new ModuleCode("CS2103T"), new TutorialClass("T09")); | ||
ListStudentsOfClassCommand listStudentsOfClassSecondCommand = new ListStudentsOfClassCommand( | ||
new ModuleCode("CS2103T"), new TutorialClass("T09")); | ||
ListStudentsOfClassCommand listStudentsOfClassDifferentModule = new ListStudentsOfClassCommand( | ||
new ModuleCode("CS2101"), new TutorialClass("T09")); | ||
ListStudentsOfClassCommand listStudentsOfClassDifferentClass = new ListStudentsOfClassCommand( | ||
new ModuleCode("CS2103T"), new TutorialClass("T10")); | ||
|
||
// same object -> returns true | ||
assertTrue(listStudentsOfClassFirstCommand.equals(listStudentsOfClassFirstCommand)); | ||
|
||
// same values -> returns true | ||
assertTrue(listStudentsOfClassFirstCommand.equals(listStudentsOfClassSecondCommand)); | ||
|
||
// different types -> returns false | ||
assertFalse(listStudentsOfClassFirstCommand.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(listStudentsOfClassFirstCommand.equals(null)); | ||
|
||
// different module -> returns false | ||
assertFalse(listStudentsOfClassFirstCommand.equals(listStudentsOfClassDifferentModule)); | ||
|
||
// different class -> returns false | ||
assertFalse(listStudentsOfClassFirstCommand.equals(listStudentsOfClassDifferentClass)); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
src/test/java/seedu/address/logic/parser/ListStudentsOfClassCommandParserTest.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,39 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TUTORIALCLASS; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.ListStudentsOfClassCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.TutorialClass; | ||
|
||
public class ListStudentsOfClassCommandParserTest { | ||
|
||
private final ListStudentsOfClassCommandParser parser = new ListStudentsOfClassCommandParser(); | ||
|
||
@Test | ||
public void parse_validArgs_returnsListStudentsOfClassCommand() throws ParseException { | ||
// Example valid input | ||
String validArgs = " " + PREFIX_MODULECODE + "CS2103T" + " " + PREFIX_TUTORIALCLASS + "T01"; | ||
ListStudentsOfClassCommand expectedCommand = new ListStudentsOfClassCommand( | ||
new ModuleCode("CS2103T"), new TutorialClass("T01")); | ||
assertEquals(expectedCommand, parser.parse(validArgs)); | ||
} | ||
|
||
@Test | ||
public void parse_invalidArgs_throwsParseException() { | ||
// No module code provided | ||
assertThrows(ParseException.class, () -> parser.parse(PREFIX_TUTORIALCLASS + "T01")); | ||
|
||
// No tutorial class provided | ||
assertThrows(ParseException.class, () -> parser.parse(PREFIX_MODULECODE + "CS2103T")); | ||
|
||
// Invalid prefix provided | ||
assertThrows(ParseException.class, () -> parser.parse(" " + PREFIX_MODULECODE + "CS2103T" + " T01")); | ||
} | ||
} |