111
社区成员




这个作业属于哪个课程 | https://bbs.csdn.net/forums/2401_CS_SE_FZU |
---|---|
这个作业要求在哪里 | https://bbs.csdn.net/topics/619294904 |
这个作业的目标 | 完成对2024年巴黎奥运会相关数据的收集,并实现一个能够对国家排名及奖牌个数统计的控制台程序。 |
其他参考文献 | 无 |
PSP | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 20 | 20 |
• Estimate | • 估计这个任务需要多少时间 | 20 | 20 |
Development | 开发 | 2180 | 2040 |
• Analysis | • 需求分析 (包括学习新技术) | 1200 | 1150 |
• Design Spec | • 生成设计文档 | 20 | 20 |
• Design Review | • 设计复审 | 10 | 10 |
• Coding Standard | • 代码规范 (为目前的开发制定合适的规范) | 30 | 30 |
• Design | • 具体设计 | 60 | 40 |
• Coding | • 具体编码 | 600 | 560 |
• Code Review | • 代码复审 | 60 | 50 |
• Test | • 测试(自我测试,修改代码,提交修改) | 200 | 180 |
Reporting | 报告 | 300 | 265 |
• Test Repor | • 测试报告 | 200 | 200 |
• Size Measurement | • 计算工作量 | 30 | 15 |
• Postmortem & Process Improvement Plan | • 事后总结, 并提出过程改进计划 | 70 | 50 |
合计 | 2500 | 2325 |
先爬取奥运会的网站数据(赛程数据,奖牌排行榜),然后运用数据进行数据处理编程。
项目分为三个大类,两个小类。分别为,主函数OlympicSearchMain,指令函数TotalCommand,指令函数ScheduleCommand,以及Medal和Course两个数据对象。主要通过主函数调用两个指令函数来完成,功能1:输出所有选手信息,功能2:输出每日赛程。
主函数OlympicSearchMain:
1、创建输入输出文件,并处理异常。
try {
if (!out_file.exists()) {
out_file.createNewFile();// 创建输出文件
}
writer = new FileWriter(out_file, false);
writer.append("");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != writer)
writer.close();
}
try {
if (!input_file.exists()) {
input_file.createNewFile();// 创建输入文件
Files.write(Paths.get(out_file.getPath()), "指令集为空!".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != writer)
writer.close();
}
try (Scanner scanner = new Scanner(input_file,"UTF-8")) {
if (!scanner.hasNextLine()) {
Files.write(Paths.get(out_file.getPath()), "指令集为空!".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}
2、读取输入文件的指令,并处理异常
try (Scanner scanner = new Scanner(input_file,"UTF-8")) {
if (!scanner.hasNextLine()) {
Files.write(Paths.get(out_file.getPath()), "指令集为空!".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] strs=line.split(" ");
if (strs[0].equals("total")) {
//若为total,则调用TotalCommand类函数
TotalCommand t = new TotalCommand(output_txt);
t.Command();
}else if (strs.length==2&&strs[0].equals("schedule")) {
//若一行的第一个字符串(以" "为分隔符)为schedule,则判断第二个日期字符串,若日期字符串正确,则调用 ScheduleCommand类函数,否则输出日期格式错误。
ScheduleCommand s = new ScheduleCommand(output_txt, strs[1]);
if (!s.isBoolean()) {
Files.write(Paths.get(out_file.getPath()), "N/A\n-----\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}else{
s.Command();
}
}else{
//都不是则输出指令错误
Files.write(Paths.get(out_file.getPath()), "Error\n-----\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}
}
}
指令代码展示,以TotalCommand 类为例。
通过Gson将json转化为java对象,用Medal对象储存数据。
然后写入输出文件
public void Command() throws IOException{
File out_file = new File(output_txt);
FileReader fileReader;
fileReader = new FileReader("src\\main\\resources\\data\\medal.json");
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Medal>>() { }.getType();
ArrayList<Medal> list = gson.fromJson(fileReader, type);
list.forEach(medal -> {
String line1 = "rank" + medal.getRank() + ":" + medal.getCountryid()+"\n";
String line2 = "gold:" + medal.getGold()+"\n";
String line3 = "silver:" + medal.getSilver()+"\n";
String line4 = "bronze:" + medal.getBronze()+"\n";
String line5 = "total:" + medal.getCount()+"\n";
String line6 = "-----"+"\n";
try {
Files.write(Paths.get(out_file.getPath()), line1.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line2.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line3.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line4.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line5.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line6.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
});
}
public void Command() throws IOException{
File out_file = new File(output_txt);
FileReader file_reader= new FileReader("src\\main\\resources\\data\\schedule"+" "+date+".json",StandardCharsets.UTF_8);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Course>>() { }.getType();
ArrayList<Course> list = gson.fromJson(file_reader, type);
list.forEach(course -> {
String line1 = "time:" + course.getStardate()+"\n";
String line2 = "sport:" + course.getItemcodename()+"\n";
String line3;
if (course.getHomename().length()<1) {
line3 = "name:" + course.getTitle()+"\n";
}else{
line3 = "name:" + course.getTitle()+" "+course.getHomename() + "VS"+course.getAwayname()+"\n";
}
String line4 = "venue:" + course.getVenuename()+"\n";
String line5 = "-----"+"\n";
try {
Files.write(Paths.get(out_file.getPath()), line1.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line2.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line3.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line4.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
Files.write(Paths.get(out_file.getPath()), line5.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
});
}
改进后:
public void Command() throws IOException{
File outFile = new File(output_txt);
String filePath = "src\\main\\resources\\data\\schedule"+" "+date+".json";
try (FileReader fileReader = new FileReader(filePath, StandardCharsets.UTF_8)) {
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Course>>() {}.getType();
ArrayList<Course> list = gson.fromJson(fileReader, type);
StringBuilder content = new StringBuilder();
for (Course course : list) {
content.append("time:").append(course.getStartdate()).append("\n");
content.append("sport:").append(course.getItemcodename()).append("\n");
content.append("name:");
if (course.getHomename().isEmpty()) {
content.append(course.getTitle()).append("\n");
} else {
content.append(course.getTitle()).append(" ").append(course.getHomename()).append("VS").append(course.getAwayname()).append("\n");
}
content.append("venue:").append(course.getVenuename()).append("\n");
content.append("-----\n");
}
Files.write(Paths.get(outFile.getPath()), content.toString().getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
}
}
代码覆盖率:
这次作业是一次综合能力很强的作业,要求的内容基本需要新学,包括爬虫,maven依赖管理,json数据处理等等,对我来说是一项很大的挑战。一开始起步很艰难,学了很多关于爬虫的知识,也询问了很多同学的有关经验。到后面编写代码的过程才开始逐步上手,尽管还是举步维艰。经过这次作业,我认识到了很多自己的不足之处,也收获到了很多新知识,新领域,总得来说,对这个学期是一个很好的开始,对于下一次的作业,更要加倍努力。