代码地址:https://github.com/maymaymaymaymay/WordCount

如下是PSP表格:

PSP2.1表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 30  20

· Estimate

· 估计这个任务需要多少时间

 30  20

Development

开发

 740  910

· Analysis

· 需求分析 (包括学习新技术)

 120  200

· Design Spec

· 生成设计文档

 30  30

· Design Review

· 设计复审 (和同事审核设计文档)

 20  20

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 30  60

· Design

· 具体设计

 60  60

· Coding

· 具体编码

 300  360

· Code Review

· 代码复审

 120  120

· Test

· 测试(自我测试,修改代码,提交修改)

 60  60

Reporting

报告

 180  270

· Test Report

· 测试报告

 60  120

· Size Measurement

· 计算工作量

 60  120

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 60  30
 

合计

 960  1200

 

解题思路:

将问题细化,如基本功能,知识要点可分为:

1、如何读取一个文件,并将其转为字符串

2、处理获得的字符串:读取字符数,通过判断逗号和空格得到单词书,读取字符串的行数

3、将内容输入到指定文件

4、如何将Java文件转化为exe文件

5、获取main函数参数之后进行处理,通过对比参数与相关指令从而决定调用那个函数

 

 程序设计实现过程:

 代码涉及的类有:

readFileChars(用于读取指定文件中的字符数)

readFileWords(用于读取指定文件中的字符数)

readFileLine(用于读取指定文件中内容的行数)

writeToFile(用于把指定内容写入指定文件中)

readMoreData(用于获取指定文件的代码行/注释行/空行)

searchFiles(用于遍历当前目录)

 

由主函数的参数获得用户输入的是哪个指令(如-c,-w,-l,-o等),以此决定调用哪个函数

 

代码说明:

readFileChars()

//读取文件中的字符数   -c
	public static String readFileChars(String fileName) {
		File file=new File(fileName);
		long len=file.length();
		String text=fileName+",字符数:"+len+"\r\n";
		System.out.println(text);
		//textToFile(text);
		return text;
	}


readFileWords()

//计算单词数的过程,除开逗号和空格在开头和结尾,或者两者连续出现的情况外,
//每次出现逗号和空格都记为一个单词的出现
for(int i=1;i<str.length()-1;i++){
				if((String.valueOf(str.charAt(i))).equals(",")||(String.valueOf(str.charAt(i))).equals(" ")){
					if(judgeWord==false){
						count++;
						judgeWord=true;
					}
					
				} else{
					judgeWord=false;
				}
			}

 

readFileLine()

while((s=bReader.readLine())!=null){
					sb.append(s);
					count++;
				}

 

writeToFile()

try {
			reader = new FileReader(tempFile);
			BufferedReader bReader=new BufferedReader(reader);
			StringBuilder sb=new StringBuilder();
			String s="";
			try {
				while((s=bReader.readLine())!=null){
					sb.append(s+"\r\n");
				}
				bReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			str=sb.toString();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		textToFile(str,fileName);   //将之前获得的内容写入指定文件

 

searchFile():

	public static List<File> searchFiles(String fileDir,String str){
		List<File> fileList=new ArrayList<File>();
		File file=new File(fileDir);
		File[] files=file.listFiles();

		for(File f:files){
			if(f.isFile()){
				fileList.add(f);
			}else if(f.isDirectory()){
				//System.out.println(f.getAbsolutePath());
				searchFiles(f.getAbsolutePath(),str);
			}
		}
//		for(File f1:fileList){
//			if(f1.getName().endsWith(str)){
//				
//			System.out.println(f1.getName());
//			}
//		}
		return fileList;
		
	}

  

 

 

测试用例的设计

总结白盒测试的各种方法:语句覆盖,判定覆盖,条件覆盖,路径覆盖等。

得出以下测试用例:

测试用例 预期结果
wc.exe -c -w - l note.txt 分别显示出note.txt中的字符数,单词数,行数
wc.exe -w -l -c result.txt 即使指令输入顺序不一样,但是结果与上面的一致
wc.exe -o result.txt 把以上输出的结果写入result.txt中
wc.exe -c -w - l note.txt -o result.txt  以上结果直接显示且同时添加到result.txt中
wc.exe -o  结果提示:-o后面需要加入文件名
 wc.exe -a note.txt  结果显示note.txt 的代码行/空行/注释行
 wc.exe -s *txt  显示当前目录下的txt文件
 wc.exe -a -s *txt  实现当前目录下的txt文件中的代码行/空行/注释行
 wc.exe  提示指令不完整

 

部分运行结果:

 

 

 

 

 

参考文献链接

一个更简单的Java转exe的方法:https://jingyan.baidu.com/article/ca00d56c59f881e99eebcff7.html

Java相关操作文件的例子:http://blog.csdn.net/liuweiyuxiang/article/details/69487326

Java判断指定字符:http://blog.csdn.net/yeyingss/article/details/53690098