Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#79 from chunyongg/add-delete-s…
Browse files Browse the repository at this point in the history
…tudent

Add and delete student
  • Loading branch information
Nijnxw authored Oct 12, 2020
2 parents 08353fd + 7151020 commit 47e3e86
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public class Messages {

// Serenity messages
public static final String MESSAGE_GROUP_LISTED_OVERVIEW = "You are in tutorial group %1$s.";
public static final String MESSAGE_GROUP_EMPTY = "no such group!";
public static final String MESSAGE_GROUP_EMPTY = "No such group!";
public static final String MESSAGE_LESSON_LISTED_OVERVIEW = "You are in tutorial group %1$s, lesson %2$s.";
public static final String MESSAGE_STUDENT_EMPTY = "No such student!";
public static final String MESSAGE_LESSON_EMPTY = "no such lesson!";
public static final String MESSAGE_NOT_VIEWING_A_GROUP = "Group not specified.";
public static final String MESSAGE_NOT_VIEWING_A_LESSON = "Lesson not specified.";
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddStudentCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package seedu.address.logic.commands;

import static seedu.address.commons.core.Messages.MESSAGE_GROUP_EMPTY;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.group.GrpContainsKeywordPredicate;
import seedu.address.model.group.Student;

public class AddStudentCommand extends Command {

public static final String COMMAND_WORD = "addstudent";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds a new Student to a specified tutorial group. \n"
+ "Parameters: "
+ PREFIX_GRP + "GRP "
+ PREFIX_STUDENT + "NAME "
+ PREFIX_ID + "Student ID \n"
+ "Example: " + COMMAND_WORD + " " + PREFIX_GRP + "G04"
+ " " + PREFIX_STUDENT + "Ryan" + " " + PREFIX_ID + "e1234567";

public static final String MESSAGE_SUCCESS = "You added %s (%s) to tutorial group %s";

private final String studentName;
private final String studentId;
private final GrpContainsKeywordPredicate predicate;

/**
* Creates an AddStudentCommand to add the specified {@code Student}
* @param studentName
* @param studentId
* @param predicate
*/
public AddStudentCommand(String studentName, String studentId, GrpContainsKeywordPredicate predicate) {
requireAllNonNull(studentName, studentId, predicate);
this.studentName = studentName;
this.studentId = studentId;
this.predicate = predicate;
}

@Override
public CommandResult execute(Model model) throws CommandException {
Student student = new Student(studentName, studentId);
model.addStudentToGroup(student, predicate);
if (model.getFilteredGroupList().isEmpty()) {
//no such group exists
return new CommandResult(MESSAGE_GROUP_EMPTY);
} else {
return new CommandResult(
String.format(MESSAGE_SUCCESS, studentName, studentId, model.getFilteredGroupList().get(0).getName()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package seedu.address.logic.commands;

import static seedu.address.commons.core.Messages.MESSAGE_GROUP_EMPTY;
import static seedu.address.commons.core.Messages.MESSAGE_STUDENT_EMPTY;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;

import javafx.collections.ObservableList;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.group.Group;
import seedu.address.model.group.GrpContainsKeywordPredicate;
import seedu.address.model.group.Student;

public class DeleteStudentCommand extends Command {

public static final String COMMAND_WORD = "delstudent";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Removes a new Student from a specified tutorial group. \n"
+ "Parameters: "
+ PREFIX_GRP + "GRP "
+ PREFIX_STUDENT + "NAME "
+ PREFIX_ID + "Student ID \n"
+ "Example: " + COMMAND_WORD + " " + PREFIX_GRP + "G04" + " " + PREFIX_STUDENT + "Ryan" + " " + PREFIX_ID
+ "e1234567";

public static final String MESSAGE_SUCCESS = "You removed %s (%s) from tutorial group %s";

private final String studentName;
private final String studentId;
private final GrpContainsKeywordPredicate predicate;

/**
* Creates a DeleteStudentCommand to remove the specified {@code Student}
* @param studentName Name of Student
* @param studentId Id of Student
* @param predicate Group predicate
*/
public DeleteStudentCommand(String studentName, String studentId, GrpContainsKeywordPredicate predicate) {
requireAllNonNull(studentName, studentId, predicate);
this.studentName = studentName;
this.studentId = studentId;
this.predicate = predicate;
}


@Override
public CommandResult execute(Model model) throws CommandException {
Student student = new Student(studentName, studentId);
model.updateFilteredGroupList(predicate);
ObservableList<Group> groups = model.getFilteredGroupList();
if (groups.isEmpty()) {
//no such group
return new CommandResult(MESSAGE_GROUP_EMPTY);
}
if (!groups.get(0).getStudents().contains(student)) {
//student does not exist
return new CommandResult(MESSAGE_STUDENT_EMPTY);
} else {
model.removeStudentFromGroup(student, predicate);
model.updateFilteredGroupList(predicate);
return new CommandResult(
String.format(MESSAGE_SUCCESS, studentName, studentId, model.getFilteredGroupList().get(0).getName()));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;

import java.util.stream.Stream;

import seedu.address.logic.commands.AddStudentCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.group.GrpContainsKeywordPredicate;

public class AddStudentCommandParser implements Parser<AddStudentCommand> {

private final ParseException addStudentCommandParserException = new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddStudentCommand.MESSAGE_USAGE));

@Override
public AddStudentCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GRP, PREFIX_STUDENT, PREFIX_ID);

if (!arePrefixesPresent(argMultimap, PREFIX_GRP, PREFIX_STUDENT, PREFIX_ID) || !argMultimap.getPreamble()
.isEmpty()) {
throw addStudentCommandParserException;
}

String[] grpKeywordArray = argMultimap.getValue(PREFIX_GRP).get().split("\\s+");
String[] studentNameArray = argMultimap.getValue(PREFIX_STUDENT).get().split("\\s+");
String[] studentIdArray = argMultimap.getValue(PREFIX_ID).get().split("\\s+");

//if id or group keyword is more than 1, or if student name has more than 10 letters, throw exception
boolean matchesGrp = grpKeywordArray.length == 1;
boolean matchesId = studentIdArray.length == 1 && studentIdArray[0].length() == 8;
boolean matchesStudentName = studentNameArray.length <= 10;
if (!matchesGrp || !matchesId || !matchesStudentName) {
throw addStudentCommandParserException;
}

String studentName = String.join(" ", studentNameArray);
String studentId = studentIdArray[0];
String grpName = grpKeywordArray[0];

return new AddStudentCommand(studentName, studentId, new GrpContainsKeywordPredicate(grpName));
}

private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;

import java.util.stream.Stream;

import seedu.address.logic.commands.DeleteStudentCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.group.GrpContainsKeywordPredicate;

public class DeleteStudentCommandParser implements Parser<DeleteStudentCommand> {

private final ParseException deleteStudentCommandParserException = new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteStudentCommand.MESSAGE_USAGE));

@Override
public DeleteStudentCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GRP, PREFIX_STUDENT, PREFIX_ID);

if (!arePrefixesPresent(argMultimap, PREFIX_GRP, PREFIX_STUDENT, PREFIX_ID) || !argMultimap.getPreamble()
.isEmpty()) {
throw deleteStudentCommandParserException;
}

String[] grpKeywordArray = argMultimap.getValue(PREFIX_GRP).get().split("\\s+");
String[] studentNameArray = argMultimap.getValue(PREFIX_STUDENT).get().split("\\s+");
String[] studentIdArray = argMultimap.getValue(PREFIX_ID).get().split("\\s+");

//if id or group keyword is more than 1, or if student name has more than 10 letters, throw exception
boolean matchesGrp = grpKeywordArray.length == 1;
boolean matchesId = studentIdArray.length == 1 && studentIdArray[0].length() == 8;
boolean matchesStudentName = studentNameArray.length <= 10;
if (!matchesGrp || !matchesId || !matchesStudentName) {
throw deleteStudentCommandParserException;
}

String studentName = String.join(" ", studentNameArray);
String studentId = studentIdArray[0];
String grpName = grpKeywordArray[0];

return new DeleteStudentCommand(studentName, studentId, new GrpContainsKeywordPredicate(grpName));
}

private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/logic/parser/SerenityParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import seedu.address.logic.commands.AddLsnCommand;
import seedu.address.logic.commands.AddQnCommand;
import seedu.address.logic.commands.AddScoreCommand;
import seedu.address.logic.commands.AddStudentCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DelGrpCommand;
import seedu.address.logic.commands.DelQnCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DeleteLsnCommand;
import seedu.address.logic.commands.DeleteStudentCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
Expand Down Expand Up @@ -61,6 +63,12 @@ public Command parseCommand(String userInput) throws ParseException {
case AddGrpCommand.COMMAND_WORD:
return new AddGrpCommandParser().parse(arguments);

case AddStudentCommand.COMMAND_WORD:
return new AddStudentCommandParser().parse(arguments);

case DeleteStudentCommand.COMMAND_WORD:
return new DeleteStudentCommandParser().parse(arguments);

case AddLsnCommand.COMMAND_WORD:
return new AddLsnCommandParser().parse(arguments);

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public interface Model {
* {@code Predicate} that always evaluate to true
*/
Predicate<Person> PREDICATE_SHOW_ALL_PERSONS = unused -> true;
Predicate<Student> PREDICATE_SHOW_ALL_STUDENTS = unused -> true;

/**
* Replaces user prefs data with the data in {@code userPrefs}.
Expand Down Expand Up @@ -134,6 +133,16 @@ public interface Model {
*/
void addGroup(Group group);

/**
* Adds a Student to a Group
*/
void addStudentToGroup(Student student, Predicate<Group> predicate);

/**
* Removes a Student from a Group.
*/
void removeStudentFromGroup(Student student, Predicate<Group> predicate);

/**
* Updates the filter of the filtered group list to filter by the given {@code predicate}.
*
Expand Down Expand Up @@ -192,10 +201,8 @@ public interface Model {
* Returns an unmodifiable view of the student info list
*/
ObservableList<StudentInfo> getStudentInfoList();

/**
* Returns an unmodifiable view of the question list.
*/
ObservableList<Question> getQuestionList();

}
29 changes: 26 additions & 3 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public class ModelManager implements Model {
* Initializes a ModelManager with the given addressBook, userPrefs and serenity.
*/
public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs,
ReadOnlySerenity serenity) {
ReadOnlySerenity serenity) {
super();
requireAllNonNull(addressBook, userPrefs, serenity);

logger.fine("Initializing with address book: " + addressBook
+ " and user prefs " + userPrefs
+ " and serenity " + serenity);
+ " and user prefs " + userPrefs
+ " and serenity " + serenity);

this.addressBook = new AddressBook(addressBook);
this.userPrefs = new UserPrefs(userPrefs);
Expand Down Expand Up @@ -207,6 +207,29 @@ public void addGroup(Group group) {
serenity.addGroup(group);
}

@Override
public void addStudentToGroup(Student student, Predicate<Group> predicate) {
requireAllNonNull(student, predicate);
updateFilteredGroupList(predicate);
if (!filteredGroups.isEmpty()) {
students.add(student);
Group currentGroup = filteredGroups.get(0);
currentGroup.addStudentToGroup(student);
}
}

@Override
public void removeStudentFromGroup(Student student, Predicate<Group> predicate) {
requireAllNonNull(student, predicate);
updateFilteredGroupList(predicate);
UniqueStudentList students = filteredGroups.get(0).getStudents();
if (!filteredGroups.isEmpty() && students.contains(student)) {
students.remove(student);
Group currentGroup = filteredGroups.get(0);
currentGroup.removeStudentFromGroup(student);
}
}

@Override
public void updateFilteredGroupList(Predicate<Group> predicate) {
requireAllNonNull(predicate);
Expand Down
Loading

0 comments on commit 47e3e86

Please sign in to comment.