Skip to content

Commit 7e84230

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#90 from chunyongg/better-testcases
Better testcases
2 parents 6a7bc8a + 385cb12 commit 7e84230

13 files changed

Lines changed: 705 additions & 244 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Messages {
1515
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.";
1717
public static final String MESSAGE_STUDENT_EMPTY = "No such student!";
18+
public static final String MESSAGE_DUPLICATE_STUDENT = "Student already exists!";
1819
public static final String MESSAGE_LESSON_EMPTY = "no such lesson!";
1920
public static final String MESSAGE_NOT_VIEWING_A_GROUP = "Group not specified.";
2021
public static final String MESSAGE_NOT_VIEWING_A_LESSON = "Lesson not specified.";

src/main/java/seedu/address/logic/commands/AddStudentCommand.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package seedu.address.logic.commands;
22

3+
import static seedu.address.commons.core.Messages.MESSAGE_DUPLICATE_STUDENT;
34
import static seedu.address.commons.core.Messages.MESSAGE_GROUP_EMPTY;
45
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
56
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP;
67
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
78
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;
89

10+
import java.util.function.Predicate;
11+
912
import seedu.address.logic.commands.exceptions.CommandException;
1013
import seedu.address.model.Model;
11-
import seedu.address.model.group.GrpContainsKeywordPredicate;
14+
import seedu.address.model.group.Group;
1215
import seedu.address.model.group.Student;
1316

1417
public class AddStudentCommand extends Command {
@@ -27,15 +30,16 @@ public class AddStudentCommand extends Command {
2730

2831
private final String studentName;
2932
private final String studentId;
30-
private final GrpContainsKeywordPredicate predicate;
33+
private final Predicate<Group> predicate;
3134

3235
/**
3336
* Creates an AddStudentCommand to add the specified {@code Student}
37+
*
3438
* @param studentName
3539
* @param studentId
3640
* @param predicate
3741
*/
38-
public AddStudentCommand(String studentName, String studentId, GrpContainsKeywordPredicate predicate) {
42+
public AddStudentCommand(String studentName, String studentId, Predicate<Group> predicate) {
3943
requireAllNonNull(studentName, studentId, predicate);
4044
this.studentName = studentName;
4145
this.studentId = studentId;
@@ -45,13 +49,30 @@ public AddStudentCommand(String studentName, String studentId, GrpContainsKeywor
4549
@Override
4650
public CommandResult execute(Model model) throws CommandException {
4751
Student student = new Student(studentName, studentId);
48-
model.addStudentToGroup(student, predicate);
52+
model.updateFilteredGroupList(predicate);
4953
if (model.getFilteredGroupList().isEmpty()) {
5054
//no such group exists
51-
return new CommandResult(MESSAGE_GROUP_EMPTY);
55+
throw new CommandException(MESSAGE_GROUP_EMPTY);
56+
}
57+
Group targetGroup = model.getFilteredGroupList().get(0);
58+
if (targetGroup.getStudents().contains(student)) {
59+
throw new CommandException(MESSAGE_DUPLICATE_STUDENT);
60+
}
61+
model.addStudentToGroup(student, predicate);
62+
return new CommandResult(
63+
String.format(MESSAGE_SUCCESS, studentName, studentId, model.getFilteredGroupList().get(0).getName()));
64+
}
65+
66+
@Override
67+
public boolean equals(Object obj) {
68+
if (obj == this) {
69+
return true;
70+
} else if (obj instanceof AddStudentCommand) {
71+
AddStudentCommand other = (AddStudentCommand) obj;
72+
return studentName.equals(other.studentName) && studentId.equals(other.studentId)
73+
&& predicate.equals(other.predicate);
5274
} else {
53-
return new CommandResult(
54-
String.format(MESSAGE_SUCCESS, studentName, studentId, model.getFilteredGroupList().get(0).getName()));
75+
return false;
5576
}
5677
}
5778
}

src/main/java/seedu/address/logic/commands/DelStudentCommand.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
88
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;
99

10+
import java.util.function.Predicate;
11+
1012
import javafx.collections.ObservableList;
1113
import seedu.address.logic.commands.exceptions.CommandException;
1214
import seedu.address.model.Model;
1315
import seedu.address.model.group.Group;
14-
import seedu.address.model.group.GrpContainsKeywordPredicate;
1516
import seedu.address.model.group.Student;
1617

1718
public class DelStudentCommand extends Command {
@@ -30,15 +31,15 @@ public class DelStudentCommand extends Command {
3031

3132
private final String studentName;
3233
private final String studentId;
33-
private final GrpContainsKeywordPredicate predicate;
34+
private final Predicate<Group> predicate;
3435

3536
/**
3637
* Creates a DelStudentCommand to remove the specified {@code Student}
3738
* @param studentName Name of Student
3839
* @param studentId Id of Student
3940
* @param predicate Group predicate
4041
*/
41-
public DelStudentCommand(String studentName, String studentId, GrpContainsKeywordPredicate predicate) {
42+
public DelStudentCommand(String studentName, String studentId, Predicate<Group> predicate) {
4243
requireAllNonNull(studentName, studentId, predicate);
4344
this.studentName = studentName;
4445
this.studentId = studentId;
@@ -53,11 +54,11 @@ public CommandResult execute(Model model) throws CommandException {
5354
ObservableList<Group> groups = model.getFilteredGroupList();
5455
if (groups.isEmpty()) {
5556
//no such group
56-
return new CommandResult(MESSAGE_GROUP_EMPTY);
57+
throw new CommandException(MESSAGE_GROUP_EMPTY);
5758
}
5859
if (!groups.get(0).getStudents().contains(student)) {
5960
//student does not exist
60-
return new CommandResult(MESSAGE_STUDENT_EMPTY);
61+
throw new CommandException(MESSAGE_STUDENT_EMPTY);
6162
} else {
6263
model.removeStudentFromGroup(student, predicate);
6364
model.updateFilteredGroupList(predicate);
@@ -66,4 +67,16 @@ public CommandResult execute(Model model) throws CommandException {
6667
}
6768
}
6869

70+
@Override
71+
public boolean equals(Object obj) {
72+
if (obj == this) {
73+
return true;
74+
} else if (obj instanceof DelStudentCommand) {
75+
DelStudentCommand other = (DelStudentCommand) obj;
76+
return studentName.equals(other.studentName) && studentId.equals(other.studentId)
77+
&& predicate.equals(other.predicate);
78+
} else {
79+
return false;
80+
}
81+
}
6982
}

src/main/java/seedu/address/model/group/Student.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
*/
1010
public class Student {
1111
public static final String STUDENT_NAME_ERROR = "Name cannot be empty";
12-
public static final String STUDENT_NUMBER_ERROR = "Student number cannot be empty";
12+
public static final String STUDENT_NUMBER_ERROR = "Student number cannot be empty "
13+
+ "and must follow the format 'eXXXXXXX' "
14+
+ "where X is a digit from 0 to 9";
1315
private String name;
1416
private String studentNumber;
1517

@@ -40,6 +42,11 @@ public static boolean isValidString(String s) {
4042
public static boolean isValidStudentNumber(String s) {
4143
//8 digits long
4244
s = s.toLowerCase();
45+
boolean matchesLength = s.length() == 8;
46+
boolean matchesChar = s.charAt(0) == 'e';
47+
if (!matchesChar || !matchesLength) {
48+
System.out.println(s);
49+
}
4350
return s.length() == 8 && s.charAt(0) == 'e';
4451
}
4552

@@ -58,7 +65,13 @@ public String toString() {
5865

5966
@Override
6067
public boolean equals(Object obj) {
61-
Student other = (Student) obj;
62-
return other.getName().equals(getName()) && other.getStudentNumber().equals(getStudentNumber());
68+
if (obj == this) {
69+
return true;
70+
} else if (obj instanceof Student) {
71+
Student other = (Student) obj;
72+
return other.getName().equals(getName()) && other.getStudentNumber().equals(getStudentNumber());
73+
} else {
74+
return false;
75+
}
6376
}
6477
}

0 commit comments

Comments
 (0)