From 813fa09444c9155f79f736faadd281e068a13fe7 Mon Sep 17 00:00:00 2001 From: ryanlohyr Date: Sun, 15 Oct 2023 00:57:05 +0800 Subject: [PATCH 1/3] Add pace feature --- src/main/java/seedu/duke/models/Module.java | 2 +- src/main/java/seedu/duke/utils/Parser.java | 2 ++ .../seedu/duke/controllers/ModulePlannerControllerTest.java | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 src/main/java/seedu/duke/utils/Parser.java create mode 100644 src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java diff --git a/src/main/java/seedu/duke/models/Module.java b/src/main/java/seedu/duke/models/Module.java index b27279233b..79dea25019 100644 --- a/src/main/java/seedu/duke/models/Module.java +++ b/src/main/java/seedu/duke/models/Module.java @@ -1,4 +1,4 @@ -package seedu.duke.models; +package seedu.duke.models.objectModels; public class Module { //defining your module object etc } diff --git a/src/main/java/seedu/duke/utils/Parser.java b/src/main/java/seedu/duke/utils/Parser.java new file mode 100644 index 0000000000..edb504a732 --- /dev/null +++ b/src/main/java/seedu/duke/utils/Parser.java @@ -0,0 +1,2 @@ +package seedu.duke.utils;public class Parser { +} diff --git a/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java b/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java new file mode 100644 index 0000000000..fcec9424b6 --- /dev/null +++ b/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java @@ -0,0 +1,4 @@ +import static org.junit.jupiter.api.Assertions.*; +class ModulePlannerControllerTest { + +} \ No newline at end of file From 92685f4a8354d7c9339df54862e8ad730875ae31 Mon Sep 17 00:00:00 2001 From: ryanlohyr Date: Sun, 15 Oct 2023 01:00:39 +0800 Subject: [PATCH 2/3] Add pace feature v2 --- .../controllers/ModulePlannerController.java | 50 ++++++++++- src/main/java/seedu/duke/models/Module.java | 2 +- src/main/java/seedu/duke/utils/Parser.java | 39 +++++++- .../ModulePlannerControllerTest.java | 89 ++++++++++++++++++- 4 files changed, 174 insertions(+), 6 deletions(-) diff --git a/src/main/java/seedu/duke/controllers/ModulePlannerController.java b/src/main/java/seedu/duke/controllers/ModulePlannerController.java index 902074d70a..ad732347b0 100644 --- a/src/main/java/seedu/duke/controllers/ModulePlannerController.java +++ b/src/main/java/seedu/duke/controllers/ModulePlannerController.java @@ -1,12 +1,15 @@ package seedu.duke.controllers; -import seedu.duke.views.CommandLineView; +import seedu.duke.views.CommandLineView; +import seedu.duke.utils.Parser; import java.util.Scanner; public class ModulePlannerController { private CommandLineView view; + private Parser parser; public ModulePlannerController(){ this.view = new CommandLineView(); + this.parser = new Parser(); } public void start(){ view.displayWelcome(); @@ -28,6 +31,15 @@ public void start(){ view.displayMessage("yup"); break; } + case "pace":{ + //assumed that everyone graduates at y4s2 + //waiting for retrieving logic + int modulesCreditsCompleted = 100; + int totalCreditsToGraduate = 160; + int creditsLeft = totalCreditsToGraduate - modulesCreditsCompleted; + computePace(words, creditsLeft); + break; + } default:{ view.displayMessage("Hello " + userInput); break; @@ -37,4 +49,40 @@ public void start(){ userInput = in.nextLine(); } } + + /** + * Computes the recommended pace for completing a degree based on the provided academic year + * and credits left until graduation. + * + * @param userInput An array of user input where userInput[0] is the command and userInput[1] is the academic year. + * @param creditsLeft The number of credits left until graduation. + * @throws IllegalArgumentException if the provided academic year is invalid. + */ + public void computePace(String[] userInput,int creditsLeft){ + boolean argumentProvided = userInput.length != 1; + //wait for text file logic + if(!argumentProvided){ + view.displayMessage("You currently have " + creditsLeft + " MCs till graduation"); + return; + } + if(!parser.isValidAcademicYear(userInput[1])){ + return; + } + + String[] parts = userInput[1].split("/"); + String year = parts[0].toUpperCase(); + String semester = parts[1].toUpperCase(); + + int lastSemesterOfYear = 2; + int lastYearOfDegree = 4; + + + int yearIntValue = Character.getNumericValue(year.charAt(1)); + int semesterIntValue = Character.getNumericValue(semester.charAt(1)); + //if we are at y2s1, we have 5 semesters left + int semestersLeft = (lastYearOfDegree - yearIntValue) * 2 + (lastSemesterOfYear - semesterIntValue); + int creditsPerSem = Math.round((float) creditsLeft /semestersLeft) ; + view.displayMessage("You currently have " + creditsLeft + "MCs for " + semestersLeft + " semesters"); + view.displayMessage("Recommended Pace: " +creditsPerSem + "MCs per sem until graduation"); + } } diff --git a/src/main/java/seedu/duke/models/Module.java b/src/main/java/seedu/duke/models/Module.java index 79dea25019..b27279233b 100644 --- a/src/main/java/seedu/duke/models/Module.java +++ b/src/main/java/seedu/duke/models/Module.java @@ -1,4 +1,4 @@ -package seedu.duke.models.objectModels; +package seedu.duke.models; public class Module { //defining your module object etc } diff --git a/src/main/java/seedu/duke/utils/Parser.java b/src/main/java/seedu/duke/utils/Parser.java index edb504a732..e903acd726 100644 --- a/src/main/java/seedu/duke/utils/Parser.java +++ b/src/main/java/seedu/duke/utils/Parser.java @@ -1,2 +1,39 @@ -package seedu.duke.utils;public class Parser { +package seedu.duke.utils; + +public class Parser { + + //we need to consider if + public boolean isValidAcademicYear(String userInput ) { + try { + String[] parts = userInput.split("/"); + if(parts.length != 2){ + throw new IllegalArgumentException("Needs to be in format of Y2/S1"); + } + String year = parts[0].toUpperCase(); + String semester = parts[1].toUpperCase(); + + //last year + if(year.equals("Y4") && semester.equals("S2")){ + throw new IllegalArgumentException("Its your last sem!! A bit too late ya...."); + } + //validate semester + if(!semester.equals("S1") && !semester.equals("S2")){ + throw new IllegalArgumentException("Invalid Semester"); + } + + //validate year + if (!(year.equals("Y1") || year.equals("Y2") || year.equals("Y3") || year.equals("Y4"))) { + // The input is not "Y1," "Y2," "Y3," or "Y4" + throw new IllegalArgumentException("Invalid Year"); + } + return true; + } catch (Exception e) { + System.out.println(e.getMessage()); + return false; + } + + } + + + } diff --git a/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java b/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java index fcec9424b6..fe4dcc7616 100644 --- a/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java +++ b/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java @@ -1,4 +1,87 @@ -import static org.junit.jupiter.api.Assertions.*; +package seedu.duke.controllers; + +import org.junit.jupiter.api.Test; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import static org.junit.jupiter.api.Assertions.assertEquals; class ModulePlannerControllerTest { - -} \ No newline at end of file + private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + @BeforeEach + public void setUpStreams() { + System.setOut(new PrintStream(outputStream)); + } + + @AfterEach + public void restoreStreams() { + System.setOut(originalOut); + } + + + @Test + void computePaceWithoutArgument() { + ModulePlannerController controller = new ModulePlannerController(); + String[] userInput = {"pace"}; + int creditsLeft = 60; + controller.computePace(userInput,creditsLeft); + // Capture the printed output + String printedOutput = outputStream.toString().trim(); + // Assert the printed output matches the expected value + assertEquals(String.format("You currently have %s MCs till graduation",creditsLeft), printedOutput); + } + + @Test + void computePaceInvalidArgument() { + ModulePlannerController controller = new ModulePlannerController(); + String[] userInput = {"pace","y2s1"}; + int creditsLeft = 60; + controller.computePace(userInput,creditsLeft); + // Capture the printed output + String printedOutput = outputStream.toString().trim(); + // Assert the printed output matches the expected value + assertEquals("Needs to be in format of Y2/S1", printedOutput); + } + + @Test + void computePaceInvalidSemester() { + ModulePlannerController controller = new ModulePlannerController(); + String[] userInput = {"pace","y2/s10"}; + int creditsLeft = 60; + controller.computePace(userInput,creditsLeft); + // Capture the printed output + String printedOutput = outputStream.toString().trim(); + // Assert the printed output matches the expected value + assertEquals("Invalid Semester", printedOutput); + } + + @Test + void computePaceInvalidYear() { + ModulePlannerController controller = new ModulePlannerController(); + String[] userInput = {"pace","y20/s1"}; + int creditsLeft = 60; + controller.computePace(userInput,creditsLeft); + // Capture the printed output + String printedOutput = outputStream.toString().trim(); + // Assert the printed output matches the expected value + assertEquals("Invalid Year", printedOutput); + } + @Test + void computePaceValidYear() { + ModulePlannerController controller = new ModulePlannerController(); + String[] userInput = {"pace","y2/s1"}; + int creditsLeft = 60; + controller.computePace(userInput,creditsLeft); + String test = "hi"; + // Capture the printed output + String printedOutput = outputStream.toString().trim(); + String lineOne = "You currently have 60MCs for 5 semesters\n"; + String lineTwo = "Recommended Pace: 12MCs per sem until graduation"; + String expectedOutput = lineOne + lineTwo; + // Assert the printed output matches the expected value + assertEquals(printedOutput, expectedOutput); + } +} From e4f352e491e6bee1e281a3aeac2b5cbec16e50ce Mon Sep 17 00:00:00 2001 From: ryanlohyr Date: Sun, 15 Oct 2023 01:22:18 +0800 Subject: [PATCH 3/3] fix window os bug --- .../controllers/ModulePlannerController.java | 35 ++++++++++--------- .../ModulePlannerControllerTest.java | 6 ++-- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/main/java/seedu/duke/controllers/ModulePlannerController.java b/src/main/java/seedu/duke/controllers/ModulePlannerController.java index ad732347b0..738344246e 100644 --- a/src/main/java/seedu/duke/controllers/ModulePlannerController.java +++ b/src/main/java/seedu/duke/controllers/ModulePlannerController.java @@ -2,36 +2,39 @@ import seedu.duke.views.CommandLineView; import seedu.duke.utils.Parser; + import java.util.Scanner; public class ModulePlannerController { private CommandLineView view; - private Parser parser; - public ModulePlannerController(){ + private Parser parser; + + public ModulePlannerController() { this.view = new CommandLineView(); this.parser = new Parser(); } - public void start(){ + + public void start() { view.displayWelcome(); Scanner in = new Scanner(System.in); String userInput = in.nextLine(); - while(!userInput.equals("Bye")){ + while (!userInput.equals("Bye")) { String[] words = userInput.split(" "); String initialWord = words[0]; - switch(initialWord){ - case "hi":{ + switch (initialWord) { + case "hi": { view.displayMessage("can put the commands here"); break; } - case "hello":{ + case "hello": { view.displayMessage("yup"); break; } - case "pace":{ + case "pace": { //assumed that everyone graduates at y4s2 //waiting for retrieving logic int modulesCreditsCompleted = 100; @@ -40,7 +43,7 @@ public void start(){ computePace(words, creditsLeft); break; } - default:{ + default: { view.displayMessage("Hello " + userInput); break; } @@ -54,18 +57,18 @@ public void start(){ * Computes the recommended pace for completing a degree based on the provided academic year * and credits left until graduation. * - * @param userInput An array of user input where userInput[0] is the command and userInput[1] is the academic year. + * @param userInput An array of user input where userInput[0] is the command and userInput[1] is the academic year. * @param creditsLeft The number of credits left until graduation. * @throws IllegalArgumentException if the provided academic year is invalid. */ - public void computePace(String[] userInput,int creditsLeft){ + public void computePace(String[] userInput, int creditsLeft) { boolean argumentProvided = userInput.length != 1; //wait for text file logic - if(!argumentProvided){ + if (!argumentProvided) { view.displayMessage("You currently have " + creditsLeft + " MCs till graduation"); return; } - if(!parser.isValidAcademicYear(userInput[1])){ + if (!parser.isValidAcademicYear(userInput[1])) { return; } @@ -81,8 +84,8 @@ public void computePace(String[] userInput,int creditsLeft){ int semesterIntValue = Character.getNumericValue(semester.charAt(1)); //if we are at y2s1, we have 5 semesters left int semestersLeft = (lastYearOfDegree - yearIntValue) * 2 + (lastSemesterOfYear - semesterIntValue); - int creditsPerSem = Math.round((float) creditsLeft /semestersLeft) ; - view.displayMessage("You currently have " + creditsLeft + "MCs for " + semestersLeft + " semesters"); - view.displayMessage("Recommended Pace: " +creditsPerSem + "MCs per sem until graduation"); + int creditsPerSem = Math.round((float) creditsLeft / semestersLeft); + view.displayMessage("You have " + creditsLeft + "MCs for " + semestersLeft + " semesters. " + + "Recommended Pace: "+ creditsPerSem + "MCs per sem until graduation"); } } diff --git a/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java b/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java index fe4dcc7616..96fc877626 100644 --- a/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java +++ b/src/test/java/seedu/duke/controllers/ModulePlannerControllerTest.java @@ -78,10 +78,8 @@ void computePaceValidYear() { String test = "hi"; // Capture the printed output String printedOutput = outputStream.toString().trim(); - String lineOne = "You currently have 60MCs for 5 semesters\n"; - String lineTwo = "Recommended Pace: 12MCs per sem until graduation"; - String expectedOutput = lineOne + lineTwo; + String line = "You have 60MCs for 5 semesters. Recommended Pace: 12MCs per sem until graduation"; // Assert the printed output matches the expected value - assertEquals(printedOutput, expectedOutput); + assertEquals(printedOutput, line); } }