-
-
Save davelnewton/258761cbf3143da4288c to your computer and use it in GitHub Desktop.
Flattening a Java class; no other refactorings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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