这个作业属于哪个课程 | 2022年福大-软件工程;软件工程实践-W班 |
---|---|
这个作业要求在哪里 | 软工实践第二次作业 |
这个作业的目标 | 冬奥会事数据查询:学习JSON数据爬取和读取;熟悉文件读取;了解软件开发基本步骤,学习单元测试和性能分析; |
其他参考文献 | JProfiler,idea中如何生成jar文件,java读写文件操作 |
目录
本博客所提到的数据爬取仅作为教学研究使用,无其他目的
1.Gitcode项目地址。
2.PSP表格:
PSP | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | ||
• Estimate | • 估计这个任务需要多少时间 | 25 | 30 |
Development | • 开发 | ||
• Analysis | • 需求分析 (包括学习新技术) | 90 | 240 |
• Design Spec | • 生成设计文档 | 15 | 25 |
• Design Review | • 设计复审 | 20 | 25 |
• Coding Standard | • 代码规范 (为目前的开发制定合适的规范) | 15 | 30 |
• Design | • 具体设计 | 20 | 25 |
• Coding | • 具体编码 | 270 | 360 |
• Code Review | • 代码复审 | 25 | 30 |
• Test | • 测试(自我测试,修改代码,提交修改) | 120 | 480 |
Reporting | 报告 | ||
• Test Repor | • 测试报告 | 60 | 120 |
• Size Measurement | • 计算工作量 | 10 | 25 |
• Postmortem & Process Improvement Plan | • 事后总结, 并提出过程改进计划 | 35 | 35 |
合计 | 705 | 1545 |
3.解题思路描述
3.1明确题目要求:
- 读取Json数据并输出奖牌总榜
- 读取Json数据并输出每日赛程
- 读取input.txt中的指令以及将最终的数据写入output.txt
Q1:对缺失的比赛数据爬取
在央视冬奥专栏官网,打开 开发者工具 页面,查看并寻找到所需日期的比赛数据,将所需数据通过工具网站转换成我们所需的Json格式数据。
Q2:解析JSON数据
通过导入google 的 Gson第三方库来实现JSON数据的解析
Q3:如何读取指令文件中命令和将数据写入目标文件
使用InputStreamReader和OutputStreamWriter函数进行文件的读写操作,再根据具体的要求选择是“追加写”还是"重写"。
4.设计实现过程
4.1类的分布
4.2函数分布
4.3关键函数流程图
5.展示出项目关键代码,并解释思路与注释说明。
5.1判断指令合法性
//利用数组【i】逐个输出第i行的文件指令
ArrayList<String> arrayList=fileTest.readFileByLine(input);
for (int i = 0; i < arrayList.size(); i++) {
//逐行实现命令
//空行跳过
if (arrayList.get(i).trim().equals("")||arrayList.get(i).length()==0) {
System.out.println("空行命令");
}
//total指令
else if (arrayList.get(i).trim().equals("total")) {
System.out.println(arrayList.get(i).trim());
new ReadJson().ReadMedals(output);
}
else if (arrayList.get(i).length()>=12) {
String str1=arrayList.get(i).replaceAll(" ","").substring(0,8);
String str2=arrayList.get(i).replaceAll(" ","").substring(8,12);
if (str1.equals("schedule")){
//判断日期是否合法
if (str2.substring(0,2).equals("02")) {
int date=Integer.parseInt(str2.substring(2,4));
System.out.println(date);
if (date<=20&&date>=2) {
new ReadJson().ReadMatch(output,str2);
}
else {
new FileHandle().appendTFile(output,"N/A");
}
}
else{
new FileHandle().appendTFile(output,"N/A");
}
}
}
else{
new FileHandle().appendTFile(output,"Error");
}
}
5.2Json数据解析
JsonParser parse = new JsonParser();
try {
JsonObject json = (JsonObject)parse.parse(new FileReader("data\\"+date+".json"));
JsonObject result = json.get("data").getAsJsonObject();
JsonArray futureArray = result.get("matchList").getAsJsonArray();
for (int i = 0; i < futureArray.size(); ++i) {
JsonObject subObj = futureArray.get(i).getAsJsonObject();
String homename=subObj.get("homename").getAsString();
String awayname=subObj.get("awayname").getAsString();
//将从JSON文件读出来的数据分别存4个字符串中
//比赛时间:
String str1="time:" + subObj.get("startdatecn").getAsString().substring(11,16);
//...........
}
catch(){
//.....
}
6.单元测试
6.1 测试结果:
6.2覆盖率
7.性能分析及改进的思路
改进前:运行10w条指令(8成正确指令)的命令文件,耗时678000ms
8.异常处理
try {
File file = new File(name);
InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file));
BufferedReader bf = new BufferedReader(inputReader);
// 按行读取字符串
String str;
while ((str = bf.readLine()) != null) {
arrayList.add(str);
}
bf.close();
inputReader.close();
} catch (IOException e) {
e.printStackTrace();
}
9.心得体会
- 要学会分析需求以及逐步分解问题,一步步寻找解决方法;
- 学习到了新的知识,更加意识到知识的匮乏;
- 程序的编写很重要,但是程序发布前程序猿的单元测试和性能分析同样要加以重视;