软件工程实践第二次作业

222200111李敬毅 2024-09-10 17:14:03
这个作业属于哪个课程https://bbs.csdn.net/forums/2401_CS_SE_FZU
这个作业要求在哪里https://bbs.csdn.net/topics/619294904
这个作业的目标完成对2024年巴黎奥运会相关数据的收集,并实现一个能够对国家排名及奖牌个数统计的控制台程序。
其他参考文献

目录

  • 1、Gitcode项目地址
  • 2、PSP表格
  • 3、解题思路描述
  • 4、接口设计和实现过程
  • 4.1 代码设计
  • 4.2 实现过程
  • 5、关键代码展示
  • 6、性能改进
  • 7、单元测试
  • 8、心得体会

1、Gitcode项目地址

项目地址

2、PSP表格

PSPPersonal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning计划2020
• Estimate• 估计这个任务需要多少时间2020
Development开发21802040
• Analysis• 需求分析 (包括学习新技术)12001150
• Design Spec• 生成设计文档2020
• Design Review• 设计复审1010
• Coding Standard• 代码规范 (为目前的开发制定合适的规范)3030
• Design• 具体设计6040
• Coding• 具体编码600560
• Code Review• 代码复审6050
• Test• 测试(自我测试,修改代码,提交修改)200180
Reporting报告300265
• Test Repor• 测试报告200200
• Size Measurement• 计算工作量3015
• Postmortem & Process Improvement Plan• 事后总结, 并提出过程改进计划7050
合计25002325

3、解题思路描述

先爬取奥运会的网站数据(赛程数据,奖牌排行榜),然后运用数据进行数据处理编程。

在这里插入图片描述

4、接口设计和实现过程

4.1 代码设计

项目分为三个大类,两个小类。分别为,主函数OlympicSearchMain,指令函数TotalCommand,指令函数ScheduleCommand,以及Medal和Course两个数据对象。主要通过主函数调用两个指令函数来完成,功能1:输出所有选手信息,功能2:输出每日赛程。

4.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);
                }
           }
        }

5、关键代码展示

指令代码展示,以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();
            }
        });      
    }

6、性能改进

  • 将所有的写入内容合并到一个StringBuilder中,最后一次性写入文件,减少文件操作次数。
    改进前:
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);
        }
    }
  • 优化了字符串拼接逻辑,提高了代码的可读性和性能。

7、单元测试

代码覆盖率:

在这里插入图片描述


单元测试

在这里插入图片描述

8、心得体会

这次作业是一次综合能力很强的作业,要求的内容基本需要新学,包括爬虫,maven依赖管理,json数据处理等等,对我来说是一项很大的挑战。一开始起步很艰难,学了很多关于爬虫的知识,也询问了很多同学的有关经验。到后面编写代码的过程才开始逐步上手,尽管还是举步维艰。经过这次作业,我认识到了很多自己的不足之处,也收获到了很多新知识,新领域,总得来说,对这个学期是一个很好的开始,对于下一次的作业,更要加倍努力。

...全文
86 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111

社区成员

发帖
与我相关
我的任务
社区描述
202401_CS_SE_FZU
软件工程 高校
社区管理员
  • FZU_SE_TeacherL
  • 言1837
  • 防震水泥
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