forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
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
tsulim
merged 4 commits into
AY2324S2-CS2103-F08-3:master
from
whitesnowx:add-module-attribute-4
Mar 12, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2089efb
add Module.java class and ModuleTest.java
whitesnowx c7fe269
Change class description to module code
whitesnowx 19ec67c
fix validation_regex for module codes in Module.java
whitesnowx 779c1b7
added different types for equals() test
whitesnowx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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; | ||
} | ||
|
||
@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(); | ||
} | ||
|
||
} |
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 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 | ||
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"))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do