HQ9+をJavaで実装してみた

前ポストにあったesolang本の頭の方に書いてあったHQ9+言語を
Javaで実装してみました。
思うが侭に適当に実装したので、
ソースはかなり汚いし、スマートじゃないです。

実装間違いがありました。修正版の方を参照してください
HQ9+修正版 - 草木のにをいに誘われた

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FileReader file = null;
        BufferedReader source = null;
        Hq9 hq9 = new Hq9();

        try {
            file = new FileReader(args[0]);
            source = new BufferedReader(file);

            String read;
            String line = "";

            while ((read = source.readLine()) != null) {
                line = line + read + "\n";
            }

            hq9.setSource(line);
            for (int i = 0; i < line.length(); i++) {
                switch (line.charAt(i)) {
                    case 'H':
                        hq9.hello();
                        break;
                    case '9':
                        hq9.bottles();
                        break;
                    case '+':
                        hq9.setCount();
                        break;
                    case 'Q':
                        hq9.getSource();
                        break;
                }
            }
        } catch (Exception e) {
            System.err.println("エラーが発生しました");
        } finally {
            try {
                if (file != null) {
                    file.close();
                }
                if (source != null) {
                    source.close();
                }
            } catch (Exception ex) {
                System.err.println("エラーが発生しました");
            }
        }
    }
}

public class Hq9 {

    private String source;
    private int count = 0;
    private int qcount = 0;

    public int getCount() {
        return count;
    }

    public void setCount() {
        this.count++;
    }

    public void getSource() {
        if (qcount <= 0) {
            System.out.print(this.source);
            qcount++;
        }
    }

    public void setSource(String source) {
        this.source = source;
    }

    public void hello() {
        System.out.println("Hello world!");
    }

    public void bottles() {
        for (int i = 99; i >= 0; i--) {
            switch (i) {
                case 2:
                    System.out.println("2 bottles of beer on the wall, 2 bottles of beer.");
                    System.out.println("Take one down and pass around, 1 bottle of beer on the wall.");
                    System.out.println("");
                    break;
                case 1:
                    System.out.println("1 bottle of beer on the wall, 1 bottle of beer.");
                    System.out.println("Take one down and pass around, no more bottles of beer on the wall.");
                    System.out.println("");
                    break;
                case 0:
                    System.out.println("No more bottles of beer on the wall, no more bottles of beer.");
                    System.out.println("Go to the store and buy some more, 99 bottles of beer on the wall.");
                    break;
                default:
                    System.out.println(i + " bottles of beer on the wall, " + i + " bottles of beer.");
                    System.out.println("Take one down and pass around, " + (i - 1) + " bottle of beer on the wall.");
                    System.out.println("");
            }
        }
    }
}


実行するとこんな感じ。


対象コード:

+  Q
H

実行結果:

+  Q
H
Hello world!


まあ、これくらいのJavaプログラムならちゃっちゃと出来るので、
もうすでにやっている人も多いと思いますが。

それにしても、一年ぶりくらいにJava触ったらあまりの書けなさに絶望してしまった。