public class NumberWords {
public String toWords(int number) {
String result = "";
if (number == -1) {
result = "Number out of range";
}
if (number >= 0 && number <= 999) {
if (number == 0) {
System.out.println("NUMBER AFTER CONVERSION:\tZERO");
} else {
System.out.print("NUMBER AFTER CONVERSION:\t");
numberToWord(((number / 100) % 10), " HUNDRED");
numberToWord((number % 100), " ");
}
}else if(number >= 1000){
System.out.println("Number not in range");
}
if (number == 0) {
System.exit(number);
}
return result.trim();
}
public static void numberToWord(int num, String val) {
String ones[] = {" ", " ONE", " TWO", " THREE", " FOUR", " FIVE", " SIX", " SEVEN", " EIGHT", " NINE", " TEN", " ELEVEN", " TWELVE", " THIRTEEN", " FOURTEEN", " FIFTEEN", " SIXTEEN", " SEVENTEEN", " EIGHTEEN", " NINETEEN"
};
String tens[] = {" ", " ", " TWENTY", " THIRTY", " FOURTY", " FIFTY", " SIXTY", " SEVENTY", " EIGHTY", " NINETY"};
if (num > 19) {
System.out.print(tens[num / 10] + " " + ones[num % 10]);
} else {
System.out.print(ones[num]);
}
if (num > 0) {
System.out.print(val);
}
}
}
public class NumberWordsApplication {
private final NumberWords numberWords;
private final BufferedReader reader;
public NumberWordsApplication() {
numberWords = new NumberWords();
reader = new BufferedReader(new InputStreamReader(System.in));
}
public void execute() {
while (true) {
try {
System.out.print("\nPlease type a number between 1 and 999 OR (0 to exit) : ");
String value = reader.readLine();
int number = Integer.parseInt(value);
String toWords = numberWords.toWords(number);
} catch (NumberFormatException | IOException e) {
System.out.println("Invalid number");
}
}
}
public static void main(String[] args) {
new NumberWordsApplication().execute();
}
}
这是用于生成数字到单词的主类,例如57,然后输出应为“ 57”
我想为卡住的NumberWords类生成测试用例
使用TDD开发数字转单词应用程序
答案 0 :(得分:1)
由于这是关于测试驱动开发的(homework?)问题,因此第一步应该是:
ant
)来运行此测试。一旦启动并运行,您可以逐渐添加更多的测试用例,直到您的类(将是NumberWords
的简化版本)可以转换任何输入。
为了使应用程序可测试,最好不要使用System.exit()
,并尽量减少while(true)
中包含的代码。对于给定的分配,这很容易做到。
您似乎尝试以错误的顺序应用TDD,进行了后测试而不是先测试的开发。