寒假作业2
软件工程寒假作业2
| 这个作业属于哪个课程 | [班级][1] |
|---|---|
| 这个作业要求在哪里 | [作业要求][2] |
| 这个作业的目标 | 开发一个疫情统计程序 |
| 作业正文 | [寒假作业2(未修改)][3] |
| 其他参考文献 | ... |
[Github仓库地址]
[代码规范链接]
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 80 | 120 |
| Estimate | 估计这个任务需要多少时间 | 400 | 620 |
| Development | 开发 | 300 | 450 |
| Analysis | 需求分析 (包括学习新技术) | 200 | 200 |
| Design Spec | 生成设计文档 | 100 | 80 |
| Design Review | 设计复审 | 30 | 20 |
| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 20 | 20 |
| Design | 具体设计 | 100 | 70 |
| Coding | 具体编码 | 160 | 120 |
| Code Review | 代码复审 | 80 | 120 |
| Test | 测试(自我测试,修改代码,提交修改) | 100 | 80 |
| Reporting | 报告 | 60 | 80 |
| Test Repor | 测试报告 | 60 | 60 |
| Size Measurement | 计算工作量 | 10 | 10 |
| Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 30 | 30 |
| 合计 | 1730 | 1840 |
+解题思路
该道需要我们通过日志文件将各省隔各类人数的统计起来,并且经将其写入文档中,我觉得需要先将日志文件的各种格式进行解析,然后将命令的功能通过程序进行实现,最后设计人数的算法。
- 设计实现
首先,我们需要对日志文档进行读取与解析,先将日志问档一行一行的读取,遇到"//"符号停止,对读取到的每一行进行解析,以空格符分隔,然后进行各个字符串的解析,先统计会出现的情况,然后统计人数,进行各项的加减。
其次是进行命令行的辨别和具体操作,统计可能会出现的所有命令和参数,用args数组传递给主函数,主函数对args数组进行解析,-log,-out是必须的参数,读取到参数后将紧邻参数的值读取到主函数中,以便输出时使用。
最后,我们要做的时如何统计人数,我是设计多个数组,记录各个省的各种人数,然后每读取一个文件就进行统计,最后就是我们需要的结果,每次执行命令,都是从最开始的那个日志读起,一直累积到我们所需要的这天的日志,将累积的结果输出到-out参数指定的目录下。
- 代码说明
解析日志的代码
public static void ReadFile(String pathname) { //通过路径名打开文件 try (FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader); ) { String line; //逐行读取文件 while ((line = br.readLine()) != null) { if(line.toCharArray()[0]=='/')break; //将每行的字符串使用空格分隔 String []ss=line.split("\\s+"); if(ss.length==3){ int i=Provin(ss[0]); Adddel3(ss,i);//对具体的省的各类人员进行加减 } else if(ss.length==4){ int i=Provin(ss[0]); Adddel4(ss,i);//对具体的省的各类人员进行加减 } else if(ss.length==5){ int i1=Provin(ss[0]); int i2=Provin(ss[3]); Adddel5(ss,i1,i2);//对具体的省的各类人员进行加减 } } } catch (IOException e) { e.printStackTrace(); }}
对命令进行操作的代码
for(int i=0;i<args.length;){
if(args[i].equals("-log")){//记录log参数的内容
_log=args[i+1];
i+=2;
}
else if(args[i].equals("-out")){//记录out参数的内容
_out=args[i+1];
i+=2;
}
else if(args[i].equals("-date")){//记录date参数
_date=args[i+1];
i+=2;
}
else if(args[i].equals("-type")){//记录type参数
tempt=1;
for(int j=0;j<ty.length;j++){
if(args[i+1].equals(_type[j]))ty[j]=1;
}
i+=2;
}
else if(args[i].equals("-province")){//记录province参数
tempp=1;
for(int j=0;j<pr.length;j++){
if(args[i+1].equals(province[j]))pr[j]=1;
}
i+=2;
}
else if(args[i].equals("list"))i++;
}
各个类型的日志的加减
public static void Adddel3(String[] ss,int n){//三个字符串
String op=ss[1];
int num=Cuts(ss[2]);
if(op.equals("死亡")){
typ[3][n]+=num;
typ[0][n]-=1;
}
else if(op.equals("治愈"))typ[2][n]+=num;
}
public static void Adddel4(String []ss,int n){//四个字符串
int num=Cuts(ss[3]);
if(ss[1].equals("新增")){
if(ss[2].equals("感染患者"))typ[0][n]+=num;
else if(ss[2].equals("疑似患者"))typ[1][n]+=num;
}
else if(ss[1].equals("疑似患者")&&ss[2].equals("确诊感染")){
typ[0][n]+=num;
typ[1][n]-=num;
}
else if(ss[1].equals("排除")&&ss[2].equals("疑似患者")){
typ[1][n]-=num;
}
}
public static void Adddel5(String []ss,int i1,int i2){//五个字符串
int num=Cuts(ss[4]);
if(ss[1].equals("疑似患者")){
typ[1][i1]-=num;
typ[1][i2]+=num;
}
else if(ss[1].equals("感染患者")){
typ[0][i1]-=num;
typ[0][i2]+=num;
}
}
输出到文件
public static void writeFile(String pathname) {
try {
String path=pathname;
File writeName = new File(path); // 相对路径,如果没有则要建立一个新的output.txt文件
writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
try (FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer)
) {
for(int i=1;i<province.length;i++){
typ[0][0]+=typ[0][i];
typ[1][0]+=typ[1][i];
typ[2][0]+=typ[2][i];
typ[3][0]+=typ[3][i];
}
for(int i=0;i<province.length;i++){
if(tempp==1){
if(pr[i]==1){
out.write(province[i]);
if(tempt==1){
for(int k=0;k<ty.length;k++){
if(ty[k]==1){
out.write(" "+_type[k]+" "+typ[k][i]+"人");
}
}
}
else{
for(int k=0;k<ty.length;k++){
if(ty[k]==0){
out.write(" "+_type[k]+" "+typ[k][i]+"人");
}
}
}
out.write("\r\n");
}
}
else{
if(pr[i]==0){
out.write(province[i]);
if(tempt==1){
for(int k=0;k<ty.length;k++){
if(ty[k]==1){
out.write(" "+_type[k]+" "+typ[k][i]+"人");
}
}
}
else{
for(int k=0;k<ty.length;k++){
if(ty[k]==0){
out.write(" "+_type[k]+" "+typ[k][i]+"人");
}
}
}
out.write("\r\n");
}
}
}
out.write("// 该文档并非真实数据,仅供测试使用\r\n");
out.flush(); // 把缓存区内容压入文件
}
} catch (final IOException e) {
e.printStackTrace();
}
}
+ 单元测试截图和描述
代码覆盖率

