提交 2636ab08 编写于 作者: S SilentSamsara

rm void

上级 79776a2f
*.class
*.log
*.lock
*.txt
target/
# idea
.idea/
*.iml
### IntelliJ IDEA ###
*.iml
*.ipr
*.iws
.idea
.classpath
.project
.settings/
bin/
*.log
tmp/
*.zip
*.class
*.exe
*.project
*.txt
|这个作业属于哪个课程|[软件工程实践2022年春-F班 ](https://bbs.csdn.net/forums/se-learning "软件工程实践2022年春-F班")|
|-- |-- |
|这个作业要求在哪里|[软件工程实践第二次作业——个人实战](https://bbs.csdn.net/topics/604903684 "软件工程实践第二次作业——个人实战")|
|这个作业的目标|获取要求的相应数据,实现一个可以输出总榜以及当日赛程的cmd程序|
|其他参考文献|csdn,博客园,菜鸟教程|
[toc]
作业基本信息...
## Gitcode项目地址
[Gitcode项目地址 ](https://gitcode.net/C_Menethil_L/personalproject-java "Gitcode项目地址")
## PSP表格
|PSP|Personal Software Process Stages|预估耗时(分钟) |实际耗时(分钟) |
|-- |-- |-- |-- |
|• Estimate|• 估计这个任务需要多少时间|30 | 30|
|Development|• 开发|60 |14 |
|• Analysis|• 需求分析 (包括学习新技术)|50 |42 |
|• Design Spec|• 生成设计文档|30 |30 |
|• Design Review| • 设计复审|40 |40 |
|• Coding Standard|• 代码规范 (为目前的开发制定合适的规范)|50 |25 |
|• Design| • 具体设计|20|20 |
|• Coding|• 具体编码|420 |560 |
|• Code Review|• 代码复审|60 |40 |
|• Test|• 测试(自我测试,修改代码,提交修改)|60 |120 |
|Reporting|报告|60 |60 |
|• Test Repor|• 测试报告|30 |30 |
|• Size Measurement|• 计算工作量|10 |8 |
|• Postmortem & Process Improvement Plan| • 事后总结, 并提出过程改进计划|40 |50 |
| |合计| 1020 | 1049 |
## 解题思路描述
### 1.获取赛事网站上的相应数据
通过阅读题目以及查阅数据,发现使用url可以获取网站上的特定链接的数据并下载
### 2.对获取的数据进行处理
获得的数据是json格式,运用gson可以进行对于json字段的解析,用上对应的字符串操作方法,提取所需要的数据
### 3.编写total函数
通过获得的数据,判断输入的total指令的正确与否,进行相应的赛程数据的输出
### 4.编写schedule函数
通过获得的数据,判断输入的schedule指令的正确与否,进行相应的赛程数据的输出
### 5.打包成jar文件
用idel自带的功能,将主函数打包成jar文件,设置好对应的实参
## 接口设计和实现过程
### 1.json函数的解析类
json函数解析下的String数组需要一个对应的容器类来装填,按照题目要求,类里面需要有对应存储的字段以及相应的get,set方法,例子如下
```java
public String getSilver() {
return silver;
}
public void setSilver(String silver) {
this.silver = silver;
}
```
### 2.url类
url类需要能访问目标网站,并且将网站上的json文件存储到本地的String数组上,需要有一个loadJson的函数
```java
public static String loadJson (String url) {
...........
}
```
### 3.文件的读取写入
文件写入读取必须设置好UTF-8的编码,文件写入时需要一行一行读取,为此需要一下函数
```java
public static String readTxt(String txtPath) {
...............
}
public static void writeTxt(String txtPath,String content){
...............
}
```
### 4.字符串的处理判断
对于传入的字符串,我们要对其正确性进行判断,对于不符合要求的输入,应该有相应的返回值。
例如total输入如果拼写错误,应该传出输出Error的返回值
schedule 的日期错误,要传出输出N/A的返回值
正确的输入,则应该传出输出正确的结果的返回值
```java
//判断schedule后面字符串的正误
public static int istrdate(String str)
{
.....................
}
//判断首个指令是否正确
public static int isvalid(String str)
{
......................
}
```
### 5.赛事输出
按照题目要求,将输出结果单独出一个类,其中这个类的getoutput函数要能接受字符串的处理判断传过来的放回值,并且按照
返回值输出结果
```java
class Outputtext
{
public static void getoutput(String inputfile,String outputfile)
{
..........................
}
}
```
## 关键代码展示
### 1.Json获取
```java
public static String loadJson (String url) {
StringBuilder json = new StringBuilder();
try {
URL urlObject = new URL(url);
URLConnection uc = urlObject.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
String inputLine = null;
while ( (inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json.toString();
}
```
### 2.字符串处理
```java
public static int istrdate(String str)
{
char[] strarray=str.toCharArray();
int st1=(strarray[0]-48)*1000+(strarray[1]-48)*100+(strarray[2]-48)*10+(strarray[3]-48);
if(st1>=202&&st1<=220)
return st1;//st1代表正确日期
else{
if(st1>220&&st1<1231)
return 1;
else
{if(st1>101&&st1<202)
return 1;//1代表属于日期但不是赛程
else return -1;//-1代表错误指令
}
}
}
```
### 3.输出函数
```java
public static void getoutput(String inputfile,String outputfile)
{
String url = "https://api.cntv.cn/olympic/getBjOlyMedals?serviceId=2022dongao&itemcode=GEN-------------------------------";
Dm dm=urlget.gettotaljson(url);
schDm schdm=new schDm();
FileReader fr = null;
LineNumberReader lnr = null;
ReadTxt t1=new ReadTxt();
String input;
int da2;
stringcheck s1=new stringcheck();
File file = new File(outputfile);
file.delete();
boolean i1=true;
try{
if (!input.trim().equals(""))
{
if (i1 == true) {
i1 = false;
} else {
t1.writeTxt(outputfile, "-----" + "\n");
}
if (input.trim().equals("total")) {
for (int i = 1; i <= dm.getData().getTotal(); i++) {
t1.writeTxt(outputfile, "rank" + dm.getData().getMedalsList().get(i - 1).getRank() + ":" + dm.getData().getMedalsList().get(i - 1).getCountryid() + "\n");
t1.writeTxt(outputfile, "gold" + ":" + dm.getData().getMedalsList().get(i - 1).getGold() + "\n");
t1.writeTxt(outputfile, "silver" + ":" + dm.getData().getMedalsList().get(i - 1).getSilver() + "\n");
t1.writeTxt(outputfile, "bronze" + ":" + dm.getData().getMedalsList().get(i - 1).getBronze() + "\n");
t1.writeTxt(outputfile, "total" + ":" + dm.getData().getMedalsList().get(i - 1).getCount() + "\n");
if (i != dm.getData().getTotal())
t1.writeTxt(outputfile, "-----" + "\n");
}
} else {
da2 = s1.isvalid(input);
if (da2 > 1) {
System.out.println(da2);
String url1 = "https://api.cntv.cn/Olympic/getBjOlyMatchList?startdatecn=20220" + da2 + "&t=jsonp&cb=OM&serviceId=2022dongao";
schdm = urlget.getschdulejson(url1);
for (int i = 1; i <= schdm.getData().getTotal(); i++) {
t1.writeTxt(outputfile, "time:" + getTimeShort(strToDateLong(schdm.getData().getMatchList().get(i - 1).getStartdatecn())) + "\n");
t1.writeTxt(outputfile, "sport" + ":" + schdm.getData().getMatchList().get(i - 1).getItemcodename() + "\n");
t1.writeTxt(outputfile, "name" + ":" + schdm.getData().getMatchList().get(i - 1).getTitle());
if (!schdm.getData().getMatchList().get(i - 1).getHomename().equals("")) {
t1.writeTxt(outputfile, " " + schdm.getData().getMatchList().get(i - 1).getHomename() + "VS" + schdm.getData().getMatchList().get(i - 1).getAwayname() + "\n");
} else {
t1.writeTxt(outputfile, "\n");
}
t1.writeTxt(outputfile, "venue" + ":" + schdm.getData().getMatchList().get(i - 1).getVenuename() + "\n");
if (i != schdm.getData().getTotal())
t1.writeTxt(outputfile, "-----" + "\n");
}
} else {
if (da2 == 1)
t1.writeTxt(outputfile, "N/A" + "\n");
else {
if (da2 == -1)
t1.writeTxt(outputfile, "Error" + "\n");
}
}
}
}
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
```
## 单元测试
运用Junit 对于输入的数据进行判断
![img](https://img-community.csdnimg.cn/images/beaec5fe32404b82a3b593d1644223b9.png "#left")
对于助教给出的部分测试用例
![img](https://img-community.csdnimg.cn/images/666aefeba0e44b969389f4c7ae715288.png "#left")
可以很好地得到答案
![img](https://img-community.csdnimg.cn/images/025d9ce60e8f4f23aad1a22818919098.png "#left")
## 异常处理
如果输出出现错误则会放回异常
![img](https://img-community.csdnimg.cn/images/732f943e549748f8a71ae323292a446b.png "#left")
## 心得体会
1.在处理逻辑函数的时候要先思考好再动手,以免后期debug时大动筋骨。
2.做之前理清需求分析,计划好每一步的时间,分清楚主次,可以大大提升效率
3.debug时候善于利用编译器自带的调试程序,可以轻松不少
代码规范
1.缩进
- 每个变量与符号之间都会有空格隔开
- 单行代码一般不超过80个字符
命名
类名使用“大驼峰”命名法
方法名、变量名会尽量使用英文
方法名大部分为动名词组合
其他
个别函数可能会有很多行
\ No newline at end of file
import java.io.*;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.InputStreamReader;
/**
* 用GSON解析Json数组
*/
public class OlympicSearch {
public static void main(String[] args) {
// Json数组最外层要加"[]"
Outputtext o1=new Outputtext();
o1.getoutput(args[0],args[1]);
}
}
/*
* 封装的GSON解析工具类,提供泛型参数
*/
class GsonUtil {
// 将Json数据解析成相应的映射对象
public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {
Gson gson = new Gson();
T result = gson.fromJson(jsonData, type);
return result;
}
// 将Json数组解析成相应的映射对象列表
public static <T> List<T> parseJsonArrayWithGson(String jsonData,
Class<T> type) {
Gson gson = new Gson();
List<T> result = gson.fromJson(jsonData, new TypeToken<List<T>>() {
}.getType());
return result;
}
}
class MedalsList {
private String bronze;
private String rank;
private String count;
private String silver;
private String countryname;
private String gold;
private String countryid;
public String getBronze() {
return bronze;
}
public void setBronze(String bronze) {
this.bronze = bronze;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getSilver() {
return silver;
}
public void setSilver(String silver) {
this.silver = silver;
}
public String getCountryname() {
return countryname;
}
public void setCountryname(String countryname) {
this.countryname = countryname;
}
public String getGold() {
return gold;
}
public void setGold(String gold) {
this.gold = gold;
}
public String getCountryid() {
return countryid;
}
public void setCountryid(String countryid) {
this.countryid = countryid;
}
}
class Medal {
private int total;
private List<MedalsList> medalsList; // 因为grade是个数组,所以要定义成List
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<MedalsList> getMedalsList() {
return medalsList;
}
public void setMedalsList(List<MedalsList> medalsList) {
this.medalsList = medalsList;
}
}
class Dm {
private Medal data;
public Medal getData() {
return data;
}
public void setData(Medal data) {
this.data = data;
}
public Dm(Medal data) {
this.data = data;
}
}
class schDm {
private sch data;
public sch getData() {
return data;
}
public void setData(sch data) {
this.data = data;
}
}
class sch {
private int total;
private List<schdata> matchList; // 因为grade是个数组,所以要定义成List
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<schdata> getMatchList() {
return matchList;
}
public void setMatchList(List<schdata> matchList) {
this.matchList = matchList;
}
}
class schdata
{
private String startdatecn;
private String itemcodename;
private String title;
private String venuename;
private String homename;
private String awayname;
public String getStartdatecn() {
return startdatecn;
}
public void setStartdatecn(String startdatecn) {
this.startdatecn = startdatecn;
}
public String getItemcodename() {
return itemcodename;
}
public void setItemcodename(String itemcodename) {
this.itemcodename = itemcodename;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getVenuename() {
return venuename;
}
public void setVenuename(String venuename) {
this.venuename = venuename;
}
public String getHomename() {
return homename;
}
public void setHomename(String homename) {
this.homename = homename;
}
public String getAwayname() {
return awayname;
}
public void setAwayname(String awayname) {
this.awayname = awayname;
}
}
class urlget{
public static String loadJson (String url) {
StringBuilder json = new StringBuilder();
try {
URL urlObject = new URL(url);
URLConnection uc = urlObject.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
String inputLine = null;
while ( (inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json.toString();
}
public static Dm gettotaljson(String url)
{
url = "https://api.cntv.cn/olympic/getBjOlyMedals?serviceId=2022dongao&itemcode=GEN-------------------------------";
String json = loadJson(url);
// System.out.println(json);
Gson gson = new Gson();
return gson.fromJson(json, Dm.class);
}
public static schDm getschdulejson(String url)
{
String json1 = loadJson(url);
//截取_之后字符串
String str = json1.substring(0, json1.indexOf("("));
String str1 = json1.substring(str.length()+1, json1.length()-2);
Gson gson = new Gson();
return gson.fromJson(str1, schDm.class);
}
}
class Outputtext
{
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public static String getTimeShort(Date d1) {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
Date currentTime = d1;
String dateString = formatter.format(currentTime);
return dateString;
}
public static void getoutput(String inputfile,String outputfile)
{
String url = "https://api.cntv.cn/olympic/getBjOlyMedals?serviceId=2022dongao&itemcode=GEN-------------------------------";
Dm dm=urlget.gettotaljson(url);
schDm schdm=new schDm();
FileReader fr = null;
LineNumberReader lnr = null;
ReadTxt t1=new ReadTxt();
String input;
int da2;
stringcheck s1=new stringcheck();
File file = new File(outputfile);
file.delete();
boolean i1=true;
try{
// create new reader
fr = new FileReader(inputfile);
lnr = new LineNumberReader(fr);
// read lines till the end of the stream
while((input=lnr.readLine())!=null) {
String[] inputarray = input.split(" ");
if (!input.trim().equals(""))
{
if (input.trim().equals("total")) {
for (int i = 1; i <= dm.getData().getTotal(); i++) {
if (i1 == true) {
i1 = false;
} else {
t1.writeTxt(outputfile, "\n");
}
t1.writeTxt(outputfile, "rank" + dm.getData().getMedalsList().get(i - 1).getRank() + ":" + dm.getData().getMedalsList().get(i - 1).getCountryid() + "\n");
t1.writeTxt(outputfile, "gold" + ":" + dm.getData().getMedalsList().get(i - 1).getGold() + "\n");
t1.writeTxt(outputfile, "silver" + ":" + dm.getData().getMedalsList().get(i - 1).getSilver() + "\n");
t1.writeTxt(outputfile, "bronze" + ":" + dm.getData().getMedalsList().get(i - 1).getBronze() + "\n");
t1.writeTxt(outputfile, "total" + ":" + dm.getData().getMedalsList().get(i - 1).getCount() + "\n");
t1.writeTxt(outputfile, "-----");
}
} else {
da2 = s1.isvalid(input);
if (da2 > 1) {
String url1 = "https://api.cntv.cn/Olympic/getBjOlyMatchList?startdatecn=20220" + da2 + "&t=jsonp&cb=OM&serviceId=2022dongao";
schdm = urlget.getschdulejson(url1);
for (int i = 1; i <= schdm.getData().getTotal(); i++) {
if (i1 == true) {
i1 = false;
} else {
t1.writeTxt(outputfile, "\n");
}
t1.writeTxt(outputfile, "time:" + getTimeShort(strToDateLong(schdm.getData().getMatchList().get(i - 1).getStartdatecn())) + "\n");
t1.writeTxt(outputfile, "sport" + ":" + schdm.getData().getMatchList().get(i - 1).getItemcodename() + "\n");
t1.writeTxt(outputfile, "name" + ":" + schdm.getData().getMatchList().get(i - 1).getTitle());
if (!schdm.getData().getMatchList().get(i - 1).getHomename().equals("")) {
t1.writeTxt(outputfile, " " + schdm.getData().getMatchList().get(i - 1).getHomename() + "VS" + schdm.getData().getMatchList().get(i - 1).getAwayname() + "\n");
} else {
t1.writeTxt(outputfile, "\n");
}
t1.writeTxt(outputfile, "venue" + ":" + schdm.getData().getMatchList().get(i - 1).getVenuename() + "\n");
t1.writeTxt(outputfile, "-----");
}
} else {
if (da2 == 1)
{ if (i1 == true) {
i1 = false;
} else {
t1.writeTxt(outputfile, "\n");
}
t1.writeTxt(outputfile, "N/A" + "\n"+"-----");
}
else {
if (da2 == -1)
{
if (i1 == true) {
i1 = false;
} else {
t1.writeTxt(outputfile, "\n");
}
t1.writeTxt(outputfile, "Error" + "\n"+"-----");
}
}
}
}
}
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册