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
99 changes: 40 additions & 59 deletions src/main/java/api/MongoGradeDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,13 @@ public static String getAPIToken() {

@Override
public Grade getGrade(String username, String course) {

// Build the request to get the grade.
// Note: The API requires the token to be passed as a header.
// Note: The API requires the course and username to be passed as query parameters.
final OkHttpClient client = new OkHttpClient().newBuilder()
.build();
final OkHttpClient client = new OkHttpClient().newBuilder().build();
final Request request = new Request.Builder()
.url(String.format("%s/grade?course=%s&username=%s", API_URL, course, username))
.addHeader(TOKEN, getAPIToken())
.addHeader(CONTENT_TYPE, APPLICATION_JSON)
.build();

// Hint: look at the API documentation to understand what the response looks like.
try {
final Response response = client.newCall(request).execute();
final JSONObject responseBody = new JSONObject(response.body().string());
Expand All @@ -62,33 +56,24 @@ public Grade getGrade(String username, String course) {
.course(grade.getString(COURSE))
.grade(grade.getInt(GRADE))
.build();
}
else {
} else {
throw new RuntimeException("Grade could not be found for course: " + course
+ " and username: " + username);
+ " and username: " + username);
}
}
catch (IOException | JSONException event) {
} catch (IOException | JSONException event) {
throw new RuntimeException(event);
}
}

@Override
public Grade[] getGrades(String username) {

// Build the request to get all grades for a user.
// Note: The API requires the token to be passed as a header.
// Note: The API requires the username to be passed as a query parameter.
final OkHttpClient client = new OkHttpClient().newBuilder()
.build();
final OkHttpClient client = new OkHttpClient().newBuilder().build();
final Request request = new Request.Builder()
.url(String.format("%s/grade?username=%s", API_URL, username))
.addHeader(TOKEN, getAPIToken())
.addHeader(CONTENT_TYPE, APPLICATION_JSON)
.build();

// Note: you can look at the API documentation to understand what the response looks like
// to better understand how this parses the response.
try {
final Response response = client.newCall(request).execute();
final JSONObject responseBody = new JSONObject(response.body().string());
Expand All @@ -105,20 +90,17 @@ 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);
}
}

@Override
public Grade logGrade(String course, int grade) throws JSONException {
final OkHttpClient client = new OkHttpClient().newBuilder()
.build();
final OkHttpClient client = new OkHttpClient().newBuilder().build();
final MediaType mediaType = MediaType.parse(APPLICATION_JSON);
final JSONObject requestBody = new JSONObject();
requestBody.put(COURSE, course);
Expand All @@ -137,20 +119,17 @@ 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);
}
}

@Override
public Team formTeam(String name) throws JSONException {
final OkHttpClient client = new OkHttpClient().newBuilder()
.build();
final OkHttpClient client = new OkHttpClient().newBuilder().build();
final MediaType mediaType = MediaType.parse(APPLICATION_JSON);
final JSONObject requestBody = new JSONObject();
requestBody.put(NAME, name);
Expand Down Expand Up @@ -178,20 +157,17 @@ 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);
}
}

@Override
public Team joinTeam(String name) throws JSONException {
final OkHttpClient client = new OkHttpClient().newBuilder()
.build();
final OkHttpClient client = new OkHttpClient().newBuilder().build();
final MediaType mediaType = MediaType.parse(APPLICATION_JSON);
final JSONObject requestBody = new JSONObject();
requestBody.put(NAME, name);
Expand All @@ -209,20 +185,17 @@ 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);
}
}

