Created
August 8, 2012 04:51
-
-
Save mattn/3292173 to your computer and use it in GitHub Desktop.
業務プログラマがFizzBuzz書いたらどうなるか ( ref: http://d.hatena.ne.jp/irof/20120808/p1 )
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
<?xml version="1.0" encoding="UTF-8"?> | |
<classpath> | |
<classpathentry kind="src" path="src/main/java"/> | |
<classpathentry kind="src" path="src/test/java"/> | |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | |
<classpathentry kind="lib" path="/usr/share/java/commons-logging-1.1.1.jar"/> | |
<classpathentry kind="lib" path="/usr/share/java/junit4.jar"/> | |
<classpathentry kind="output" path="bin"/> | |
</classpath> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<projectDescription> | |
<name>FizzBuzz</name> | |
<comment></comment> | |
<projects> | |
</projects> | |
<buildSpec> | |
<buildCommand> | |
<name>org.eclipse.jdt.core.javabuilder</name> | |
<arguments> | |
</arguments> | |
</buildCommand> | |
</buildSpec> | |
<natures> | |
<nature>org.eclipse.jdt.core.javanature</nature> | |
</natures> | |
</projectDescription> |
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
apply { | |
plugin 'application' | |
plugin 'java' | |
} | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile 'commons-logging:commons-logging:1.1.1' | |
testCompile group: 'junit', name: 'junit', version: '4.+' | |
} | |
jar { | |
baseName = 'fizzbuzz' | |
} | |
javadoc { | |
options.encoding = "UTF-8" | |
project.configure(options) { | |
memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED | |
charSet = "UTF-8" | |
docTitle = "FizzBuzz Convertion System" | |
windowTitle = "FizzBuzz Convertion System" | |
header = "<b>FizzBuzz Convertion System</b>" | |
author = "true" | |
use = "true" | |
} | |
} | |
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' | |
mainClassName = "net.kaoriya.mattn.joke.FizzBuzz" | |
manifest.mainAttributes("Main-Class" : "net.kaoriya.mattn.joke.FizzBuzz") |
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
/** | |
* FizzBuzz変換システム. | |
* | |
* <pre> | |
* システムID: HoganSystem | |
* プログラムID: FizzBuzz | |
* </pre> | |
* | |
* @author mattn | |
* @version 1.2 | |
*/ | |
package net.kaoriya.mattn.joke; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.IOException; | |
import java.util.regex.Pattern; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
/** | |
* FizzBuzz変換クラス. | |
* | |
* <p>FizzBuzz変換処理を提供する</p> | |
*/ | |
public final class FizzBuzz { | |
/** Fizzになる値(3). */ | |
private static final int NUMBER_FIZZ = 3; | |
/** Buzzになる値(5). */ | |
private static final int NUMBER_BUZZ = 5; | |
/** FizzBuzzになる値(15). */ | |
private static final int NUMBER_FIZZBUZZ = 15; | |
/** 終了コード(入力値が数値以外). */ | |
private static final int ERROR_NOT_NUMBER = -1; | |
/** 終了コード(入力値が0以下). */ | |
private static final int ERROR_NEGATIVE = 1; | |
/** 終了コード(正常終了). */ | |
private static final int ERROR_SUCCESS = 0; | |
/** ロガー. */ | |
private Log log = LogFactory.getLog(FizzBuzz.class); | |
/** | |
* デフォルトコンストラクタ. | |
*/ | |
public FizzBuzz() { | |
} | |
/** | |
* FizzBuzz変換処理. | |
* | |
* <p>FizzBuzz変換処理を実行する</p> | |
* <ul> | |
* <li>1.1: 入力値を受け取る。</li> | |
* <ul> | |
* <li>(1) 入力値が数値でない場合</li> | |
* <li>(2) 入力値が0以下の場合</li> | |
* <li>(3) 処理を継続する</li> | |
* </ul> | |
* <li>1.2: 1.1で受け取った入力値を変換する</li> | |
* <ul> | |
* <li>(1) 15の倍数の場合、FizzBuzzに変換する</li> | |
* <li>(2) 3の倍数の場合、Fizzに変換する</li> | |
* <li>(3) 5の倍数の場合、Buzzに変換する</li> | |
* <li>(4) 上記以外の場合、そのまま文字列に変換する</li> | |
* </ul> | |
* <li>1.3: 変換した文字列を表示する。</li> | |
* <li>1.4: 処理を正常終了する。</li> | |
* </ul> | |
* @return 終了コード(-1: 入力値が数値以外, 1: 入力値が0以下). | |
* @throws IOException 標準入力読み取りに失敗した際に発生 | |
*/ | |
public int runFizzBuzzConvert() throws IOException { | |
// 1.1: 入力値を受け取る。 | |
InputStreamReader isr = new InputStreamReader(System.in); | |
BufferedReader br = new BufferedReader(isr); | |
String line = br.readLine(); | |
// (1) 入力値が数値でない場合 | |
Pattern p = Pattern.compile("^\\d+$"); | |
if (line == null || !p.matcher(line).find()) { | |
log.error("入力文字列が数値ではありません:" + line); | |
return ERROR_NOT_NUMBER; | |
} | |
// (2) 入力値が0以下の場合 | |
int number = -1; | |
try { | |
number = Integer.parseInt(line); | |
} catch (NumberFormatException e) { | |
log.error("入力文字列が数値ではありません:" + line); | |
return ERROR_NOT_NUMBER; | |
} | |
if (number <= 0) { | |
log.error("入力文字列が0以下です:" + number); | |
return ERROR_NEGATIVE; | |
} | |
// (3) 処理を継続する | |
// 1.2: 1.1で受け取った入力値を変換する | |
String output = null; | |
if (number % NUMBER_FIZZBUZZ == 0) { | |
// (1) 15の倍数の場合、FizzBuzzに変換する | |
output = "FizzBuzz"; | |
} else if (number % NUMBER_FIZZ == 0) { | |
// (2) 3の倍数の場合、Fizzに変換する | |
output = "Fizz"; | |
} else if (number % NUMBER_BUZZ == 0) { | |
// (3) 5の倍数の場合、Buzzに変換する | |
output = "Buzz"; | |
} else { | |
// (4) 上記以外の場合、そのまま文字列に変換する | |
output = "" + number; | |
} | |
// 1.3: 変換した文字列を表示する。 | |
System.out.println(output); | |
// 1.4: 処理を正常終了する。 | |
return ERROR_SUCCESS; | |
} | |
/** | |
* メイン. | |
* | |
* @param args コマンドライン引数. | |
*/ | |
public static void main(String[] args) { | |
try { | |
// FizzBuzz変換クラスを生成 | |
FizzBuzz fizzbuzz = new FizzBuzz(); | |
// FizzBuzz変換処理を実行 | |
fizzbuzz.runFizzBuzzConvert(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
import java.io.BufferedOutputStream; | |
import java.io.BufferedReader; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.InputStream; | |
import java.io.IOException; | |
import java.io.PrintStream; | |
import java.io.StringReader; | |
import net.kaoriya.mattn.joke.FizzBuzz; | |
/** | |
* FizzBuzzテストクラス. | |
*/ | |
public class FizzBuzzTest { | |
/** 標準入力退避用. */ | |
private InputStream in; | |
/** 標準出力退避用. */ | |
private PrintStream out; | |
/** | |
* テスト開始前処理. | |
* | |
* <p>標準入力および標準出力を退避する</p> | |
*/ | |
@Before | |
public void setUp() { | |
in = System.in; | |
out = System.out; | |
} | |
/** | |
* テスト開始後処理. | |
* | |
* <p>標準入力および標準出力を復帰する</p> | |
*/ | |
@After | |
public void tearDown() { | |
System.setIn(in); | |
System.setOut(out); | |
} | |
/** | |
* テスト実行. | |
* | |
* <ul> | |
* <li>標準出力を差し替える</li> | |
* <li>標準入力を引数nを数値化した入力に差し替える</li> | |
* <li>FizzBuzzクラスを生成する</li> | |
* <li>FizzBuzz変換処理を実行する</li> | |
* <li>標準出力された文字列と引数sで検証する</li> | |
* </ul> | |
* | |
* @param n FizzBuzz変換処理に与える数値 | |
* @param s 期待する結果 | |
* @throws IOException 標準入出力処理に失敗した場合に発生 | |
*/ | |
private void doFizzBuzz(int n, String s) throws IOException { | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
BufferedOutputStream bos = new BufferedOutputStream(baos); | |
System.setOut(new PrintStream(bos)); | |
byte[] input = ("" + n + "\n").getBytes(); | |
System.setIn(new ByteArrayInputStream(input)); | |
FizzBuzz fb = new FizzBuzz(); | |
fb.runFizzBuzzConvert(); | |
System.out.flush(); | |
StringReader sr = new StringReader(baos.toString()); | |
String result = new BufferedReader(sr).readLine(); | |
assertEquals(s, result); | |
} | |
/** | |
* FizzBuzz変換処理テスト. | |
* | |
* @throws IOException 標準入出力処理に失敗した場合に発生 | |
*/ | |
@Test | |
@SuppressWarnings("unchecked") | |
public void testFizzBuzz() throws IOException { | |
// CHECKSTYLE:OFF | |
doFizzBuzz(1, "1"); | |
doFizzBuzz(2, "2"); | |
doFizzBuzz(3, "Fizz"); | |
doFizzBuzz(4, "4"); | |
doFizzBuzz(5, "Buzz"); | |
doFizzBuzz(6, "Fizz"); | |
doFizzBuzz(7, "7"); | |
doFizzBuzz(8, "8"); | |
doFizzBuzz(9, "Fizz"); | |
doFizzBuzz(10, "Buzz"); | |
doFizzBuzz(11, "11"); | |
doFizzBuzz(12, "Fizz"); | |
doFizzBuzz(13, "13"); | |
doFizzBuzz(14, "14"); | |
doFizzBuzz(15, "FizzBuzz"); | |
doFizzBuzz(16, "16"); | |
// CHECKSTYLE:ON | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ぜひFizzBuzz Enterprise Editionもご参照ください。
https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition