Head First Object-Oriented Analysis and Design
A Brain Friendly Guide to OOA&D
(O'Reilly Media)
Brett McLaughlin (著), Gary Pollice (著)
David West (著)
邦訳
Head Firstオブジェクト指向分析設計
頭とからだで覚えるオブジェクト指向の基本
長瀬 嘉秀 (編集), 永田 渉 (編集)
株式会社テクノロジックアート (翻訳)
開発環境
- 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、FEATURE PUZZLE(No. 6228)を解いてみる。
その他参考書籍
FEATURE PUZZLE(No. 6228)
コード(BBEdit, Emacs)
UnitTester.java
import java.util.List;
import java.util.ArrayList;
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");
unitTester.testCreateUnits();
List<Unit> unitList = new ArrayList<Unit>();
unitList.add(unit);
unitList.add(new Unit(2));
unitTester.testCreateUnitsFromList(unitList);
Units units = new Units(unitList);
unitTester.testAddUnit(units);
unitTester.testRemoveUnit(units, unit);
}
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);
}
public void testCreateUnits() {
System.out.println("Creat units");
Units units = new Units();
if (units.getUnits().isEmpty()) {
System.out.println("Test passed");
} else {
System.out.println("Test failed: ");
}
}
public void testCreateUnitsFromList(List<Unit> unitList) {
System.out.println("Create units from list");
Units units = new Units(unitList);
if (!units.getUnits().isEmpty()) {
System.out.println("Test passed");
} else {
System.out.println("Test failed: ");
}
}
public void testAddUnit(Units units) {
System.out.println("Adding unit");
int n = units.getUnits().size();
Unit unit = new Unit(1);
units.addUnit(unit);
if (units.getUnits().size() == n + 1) {
System.out.println("Test passed");
} else {
System.out.println("Test failed");
}
}
public void testRemoveUnit(Units units, Unit unit) {
System.out.println("Removing unit");
int n = units.getUnits().size();
units.removeUnit(unit);
if (units.getUnits().size() == n - 1) {
System.out.println("Test passed");
} else {
System.out.println("Test failed");
}
}
}
Units.java
import java.util.List;
import java.util.ArrayList;
public class Units {
private List<Unit> units;
public Units() {
this.units = new ArrayList<Unit>();
}
public Units(List<Unit> units){
this.units = units;
}
public List<Unit> getUnits() {
return units;
}
public void addUnit(Unit unit) {
units.add(unit);
}
public void removeUnit(Unit unit) {
units.remove(unit);
}
}
入出力結果(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 Creat units Test passed Create units from list Test passed Adding unit Test passed Removing unit Test passed $
0 コメント:
コメントを投稿