软件工程实践第二次作业---文件读取

222100202李刘琛 2024-02-29 17:30:16
这个作业属于哪个课程2302软件工程社区
这个作业要求在哪里软件工程第二次作业--文件读取
这个作业的目标完成文件读取小练习
其他参考文献阿里巴巴Java开发手册》 工程师的能力评估和发展、单元测试和回归测试

目录

  • Gitcode项目地址
  • PSP表格
  • 1.解题思路描述
  • JSON文件读取与处理
  • 2.接口设计和实现过程
  • 项目结构
  • 3.核心功能接口设计
  • 4.关键代码展示
  • 5.性能改进
  • 6.单元测试
  • 7.异常处理
  • 8.心得体会

Gitcode项目地址

项目地址

PSP表格

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

1.解题思路描述

JSON文件读取与处理

在解析和处理json文件中,我用的是fastjson。Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象。
fastJson提供的json对象了JSONObject和json数组对象 JSONArray

数据说明
athletes:存储运动员信息
results:存储具体比赛结果信息

img

运动员信息

img

比赛信息
通过具体的比赛名找到对应的文件,比赛结果存储在Heats数组之下的Result数据(包括Fullname、Rank、TotalPoints、每次小比赛的得分DivePoints在Dives里)

img

2.接口设计和实现过程

项目结构

.
├─datas // json数据文件
└─src
├─codes
├─entity // 实体对象封装

3.核心功能接口设计

  1. 解析并获取运动员信息
  2. 解析并获取比赛项目结果

img

4.关键代码展示

主函数

public class DWASearch {
    public static void main(String[] args) throws IOException {

        if(args.length!=2)
        {
            System.out.println("输入参数数量错误");
            System.exit(-1);
        }

        //文件名错误的话报错
        if(!Files.exists(Paths.get(args[0]))||!Files.exists(Paths.get(args[1])))
        {
            System.out.println("文件不存在");
            //System.exit(-1);
        }
        //提取input.txt里面的内容
        String[] arr= FileUtils.ReadFromFile("src\\input.txt");
        //清空output.txt的内容
        FileUtils.ClearFile("src\\output.txt");
        for(String s : arr) {
            //写入output.txt
            doWrite(s,"src\\output.txt");
        }
    }
    public static void doWrite (String s,String writePath) throws IOException {
        //isNameValid判断input.txt的内容是否合法
        int flag=isNameValid(s);
        if(flag==-2)
        {
            FileUtils.WriteToFile("N/A\r\n"+"----\r\n",writePath);
        } else if (flag==-1) {
            FileUtils.WriteToFile("Error\r\n"+"----\r\n",writePath);
        } else if (flag==1) {
            List<Player> playerList=JsonParse.parsePlayers();
            FileUtils.WritePlayerToFile(playerList,writePath);
        } else if (flag==2) {
            s=s.substring(7);
            List<Result> resultList=JsonParse.parseResult(s);
            FileUtils.WriteResultToFile(resultList,writePath);
        }
    }
}

img


img

5.性能改进

分析:用FileWriter比较慢
解决:用BufferWriter封装FileWriter

FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer);

6.单元测试

部分测试:

img

img

7.异常处理

当用户输入参数不是两个时,提示输入参数数量错误
当用户输入的文件名错误时,提示文件不存在

if(args.length!=2)
        {
            System.out.println("输入参数数量错误");
            System.exit(-1);
        }

if(!readName.exists()||!writeName.exists())
        {
            System.out.println("文件不存在");
            System.exit(-1);
        }

try{
            String root = System.getProperty("user.dir");
            String filePath = root+"/src/data/athletes.json";
            String content = FileUtil.readString(filePath, StandardCharsets.UTF_8);
            List<Player> list = new ArrayList<>();
            for (JSONObject entries : JSONUtil.parseArray(content).jsonIter()) {
                JSONArray participations = entries.getJSONArray("Participations");
                for (JSONObject object : participations.jsonIter()) {
                    Player player = new Player();
                    player.setCountry(entries.getStr("CountryName"));
                    player.setFullName( object.getStr("PreferredLastName") + " "+ object.getStr("PreferredFirstName"));
                    player.setGender(object.getStr("Gender"));
                    list.add(player);
                }
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
            System.exit(0);
        }

8.心得体会

完成这次作业我学会了如何获取解析并处理json文件的数据,学会了json与类的序列化和反序列化,学会了文件的读入和写入。最开始完成代码的时候其实我函数的封装是一坨狗屎,但我偷懒觉得能跑就行了,结果自己写测试类的时候才发现函数不好好封装写测试非常麻烦,于是又回去改。所以我以后一定要好好封装函数。

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

122

社区成员

发帖
与我相关
我的任务
社区描述
FZU-SE
软件工程 高校
社区管理员
  • LinQF39
  • 助教-吴可仪
  • 一杯时间
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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