Skip to content

Commit

Permalink
add find group command
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielWLM committed Oct 29, 2021
1 parent cf8e8bc commit 9edc419
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 89 deletions.
1 change: 0 additions & 1 deletion src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 tutorial classes listed!";
public static final String MESSAGE_TUTORIAL_GROUP_LISTED_OVERVIEW = "%1$d students from tutorial group listed!";
Expand Down
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 @@ -16,16 +17,15 @@ public class AddGroupCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a group to a class to Classmate. "
+ "Parameters: "
+ PREFIX_CLASSCODE + "CLASSCODE "
+ PREFIX_GROUPNAME + "GROUPNAME "
+ 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_DOES_NOT_EXIST = "The class does not exist in Classmate";

private final TutorialGroup toAdd;
private final TutorialClass toAddTutorialClass;
Expand Down
38 changes: 26 additions & 12 deletions src/main/java/seedu/address/logic/commands/ViewGroupCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CLASSCODE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GROUPNAME;
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.ClassMemberPredicate;
import seedu.address.model.tutorialgroup.TutorialGroupContainsKeywordsPredicate;
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
Expand All @@ -23,26 +25,38 @@ public class ViewGroupCommand extends Command {
+ "with index numbers.\n"
+ "Parameters: "
+ PREFIX_CLASSCODE + "CLASSCODE "
+ PREFIX_GROUPNAME + "GROUPNAME "
+ PREFIX_TYPE + "TYPE "
+ PREFIX_GROUPNUMBER + "GROUPNUMBER "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_GROUPNAME + "3 "
+ PREFIX_GROUPNUMBER + "3 "
+ PREFIX_CLASSCODE + "G06 "
+ PREFIX_TYPE + "OP2 ";

private final TutorialGroupContainsKeywordsPredicate predicate;
private final TutorialGroup toView;
private final TutorialClass toViewTutorialClass;

public ViewGroupCommand(TutorialGroupContainsKeywordsPredicate predicate) {
this.predicate = predicate;
/**
* 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) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.updateFilteredTutorialGroupList(predicate);

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

model.updateFilteredStudentList(new ClassMemberPredicate(targetClassCode));


model.updateFilteredStudentList(new GroupMemberPredicate(toView));
return new CommandResult(
String.format(Messages.MESSAGE_TUTORIAL_GROUP_LISTED_OVERVIEW,
model.getFilteredStudentList().size()));
Expand All @@ -52,6 +66,6 @@ public CommandResult execute(Model model) {
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ViewGroupCommand // instanceof handles nulls
&& predicate.equals(((ViewGroupCommand) other).predicate)); // state check
&& 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
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
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.Arrays;
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.tutorialgroup.TutorialGroupContainsKeywordsPredicate;
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
Expand All @@ -19,15 +26,24 @@ public class ViewGroupCommandParser implements Parser<ViewGroupCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public ViewGroupCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewGroupCommand.MESSAGE_USAGE));
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));
}

String[] nameKeywords = trimmedArgs.split("\\s+");
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(new TutorialGroupContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
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
}
}
30 changes: 0 additions & 30 deletions src/main/java/seedu/address/model/student/GroupNamePredicate.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/main/java/seedu/address/model/student/GroupTypePredicate.java

This file was deleted.

0 comments on commit 9edc419

Please sign in to comment.