Skip to content

Commit

Permalink
Add Username class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ChenLeiyu committed Oct 13, 2024
1 parent 848ae43 commit 9b44140
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/java/seedu/address/model/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,30 @@ 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.
*/
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);
}

/**
Expand Down Expand Up @@ -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();
}
}

}
73 changes: 73 additions & 0 deletions src/main/java/seedu/address/model/game/Username.java
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;
}

}

0 comments on commit 9b44140

Please sign in to comment.