Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Module.java class and ModuleTest.java #59

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/main/java/staffconnect/model/person/Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package staffconnect.model.person;

import static java.util.Objects.requireNonNull;
import static staffconnect.commons.util.AppUtil.checkArgument;

/**
* Represents a Person's phone number in the staff book.
* Guarantees: immutable; is valid as declared in {@link #isValidModule(String)}
*/
public class Module {


public static final String MESSAGE_CONSTRAINTS =
"Module code should contain 2-4 capital letters followed by 4 digits long and at most 1 capitalised suffix";
public static final String VALIDATION_REGEX = "[A-Z]{2,5}\\d{4}[A-Z]{0,2}";

public final String value;

/**
* Constructs a {@code Module}.
*
* @param module A valid module code.
*/
public Module(String module) {
requireNonNull(module);
checkArgument(isValidModule(module), MESSAGE_CONSTRAINTS);
value = module;
}

/**
* Returns true if a given string is a valid module code.
*/
public static boolean isValidModule(String test) {
return test.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
return value;

Check warning on line 39 in src/main/java/staffconnect/model/person/Module.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/person/Module.java#L39

Added line #L39 was not covered by tests
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof Module)) {
return false;
}

Module otherModule = (Module) other;
return value.equals(otherModule.value);
}

@Override
public int hashCode() {
return value.hashCode();

Check warning on line 59 in src/main/java/staffconnect/model/person/Module.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/person/Module.java#L59

Added line #L59 was not covered by tests
}

}
66 changes: 66 additions & 0 deletions src/test/java/staffconnect/model/person/ModuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package staffconnect.model.person;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static staffconnect.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Test;

public class ModuleTest {

@Test
public void constructor_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new Module(null));
}

@Test
public void constructor_invalidModule_throwsIllegalArgumentException() {
String invalidModule = "";
assertThrows(IllegalArgumentException.class, () -> new Module(invalidModule));
}

@Test
public void isValidModule() {
// null phone number
assertThrows(NullPointerException.class, () -> Module.isValidModule(null));

// invalid phone numbers
assertFalse(Module.isValidModule("")); // empty string
assertFalse(Module.isValidModule(" ")); // spaces only
assertFalse(Module.isValidModule("ABCD")); // letters only
assertFalse(Module.isValidModule("1234")); // numbers only
assertFalse(Module.isValidModule("2103T")); // missing prefix
assertFalse(Module.isValidModule("C2103T")); // only 1 prefix
assertFalse(Module.isValidModule("CS2103TTTT")); // too many suffix
assertFalse(Module.isValidModule("CSCSCS2103T")); // too many prefix
assertFalse(Module.isValidModule("CS210310101010T")); // too many numbers
assertFalse(Module.isValidModule("CS21T")); // too little numbers
assertFalse(Module.isValidModule("cs2103t")); // suffix not capitalised
assertFalse(Module.isValidModule("cs2103T")); // prefix not capitalised

// valid phone numbers
Copy link

@Pluiexo Pluiexo Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits misleading phone numbers comments in this file, please fix in future commits!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

assertTrue(Module.isValidModule("CS2103")); // 2 prefix, 4 letters without 1 optional suffix
assertTrue(Module.isValidModule("CS2103T")); // 2 prefix, 4 letters with 1 optional suffix
assertTrue(Module.isValidModule("GEN2050")); // 3 prefix, 4 letters without 1 optional suffix
assertTrue(Module.isValidModule("GEN2050Y")); // 3 prefix, 4 letters with 1 optional suffix
assertTrue(Module.isValidModule("GESS1035")); // 4 prefix, 4 letters without 1 optional suffix
assertTrue(Module.isValidModule("GESS1035X")); // 4 prefix, 4 letters with 1 optional suffix
}

@Test
public void equals() {
Module module = new Module("CS2103");

// same values -> returns true
assertTrue(module.equals(new Module("CS2103")));

// same object -> returns true
assertTrue(module.equals(module));

// null -> returns false
assertFalse(module.equals(null));

// different values -> returns false
assertFalse(module.equals(new Module("MS2345")));
}
}
Loading