Skip to content
Snippets Groups Projects
Commit daeec119 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Fixed small DRY problem; fixed what would've been bug in student code

parent f422311d
No related branches found
No related tags found
No related merge requests found
...@@ -220,7 +220,7 @@ public class Chat { ...@@ -220,7 +220,7 @@ public class Chat {
PrintStream remoteOutput) { PrintStream remoteOutput) {
// "Connection established. Host goes first." // "Connection established. Host goes first."
System.out.println(bundle.getString("connection.info.ready")); System.out.println(bundle.getString("connection.info.ready"));
String message; String message = "";
boolean keepTalking = true; boolean keepTalking = true;
boolean myTurnToTalk = isHost; boolean myTurnToTalk = isHost;
try { try {
...@@ -229,13 +229,11 @@ public class Chat { ...@@ -229,13 +229,11 @@ public class Chat {
if (myTurnToTalk) { if (myTurnToTalk) {
message = localInput.readLine(); message = localInput.readLine();
remoteOutput.println(encipher(message)); remoteOutput.println(encipher(message));
keepTalking = !keywords.contains(message) || handleKeyword(message, localInput, localOutput);
} else { } else {
String encipheredMessage = remoteInput.readLine(); String encipheredMessage = remoteInput.readLine();
if (encipheredMessage != null) { if (encipheredMessage != null) {
message = decipher(encipheredMessage); message = decipher(encipheredMessage);
localOutput.println(message); localOutput.println(message);
keepTalking = !keywords.contains(message) || handleKeyword(message, localInput, localOutput);
} else { } else {
// "Received null message: lost connection to remote chatter. Terminating." // "Received null message: lost connection to remote chatter. Terminating."
localOutput.println(bundle.getString("communicate.error.nullMessageFromRemote")); localOutput.println(bundle.getString("communicate.error.nullMessageFromRemote"));
...@@ -247,6 +245,9 @@ public class Chat { ...@@ -247,6 +245,9 @@ public class Chat {
localOutput.println(bundle.getString("communicate.error.cannotSendMessage")); localOutput.println(bundle.getString("communicate.error.cannotSendMessage"));
keepTalking = false; keepTalking = false;
} }
if (keepTalking && keywords.contains(message)) {
keepTalking = handleKeyword(message, myTurnToTalk, localInput, localOutput);
}
myTurnToTalk = !myTurnToTalk; myTurnToTalk = !myTurnToTalk;
} }
} catch (IOException ioException) { } catch (IOException ioException) {
...@@ -255,12 +256,12 @@ public class Chat { ...@@ -255,12 +256,12 @@ public class Chat {
} }
} }
private boolean handleKeyword(String keyword, BufferedReader input, PrintStream output) { private boolean handleKeyword(String keyword, boolean fromRemote, BufferedReader input, PrintStream output) {
if (keyword.equals(bundle.getString("communicate.keyword.exit"))) { if (keyword.equals(bundle.getString("communicate.keyword.exit"))) {
return false; return false;
/* /*
} else if (keyword.equals(bundle.getString("communicate.keyword.setLocale"))) { } else if (keyword.equals(bundle.getString("communicate.keyword.setLocale"))) {
if (isHost) { if (fromRemote) {
Prompt user using output.println() (be sure to use i18n properties) Prompt user using output.println() (be sure to use i18n properties)
and get response using input.readLine(). Get the appropriate Locale and call and get response using input.readLine(). Get the appropriate Locale and call
setLocale( ... ); setLocale( ... );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment