2014年12月15日月曜日

開発環境

Head First Object-Oriented Analysis and Design: A Brain Friendly Guide to OOA&D (Brett McLaughlin (著)、Gary Pollice (著)、David West (著)、 O'Reilly Media)のChapter 1. Well-Designed Apps Rock: Great Software Begins Here、DESIGN PUZZLE(No. 931)を解いてみる。

その他参考書籍

DESIGN PUZZLE(No. 931)

コード(BBEdit, Emacs)

FindGuitarTester.java

import java.util.List;
public class FindGuitarTester {
    public static void main(String[] args) {
        Inventory inventory = new Inventory();
        initializeInventory(inventory);

        GuitarSpec whatErinLikes =
            new GuitarSpec("fender", "Stratocastor", "electric", "Alder",
                           "Alder", 5);
                                                                     
        List<Guitar> guitars = inventory.search(whatErinLikes);
        if (guitars.isEmpty()) {
            System.out.println("Sorry, Erin, we have nothing for you.");
        } else {
            for (Guitar guitar: guitars) {
                GuitarSpec spec = guitar.getSpec();
                System.out.println("Erin, you might like this " +
                                   spec.getBuilder() + " " +
                                   spec.getModel() + " " + spec.getType() +
                                   " guitar:\n   " + spec.getBackWood() +
                                   " back and sides,\n   " +
                                   spec.getTopWood() +
                                   " top.\nYou can have it for only $" +
                                   guitar.getPrice() + "!");
            }
        }
    }

    private static void initializeInventory(Inventory inventory) {
        inventory.addGuitar("11277", 3999.95,
                            new GuitarSpec("Collings", "CJ", "acoustic",
                                           "Indian Rosewood", "Sitka", 5));
        inventory.addGuitar("V95693", 1499.95,
                            new GuitarSpec("Fender", "Stratocastor", "electric",
                                           "Alder", "Alder", 5));
        inventory.addGuitar("V9512", 1549.95,
                            new GuitarSpec("Fender", "Stratocastor", "electric",
                                           "Alder", "Alder", 5));
        inventory.addGuitar("122784", 5495.95,
                            new GuitarSpec("Martin", "D-18", "acoustic",
                                           "Mahogany", "Adirondack", 5));
        inventory.addGuitar("76531", 6295.95,
                            new GuitarSpec("Martin", "OM-28", " acoustic",
                                           "Brazilian Rosewood", "Adriondack",
                                           5));
        inventory.addGuitar("70108276", 2295.95,
                            new GuitarSpec("Gibson", "Les Paul", "electric",
                                           "Mahogany", "Maple", 5));
        inventory.addGuitar("82765501", 1890.95,
                            new GuitarSpec("Gibson", "SG '61 Reissue",
                                           "electric", "Mahogany", "Mahogany",
                                           5));
        inventory.addGuitar("77023", 6275.95,
                            new GuitarSpec("Martin", "D-28", "acoustic",
                                           "Brazilian Rosewood", "Adirondack",
                                           5));
        inventory.addGuitar("1092", 12995.95,
                            new GuitarSpec("Olson", "SJ", "acoustic",
                                           "Indian Rosewood", "Cedar", 5));
        inventory.addGuitar("566-62", 8999.95,
                            new GuitarSpec("Ryan", "Cathedral", "acoustic",
                                           "Cocobolo", "Cedar", 5));
        inventory.addGuitar("6 29584", 2100.95,
                            new GuitarSpec("PRS", "Dave Navarro Signature",
                                           "electric", "Mahogany", "Maple", 5));
    }
}

Guitar.java

public class Guitar {
    private String serialNumber, builder, model, type, backWood, topWood;
    private double price;
    private GuitarSpec spec;

    public Guitar(String serialNumber, double price, GuitarSpec spec) {
        this.serialNumber = serialNumber;
        this.price = price;
        this.spec = spec;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public double getPrice() {
        return price;
    }
    public void setPrice(float newPrice) {
        this.price = newPrice;
    }
    public GuitarSpec getSpec() {
        return spec;
    }
}

GuitarSpec.java

public class GuitarSpec {
    private String builder, model, type, backWood, topWood;
    private int numStrings;
    
    public GuitarSpec (String builder, String model, String type,
                       String backWood, String topWood, int numStrings) {
        this.builder = builder;
        this.model = model;
        this.type = type;
        this.backWood = backWood;
        this.topWood = topWood;
        this.numStrings = numStrings;
    }
    public String getBuilder() {
        return builder;
    }
    public String getModel() {
        return model;
    }
    public String getType() {
        return type;
    }
    public String getBackWood() {
        return backWood;
    }
    public String getTopWood() {
        return topWood;
    }
    public int getNumstring() {
        return numStrings;
    }
    public boolean match(GuitarSpec spec) {
        if (builder.toLowerCase().equals(spec.getBuilder().toLowerCase()) &&
            model.toLowerCase().equals(spec.getModel().toLowerCase()) &&
            type == spec.getType() &&
            backWood == spec.getBackWood() &&
            topWood == spec.getTopWood() &&
            numStrings == spec.getNumstring()) {
            return true;
        } else {
            return false;
        }
    }
}

Inventory.java

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Inventory {
    private List<Guitar> guitars;

    public Inventory() {
        guitars = new LinkedList<Guitar>();
    }

    public void addGuitar(String serialNumber, double price, GuitarSpec spec) {
        Guitar guitar = new Guitar(serialNumber, price, spec);
        guitars.add(guitar);
    }
    public Guitar getGuitar(String serialNumber) {
        for (Iterator i = guitars.iterator(); i.hasNext(); ) {
            Guitar guitar = (Guitar)i.next();
            if (guitar.getSerialNumber().equals(serialNumber)) {
                return guitar;
            }
        }
        return null;
    }
    public List<Guitar> search(GuitarSpec searechSpec) {
        List<Guitar> matchingGuitars = new LinkedList<Guitar>();        
        for (Iterator i = guitars.iterator(); i.hasNext();) {
            Guitar guitar = (Guitar)i.next();
            GuitarSpec guitarSpec = guitar.getSpec();
            if (guitarSpec.match(searechSpec)) {
                matchingGuitars.add(guitar);
            }
        }
        return matchingGuitars;
    }
}

入出力結果(Terminal)

$ jrun.sh FindGuitarTester
Erin, you might like this Fender Stratocastor electric guitar:
   Alder back and sides,
   Alder top.
You can have it for only $1499.95!
Erin, you might like this Fender Stratocastor electric guitar:
   Alder back and sides,
   Alder top.
You can have it for only $1549.95!
$

0 コメント:

コメントを投稿