一、github地址
https://github.com/pobabyguazi/-wordcount1
二、PSP表格
|
PSP2.1 |
PSP阶段 |
预估耗时 (分钟) |
实际耗时 (分钟) |
|
Planning |
计划 |
10 | 15 |
|
· Estimate |
· 估计这个任务需要多少时间 |
500 | 410 |
|
Development |
开发 |
||
|
· Analysis |
· 需求分析 (包括学习新技术) |
10 | 10 |
|
· Design Spec |
· 生成设计文档 |
20 | 20 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
15 | 15 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 | 30 |
|
· Design |
· 具体设计 |
30 | 30 |
|
· Coding |
· 具体编码 |
120 | 90 |
|
· Code Review |
· 代码复审 |
30 | 10 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
90 | 100 |
|
Reporting |
报告 |
10 | 20 |
|
· Test Report |
· 测试报告 |
30 | 15 |
|
· Size Measurement |
· 计算工作量 |
20 | 20 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
10 | 15 |
|
合计 |
425 |
410 |
三、接口实现
本人实现的是输出类的接口
Output类函数作用为初始化传输流,初始化文件路径,初始化list列(保存单词及其频率)
public Output(String path,List<Map.Entry<String, Integer>> wordsFrequencyList) throws IOException { setPath(path); setWordsFrequencyList(wordsFrequencyList); FileWriter f = new FileWriter(getOutputFile()); setFw(f); }
saveResultIntoFile()函数的作用是将字符串保存在输出文件中(这里的字符串是指单词和其出现频率共同组成)
public void saveResultIntoFile(String str) throws IOException { if(!getOutputFile().exists())//输出文件“result.txt”不存在,则新建一个 getOutputFile().createNewFile(); getFw().write(str); }
printResult函数的作用是调用saveResultIntoFile()函数将出现频率前100的单词及其频率输出到result文件中
//把频率前100的单词,按从高到低输出,并存在文件result.txt中 public void printResult() throws IOException { System.out.println(getPath()); saveResultIntoFile(getPath()+"\r\n"); int count=0; String str; System.out.println(getWordsFrequencyList()); for (Map.Entry<String, Integer> entry : getWordsFrequencyList()) { //只输出value排序从高到低的前100个 if(count>=100) break; str=entry.getKey().toLowerCase() + " " + entry.getValue(); System.out.println(str); if (count==99||count==getWordsFrequencyList().size()-1)//使最后一个单词或者第100个单词没有多余的换行符 saveResultIntoFile(str); else { saveResultIntoFile(str); saveResultIntoFile("\r\n"); } count=count+1; } getFw().close(); }
最后是整合模块,实例化引用接口,测试用例
ublic class main { public static void main(String args[]) { try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw /* 读入TXT文件 */ Map<String, Integer> map = new HashMap<String, Integer>(); int count=1; String pathname = "C:\\Users\\pobaby\\Workspaces\\MyEclipse 2016 CI\\wordcount\\file20.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径 File filename = new File(pathname); // 要读取以上路径的input。txt文件 InputStreamReader reader = new InputStreamReader( new FileInputStream(filename)); // 建立一个输入流对象reader BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言 String line = ""; line = br.readLine(); String myPath=line; while ( (line = br.readLine()) != null) { String[] arr = line.split("\\s++"); int c = Integer.valueOf(arr[1]); Integer b=(int)c; map.put(arr[0],b); System.out.println(arr[0]); System.out.println(b); } List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); /* 写入Txt文件 */ System.out.println(map.size()); Output output=new Output(myPath,list); output.printResult(); } catch (Exception e) { e.printStackTrace(); } }
四、测试用例
在测试过程中建立20个输入文件,20个输出文件,分别为inputfile1.txt....inputfile20.txt,result1.txt.......result20.txt
由于整个模块的分支和判断不是很多,因此黑盒测试多一些,白盒测试相对少一些(其中一些测试用例用来测试初始化函数,一些用来测试输出函数,一些用来测试频率判断函数(对词组越界的处理))
黑盒测试主要有单词频率不是数字的情况,单词是字符,数字的情况,文件夹路径不对,单词后缺少频率,以及正常情况
(如果出现单词频率不是数字等状况,将不会输出)