例子测试


无其他参数的命令 默认最新的日志


使用了-date 参数


使用-type 参数


使用 -type -province参数


只使用- province参数


选择最新一天的日期


不选择日期
+性能优化
最初使用题目提供的数据

使用 java InfectStatistic list -log _path -out D:\out2.txt -date 2020-10-22

使用 java InfectStatistic list -log _path -out D:\out3.txt
功能的测试

+心路历程及收获
一开始看到题目,自己是感觉啥都不会,看也看不懂,后来查阅了许多资料才慢慢有一点感觉,然后就是修改代码,还有就是关于github的使用,还是不太会,桌面版感觉不太会用,各种按钮,好在程序基本实现了功能,在家里一个人开发真的是太难了。QAQ。。。
+五个仓库
1.[作业的主仓库]
这个仓库主要是该次作业的基础模版,需要从这个仓库fork到本地进行修改。
2.[阿里巴巴代码规范]
这个仓库提供阿里巴巴这家公司要求的代码规范,十分的详细,值得好好深究面对将来有好处。
3.[spring项目demo]
里面提供了大量的demo
4.[java数据结构与算法]
详细讲述了有关java的数据结构算法
5.[wemall]
微信小程序,基于react, node.js, go开发的微商城。
[1]: https://edu.cnblogs.com/campus/fzu/2020SpringW/join?id=CfDJ8FKe-Oc4rmBCjdz4t-OOIu1AjO8Y6cogRK5314SivcKLcAXWxVAh6UpPO66BuezrN2-Myd9gXXgcN4n5nfCg97r5nYI6FkO4LELRJ9W4OU_l43lI2bxXPApTNm7syM4Bujj2C3UNZ5nbUDmLgkrX0wQ
[2]: https://edu.cnblogs.com/campus/fzu/2020SpringW/homework/10281
[3]: https://www.cnblogs.com/QZcn/p/12263301.html
[Github仓库地址]:https://github.com/erniumo/InfectStatistic-main
[代码规范链接]:https://github.com/erniumo/erniumo/blob/master/22171319/codestyle.md
[作业的主仓库]:https://github.com/numb-men/InfectStatistic-main
[阿里巴巴代码规范]:https://github.com/chjw8016/alibaba-java-style-guide
[spring项目demo]:https://github.com/xkcoding/spring-boot-demo
[java数据结构与算法]:TheAlgorithms/Java
[wemall]:https://github.com/Mauue/gxgk-wechat-server微信小程序,基于react, node.js, go开发的微商城。


浙公网安备 33010602011771号