Я знаю, что у вас уже есть ответ, но другое возможное решение - использовать ENUM.EG:
public class Dice
{
private enum DieFace
{
ONE('1', " \n * \n "),
TWO('2', "* \n \n *"),
THREE('3', "* \n * \n *"),
FOUR('4', "* *\n \n* *"),
FIVE('5', "* *\n * \n* *"),
SIX('6', "* *\n* *\n* *");
private char characterCode;
private String representation;
DieFace(char characterCode, String representation)
{
this.characterCode = characterCode;
this.representation = representation;
}
public static DieFace getDieFaceFromCharacterCode(char characterCode)
{
DieFace dieFaceFound = null;
for (DieFace dieFace : values())
{
if (dieFace.characterCode == characterCode)
{
dieFaceFound = dieFace;
break;
}
}
return dieFaceFound;
}
@Override
public String toString()
{
return this.representation;
}
}
public static String getDieFaceFromCharacter(char characterInput)
{
DieFace dieFace = DieFace.getDieFaceFromCharacterCode(characterInput);
return dieFace == null ? null : dieFace.toString();
}
}
А вот тест для класса:
public class DieTest
{
@Test
public void testGetOne()
{
String expectedResult = " \n * \n ";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('1'));
}
@Test
public void testGetTwo()
{
String expectedResult = "* \n \n *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('2'));
}
@Test
public void testGetThree()
{
String expectedResult = "* \n * \n *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('3'));
}
@Test
public void testGetFour()
{
String expectedResult = "* *\n \n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('4'));
}
@Test
public void testGetFive()
{
String expectedResult = "* *\n * \n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('5'));
}
@Test
public void testGetSix()
{
String expectedResult = "* *\n* *\n* *";
assertEquals(expectedResult, Dice.getDieFaceFromCharacter('6'));
}
@Test
public void testGetInvalid()
{
// < 1 is invalid
assertNull(Dice.getDieFaceFromCharacter('0'));
// invalid character (non-number)
assertNull(Dice.getDieFaceFromCharacter('a'));
// > 6 is invalid
assertNull(Dice.getDieFaceFromCharacter('7'));
}
}