forked from nus-cs2103-AY2021S1/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.
Merge pull request nus-cs2103-AY2021S1#79 from chunyongg/add-delete-s…
…tudent Add and delete student
- Loading branch information
Showing
11 changed files
with
339 additions
and
8 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
57 changes: 57 additions & 0 deletions
57
src/main/java/seedu/address/logic/commands/AddStudentCommand.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,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())); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/seedu/address/logic/commands/DeleteStudentCommand.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,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())); | ||
} | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/seedu/address/logic/parser/AddStudentCommandParser.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,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()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/seedu/address/logic/parser/DeleteStudentCommandParser.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,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()); | ||
} | ||
} |
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
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
Oops, something went wrong.