Skip to content
Open

3b2 #115

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: 26 additions & 7 deletions src/main/java/api/MongoGradeDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,32 @@ public Team getMyTeam() {
.addHeader(CONTENT_TYPE, APPLICATION_JSON)
.build();

final Response response;
final JSONObject responseBody;
try {
final Response response = client.newCall(request).execute();
final String raw = response.body() != null ? response.body().string() : "";
if (raw.isEmpty()) {
throw new RuntimeException("Empty response from server.");
}

final JSONObject responseBody = new JSONObject(raw);

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 null;
return Team.builder()
.name(team.getString(NAME))
.members(members)
.build();
} else {
throw new RuntimeException(responseBody.optString(MESSAGE, "Failed to get team."));
}
} catch (IOException | JSONException e) {
throw new RuntimeException(e);
}
}
}
5 changes: 4 additions & 1 deletion 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);
}

// TODO Task 4: add code for the new GetTopGradeUseCase following the same pattern as the other use cases above.


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

32 changes: 23 additions & 9 deletions src/main/java/app/gui/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@
import api.MongoGradeDataBase;
import app.Config;
import entity.Grade;
import usecase.FormTeamUseCase;
import usecase.GetAverageGradeUseCase;
import usecase.GetGradeUseCase;
import usecase.JoinTeamUseCase;
import usecase.LeaveTeamUseCase;
import usecase.LogGradeUseCase;
import usecase.*;

/**
* GUI class to run the GUI for the Grade App.
Expand Down Expand Up @@ -55,6 +50,8 @@ 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 +67,9 @@ 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,14 +323,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");
// TODO Task 4: Add another button for "Get Top Grade" (check the getAverageButton for example)

final JButton getTopButton = new JButton("Get Top Grade");
final JButton leaveTeamButton = new JButton("Leave Team");
final JLabel resultLabel = new JLabel();

Expand All @@ -357,6 +356,20 @@ public void actionPerformed(ActionEvent e) {
});

// TODO Task 4: Add action listener for getTopGrade button, follow example of getAverageButton
getTopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final String course = courseField.getText();
try {
final float avg = getTopGradeUseCase.getTopGrade(course);
JOptionPane.showMessageDialog(jFrame, "Top Grade: " + avg);
courseField.setText("");
}
catch (JSONException ex) {
JOptionPane.showMessageDialog(jFrame, ex.getMessage());
}
}
});

leaveTeamButton.addActionListener(new ActionListener() {
/**
Expand All @@ -379,6 +392,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(getTopButton);
theCard.add(leaveTeamButton);
theCard.add(resultLabel);
return theCard;
Expand Down
34 changes: 27 additions & 7 deletions src/main/java/usecase/GetAverageGradeUseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,38 @@ 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;
float sum = 0f;
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.
if (team == null || team.getMembers() == null || team.getMembers().length == 0) {
throw new IllegalStateException();
}

for (String username : team.getMembers()) {
if (username == null || username.isBlank()) continue;
Grade[] grades;
try {
grades = gradeDataBase.getGrades(username);
} catch (RuntimeException ex) {
continue;
}
if (grades == null) continue;

for (Grade g : grades) {
if (g == null) continue;
if (course.equals(g.getCourse())) {
sum += g.getGrade();
count++;
break;
}
}
}

if (count == 0) {
return 0;
throw new IllegalStateException();
}
return sum / count;
}

}