Skip to content

Commit

Permalink
Merge pull request #151 from GabrielWLM/add-view-group-command
Browse files Browse the repository at this point in the history
Add view group command
  • Loading branch information
juliussneezer04 authored Oct 29, 2021
2 parents 9816ec7 + 9edc419 commit cab9c5d
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public class Messages {
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX = "The student index provided is invalid";
public static final String MESSAGE_INVALID_CLASS_DISPLAYED_INDEX = "The class index provided is invalid";
public static final String MESSAGE_INVALID_GROUP_DISPLAYED_INDEX = "The group index provided is invalid";
public static final String MESSAGE_STUDENTS_LISTED_OVERVIEW = "%1$d students listed!";
public static final String MESSAGE_TUTORIAL_CLASSES_LISTED_OVERVIEW = "%1$d classes listed!";

public static final String MESSAGE_TUTORIAL_CLASSES_LISTED_OVERVIEW = "%1$d tutorial classes listed!";
public static final String MESSAGE_TUTORIAL_GROUP_LISTED_OVERVIEW = "%1$d students from tutorial group listed!";
public static final String MESSAGE_CLASS_DOES_NOT_EXIST = "This class does not exist in Classmate";
public static final String MESSAGE_GROUP_DOES_NOT_EXIST = "This group does not exist in Classmate";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_CLASS_DOES_NOT_EXIST;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLASSCODE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUPNUMBER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TYPE;
Expand All @@ -15,17 +16,16 @@ public class AddGroupCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a group to a class to Classmate. "
+ "Parameters: "
+ PREFIX_GROUPNUMBER + "GROUPNAME "
+ PREFIX_CLASSCODE + "CLASSCODE "
+ PREFIX_TYPE + "TYPE "
+ PREFIX_GROUPNUMBER + "GROUPNUMBER "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_GROUPNUMBER + "1 "
+ PREFIX_CLASSCODE + "G06 "
+ PREFIX_TYPE + "OP1 ";

public static final String MESSAGE_SUCCESS = "New group added: %1$s";
public static final String MESSAGE_DUPLICATE_GROUP = "This group already exists in Classmate";
public static final String MESSAGE_CLASS_NOT_EXIST = "The class does not exist in Classmate";

private final TutorialGroup toAdd;
private final TutorialClass toAddTutorialClass;
Expand All @@ -46,7 +46,7 @@ public CommandResult execute(Model model) throws CommandException {

// check if tutorial class already exists in ClassMATE
if (!model.hasTutorialClass(toAddTutorialClass)) {
throw new CommandException(MESSAGE_CLASS_NOT_EXIST);
throw new CommandException(MESSAGE_CLASS_DOES_NOT_EXIST);
}

if (model.hasTutorialGroup(toAdd)) {
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewGroupCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLASSCODE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUPNUMBER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TYPE;

import seedu.address.commons.core.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.student.GroupMemberPredicate;
import seedu.address.model.tutorialclass.TutorialClass;
import seedu.address.model.tutorialgroup.TutorialGroup;

/**
* Lists all students in a tutorial group in ClassMATE given a class code, tutorial group type and tutorial group name
* Keyword matching is case-insensitive.
*/
public class ViewGroupCommand extends Command {

public static final String COMMAND_WORD = "viewg";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all students in a tutorial group given a class "
+ "code, tutorial group type and tutorial group name (case-insensitive) and displays them as a list"
+ "with index numbers.\n"
+ "Parameters: "
+ PREFIX_CLASSCODE + "CLASSCODE "
+ PREFIX_TYPE + "TYPE "
+ PREFIX_GROUPNUMBER + "GROUPNUMBER "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_GROUPNUMBER + "3 "
+ PREFIX_CLASSCODE + "G06 "
+ PREFIX_TYPE + "OP2 ";

private final TutorialGroup toView;
private final TutorialClass toViewTutorialClass;

/**
* Creates a ViewGroupCommand to show the students in the specified {@code TutorialGroup}
*/
public ViewGroupCommand(TutorialGroup tutorialGroup) {
requireNonNull(tutorialGroup);
toView = tutorialGroup;
// new class with the same class code created to check whether it exists in ClassMATE
toViewTutorialClass = TutorialClass.createTestTutorialClass(toView.getClassCode());
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

// check if tutorial class exists in ClassMATE
if (!model.hasTutorialClass(toViewTutorialClass)) {
throw new CommandException(Messages.MESSAGE_CLASS_DOES_NOT_EXIST);
}



model.updateFilteredStudentList(new GroupMemberPredicate(toView));
return new CommandResult(
String.format(Messages.MESSAGE_TUTORIAL_GROUP_LISTED_OVERVIEW,
model.getFilteredStudentList().size()));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ViewGroupCommand // instanceof handles nulls
&& toView.equals(((ViewGroupCommand) other).toView)); // state check
}
}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/ClassmateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import seedu.address.logic.commands.ListClassCommand;
import seedu.address.logic.commands.ListStudentCommand;
import seedu.address.logic.commands.ViewClassCommand;
import seedu.address.logic.commands.ViewGroupCommand;
import seedu.address.logic.commands.ViewStudentCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -97,6 +98,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ViewClassCommand.COMMAND_WORD:
return new ViewClassCommandParser().parse(arguments);

case ViewGroupCommand.COMMAND_WORD:
return new ViewGroupCommandParser().parse(arguments);

case ViewStudentCommand.COMMAND_WORD:
return new ViewStudentCommandParser().parse(arguments);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
public class CliSyntax {

/* Student Prefix definitions */
// Student Prefix definitions
public static final Prefix PREFIX_NAME = new Prefix("n/");
public static final Prefix PREFIX_PHONE = new Prefix("p/");
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import seedu.address.model.tutorialclass.ClassCodeContainsKeywordsPredicate;

/**
* Parses input arguments and creates a new FindCommand object
* Parses input arguments and creates a new FindClassCommand object
*/
public class FindClassCommandParser implements Parser<FindClassCommand> {

/**
* Parses the given {@code String} of arguments in the context of the FindCommand
* and returns a FindCommand object for execution.
* Parses the given {@code String} of arguments in the context of the FindClassCommand
* and returns a FindClassCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public FindClassCommand parse(String args) throws ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import seedu.address.model.student.NameContainsKeywordsPredicate;

/**
* Parses input arguments and creates a new FindCommand object
* Parses input arguments and creates a new FindStudentCommand object
*/
public class FindStudentCommandParser implements Parser<FindStudentCommand> {

/**
* Parses the given {@code String} of arguments in the context of the FindCommand
* and returns a FindCommand object for execution.
* Parses the given {@code String} of arguments in the context of the FindStudentCommand
* and returns a FindStudentCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public FindStudentCommand parse(String args) throws ParseException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLASSCODE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUPNUMBER;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TYPE;

import java.util.stream.Stream;

import seedu.address.logic.commands.AddGroupCommand;
import seedu.address.logic.commands.ViewGroupCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.student.ClassCode;
import seedu.address.model.tutorialgroup.GroupNumber;
import seedu.address.model.tutorialgroup.GroupType;
import seedu.address.model.tutorialgroup.TutorialGroup;

/**
* Parses input arguments and creates a new ViewGroupCommand object
*/
public class ViewGroupCommandParser implements Parser<ViewGroupCommand> {

/**
* Parses the given {@code String} of arguments in the context of the ViewGroupCommand
* and returns a ViewGroupCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ViewGroupCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_CLASSCODE, PREFIX_TYPE, PREFIX_GROUPNUMBER);

if (!arePrefixesPresent(argMultimap, PREFIX_CLASSCODE, PREFIX_TYPE, PREFIX_GROUPNUMBER)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddGroupCommand.MESSAGE_USAGE));
}

GroupNumber groupNumber = ParserUtil.parseGroupName(argMultimap.getValue(PREFIX_GROUPNUMBER).get());
ClassCode classCode = ParserUtil.parseClassCode(argMultimap.getValue(PREFIX_CLASSCODE).get());
GroupType groupType = ParserUtil.parseGroupType(argMultimap.getValue(PREFIX_TYPE).get());

TutorialGroup tutorialGroup = new TutorialGroup(groupNumber, classCode, groupType);

return new ViewGroupCommand(tutorialGroup);
}

private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}
11 changes: 7 additions & 4 deletions src/main/java/seedu/address/model/Classmate.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.model.tutorialclass.TutorialClass;
import seedu.address.model.tutorialclass.UniqueTutorialClassList;
import seedu.address.model.tutorialgroup.TutorialGroup;
import seedu.address.model.tutorialgroup.UniqueTutorialGroupList;

/**
* Wraps all data at the address-book level
Expand All @@ -22,6 +23,7 @@ public class Classmate implements ReadOnlyClassmate {

private final UniqueStudentList students;
private final UniqueTutorialClassList tutorialClasses;
private final UniqueTutorialGroupList tutorialGroups;

/*
* The 'unusual' code block below is a non-static initialization block, sometimes used to avoid duplication
Expand All @@ -33,6 +35,7 @@ public class Classmate implements ReadOnlyClassmate {
{
students = new UniqueStudentList();
tutorialClasses = new UniqueTutorialClassList();
tutorialGroups = new UniqueTutorialGroupList();
}

public Classmate() {}
Expand Down Expand Up @@ -155,30 +158,30 @@ public void removeTutorialClass(TutorialClass key) {
* {@code key} must exist in the ClassMATE.
*/
public void removeTutorialGroup(TutorialGroup key) {
tutorialClasses.remove(key);
tutorialGroups.remove(key);
}

/**
* Returns true if a tutorialGroup with the same identity as {@code tutorialGroup} exists in the ClassMATE.
*/
public boolean hasTutorialGroup(TutorialGroup tutorialGroup) {
requireNonNull(tutorialGroup);
return tutorialClasses.contains(tutorialGroup);
return tutorialGroups.contains(tutorialGroup);
}

/**
* Adds a tutorialGroup to the ClassMATE.
* The tutorial group must not already exist in the ClassMATE.
*/
public void addTutorialGroup(TutorialGroup tutorialGroup) {
tutorialClasses.add(tutorialGroup);
tutorialGroups.add(tutorialGroup);
}

/**
* Sorts the tutorial groups in ClassMATE.
*/
public void sortTutorialGroups() {
tutorialClasses.sort();
tutorialGroups.sort();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.model.student;

import java.util.function.Predicate;

import seedu.address.model.tutorialgroup.TutorialGroup;

/**
* Tests that a {@code Student}'s {@code TutorialGroup} matches the given TutorialGroup.
*/
public class GroupMemberPredicate implements Predicate<Student> {
private final TutorialGroup tutorialGroup;

public GroupMemberPredicate(TutorialGroup tutorialGroup) {
this.tutorialGroup = tutorialGroup;
}

public TutorialGroup getTutorialGroup() {
return tutorialGroup;
}

@Override
public boolean test(Student student) {
return student.getTutorialGroups().contains(tutorialGroup);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same instance
|| other instanceof GroupMemberPredicate // instanceof handles null
&& tutorialGroup.equals(((GroupMemberPredicate) other).getTutorialGroup()); // state check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import seedu.address.commons.util.StringUtil;

/**
* Tests that a {@code Student}'s {@code Name} matches any of the keywords given.
* Tests that a {@code TutorialClass}'s {@code ClassCode} matches any of the keywords given.
*/
public class ClassCodeContainsKeywordsPredicate implements Predicate<TutorialClass> {
private final List<String> keywords;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.address.model.tutorialgroup;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;

/**
* Tests that a {@code TutorialGroup}'s {@code GroupName} matches any of the keywords given.
*/
public class TutorialGroupContainsKeywordsPredicate implements Predicate<TutorialGroup> {
private final List<String> keywords;

public TutorialGroupContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

@Override
public boolean test(TutorialGroup tutorialGroup) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(
tutorialGroup.getClassCode().toString(), keyword));
//TODO Add GroupType as a keyword
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof TutorialGroupContainsKeywordsPredicate // instanceof handles nulls
&& keywords.equals(((TutorialGroupContainsKeywordsPredicate) other).keywords)); // state check
}

}

0 comments on commit cab9c5d

Please sign in to comment.