关于除法功能的TODO列表
1、 基本功能
2、 除数不能为零
3、 格式化,保留小数点后两位
代码演进过程1:基本功能(列表第一项)
代码演进过程2:添加功能-除数不能为零(列表第二项)
代码演进过程3:添加功能-格式化(列表第三项)
代码演进过程4:重构-把格式化代码分离出去
重构依据:降低程序的耦合性
最终代码清单
[img][/img][img][/img][img][/img][img][/img]
1、 基本功能
2、 除数不能为零
3、 格式化,保留小数点后两位
代码演进过程1:基本功能(列表第一项)
/**测试代码:DivisionTest.java*/
public class DivisionTest extends TestCase {
//测试基本功能
public void testDivision() {
Division division = new Division();
//0除以1等于0
assertEquals(0.00, division.compute(1, 0));
//1除以1等于1
assertEquals(1.00, division.compute(1, 1));
//2除以1等于2
assertEquals(2.00, division.compute(1, 2));
}
}
/**Division.java*/
public class Division implements ICalculator {
//计算除法
public double compute(double divisor, double dividend) {
return dividend / divisor;
}
}
代码演进过程2:添加功能-除数不能为零(列表第二项)
/**测试代码:DivisionTest.java*/
public class DivisionTest extends TestCase {
//测试基本功能
public void testDivision() {
Division division = new Division();
//0除以1等于0
assertEquals(0.00, division.compute(1, 0));
//1除以1等于1
assertEquals(1.00, division.compute(1, 1));
//2除以1等于2
assertEquals(2.00, division.compute(1, 2));
}
//测试除数不能为零
public void testException() {
try {
Division division = new Division();
//错误:3除以0
division.compute(0, 3);
fail("should throw an exception but not");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
}
/**Division.java*/
public class Division implements ICalculator {
//计算除法
public double compute(double divisor, double dividend) {
//检查除数是否为零,若为真则抛出异常
if (divisor == 0)
throw new IllegalArgumentException("divisor cannot be zero");
return dividend / divisor;
}
}
代码演进过程3:添加功能-格式化(列表第三项)
/**测试代码:DivisionTest.java*/
public class DivisionTest extends TestCase {
//测试基本功能
public void testDivision() {
Division division = new Division();
//0除以1等于0
assertEquals(0.00, division.compute(1, 0));
//1除以1等于1
assertEquals(1.00, division.compute(1, 1));
//2除以1等于2
assertEquals(2.00, division.compute(1, 2));
//测试格式化
assertEquals(1.33, division.compute(3, 4));
}
//测试除数不能为零
public void testException() {
try {
Division division = new Division();
//错误:3除以0
division.compute(0, 3);
fail("should throw an exception but not");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
}
/**Division.java*/
public class Division implements ICalculator {
//计算除法
//此时格式化功能耦合在计算功能中
public double compute(double divisor, double dividend) {
DecimalFormat df = new DecimalFormat("#.##");
if (divisor == 0)
throw new IllegalArgumentException("divisor cannot be zero");
return Double.parseDouble(df.format(dividend / divisor));
}
}
代码演进过程4:重构-把格式化代码分离出去
重构依据:降低程序的耦合性
/**Division.java*/
public class Division implements ICalculator {
//计算除法
public double compute(double divisor, double dividend) {
if (divisor == 0)
throw new IllegalArgumentException("divisor cannot be zero");
return format(dividend / divisor);
}
//格式化运算结果:保留两位有效小数点
private double format(double value) {
DecimalFormat df = new DecimalFormat("#.##");
return Double.parseDouble(df.format(value));
}
}
最终代码清单
/**计算器接口:ICalculator.java*/
public interface ICalculator {
double compute(double arg1, double arg2);
}
/**重构后的Division.java*/
public class Division implements ICalculator {
//计算除法
public double compute(double divisor, double dividend) {
if (divisor == 0)
throw new IllegalArgumentException("divisor cannot be zero");
return format(dividend / divisor);
}
//格式化运算结果:保留两位有效小数点
private double format(double value) {
DecimalFormat df = new DecimalFormat("#.##");
return Double.parseDouble(df.format(value));
}
}
/**测试代码:DivisionTest.java*/
public class DivisionTest extends TestCase {
public void testDivision() {
Division division = new Division();
assertEquals(0.00, division.compute(1, 0));
assertEquals(1.33, division.compute(3, 4));
}
public void testExceptions() {
try {
Division division = new Division();
division.compute(0, 3);
fail("should throw an exception but not");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
}
}
[img][/img][img][/img][img][/img][img][/img]