diff --git a/README.md b/README.md index 0a07ee55e68aba5295bc22c45dc996c5b6912418..2c0c32771ad2bc2a3f72cc96bf17f5df0f5391c5 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,4 @@ Socket Chat is a simple 2-way chat program, essentially a bi-directional echo server/client. Only one socket is set up, so only two chatters are possible. -It is remarkably non-robust, as its sole purpose is to demonstrate the use of -Java sockets. + diff --git a/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java b/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java index 9f5db3133fa78d16f794de2896fe0c51691afcd1..52dace647e3fd5693f724bacfb6620acbd93ba9d 100644 --- a/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java +++ b/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java @@ -3,6 +3,8 @@ package edu.unl.cse.csce361.socket_chat; import java.io.*; import java.net.*; import java.util.InputMismatchException; +import java.util.Locale; +import java.util.ResourceBundle; import java.util.Scanner; import static java.lang.Thread.sleep; @@ -12,8 +14,10 @@ public class Chat { private static final int MAXIMUM_CONNECTION_ATTEMPTS = 10; private Socket socket; private boolean isHost; + private ResourceBundle bundle; public Chat() { + bundle = ResourceBundle.getBundle("socketchat", Locale.US); socket = connect(new Scanner(System.in)); } @@ -21,15 +25,19 @@ public class Chat { @SuppressWarnings("WeakerAccess") public Socket connect(Scanner userInput) { - System.out.print("Are you the chat host? [Y] "); + String yes = bundle.getString("connection.responses.yes"); + String no = bundle.getString("connection.responses.no"); + // "Are you the chat host? [Y] " + System.out.print(bundle.getString("connection.prompts.isHost") + " [" + yes + "] "); String answerString = userInput.nextLine().toUpperCase(); - char answer = answerString.length() > 0 ? answerString.charAt(0) : 'Y'; - isHost = (answer != 'N'); + String answer = answerString.length() > 0 ? answerString.substring(0, no.length()) : yes; + isHost = (!answer.equals(no)); Socket socket = null; try { socket = isHost ? connectAsServer(userInput) : connectAsClient(userInput); } catch (IOException ioException) { - System.err.println("Connection failed: " + ioException); + // "Connection failed" + System.err.println(bundle.getString("connection.errors.generalConnectionFailure") + ": " + ioException); System.exit(1); } return socket; diff --git a/src/main/resources/.gitkeep b/src/main/resources/.gitkeep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/main/resources/socketchat_en.properties b/src/main/resources/socketchat_en.properties new file mode 100644 index 0000000000000000000000000000000000000000..c244be2e99eb1af20cae2b02a8a6506d2bcf17d9 --- /dev/null +++ b/src/main/resources/socketchat_en.properties @@ -0,0 +1,4 @@ +connection.errors.generalConnectionFailure = Connection failed +connection.prompts.isHost = Are you the chat host? +connection.responses.yes = Y +connection.responses.no = N \ No newline at end of file