Skip to content

Commit

Permalink
Merge pull request #36 from SebasFok/SebasFok-FixMajor
Browse files Browse the repository at this point in the history
Fix major bugs
  • Loading branch information
SebasFok authored Oct 24, 2023
2 parents 43d67d6 + 7ef09a8 commit d98977a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 35 deletions.
26 changes: 15 additions & 11 deletions src/main/java/seedu/duke/controllers/ModulePlannerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,21 @@ public void start() {
break;
}
case "major": {
updateMajor(words[1]);
String printMessageCommand = student.updateMajor(userInput);
switch (printMessageCommand) {
case "currentMajor":
view.displayMessage("Current major is " + student.getMajor() + ".");
break;
case "newMajor":
view.displayMessage("Major " + student.getMajor() + " selected!");
break;
case "invalidMajor":
view.displayMessage("Please select a major from this list: " + Arrays.toString(Major.values()));
break;
// Empty default branch as printMessageCommand cannot take any other value
default:
break;
}
break;
}
case "complete": {
Expand Down Expand Up @@ -203,14 +217,4 @@ public static void addValue(HashMap<String, List<String>> map, String key, Strin
map.get(key).add(value);
}



public void updateMajor(String major) {
try {
student.setMajor(Major.valueOf(major.toUpperCase()));
view.displayMessage("Major " + student.getMajor() + " selected!");
} catch (IllegalArgumentException e) {
view.displayMessage("Please select a major from this list: " + Arrays.toString(Major.values()));
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/seedu/duke/models/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ public void setName(String name) {
public void setMajor(Major major) {
this.major = major;
}

public String updateMajor(String userInput) {
String[] words = userInput.split(" ");
if (words.length < 2) {
return "currentMajor";
}
try {
setMajor(Major.valueOf(words[1].toUpperCase()));
return "newMajor";
} catch (IllegalArgumentException e) {
return "invalidMajor";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import seedu.duke.models.ModuleList;
import seedu.duke.models.Major;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -105,26 +103,4 @@ void listModulesLeftTest_expectArrayList() {
assertEquals(actualDifference.getMainModuleList().get(i), list.get(i));
}
}

//success scenario: valid major -> major updated
@Test
void updateMajor_validMajor_expectSuccessMessage() {
ModulePlannerController controller = new ModulePlannerController();
String major = "cs";
controller.updateMajor(major);
String printedOutput = outputStream.toString().trim();
assertEquals(String.format("Major %s selected!",major.toUpperCase()), printedOutput);
}

//failure scenario invalid major -> throw exception
@Test
void updateMajor_invalidMajor_expectFailureMessage() {
ModulePlannerController controller = new ModulePlannerController();
String major = "abc";
controller.updateMajor(major);
String printedOutput = outputStream.toString().trim();
assertEquals("Please select a major from this list: " + Arrays.toString(Major.values()),
printedOutput);

}
}
34 changes: 34 additions & 0 deletions src/test/java/seedu/duke/models/StudentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seedu.duke.models;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class StudentTest {
//success scenario: valid major -> major updated
@Test
void updateMajorTest_validMajor_expectNewMajorMessage() {
Student student = new Student();
String userInput = "major cs";
String printedOutputCommand = student.updateMajor(userInput);
assertEquals("newMajor", printedOutputCommand);
}

//success scenario: no major -> return current major
@Test
void updateMajorTest_noMajor_expectCurrentMajorMessage() {
Student student = new Student();
String userInput = "major";
String printedOutputCommand = student.updateMajor(userInput);
assertEquals("currentMajor", printedOutputCommand);
}

//failure scenario invalid major -> throw exception
@Test
void updateMajorTest_invalidMajor_expectFailureMessage() {
Student student = new Student();
String userInput = "major abc";
String printedOutputCommand = student.updateMajor(userInput);
assertEquals("invalidMajor", printedOutputCommand);
}
}

0 comments on commit d98977a

Please sign in to comment.