Skip to content

Instantly share code, notes, and snippets.

@davelnewton davelnewton/GuessChar2.java Secret
Created Mar 9, 2013

Embed
What would you like to do?
Flattening a Java class; no other refactorings.
public class GuessChar2 {
public boolean guessCharacter(char guess) throws A8InvalidInputException, A8AlreadyGuessedException {
String sGuess = "" + guess;
if (invalidCharacter(guess)) { // Use the string?
throw new A8InvalidInputException(sGuess);
}
guess = Character.toLowerCase(guess); // Modifying a parameter?
if (guessedCharacters.contains(guess) ) {
throw new A8AlreadyGuessedException(sGuess);
}
numberOfGuessesLeft--;
guessedCharacters.add(guess);
return keyPhrase.contains(sGuess);
}
}
package com.davelnewton.guess;
public class GuessChar3 {
public boolean guessCharacter(char guess) throws A8InvalidInputException, A8AlreadyGuessedException {
assertValidGuess(guess);
assertNotGuessed(guess);
return processValidGuess(guess);
}
public void assertValidGuess(char guess) throws A8InvalidInputException {
if (invalidCharacter(guess)) {
throw new A8InvalidInputException("" + guess);
}
}
public void assertNotGuessed(char guess) throws A8AlreadyGuessedException {
guess = Character.toLowerCase(guess);
if (guessedCharacters.contains(guess) ) {
throw new A8AlreadyGuessedException("" + guess);
}
}
public boolean processValidGuess(char guess) {
numberOfGuessesLeft--;
guessedCharacters.add(guess);
return keyPhrase.contains("" + guess);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.