Skip to content

Commit 47e3e86

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#79 from chunyongg/add-delete-student
Add and delete student
2 parents 08353fd + 7151020 commit 47e3e86

File tree

11 files changed

+339
-8
lines changed

11 files changed

+339
-8
lines changed

src/main/java/seedu/address/commons/core/Messages.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public class Messages {
1212

1313
// Serenity messages
1414
public static final String MESSAGE_GROUP_LISTED_OVERVIEW = "You are in tutorial group %1$s.";
15-
public static final String MESSAGE_GROUP_EMPTY = "no such group!";
15+
public static final String MESSAGE_GROUP_EMPTY = "No such group!";
1616
public static final String MESSAGE_LESSON_LISTED_OVERVIEW = "You are in tutorial group %1$s, lesson %2$s.";
17+
public static final String MESSAGE_STUDENT_EMPTY = "No such student!";
1718
public static final String MESSAGE_LESSON_EMPTY = "no such lesson!";
1819
public static final String MESSAGE_NOT_VIEWING_A_GROUP = "Group not specified.";
1920
public static final String MESSAGE_NOT_VIEWING_A_LESSON = "Lesson not specified.";
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package seedu.address.logic.commands;
2+
3+
import static seedu.address.commons.core.Messages.MESSAGE_GROUP_EMPTY;
4+
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
6+
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
7+
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;
8+
9+
import seedu.address.logic.commands.exceptions.CommandException;
10+
import seedu.address.model.Model;
11+
import seedu.address.model.group.GrpContainsKeywordPredicate;
12+
import seedu.address.model.group.Student;
13+
14+
public class AddStudentCommand extends Command {
15+
16+
public static final String COMMAND_WORD = "addstudent";
17+
public static final String MESSAGE_USAGE = COMMAND_WORD
18+
+ ": Adds a new Student to a specified tutorial group. \n"
19+
+ "Parameters: "
20+
+ PREFIX_GRP + "GRP "
21+
+ PREFIX_STUDENT + "NAME "
22+
+ PREFIX_ID + "Student ID \n"
23+
+ "Example: " + COMMAND_WORD + " " + PREFIX_GRP + "G04"
24+
+ " " + PREFIX_STUDENT + "Ryan" + " " + PREFIX_ID + "e1234567";
25+
26+
public static final String MESSAGE_SUCCESS = "You added %s (%s) to tutorial group %s";
27+
28+
private final String studentName;
29+
private final String studentId;
30+
private final GrpContainsKeywordPredicate predicate;
31+
32+
/**
33+
* Creates an AddStudentCommand to add the specified {@code Student}
34+
* @param studentName
35+
* @param studentId
36+
* @param predicate
37+
*/
38+
public AddStudentCommand(String studentName, String studentId, GrpContainsKeywordPredicate predicate) {
39+
requireAllNonNull(studentName, studentId, predicate);
40+
this.studentName = studentName;
41+
this.studentId = studentId;
42+
this.predicate = predicate;
43+
}
44+
45+
@Override
46+
public CommandResult execute(Model model) throws CommandException {
47+
Student student = new Student(studentName, studentId);
48+
model.addStudentToGroup(student, predicate);
49+
if (model.getFilteredGroupList().isEmpty()) {
50+
//no such group exists
51+
return new CommandResult(MESSAGE_GROUP_EMPTY);
52+
} else {
53+
return new CommandResult(
54+
String.format(MESSAGE_SUCCESS, studentName, studentId, model.getFilteredGroupList().get(0).getName()));
55+
}
56+
}
57+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package seedu.address.logic.commands;
2+
3+
import static seedu.address.commons.core.Messages.MESSAGE_GROUP_EMPTY;
4+
import static seedu.address.commons.core.Messages.MESSAGE_STUDENT_EMPTY;
5+
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
6+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
7+
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
8+
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;
9+
10+
import javafx.collections.ObservableList;
11+
import seedu.address.logic.commands.exceptions.CommandException;
12+
import seedu.address.model.Model;
13+
import seedu.address.model.group.Group;
14+
import seedu.address.model.group.GrpContainsKeywordPredicate;
15+
import seedu.address.model.group.Student;
16+
17+
public class DeleteStudentCommand extends Command {
18+
19+
public static final String COMMAND_WORD = "delstudent";
20+
public static final String MESSAGE_USAGE = COMMAND_WORD
21+
+ ": Removes a new Student from a specified tutorial group. \n"
22+
+ "Parameters: "
23+
+ PREFIX_GRP + "GRP "
24+
+ PREFIX_STUDENT + "NAME "
25+
+ PREFIX_ID + "Student ID \n"
26+
+ "Example: " + COMMAND_WORD + " " + PREFIX_GRP + "G04" + " " + PREFIX_STUDENT + "Ryan" + " " + PREFIX_ID
27+
+ "e1234567";
28+
29+
public static final String MESSAGE_SUCCESS = "You removed %s (%s) from tutorial group %s";
30+
31+
private final String studentName;
32+
private final String studentId;
33+
private final GrpContainsKeywordPredicate predicate;
34+
35+
/**
36+
* Creates a DeleteStudentCommand to remove the specified {@code Student}
37+
* @param studentName Name of Student
38+
* @param studentId Id of Student
39+
* @param predicate Group predicate
40+
*/
41+
public DeleteStudentCommand(String studentName, String studentId, GrpContainsKeywordPredicate predicate) {
42+
requireAllNonNull(studentName, studentId, predicate);
43+
this.studentName = studentName;
44+
this.studentId = studentId;
45+
this.predicate = predicate;
46+
}
47+
48+
49+
@Override
50+
public CommandResult execute(Model model) throws CommandException {
51+
Student student = new Student(studentName, studentId);
52+
model.updateFilteredGroupList(predicate);
53+
ObservableList<Group> groups = model.getFilteredGroupList();
54+
if (groups.isEmpty()) {
55+
//no such group
56+
return new CommandResult(MESSAGE_GROUP_EMPTY);
57+
}
58+
if (!groups.get(0).getStudents().contains(student)) {
59+
//student does not exist
60+
return new CommandResult(MESSAGE_STUDENT_EMPTY);
61+
} else {
62+
model.removeStudentFromGroup(student, predicate);
63+
model.updateFilteredGroupList(predicate);
64+
return new CommandResult(
65+
String.format(MESSAGE_SUCCESS, studentName, studentId, model.getFilteredGroupList().get(0).getName()));
66+
}
67+
}
68+
69+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package seedu.address.logic.parser;
2+
3+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
6+
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;
7+
8+
import java.util.stream.Stream;
9+
10+
import seedu.address.logic.commands.AddStudentCommand;
11+
import seedu.address.logic.parser.exceptions.ParseException;
12+
import seedu.address.model.group.GrpContainsKeywordPredicate;
13+
14+
public class AddStudentCommandParser implements Parser<AddStudentCommand> {
15+
16+
private final ParseException addStudentCommandParserException = new ParseException(
17+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddStudentCommand.MESSAGE_USAGE));
18+
19+
@Override
20+
public AddStudentCommand parse(String args) throws ParseException {
21+
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GRP, PREFIX_STUDENT, PREFIX_ID);
22+
23+
if (!arePrefixesPresent(argMultimap, PREFIX_GRP, PREFIX_STUDENT, PREFIX_ID) || !argMultimap.getPreamble()
24+
.isEmpty()) {
25+
throw addStudentCommandParserException;
26+
}
27+
28+
String[] grpKeywordArray = argMultimap.getValue(PREFIX_GRP).get().split("\\s+");
29+
String[] studentNameArray = argMultimap.getValue(PREFIX_STUDENT).get().split("\\s+");
30+
String[] studentIdArray = argMultimap.getValue(PREFIX_ID).get().split("\\s+");
31+
32+
//if id or group keyword is more than 1, or if student name has more than 10 letters, throw exception
33+
boolean matchesGrp = grpKeywordArray.length == 1;
34+
boolean matchesId = studentIdArray.length == 1 && studentIdArray[0].length() == 8;
35+
boolean matchesStudentName = studentNameArray.length <= 10;
36+
if (!matchesGrp || !matchesId || !matchesStudentName) {
37+
throw addStudentCommandParserException;
38+
}
39+
40+
String studentName = String.join(" ", studentNameArray);
41+
String studentId = studentIdArray[0];
42+
String grpName = grpKeywordArray[0];
43+
44+
return new AddStudentCommand(studentName, studentId, new GrpContainsKeywordPredicate(grpName));
45+
}
46+
47+
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
48+
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package seedu.address.logic.parser;
2+
3+
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
6+
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;
7+
8+
import java.util.stream.Stream;
9+
10+
import seedu.address.logic.commands.DeleteStudentCommand;
11+
import seedu.address.logic.parser.exceptions.ParseException;
12+
import seedu.address.model.group.GrpContainsKeywordPredicate;
13+
14+
public class DeleteStudentCommandParser implements Parser<DeleteStudentCommand> {
15+
16+
private final ParseException deleteStudentCommandParserException = new ParseException(
17+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteStudentCommand.MESSAGE_USAGE));
18+
19+
@Override
20+
public DeleteStudentCommand parse(String args) throws ParseException {
21+
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_GRP, PREFIX_STUDENT, PREFIX_ID);
22+
23+
if (!arePrefixesPresent(argMultimap, PREFIX_GRP, PREFIX_STUDENT, PREFIX_ID) || !argMultimap.getPreamble()
24+
.isEmpty()) {
25+
throw deleteStudentCommandParserException;
26+
}
27+
28+
String[] grpKeywordArray = argMultimap.getValue(PREFIX_GRP).get().split("\\s+");
29+
String[] studentNameArray = argMultimap.getValue(PREFIX_STUDENT).get().split("\\s+");
30+
String[] studentIdArray = argMultimap.getValue(PREFIX_ID).get().split("\\s+");
31+
32+
//if id or group keyword is more than 1, or if student name has more than 10 letters, throw exception
33+
boolean matchesGrp = grpKeywordArray.length == 1;
34+
boolean matchesId = studentIdArray.length == 1 && studentIdArray[0].length() == 8;
35+
boolean matchesStudentName = studentNameArray.length <= 10;
36+
if (!matchesGrp || !matchesId || !matchesStudentName) {
37+
throw deleteStudentCommandParserException;
38+
}
39+
40+
String studentName = String.join(" ", studentNameArray);
41+
String studentId = studentIdArray[0];
42+
String grpName = grpKeywordArray[0];
43+
44+
return new DeleteStudentCommand(studentName, studentId, new GrpContainsKeywordPredicate(grpName));
45+
}
46+
47+
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
48+
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
49+
}
50+
}

