From 9b44140c223d0717a31c02d7cafb0949a37d7362 Mon Sep 17 00:00:00 2001 From: Chen Lei Yu <128605764+ChenLeiyu@users.noreply.github.com> Date: Sun, 13 Oct 2024 19:31:47 +0800 Subject: [PATCH] Add Username class Game does not have any username information. Gamers usually associate themselves with a username when playing a game. Let's make a Game optionally have a Username. --- .../java/seedu/address/model/game/Game.java | 22 +++++- .../seedu/address/model/game/Username.java | 73 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/main/java/seedu/address/model/game/Username.java diff --git a/src/main/java/seedu/address/model/game/Game.java b/src/main/java/seedu/address/model/game/Game.java index b123cae5282..dfac6d0e043 100644 --- a/src/main/java/seedu/address/model/game/Game.java +++ b/src/main/java/seedu/address/model/game/Game.java @@ -19,9 +19,10 @@ public class Game { public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*"; public final String gameName; + public final Username username; /** - * Constructs a {@code Game}. + * Constructs a {@code Game} without a username. * * @param gameName A valid Game name. */ @@ -29,6 +30,19 @@ public Game(String gameName) { requireNonNull(gameName); checkArgument(isValidGameName(gameName), MESSAGE_CONSTRAINTS); this.gameName = gameName; + this.username = null; + } + + /** + * Constructs a {@code Game} with a username. + * + * @param gameName A valid Game name. + */ + public Game(String gameName, String username) { + requireNonNull(gameName); + checkArgument(isValidGameName(gameName), MESSAGE_CONSTRAINTS); + this.gameName = gameName; + this.username = new Username(username); } /** @@ -62,7 +76,11 @@ public int hashCode() { * Format state as text for viewing. */ public String toString() { - return '[' + gameName + ']'; + if (username == null) { + return '[' + gameName + ']'; + } else { + return '[' + gameName + "]: " + username.toString(); + } } } diff --git a/src/main/java/seedu/address/model/game/Username.java b/src/main/java/seedu/address/model/game/Username.java new file mode 100644 index 00000000000..f2801593dd5 --- /dev/null +++ b/src/main/java/seedu/address/model/game/Username.java @@ -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; + } + +}