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

Refactored for improved testability

parent 7c971ac1
No related branches found
No related tags found
No related merge requests found
......@@ -273,13 +273,32 @@ public class Chat {
PrintStream remoteOutput) {
// "Connection established. Host goes first."
System.out.println(bundle.getString("connection.info.ready"));
String message = "";
boolean keepTalking = true;
boolean myTurnToTalk = isHost;
try {
while (keepTalking) {
keepTalking = communicateOneMessage(localInput, remoteInput, localOutput, remoteOutput, myTurnToTalk);
myTurnToTalk = !myTurnToTalk;
}
}
/**
* Processes a single message from one chatter to the other.
*
* @param localInput a {@link java.io.BufferedReader} that gets user input from this end of the connection
* @param remoteInput a {@link java.io.BufferedReader} that gets user input from the other end of the connection
* @param localOutput a {@link java.io.PrintStream} that outputs to the user at this end of the connection
* @param remoteOutput a {@link java.io.PrintStream} that outputs to the user at the other end of the connection
* @param localMessage {@code true} if the message to be processed will originate locally, {@code false} if remotely
* @return {@code true} if the program should continue to execute after processing the message; {@code false}
* otherwise
*/
boolean communicateOneMessage(BufferedReader localInput, BufferedReader remoteInput,
PrintStream localOutput, PrintStream remoteOutput, boolean localMessage) {
String message = "";
boolean keepTalking = true;
try {
try {
if (myTurnToTalk) {
if (localMessage) {
message = localInput.readLine();
remoteOutput.println(encipher(message));
} else {
......@@ -299,14 +318,13 @@ public class Chat {
keepTalking = false;
}
if (keepTalking && keywords.contains(message)) {
keepTalking = handleKeyword(message, myTurnToTalk, localInput, localOutput);
}
myTurnToTalk = !myTurnToTalk;
keepTalking = handleKeyword(message, localMessage, localInput, localOutput);
}
} catch (IOException ioException) {
System.err.println("Connection dropped: " + ioException);
System.exit(1);
}
return keepTalking;
}
/**
......@@ -319,8 +337,10 @@ public class Chat {
* @param output a {@link java.io.PrintStream} for output to the user
* @return {@code true} if the program should continue to execute after processing the keyword; {@code false}
* otherwise
* @throws IOException if an I/O error occurs while reading user responses to prompts
*/
private boolean handleKeyword(String keyword, boolean localMessage, BufferedReader input, PrintStream output) {
boolean handleKeyword(String keyword, boolean localMessage, BufferedReader input, PrintStream output)
throws IOException {
if (keyword.equals(bundle.getString("communicate.keyword.exit"))) {
return false;
// Un-comment this next code block in step 3 of the assignment.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment