Skip to content

Commit

Permalink
Merge pull request #9 from ryanlohyr/ryan-paceFeature
Browse files Browse the repository at this point in the history
Add pace feature
  • Loading branch information
ryanlohyr authored Oct 14, 2023
2 parents a6429ac + e4f352e commit ab09a61
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 7 deletions.
65 changes: 58 additions & 7 deletions src/main/java/seedu/duke/controllers/ModulePlannerController.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
package seedu.duke.controllers;

import seedu.duke.views.CommandLineView;
import seedu.duke.utils.Parser;

import java.util.Scanner;

public class ModulePlannerController {
private CommandLineView view;
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;
}
default:{
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;
}
Expand All @@ -37,4 +52,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 have " + creditsLeft + "MCs for " + semestersLeft + " semesters. "
+ "Recommended Pace: "+ creditsPerSem + "MCs per sem until graduation");
}
}
39 changes: 39 additions & 0 deletions src/main/java/seedu/duke/utils/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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;
}

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 {
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 line = "You have 60MCs for 5 semesters. Recommended Pace: 12MCs per sem until graduation";
// Assert the printed output matches the expected value
assertEquals(printedOutput, line);
}
}

0 comments on commit ab09a61

Please sign in to comment.