Skip to content

Commit

Permalink
Merge pull request #96 from yeekian/branch-Component
Browse files Browse the repository at this point in the history
Add AddComponentCommand
  • Loading branch information
yeekian authored Oct 28, 2024
2 parents ba74ef7 + ef710f1 commit c87e3e8
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 235 deletions.
66 changes: 66 additions & 0 deletions src/main/java/tutorlink/command/AddComponentCommand.java
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;
}
}
3 changes: 3 additions & 0 deletions src/main/java/tutorlink/commons/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ public class Commons {

//@@author TrungBui32
//Component
public static final String ADD_COMPONENT_SUCCESS = "Component %s of weight %s, with max score %s added successfully!";
public static final String DELETE_COMPONENT_SUCCESS = "Component %s successfully deleted";
public static final String ERROR_COMPONENT_NOT_FOUND = "Error! Component (Name %s) not found";
public static final String ERROR_DUPLICATE_COMPONENT = "Error! Component (Name %s) already exists in the list!";
public static final String ERROR_INVALID_WEIGHTAGE = "Error! Weightage must be double that is between 0 and 1!";
public static final String ERROR_INVALID_MAX_SCORE = "Error! Max Score must be double that is more than or equal to 0";

//@@author RCPilot1604
//Invalid
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/tutorlink/component/Assignment.java

This file was deleted.

11 changes: 0 additions & 11 deletions src/main/java/tutorlink/component/ClassParticipation.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/tutorlink/component/Component.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tutorlink.component;

public abstract class Component {
public class Component {
private String name;
private double maxScore;
private double weight;
Expand Down Expand Up @@ -35,6 +35,6 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "[name=" + name + ", maxScore=" + maxScore + ", weight=" + weight + "]";
return name + ", maxScore = " + maxScore + ", weight = " + weight;
}
}
11 changes: 0 additions & 11 deletions src/main/java/tutorlink/component/Exam.java

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/java/tutorlink/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tutorlink.parser;

import tutorlink.command.AddComponentCommand;
import tutorlink.command.AddStudentCommand;
import tutorlink.command.Command;
import tutorlink.command.DeleteComponentCommand;
Expand All @@ -8,6 +9,7 @@
import tutorlink.command.ExitCommand;
import tutorlink.command.FindStudentCommand;
import tutorlink.command.InvalidCommand;
import tutorlink.command.ListComponentCommand;
import tutorlink.command.ListStudentCommand;
import tutorlink.command.AddGradeCommand;

Expand Down Expand Up @@ -50,9 +52,16 @@ public Command getCommand(String line) {

case DeleteGradeCommand.COMMAND_WORD:
return new DeleteGradeCommand();

case AddComponentCommand.COMMAND_WORD:
return new AddComponentCommand();

case DeleteComponentCommand.COMMAND_WORD:
return new DeleteComponentCommand();

case ListComponentCommand.COMMAND_WORD:
return new ListComponentCommand();

case ExitCommand.COMMAND_WORD:
return new ExitCommand(); // Lists all students

Expand Down
53 changes: 53 additions & 0 deletions src/test/java/tutorlink/command/AddComponentCommandTest.java
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());
}
}
}
Loading

0 comments on commit c87e3e8

Please sign in to comment.