From b40578f83ea1b809ab2d73b82ddf7ee69690b778 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 10:51:51 +0100 Subject: [PATCH] Added some validation logic for the username and password --- src/main/java/ChatServer.java | 1 - src/main/java/SignUp.java | 13 ++++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatServer.java b/src/main/java/ChatServer.java index c4b258f..0fd0416 100644 --- a/src/main/java/ChatServer.java +++ b/src/main/java/ChatServer.java @@ -4,7 +4,6 @@ import java.net.ServerSocket; import java.net.Socket; public class ChatServer { - // haha private ServerSocket serverSocket; public ChatServer(int port) { diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java index 97b4128..1eefbfd 100644 --- a/src/main/java/SignUp.java +++ b/src/main/java/SignUp.java @@ -27,13 +27,20 @@ public class SignUp { // Function to create user public static SignUp createUser(String name, String password) { - // add validation logic here - return new SignUp(name, password); + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Username cannot be empty"); + } + if (password == null || password.isEmpty()) { + throw new IllegalArgumentException("Password cannot be empty"); + } + if (password.length() < 6) { + throw new IllegalArgumentException("Password must be at least 6 characters long"); + } return new SignUp(name, password); } public static void main(String[] args) { // Example - SignUp user = createUser("JohnDoe", "password123"); + SignUp user = createUser("TestUser", "TestPasswort"); // Example of accessing properties System.out.println("User Name: " + user.getName());