From 27837ff6aedee615e8e544405fb820a853c0aa35 Mon Sep 17 00:00:00 2001 From: RCPilot1604 Date: Mon, 11 Nov 2024 15:07:09 +0800 Subject: [PATCH 1/3] Add validation to prevent excessively large max score from being added --- src/main/java/tutorlink/command/AddComponentCommand.java | 6 ++++++ src/main/java/tutorlink/commons/Commons.java | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/tutorlink/command/AddComponentCommand.java b/src/main/java/tutorlink/command/AddComponentCommand.java index 96fa235f68..973314537a 100644 --- a/src/main/java/tutorlink/command/AddComponentCommand.java +++ b/src/main/java/tutorlink/command/AddComponentCommand.java @@ -15,6 +15,8 @@ public class AddComponentCommand extends Command { public static final String[] ARGUMENT_PREFIXES = {"c/", "w/", "m/"}; public static final String COMMAND_WORD = "add_component"; + private static final double MAX_SCORE = 10000.0; + @Override public CommandResult execute(AppState appState, HashMap hashmap) throws TutorLinkException { String componentName = hashmap.get(ARGUMENT_PREFIXES[0]); @@ -58,6 +60,10 @@ private static double convertMaxScoreToValidDouble(String maxScoreNumber) { throw new IllegalValueException(Commons.ERROR_INVALID_MAX_SCORE); } + if (maxScore > MAX_SCORE) { + throw new IllegalValueException(Commons.ERROR_INVALID_MAX_SCORE); + } + return maxScore; } catch (NumberFormatException e) { diff --git a/src/main/java/tutorlink/commons/Commons.java b/src/main/java/tutorlink/commons/Commons.java index 74276c4ad1..94ba9db7e8 100644 --- a/src/main/java/tutorlink/commons/Commons.java +++ b/src/main/java/tutorlink/commons/Commons.java @@ -37,7 +37,7 @@ public class Commons { "in list!"; public static final String ERROR_INVALID_WEIGHTAGE = "Error! Weightage must be integer that is between 0 and 100!"; public static final String ERROR_INVALID_MAX_SCORE = - "Error! Max Score must be double that is more than or equal to 0!"; + "Error! Max Score must be double that is between 0 and 10000!"; //@@author RCPilot1604 public static final String ERROR_INVALID_TOTAL_WEIGHTING = "Error! Total weighting must add up to 100%%.\n" + "Current weighting (after addition): %s%%"; From 02c0d2adbf156cb20d993d4b5f060492473aada6 Mon Sep 17 00:00:00 2001 From: RCPilot1604 Date: Mon, 11 Nov 2024 15:13:34 +0800 Subject: [PATCH 2/3] Update AddComponentCommandTest to test new validation --- .../java/tutorlink/command/AddComponentCommandTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/tutorlink/command/AddComponentCommandTest.java b/src/test/java/tutorlink/command/AddComponentCommandTest.java index 8bcb677890..b2baf40a45 100644 --- a/src/test/java/tutorlink/command/AddComponentCommandTest.java +++ b/src/test/java/tutorlink/command/AddComponentCommandTest.java @@ -228,5 +228,13 @@ void update_gpa_accordingly () { command.execute(appState, arguments); assertEquals(student.getPercentageScore(), 50); } + + @Test + void execute_maxScoreLargerThanMax_exception() { + arguments.put("c/", "Quiz 1"); + arguments.put("w/", "100"); + arguments.put("m/", "20000"); + assertThrows(IllegalValueException.class, () -> command.execute(appState, arguments)); + } } //@@author From e52810458f3aded00e83da832abd12bf25b64f26 Mon Sep 17 00:00:00 2001 From: RCPilot1604 Date: Mon, 11 Nov 2024 19:07:51 +0800 Subject: [PATCH 3/3] Fix merge commit --- src/main/java/tutorlink/command/AddComponentCommand.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/tutorlink/command/AddComponentCommand.java b/src/main/java/tutorlink/command/AddComponentCommand.java index 3889c5a6e4..938e6f917b 100644 --- a/src/main/java/tutorlink/command/AddComponentCommand.java +++ b/src/main/java/tutorlink/command/AddComponentCommand.java @@ -129,14 +129,11 @@ private double parseMaxScore(String maxScoreNumber) throws IllegalValueException if (maxScore < 0.0) { throw new IllegalValueException(Commons.ERROR_INVALID_MAX_SCORE); } -<<<<<<< HEAD if (maxScore > MAX_SCORE) { throw new IllegalValueException(Commons.ERROR_INVALID_MAX_SCORE); } - -======= ->>>>>>> master + return maxScore; } catch (NumberFormatException e) { throw new IllegalValueException(Commons.ERROR_INVALID_MAX_SCORE);