forked from nus-cs2113-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from yeekian/branch-Component
Add AddComponentCommand
- Loading branch information
Showing
18 changed files
with
172 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package tutorlink.command; | ||
|
||
import java.util.HashMap; | ||
|
||
import tutorlink.appstate.AppState; | ||
import tutorlink.commons.Commons; | ||
import tutorlink.component.Component; | ||
import tutorlink.exceptions.IllegalValueException; | ||
import tutorlink.exceptions.TutorLinkException; | ||
import tutorlink.result.CommandResult; | ||
|
||
public class AddComponentCommand extends Command { | ||
|
||
public static final String[] ARGUMENT_PREFIXES = {"c/", "w/", "m/"}; | ||
public static final String COMMAND_WORD = "add_component"; | ||
|
||
@Override | ||
public CommandResult execute(AppState appState, HashMap<String, String> hashmap) throws TutorLinkException { | ||
String componentName = hashmap.get(ARGUMENT_PREFIXES[0]); | ||
String weightageNumber = hashmap.get(ARGUMENT_PREFIXES[1]); | ||
String maxScoreNumber = hashmap.get(ARGUMENT_PREFIXES[2]); | ||
if (componentName == null || weightageNumber == null || maxScoreNumber == null) { | ||
throw new IllegalValueException(Commons.ERROR_NULL); | ||
} | ||
|
||
double weightage = convertWeightageToValidDouble(weightageNumber); | ||
|
||
double maxScore = convertMaxScoreToValidDouble(maxScoreNumber); | ||
appState.components.addComponent(new Component(componentName, maxScore, weightage)); | ||
return new CommandResult(String.format(Commons.ADD_COMPONENT_SUCCESS, componentName, weightageNumber, maxScoreNumber)); | ||
} | ||
|
||
private static double convertWeightageToValidDouble(String weightageNumber) { | ||
try { | ||
double weightage = Double.parseDouble(weightageNumber); | ||
if (weightage < 0.0 || weightage > 1.0) { | ||
throw new IllegalValueException(Commons.ERROR_INVALID_WEIGHTAGE); | ||
} | ||
return weightage; | ||
|
||
} catch (NumberFormatException e) { | ||
throw new IllegalValueException(Commons.ERROR_INVALID_WEIGHTAGE); | ||
} | ||
} | ||
|
||
private static double convertMaxScoreToValidDouble(String maxScoreNumber) { | ||
try { | ||
double maxScore = Double.parseDouble(maxScoreNumber); | ||
|
||
if (maxScore < 0.0) { | ||
throw new IllegalValueException(Commons.ERROR_INVALID_MAX_SCORE); | ||
} | ||
|
||
return maxScore; | ||
|
||
} catch (NumberFormatException e) { | ||
throw new IllegalValueException(Commons.ERROR_INVALID_MAX_SCORE); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public String[] getArgumentPrefixes() { | ||
return ARGUMENT_PREFIXES; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/test/java/tutorlink/command/AddComponentCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package TutorLink.command; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import tutorlink.appstate.AppState; | ||
import tutorlink.command.AddStudentCommand; | ||
import tutorlink.commons.Commons; | ||
import tutorlink.exceptions.IllegalValueException; | ||
import tutorlink.result.CommandResult; | ||
|
||
import java.util.HashMap; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
public class AddComponentCommandTest { | ||
private AppState appState; | ||
private HashMap<String, String> arguments; | ||
private AddStudentCommand command; | ||
|
||
@BeforeEach | ||
void setup() { | ||
appState = new AppState(); | ||
arguments = new HashMap<>(); | ||
command = new AddStudentCommand(); | ||
} | ||
|
||
@Test | ||
void execute_addOne_expectOne() { | ||
arguments.put("i/","A1234567X"); | ||
arguments.put("n/", "John"); | ||
CommandResult result = command.execute(appState, arguments); | ||
assertNotNull(result); | ||
assertEquals(result.toString(), "Student John (A1234567X) added successfully!"); | ||
assertEquals(appState.students.getStudentArrayList().size(), 1); | ||
} | ||
|
||
@Test | ||
void execute_emptyInput_exceptionThrown() { | ||
arguments.put("i/","A1234567X"); | ||
try { | ||
CommandResult result = command.execute(appState, arguments); | ||
fail("Expected an exception to be thrown due to empty input"); | ||
} catch (IllegalValueException e) { | ||
// Assert that the exception message matches the expected outcome | ||
assertEquals(Commons.ERROR_NULL, e.getMessage()); | ||
} catch (Exception e) { | ||
// If any other type of exception is thrown, fail the test | ||
fail("Expected IllegalArgumentException, but got: " + e.getClass().getSimpleName()); | ||
} | ||
} | ||
} |
Oops, something went wrong.