Skip to content

Commit

Permalink
Update list students of class command
Browse files Browse the repository at this point in the history
  • Loading branch information
qinxutan committed Mar 27, 2024
1 parent e0a8e4d commit 4f28073
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TUTORIALCLASS;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.TutorialClass;
import seedu.address.model.person.Person;

import java.util.ArrayList;
import java.util.List;

/**
* A command to list all students of a particular tutorial class.
*/
public class ListStudentsOfClassCommand extends Command {

public static final String COMMAND_WORD = "/list_students_of_class";
public static final String MESSAGE_EMPTY = "No classes available!";
public static final String COMMAND_WORD = "/class_list_students";
public static final String MESSAGE_EMPTY = "No students found in the specified tutorial class";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": List students of the tutorial class. \n"
+ "Parameters: "
+ PREFIX_MODULECODE + "MODULE CODE "
+ PREFIX_TUTORIALCLASS + "TUTORIAL CLASS "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_MODULECODE + "CS2103T "
+ PREFIX_TUTORIALCLASS + "T09 ";

private final ModuleCode module;
private final TutorialClass tutorialClass;

/**
* Creates a ListStudentsCommand to list students of the specified tutorial class.
*
* @param module The module code.
* @param tutorialClass The tutorial class.
*/
public ListStudentsOfClassCommand(ModuleCode module, TutorialClass tutorialClass) {
requireNonNull(module);
this.module = module;
this.tutorialClass = tutorialClass;
}
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.getAddressBook().getTutorialList().isEmpty()) {
return new CommandResult(MESSAGE_EMPTY);
ModuleCode existingModule = model.findModuleFromList(module);
if (existingModule == null || !existingModule.hasTutorialClass(tutorialClass)) {
return new CommandResult(String.format("Module %s or tutorial class %s not found",
module, tutorialClass, MESSAGE_EMPTY));
}
StringBuilder result = new StringBuilder();

}

private List<Person> getStudentsInClass(Model model) throws CommandException {
List<Person> allStudents = model.getFilteredPersonList();
List<Person> studentsInClass = new ArrayList<>();
for (Person student : allStudents) {
if (tutorialClass.hasStudent(student)) {
studentsInClass.add(student);
}
TutorialClass existingTutorialClass = model.findTutorialClassFromList(tutorialClass, module);
if (existingTutorialClass.getStudents().isEmpty()) {
return new CommandResult(MESSAGE_EMPTY);
}
return studentsInClass;
}

private String formatStudentsList(List<Person> students) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Students in ").append(moduleCode).append(" ").append(tutorialClass).append(":\n");
for (Person student : students) {
stringBuilder.append("- ").append(student.getName()).append("\n");
}
return stringBuilder.toString().trim();
StringBuilder result = new StringBuilder();
result.append("Module: ").append(module).append(", Tutorial Class: ")
.append(tutorialClass).append("\nStudents: ");
existingTutorialClass.getStudents().forEach(student -> result.append(student.getName()).append(", "));
return new CommandResult(result.toString().trim());
}

@Override
Expand All @@ -57,6 +67,6 @@ public boolean equals(Object other) {
return false;
}
ListStudentsOfClassCommand command = (ListStudentsOfClassCommand) other;
return moduleCode.equals(command.moduleCode) && tutorialClass.equals(command.tutorialClass);
return module.equals(command.module) && tutorialClass.equals(command.tutorialClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListClassesCommand;
import seedu.address.logic.commands.ListStudentsCommand;
import seedu.address.logic.commands.ListStudentsOfClassCommand;
import seedu.address.logic.commands.SearchStudentCommand;
import seedu.address.logic.commands.addstudenttoclasscommands.AddStudentToClassCommand;
import seedu.address.logic.commands.deletestudentcommands.DeleteStudentCommand;
Expand Down Expand Up @@ -102,6 +103,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddTeamCommand.COMMAND_WORD:
return new AddTeamCommandParser().parse(arguments);

case ListStudentsOfClassCommand.COMMAND_WORD:
return new ListStudentsOfClassCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
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());
}
}

3 changes: 2 additions & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.TutorialClass;
import seedu.address.model.person.Person;
Expand Down Expand Up @@ -73,7 +74,7 @@ public interface Model {
* @param tutorialClass to be searched
* @param moduleCode to be searched
*/
TutorialClass findTutorialClassFromList(TutorialClass tutorialClass, ModuleCode moduleCode);
TutorialClass findTutorialClassFromList(TutorialClass tutorialClass, ModuleCode moduleCode) throws CommandException;

/**
* Deletes the given person.
Expand Down
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public void parse_validArgs_returnsAddStudentToClassCommand() {

// Add by email
AddStudentToClassByEmailCommand expectedAddByEmailCommand = new AddStudentToClassByEmailCommand(BOB.getEmail(),
new ModuleCode(VALID_MODULE_AMY), new TutorialClass(VALID_TUTORIAL_AMY));
new ModuleCode(VALID_MODULE_AMY), new TutorialClass(VALID_TUTORIAL_AMY));
assertParseSuccess(parser, EMAIL_DESC_BOB + commandSuffix, expectedAddByEmailCommand);

// Add by student Id
AddStudentToClassByIdCommand expectedAddByIdCommand = new AddStudentToClassByIdCommand(BOB.getStudentId(),
new ModuleCode(VALID_MODULE_AMY), new TutorialClass(VALID_TUTORIAL_AMY));
new ModuleCode(VALID_MODULE_AMY), new TutorialClass(VALID_TUTORIAL_AMY));
assertParseSuccess(parser, STUDENT_ID_DESC_BOB + commandSuffix, expectedAddByIdCommand);

}
Expand Down
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"));
}
}

0 comments on commit 4f28073

Please sign in to comment.