2015年3月4日水曜日

開発環境

  • OS X Yosemite - Apple (OS)
  • Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
  • Java (プログラミング言語)
  • javac (コンパイラ)
  • java (application launcher)

Head First Object-Oriented Analysis and Design: A Brain Friendly Guide to OOA&D (Brett McLaughlin (著)、 Gary Pollice (著)、 David West (著) 、 O'Reilly Media)のChapter 10.(The OOA&D Lifecycle: Putting It All Together)、PRINTING PUZZLE(No. 6994)を解いてみる。

その他参考書籍

PRINTING PUZZLE(No. 6994)

コード(BBEdit, Emacs)

SubwayPrinter.java

import java.io.*;
import java.util.*;

public class SubwayPrinter {
    private PrintStream out;

    public SubwayPrinter(OutputStream stream) {
        this.out = new PrintStream(stream);
    }
    
    public void printDirections(List<Connection> connections) {
        Connection connection = connections.get(0);
        Station station1 = connection.getStation1();
        Station station2 = connection.getStation2();
        String lineName = connection.getLineName();
        out.println("Start out at " + station1.getName() + ".");
        out.println("Get on the " + lineName + " heading towards " +
                    station2.getName() + ".");
        String lineName1 = lineName;
        for (int i = 1; i < connections.size(); i++) {
            connection = connections.get(i);
            station1 = connection.getStation1();
            station2 = connection.getStation2();
            lineName = connection.getLineName();
            if (lineName1.equals(lineName)) {
                out.println("Continue past " + station1.getName() + "...");
            } else {
                out.println("When you get to " + station1.getName() + ", " +
                            "get off the " + lineName1 + ".");
                out.println("Switch over to the " + lineName + ", " +
                            "heading towards + " + station2.getName() + ".");
                lineName1 = lineName;
            }
        }
        out.println("Get off at " + station2.getName() +
                    " and enjoy yourself!");
    }
}

入出力結果(Terminal)

$ javac SubwayPrinter.java
$

0 コメント:

コメントを投稿