@Override
public void leaveTeam() throws JSONException {
final OkHttpClient client = new OkHttpClient().newBuilder()
.build();
final OkHttpClient client = new OkHttpClient().newBuilder().build();
final MediaType mediaType = MediaType.parse(APPLICATION_JSON);
final JSONObject requestBody = new JSONObject();
final RequestBody body = RequestBody.create(mediaType, requestBody.toString());
Expand All @@ -240,34 +213,42 @@ 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;
try {
final Response response = client.newCall(request).execute();
final JSONObject responseBody = new JSONObject(response.body().string());

// 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 null;
if (responseBody.getInt(STATUS_CODE) == SUCCESS_CODE) {
final JSONObject teamJson = responseBody.getJSONObject("team");
final JSONArray membersArray = teamJson.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(teamJson.getString(NAME))
.members(members)
.build();
} else {
throw new RuntimeException(responseBody.getString(MESSAGE));
}
} catch (IOException | JSONException e) {
throw new RuntimeException(e);
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/app/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@ public GetAverageGradeUseCase getAverageGradeUseCase() {
return new GetAverageGradeUseCase(gradeDataBase);
}


public GetTopGradeUseCase getTopGradeUseCase() {return new GetTopGradeUseCase(gradeDataBase);}

// TODO Task 4: add code for the new GetTopGradeUseCase following the same pattern as the other use cases above.
}
31 changes: 28 additions & 3 deletions src/main/java/app/gui/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import entity.Grade;
import usecase.FormTeamUseCase;
import usecase.GetAverageGradeUseCase;
import usecase.GetTopGradeUseCase;
import usecase.GetGradeUseCase;
import usecase.JoinTeamUseCase;
import usecase.LeaveTeamUseCase;
Expand Down Expand Up @@ -55,6 +56,7 @@ public static void main(String[] args) {
final JoinTeamUseCase joinTeamUseCase = config.joinTeamUseCase();
final LeaveTeamUseCase leaveTeamUseCase = config.leaveTeamUseCase();
final GetAverageGradeUseCase getAverageGradeUseCase = config.getAverageGradeUseCase();
final GetTopGradeUseCase getTopGradeUseCase = config.getTopGradeUseCase();

// this is the code that runs to set up our GUI
SwingUtilities.invokeLater(() -> {
Expand All @@ -70,7 +72,7 @@ public static void main(String[] args) {
final JPanel logGradeCard = createLogGradeCard(frame, logGradeUseCase);
final JPanel formTeamCard = createFormTeamCard(frame, formTeamUseCase);
final JPanel joinTeamCard = createJoinTeamCard(frame, joinTeamUseCase);
final JPanel manageTeamCard = createManageTeamCard(frame, leaveTeamUseCase, getAverageGradeUseCase);
final JPanel manageTeamCard = createManageTeamCard(frame, leaveTeamUseCase, getAverageGradeUseCase, getTopGradeUseCase);

cardPanel.add(defaultCard, "DefaultCard");
cardPanel.add(getGradeCard, "GetGradeCard");
Expand Down Expand Up @@ -324,12 +326,14 @@ public void actionPerformed(ActionEvent e) {
// TODO Task 4: modify this method so that it takes in a getTopGradeUseCase
// Note: this will require you to update the code that calls this method.
private static JPanel createManageTeamCard(JFrame jFrame, LeaveTeamUseCase leaveTeamUseCase,
GetAverageGradeUseCase getAverageGradeUseCase) {
GetAverageGradeUseCase getAverageGradeUseCase, GetTopGradeUseCase getTopGradeUseCase) {
final JPanel theCard = new JPanel();
theCard.setLayout(new GridLayout(ROWS, COLS));
final JTextField courseField = new JTextField(20);
// make a separate line.
final JButton getAverageButton = new JButton("Get Average Grade");
final JButton getTopGradeButton = new JButton("Get Top Grade");

// TODO Task 4: Add another button for "Get Top Grade" (check the getAverageButton for example)

final JButton leaveTeamButton = new JButton("Leave Team");
Expand All @@ -356,7 +360,27 @@ public void actionPerformed(ActionEvent e) {
}
});

// TODO Task 4: Add action listener for getTopGrade button, follow example of getAverageButton
getTopGradeButton.addActionListener(new ActionListener() {
/**
* Invoked when an action occurs.
*
* @param e the event to be processed
*/
@Override
public void actionPerformed(ActionEvent e) {
final String course = courseField.getText();
System.out.println(course);

try {
final float top = getTopGradeUseCase.getTopGrade(course);
JOptionPane.showMessageDialog(jFrame, "Top Grade: " + top);
courseField.setText("");
}
catch (JSONException ex) {
JOptionPane.showMessageDialog(jFrame, ex.getMessage());
}
}
});

leaveTeamButton.addActionListener(new ActionListener() {
/**
Expand All @@ -379,6 +403,7 @@ public void actionPerformed(ActionEvent e) {
theCard.add(new JLabel("The course you want to calculate the team average for:"));
theCard.add(courseField);
theCard.add(getAverageButton);
theCard.add(getTopGradeButton);
theCard.add(leaveTeamButton);
theCard.add(resultLabel);
return theCard;
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/usecase/GetAverageGradeUseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ public GetAverageGradeUseCase(GradeDataBase gradeDataBase) {
* @return The average grade.
*/
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.

for (String member : team.getMembers()) {

Grade grade = gradeDataBase.getGrade(member, course);

if (grade != null) {
sum += grade.getGrade();
count++;
}
}

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