From f5c6152585295965f718fa1865cd0fa6d85a6c1a Mon Sep 17 00:00:00 2001 From: Victor Gao Date: Mon, 30 Sep 2024 14:37:15 -0400 Subject: [PATCH 1/4] Finish Task 3a --- src/main/java/usecase/GetAverageGradeUseCase.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/usecase/GetAverageGradeUseCase.java b/src/main/java/usecase/GetAverageGradeUseCase.java index 25140b1..480cac4 100644 --- a/src/main/java/usecase/GetAverageGradeUseCase.java +++ b/src/main/java/usecase/GetAverageGradeUseCase.java @@ -28,7 +28,11 @@ public float getAverageGrade(String course) { // 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(); + for (String member : members) { + sum += gradeDataBase.getGrade(member, "207").getGrade(); + count += 1; + } if (count == 0) { return 0; } From 1e3d28459df73da85067a5bb31aa8fd0f34ca00f Mon Sep 17 00:00:00 2001 From: Proedricmants Date: Mon, 30 Sep 2024 14:37:43 -0400 Subject: [PATCH 2/4] Finished Implementation Task 4 -Awaiting Testing --- pom.xml | 3 +++ src/main/java/app/Config.java | 9 ++++++- src/main/java/app/gui/Application.java | 35 ++++++++++++++++++-------- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index fee82d8..c628d74 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,7 @@ okhttp 4.10.0 + junit junit @@ -28,12 +29,14 @@ test + org.mockito mockito-core 5.13.0 test + org.junit.jupiter junit-jupiter diff --git a/src/main/java/app/Config.java b/src/main/java/app/Config.java index 12df0fd..4fcb690 100644 --- a/src/main/java/app/Config.java +++ b/src/main/java/app/Config.java @@ -65,5 +65,12 @@ 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. + // TODO (Implemented) Task 4: add code for the new GetTopGradeUseCase following the same pattern as the other use cases above. + /** + * Get the GetTopGradeUseCase object. + * @return GetTopGradeUseCase object. + */ + public GetTopGradeUseCase getTopGradeUseCase() { + return new GetTopGradeUseCase(gradeDataBase); + } } diff --git a/src/main/java/app/gui/Application.java b/src/main/java/app/gui/Application.java index a083b09..49d81af 100644 --- a/src/main/java/app/gui/Application.java +++ b/src/main/java/app/gui/Application.java @@ -17,12 +17,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. @@ -51,6 +46,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(() -> { @@ -66,7 +62,8 @@ 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"); @@ -237,16 +234,19 @@ private static JPanel createJoinTeamCard(JFrame jFrame, JoinTeamUseCase joinTeam return theCard; } - // TODO Task 4: modify this method so that it takes in a getTopGradeUseCase + // TODO (Implemented) Task 4: modify this method so that it takes in a getTopGradeUseCase // Note: this will require you to update the code which 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) + + // TODO (Implemented) Task 4: Add another button for "Get Top Grade" (check the getAverageButton for example) + final JButton getTopGradeButton = new JButton("Get Top Grade"); final JButton leaveTeamButton = new JButton("Leave Team"); final JLabel resultLabel = new JLabel(); @@ -264,7 +264,19 @@ private static JPanel createManageTeamCard(JFrame jFrame, LeaveTeamUseCase leave } }); - // TODO Task 4: Add action listener for getTopGrade button, follow example of getAverageButton + // TODO (Implemented) Task 4: Add action listener for getTopGrade button, follow example of getAverageButton + getTopGradeButton.addActionListener(event -> { + final String course = courseField.getText(); + + try { + final float topGrade = getTopGradeUseCase.getTopGrade(course); + JOptionPane.showMessageDialog(jFrame, "Top Grade: " + topGrade); + courseField.setText(""); + } + catch (Exception ex) { + JOptionPane.showMessageDialog(jFrame, ex.getMessage()); + } + }); leaveTeamButton.addActionListener(event -> { try { @@ -278,6 +290,7 @@ private static JPanel createManageTeamCard(JFrame jFrame, LeaveTeamUseCase leave 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; From c9de20d2567ea576c3bc97d5b6135bc7f1bf7441 Mon Sep 17 00:00:00 2001 From: perplop Date: Mon, 30 Sep 2024 14:40:58 -0400 Subject: [PATCH 3/4] finished logic for getMyTeam --- src/main/java/api/MongoGradeDataBase.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/api/MongoGradeDataBase.java b/src/main/java/api/MongoGradeDataBase.java index 9f52aa1..e4ab4d7 100644 --- a/src/main/java/api/MongoGradeDataBase.java +++ b/src/main/java/api/MongoGradeDataBase.java @@ -256,12 +256,25 @@ public Team getMyTeam() { .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()); + final JSONObject team = responseBody.getJSONObject("team"); + final JSONArray membersArray = team.getJSONArray("members"); + final String[] members = new String[membersArray.length()]; + + if (responseBody.getInt(STATUS_CODE) != SUCCESS_CODE) { + throw new RuntimeException(responseBody.getString(MESSAGE)); + } + else { + return Team.builder().name(team.getString(NAME)).members(members).build(); + } + } + catch (IOException | JSONException event) { + throw new RuntimeException(event); + } - return null; } } From 039ece628e0c715e22c053e718f8ce296fd08851 Mon Sep 17 00:00:00 2001 From: perplop Date: Mon, 30 Sep 2024 14:45:00 -0400 Subject: [PATCH 4/4] no changes --- src/main/java/usecase/GetAverageGradeUseCase.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/usecase/GetAverageGradeUseCase.java b/src/main/java/usecase/GetAverageGradeUseCase.java index 25140b1..2b62a54 100644 --- a/src/main/java/usecase/GetAverageGradeUseCase.java +++ b/src/main/java/usecase/GetAverageGradeUseCase.java @@ -4,6 +4,8 @@ import entity.Grade; import entity.Team; +import java.lang.reflect.Array; + /** * GetAverageGradeUseCase class. */ @@ -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;