forked from AY2425S1-CS2103T-T12-4/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
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 AY2425S1-CS2103T-T12-4#61
Add username to each game
- Loading branch information
Showing
2 changed files
with
93 additions
and
2 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
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,73 @@ | ||
package seedu.address.model.game; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
/** | ||
* Represents a username in a Game. | ||
*/ | ||
public class Username { | ||
|
||
private static final String MESSAGE_CONSTRAINTS = | ||
"Username should not be blank"; | ||
|
||
/* | ||
* Regex expression matches Strings that contain at least one non-whitespace character. | ||
*/ | ||
private static final String VALIDATION_REGEX = "^(?!\\s*$).+"; | ||
|
||
private final String username; | ||
|
||
/** | ||
* Constructs a {@code Username}. | ||
* | ||
* @param username a valid username. | ||
*/ | ||
public Username(String username) { | ||
requireNonNull(username); | ||
checkArgument(isValidGameName(username), MESSAGE_CONSTRAINTS); | ||
this.username = username; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid Username. | ||
*/ | ||
public static boolean isValidGameName(String test) { | ||
return test.matches(VALIDATION_REGEX); | ||
} | ||
|
||
/** | ||
* Getter for username field. | ||
*/ | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Username)) { | ||
return false; | ||
} | ||
|
||
Username otherName = (Username) other; | ||
return username.equals(otherName.username); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return username.hashCode(); | ||
} | ||
|
||
/** | ||
* Format state as text for viewing. | ||
*/ | ||
public String toString() { | ||
return username; | ||
} | ||
|
||
} |