Skip to content

Commit d2989a5

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#76 from xinyee20/Participation_XY
v1.2b Add Participation Score
2 parents d029de4 + 3e3781c commit d2989a5

File tree

8 files changed

+180
-1
lines changed

8 files changed

+180
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package seedu.address.logic.commands;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_ID;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;
6+
7+
import javafx.collections.ObservableList;
8+
import seedu.address.logic.commands.exceptions.CommandException;
9+
import seedu.address.model.Model;
10+
import seedu.address.model.group.Lesson;
11+
import seedu.address.model.group.Participation;
12+
import seedu.address.model.group.Student;
13+
import seedu.address.model.group.StudentInfo;
14+
import seedu.address.model.group.UniqueStudentInfoList;
15+
16+
public class AddScoreCommand extends Command {
17+
18+
public static final String COMMAND_WORD = "addscore";
19+
public static final String MESSAGE_SUCCESS = "%s: \nParticipation Score - %d";
20+
21+
public static final String MESSAGE_USAGE = COMMAND_WORD
22+
+ ": Gives a student in the class a participation score. \n"
23+
+ "Parameters: " + " " + "SCORE "
24+
+ PREFIX_STUDENT + " NAME" + " " + PREFIX_ID + " STUDENT_NUMBER\n"
25+
+ "Example: " + COMMAND_WORD + " " + "2" + " "
26+
+ PREFIX_STUDENT + " Aaron Tan" + " " + PREFIX_ID + " e0123456";
27+
28+
private Student toAddScore;
29+
private int score;
30+
31+
/**
32+
* Creates an AddScoreCommand to award the specified {@code Student} a participation score
33+
*/
34+
public AddScoreCommand(Student student, int score) {
35+
requireNonNull(student);
36+
// Specified student to add participation score
37+
toAddScore = student;
38+
this.score = score;
39+
}
40+
41+
@Override
42+
public CommandResult execute(Model model) throws CommandException {
43+
requireNonNull(model);
44+
45+
Lesson uniqueLesson = model.getFilteredLessonList().get(0);
46+
UniqueStudentInfoList uniqueStudentInfoList = uniqueLesson.getStudentsInfo();
47+
ObservableList<StudentInfo> studentsInfo = uniqueStudentInfoList.asUnmodifiableObservableList();
48+
49+
// Update single student participation score
50+
for (int i = 0; i < studentsInfo.size(); i++) {
51+
StudentInfo studentInfo = studentsInfo.get(i);
52+
boolean isCorrectStudent = studentInfo.containsStudent(toAddScore);
53+
if (isCorrectStudent) {
54+
Participation update = studentInfo.getParticipation().setScore(score);
55+
StudentInfo updatedStudentInfo = studentInfo.updateParticipation(update);
56+
uniqueStudentInfoList.setStudentInfo(studentInfo, updatedStudentInfo);
57+
}
58+
}
59+
return new CommandResult(String.format(MESSAGE_SUCCESS, toAddScore, score));
60+
61+
}
62+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_ID;
5+
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENT;
6+
7+
import java.util.Optional;
8+
9+
import seedu.address.logic.commands.AddScoreCommand;
10+
import seedu.address.logic.parser.exceptions.ParseException;
11+
import seedu.address.model.group.Student;
12+
13+
public class AddScoreCommandParser implements Parser<AddScoreCommand> {
14+
15+
/**
16+
* Parses the given {@code String} of arguments in the context of the AddScoreCommand and
17+
* returns a AddScoreCommand object for execution.
18+
*
19+
* @throws ParseException if the user input does not conform the expected format
20+
*/
21+
@Override
22+
public AddScoreCommand parse(String userInput) throws ParseException {
23+
ArgumentMultimap argMultimap =
24+
ArgumentTokenizer
25+
.tokenize(userInput, PREFIX_STUDENT, PREFIX_ID);
26+
27+
String studentName;
28+
String studentNumber;
29+
Optional<Student> student;
30+
int score;
31+
32+
if (argMultimap.getValue(PREFIX_STUDENT).isPresent() && argMultimap.getValue(PREFIX_ID).isPresent()) {
33+
34+
score = SerenityParserUtil.parseScore(argMultimap.getPreamble());
35+
if (score < 0 || score > 5) {
36+
throw new ParseException("Score should be between 0 to 5");
37+
}
38+
studentName = SerenityParserUtil.parseStudent(argMultimap.getValue(PREFIX_STUDENT).get());
39+
studentNumber = SerenityParserUtil.parseStudentID(argMultimap.getValue(PREFIX_ID).get());
40+
student = Optional.ofNullable(new Student(studentName, studentNumber));
41+
42+
return new AddScoreCommand(student.get(), score);
43+
} else {
44+
throw new ParseException(
45+
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddScoreCommand.MESSAGE_USAGE));
46+
}
47+
}
48+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import seedu.address.logic.commands.AddCommand;
1010
import seedu.address.logic.commands.AddGrpCommand;
1111
import seedu.address.logic.commands.AddQnCommand;
12+
import seedu.address.logic.commands.AddScoreCommand;
1213
import seedu.address.logic.commands.ClearCommand;
1314
import seedu.address.logic.commands.Command;
1415
import seedu.address.logic.commands.DelGrpCommand;
@@ -70,6 +71,9 @@ public Command parseCommand(String userInput) throws ParseException {
7071
case MarkAttCommand.COMMAND_WORD:
7172
return new MarkAttCommandParser().parse(arguments);
7273

74+
case AddScoreCommand.COMMAND_WORD:
75+
return new AddScoreCommandParser().parse(arguments);
76+
7377
case UnmarkAttCommand.COMMAND_WORD:
7478
return new UnmarkAttCommandParser().parse(arguments);
7579

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static java.util.Objects.requireNonNull;
44

55
import seedu.address.logic.parser.exceptions.ParseException;
6+
import seedu.address.model.group.Participation;
67
import seedu.address.model.group.Question;
78
import seedu.address.model.group.Student;
89

@@ -36,6 +37,23 @@ public static String parseStudentID(String id) throws ParseException {
3637
return trimmedId;
3738
}
3839

40+
/**
41+
* Parses {@code String inputScore} into an {@code int} and returns it. Leading and trailing whitespaces will be
42+
* trimmed.
43+
*
44+
* @throws ParseException if the specified score is invalid.
45+
*/
46+
public static int parseScore(String inputScore) throws ParseException {
47+
String trimmedScore = inputScore.trim();
48+
int score;
49+
try {
50+
score = Integer.parseInt(trimmedScore);
51+
return score;
52+
} catch (Exception e) {
53+
throw new ParseException(Participation.SCORE_ERROR);
54+
}
55+
}
56+
3957
/**
4058
* Parses a {@code String question} into a {@code String}. Leading and trailing whitespaces will be trimmed.
4159
*

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public Group(String name, UniqueStudentList students) {
6363
* @param students A list of students.
6464
* @param lessons A list of tutorial lessons.
6565
*/
66-
6766
public Group(String name, UniqueStudentList students, UniqueLessonList lessons) {
6867
requireAllNonNull(name, students, lessons);
6968
this.name = name;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22

33
public class Participation {
44

5+
public static final String SCORE_ERROR = "Score must be a number";
56
private final int score;
67

78
public Participation() {
89
this.score = 0;
910
}
1011

12+
/**
13+
* Creates a Participation object containing the score of a student
14+
* @param score Score of Student
15+
*/
16+
public Participation(int score) {
17+
this.score = score;
18+
}
19+
1120
@Override
1221
public String toString() {
1322
return Integer.toString(score);
@@ -17,6 +26,11 @@ public int getScore() {
1726
return score;
1827
}
1928

29+
public Participation setScore(int score) {
30+
Participation updatedScore = new Participation(score);
31+
return updatedScore;
32+
}
33+
2034
@Override
2135
public boolean equals(Object obj) {
2236
Participation other = (Participation) obj;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public StudentInfo updateAttendance(Attendance updatedAttendance) {
6868
return updatedStudentInfo;
6969
}
7070

71+
/**
72+
* Updates the student participation score for the class
73+
* @param updatedScore The participation score of the student for the lesson
74+
* @return The updated Participation object
75+
*/
76+
public StudentInfo updateParticipation(Participation updatedScore) {
77+
StudentInfo updatedStudentInfo = new StudentInfo(this.student, updatedScore, this.attendance);
78+
return updatedStudentInfo;
79+
}
80+
7181
@Override
7282
public boolean equals(Object obj) {
7383
StudentInfo other = (StudentInfo) obj;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package seedu.address.logic.commands;
2+
3+
import static seedu.address.testutil.Assert.assertThrows;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class AddScoreCommandTest {
8+
9+
@Test
10+
public void constructor_nullGroup_throwsNullPointerException() {
11+
assertThrows(NullPointerException.class, () -> new AddScoreCommand(null, 2));
12+
}
13+
14+
@Test
15+
public void execute_addScoreOutOfRange_failure() {
16+
17+
}
18+
19+
@Test
20+
public void execute_addScore_success() {
21+
22+
}
23+
24+
}

0 commit comments

Comments
 (0)