Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finished logic for getMyTeam #18

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
68 changes: 34 additions & 34 deletions src/main/java/api/MongoGradeDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ public Grade getGrade(String username, String course) {
.course(grade.getString("course"))
.grade(grade.getInt(GRADE))
.build();
}
else {
} else {
throw new RuntimeException(responseBody.getString(MESSAGE));
}
}
catch (IOException | JSONException event) {
} catch (IOException | JSONException event) {
throw new RuntimeException(event);
}
}
Expand Down Expand Up @@ -100,12 +98,10 @@ public Grade[] getGrades(String username) {
.build();
}
return result;
}
else {
} else {
throw new RuntimeException(responseBody.getString(MESSAGE));
}
}
catch (IOException | JSONException event) {
} catch (IOException | JSONException event) {
throw new RuntimeException(event);
}
}
Expand All @@ -132,12 +128,10 @@ public Grade logGrade(String course, int grade) throws JSONException {

if (responseBody.getInt(STATUS_CODE) == SUCCESS_CODE) {
return null;
}
else {
} else {
throw new RuntimeException(responseBody.getString(MESSAGE));
}
}
catch (IOException | JSONException event) {
} catch (IOException | JSONException event) {
throw new RuntimeException(event);
}
}
Expand Down Expand Up @@ -173,12 +167,10 @@ public Team formTeam(String name) throws JSONException {
.name(team.getString(NAME))
.members(members)
.build();
}
else {
} else {
throw new RuntimeException(responseBody.getString(MESSAGE));
}
}
catch (IOException | JSONException event) {
} catch (IOException | JSONException event) {
throw new RuntimeException(event);
}
}
Expand All @@ -204,12 +196,10 @@ public Team joinTeam(String name) throws JSONException {

if (responseBody.getInt(STATUS_CODE) == SUCCESS_CODE) {
return null;
}
else {
} else {
throw new RuntimeException(responseBody.getString(MESSAGE));
}
}
catch (IOException | JSONException event) {
} catch (IOException | JSONException event) {
throw new RuntimeException(event);
}
}
Expand All @@ -235,33 +225,43 @@ public void leaveTeam() throws JSONException {
if (responseBody.getInt(STATUS_CODE) != SUCCESS_CODE) {
throw new RuntimeException(responseBody.getString(MESSAGE));
}
}
catch (IOException | JSONException event) {
} catch (IOException | JSONException event) {
throw new RuntimeException(event);
}
}

@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();
final OkHttpClient client = new OkHttpClient().newBuilder().build();
final Request request = new Request.Builder()
.url(String.format("%s/team", API_URL))
.method("GET", null)
.addHeader(TOKEN, getAPIToken())
.addHeader(CONTENT_TYPE, APPLICATION_JSON)
.build();

final Response response;
final JSONObject responseBody;

// TODO Task 3b: Implement the logic to get the team information
// HINT: Look at the formTeam method to get an idea on how to parse the response
try {
final Response response = client.newCall(request).execute();
final JSONObject responseBody = new JSONObject(response.body().string());

return null;
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);
}
return Team.builder()
.name(team.getString(NAME))
.members(members)
.build();
}
else {
throw new RuntimeException(responseBody.getString(MESSAGE));
}
}
catch (IOException | JSONException event) {
throw new RuntimeException(event);
}
}
}
7 changes: 4 additions & 3 deletions src/main/java/usecase/GetAverageGradeUseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import entity.Grade;
import entity.Team;

import java.lang.reflect.Array;

/**
* GetAverageGradeUseCase class.
*/
Expand All @@ -25,9 +27,8 @@ public float getAverageGrade(String course) {
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.
// Call the API to get all the grades for the course for all your team member
lst = team.getMembers();

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