Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/main/java/api/MongoGradeDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public Grade getGrade(String username, String course) {
.build();
}
else {
throw new RuntimeException("Grade could not be found for course: " + course
+ " and username: " + username);
return null;
// throw new RuntimeException("Grade could not be found for course: " + course
// + " and username: " + username);
}
}
catch (IOException | JSONException event) {
Expand Down Expand Up @@ -247,10 +248,6 @@ public void leaveTeam() throws JSONException {
}

@Override
// TODO Task 3b: Implement this method
// Hint: Read the Grade API documentation for getMyTeam (link below) and refer to the above similar
// methods to help you write this code (copy-and-paste + edit as needed).
// https://www.postman.com/cloudy-astronaut-813156/csc207-grade-apis-demo/folder/isr2ymn/get-my-team
public Team getMyTeam() {
final OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Expand All @@ -263,11 +260,27 @@ public Team getMyTeam() {

final Response response;
final JSONObject responseBody;
try {
response = client.newCall(request).execute();
responseBody = new JSONObject(response.body().string());
if (responseBody.getInt(STATUS_CODE) == SUCCESS_CODE) {
final JSONObject team = responseBody.getJSONObject("team");
final JSONArray membersArray = team.getJSONArray("members");
final String[] members = new String[membersArray.length()];
for (int i = 0; i < membersArray.length(); i++) {
members[i] = membersArray.getString(i);
}

// TODO Task 3b: Implement the logic to get the team information
// HINT 1: Look at the formTeam method to get an idea on how to parse the response
// HINT 2: You may find it useful to just initially print the contents of the JSON
// then work on the details of how to parse it.
return Team.builder()
.name(team.getString(NAME))
.members(members)
.build();
}


} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
}
14 changes: 10 additions & 4 deletions src/main/java/usecase/GetAverageGradeUseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ public float getAverageGrade(String course) {
// Call the API to get usernames of all your team members
float sum = 0;
int count = 0;
// TODO Task 3b: Go to the MongoGradeDataBase class and implement getMyTeam.

final Team team = gradeDataBase.getMyTeam();
// Call the API to get all the grades for the course for all your team members
// TODO Task 3a: Complete the logic of calculating the average course grade for
// your team members. Hint: the getGrades method might be useful.
String[] members = team.getMembers();
GetGradeUseCase getGrade = new GetGradeUseCase(gradeDataBase);
for (String member : members) {
Grade grade = gradeDataBase.getGrade(member, course);
if (grade != null) {
sum += grade.getGrade();
count++;
}
}

if (count == 0) {
return 0;
Expand Down