2015年2月14日土曜日

開発環境

  • 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 9. Iterating and Testing: The Software is Still for the Customer、SHARPEN YOUR PENCIL(No. 6116)を解いてみる。

その他参考書籍

SHARPEN YOUR PENCIL(No. 6116)

コード(BBEdit, Emacs)

UnitTester.java

public class UnitTester {
    public static void main(String[] args){
        UnitTester unitTester = new UnitTester();
        Unit unit = new Unit(1);
        unitTester.testSetGetPropery(unit, "type", "infantry",
                                     "infantry");
        unitTester.testSetGetSpecificProperty(unit, "hitPoints", 25,
                                              25);
        unitTester.testChangeExistPropertyValue(unit, "hitPoints", 15,
                                                15);
        unitTester.testNonExistProperty(unit, "java");
    }
    
    public void testSetGetPropery(Unit unit, String type, Object value,
                                  Object output){
        System.out.println("Setting/Getting the type property.");
        unit.setProperty(type, value);
        if (unit.getProperty(type) == output){
            System.out.println("Test passed");
        } else {
            System.out.println("Test failed: " +
                               unit.getProperty(type));
        }
    }
    
    public void testSetGetSpecificProperty(Unit unit, String name,
                                           Object value,
                                           Object output) {
        System.out.println("Setting/Getting a unit-specific property");
        unit.setProperty(name, value);
        if (unit.getProperty(name) == output){
            System.out.println("Test passed");
        } else {
            System.out.println("Test failed: " +
                               unit.getProperty(name));
        }
    }

    public void testChangeExistPropertyValue(Unit unit, String name,
                                             Object value,
                                             Object output){
        System.out.println("Changing an existing property's value.");
        unit.setProperty(name, value);
        if (unit.getProperty(name) == output){
            System.out.println("Test passed");
        } else {
            System.out.println("Test failed: " +
                               unit.getProperty(name));            
        }
    }

    public void testNonExistProperty(Unit unit, String property) {
        System.out.println("Getting a non-existent property's value");
        try {
            Object value = unit.getProperty(property);
        } catch (RuntimeException e) {
            System.out.println("Test passed");
            return;
        }
        System.out.println("Test failed: " + property);
    }    
}

Unit.java

import java.util.List;
import java.util.Map;
import java.util.LinkedList;
import java.util.HashMap;

public class Unit {
    private String type;
    private int id;
    private String name;
    private List<Weapon> weapons;    
    private Map<String, Object> properties;
    
    public Unit(int id){
        this.id = id;
    }

    public int getId() {
        return id;
    }

    // public String getName(){}
    // public void setName(){}

    public void addWeapon(Weapon weapon){
        if (weapons == null) {
            weapons = new LinkedList<Weapon>();
        }
        weapons.add(weapon);
    }

    public List getWeapons() {
        return weapons;
    }

    public void setProperty(String property, Object value) {
        if (properties == null) {
            properties = new HashMap<String, Object>();
        }
        properties.put(property, value);
    }

    public Object getProperty(String property) {
        Object value = properties.get(property);
        if (value == null) {            
            throw new RuntimeException();
        }
        return value;
    }
}

入出力結果(Terminal)

$ jrun.sh UnitTester
Setting/Getting the type property.
Test passed
Setting/Getting a unit-specific property
Test passed
Changing an existing property's value.
Test passed
Getting a non-existent property's value
Test passed
$

0 コメント:

コメントを投稿