src/main/java/seedu/address/logic/parser/SerenityParser.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
import seedu.address.logic.commands.AddLsnCommand;
1212
import seedu.address.logic.commands.AddQnCommand;
1313
import seedu.address.logic.commands.AddScoreCommand;
14+
import seedu.address.logic.commands.AddStudentCommand;
1415
import seedu.address.logic.commands.ClearCommand;
1516
import seedu.address.logic.commands.Command;
1617
import seedu.address.logic.commands.DelGrpCommand;
1718
import seedu.address.logic.commands.DelQnCommand;
1819
import seedu.address.logic.commands.DeleteCommand;
1920
import seedu.address.logic.commands.DeleteLsnCommand;
21+
import seedu.address.logic.commands.DeleteStudentCommand;
2022
import seedu.address.logic.commands.EditCommand;
2123
import seedu.address.logic.commands.ExitCommand;
2224
import seedu.address.logic.commands.FindCommand;
@@ -61,6 +63,12 @@ public Command parseCommand(String userInput) throws ParseException {
6163
case AddGrpCommand.COMMAND_WORD:
6264
return new AddGrpCommandParser().parse(arguments);
6365

66+
case AddStudentCommand.COMMAND_WORD:
67+
return new AddStudentCommandParser().parse(arguments);
68+
69+
case DeleteStudentCommand.COMMAND_WORD:
70+
return new DeleteStudentCommandParser().parse(arguments);
71+
6472
case AddLsnCommand.COMMAND_WORD:
6573
return new AddLsnCommandParser().parse(arguments);
6674

src/main/java/seedu/address/model/Model.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public interface Model {
2121
* {@code Predicate} that always evaluate to true
2222
*/
2323
Predicate<Person> PREDICATE_SHOW_ALL_PERSONS = unused -> true;
24-
Predicate<Student> PREDICATE_SHOW_ALL_STUDENTS = unused -> true;
2524

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

136+
/**
137+
* Adds a Student to a Group
138+
*/
139+
void addStudentToGroup(Student student, Predicate<Group> predicate);
140+
141+
/**
142+
* Removes a Student from a Group.
143+
*/
144+
void removeStudentFromGroup(Student student, Predicate<Group> predicate);
145+
137146
/**
138147
* Updates the filter of the filtered group list to filter by the given {@code predicate}.
139148
*
@@ -192,10 +201,8 @@ public interface Model {
192201
* Returns an unmodifiable view of the student info list
193202
*/
194203
ObservableList<StudentInfo> getStudentInfoList();
195-
196204
/**
197205
* Returns an unmodifiable view of the question list.
198206
*/
199207
ObservableList<Question> getQuestionList();
200-
201208
}

src/main/java/seedu/address/model/ModelManager.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public class ModelManager implements Model {
4545
* Initializes a ModelManager with the given addressBook, userPrefs and serenity.
4646
*/
4747
public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs,
48-
ReadOnlySerenity serenity) {
48+
ReadOnlySerenity serenity) {
4949
super();
5050
requireAllNonNull(addressBook, userPrefs, serenity);
5151

5252
logger.fine("Initializing with address book: " + addressBook
53-
+ " and user prefs " + userPrefs
54-
+ " and serenity " + serenity);
53+
+ " and user prefs " + userPrefs
54+
+ " and serenity " + serenity);
5555

5656
this.addressBook = new AddressBook(addressBook);
5757
this.userPrefs = new UserPrefs(userPrefs);
@@ -207,6 +207,29 @@ public void addGroup(Group group) {
207207
serenity.addGroup(group);
208208
}
209209

210+
@Override
211+
public void addStudentToGroup(Student student, Predicate<Group> predicate) {
212+
requireAllNonNull(student, predicate);
213+
updateFilteredGroupList(predicate);
214+
if (!filteredGroups.isEmpty()) {
215+
students.add(student);
216+
Group currentGroup = filteredGroups.get(0);
217+
currentGroup.addStudentToGroup(student);
218+
}
219+
}
220+
221+
@Override
222+
public void removeStudentFromGroup(Student student, Predicate<Group> predicate) {
223+
requireAllNonNull(student, predicate);
224+
updateFilteredGroupList(predicate);
225+
UniqueStudentList students = filteredGroups.get(0).getStudents();
226+
if (!filteredGroups.isEmpty() && students.contains(student)) {
227+
students.remove(student);
228+
Group currentGroup = filteredGroups.get(0);
229+
currentGroup.removeStudentFromGroup(student);
230+
}
231+
}
232+
210233
@Override
211234
public void updateFilteredGroupList(Predicate<Group> predicate) {
212235
requireAllNonNull(predicate);

0 commit comments

Comments
 (0)