...
 
Commits (2)
    https://gitcode.net/SilentSamsara/personalproject-java-late/-/commit/051903fe984f077789cc5356c273f2a27f26c0d8 提交作业 2022-03-15T09:36:27+08:00 221900115-林文杰 2468013194@qq.com https://gitcode.net/SilentSamsara/personalproject-java-late/-/commit/bccc41da2a6e424a4dca6bc4c60954abb574d0f1 Merge branch 'master' into 'master' 2022-03-16T19:26:19+08:00 SilentSamsara 1449755675@qq.com 221900115林文杰 See merge request <a href="/SilentSamsara/personalproject-java-late/-/merge_requests/11" data-original="SilentSamsara/personalproject-java-late!11" data-link="false" data-link-reference="false" data-project="107676" data-merge-request="48874" data-project-path="SilentSamsara/personalproject-java-late" data-iid="11" data-mr-title="221900115林文杰" data-reference-type="merge_request" data-container="body" data-placement="top" title="" class="gfm gfm-merge_request">!11</a>
*.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
[作业要求](https://bbs.csdn.net/topics/604903684)
[我的博客](https://bbs.csdn.net/topics/605080325)
# 1.任务
完成对冬奥会的赛事数据的爬取,并实现一个能够对国家排名及奖牌个数统计的控制台程序
# 2.如何运行
将OlympicSearch.jar放在与scr相同的目录下,将input.txt和output.txt放置于data文件夹中,以命令行参数传入读取文件和输出文件,在cmd中进入文件目录后输入`Java -jar OlympicSearch.jar input.txt output.txt`就可以运行程序
## 指令
```
total //输出总将排榜
schedule 0202//输出0202的赛程
非法指令
schedule Error
totol Error
schedule0202 Error
schedule total N/A
```
# 1.标识符命名
- 尽量都用英文来表示含义,能不使用拼音就不使用拼音
- 对于一些局部变量会使用英文的缩写命名,如:计数变量 count 命名为 cnt,求和变量用 sum,中间值变量用 temp,最大值 max,最小值 min
- 常量则使用全大写 MAX MIN
- 函数名的命名使用为驼峰法,尽量使用动宾短语来明确意思如:读取 txt 文件的内容`readTxt(String path)`,读取 Json 文件的内容`readJsonFile(String path)`,输出奖牌榜`writeTotalRank()`
- 再每个函数名开始之前会用//+功能注释说明函数用途
# 2.大括号的位置
- 在函数声明时左大括号附加再函数声明后,右大括号另起一行
例如
```
public static String readJsonFile (String fileName) {
}
```
- 同样在条件语句和循环语句大括号的位置也这样放,
```
while (true) {
}
if (a==b) {
}
```
# 3.空格及缩进
- 使用 tab 键作为缩进
- 二元运算符左右需要加空格`a + b`
- 在 if,while 与()之间要加上一个空格`if (a==b)`
- 在两个函数声明之间需要空出一格
```
//输出每日赛程
public static void writeDateSchedule (String path) {
}
//输出奖牌榜
public static void writeTotalRank () {
}
```
# 4.注释
- 单行注释用//
- 多行注释用/\* \*/
- 在声明意义不明确的局部变量时加上单行注释//明确意义
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.io.IOException;
public class Lib {
public static String[] contentOfPerLine = new String[2000];//用于存放input.txt文件中每行的内容
public static int count = 0;//记录总行数
//读取json文件
//读取input.txt的内容
public static void readTxt(String filePath) {
try {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
int i=0;
while ((lineTxt = br.readLine()) != null) {
//System.out.println(lineTxt);
contentOfPerLine[i++] = lineTxt;
}
count=i;
br.close();
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文件读取错误!");
}
}
public static String readJsonFile(String fileName) {
String jsonStr = "";
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void writeTotalRank() throws FileNotFoundException {
String path = "src/data/total.json";
String s = Lib.readJsonFile(path);
JSONObject jobj = JSON.parseObject(s);
JSONArray countryInfo = jobj.getJSONObject("data").getJSONArray("medalsList");//构建JSONArray数组
//System.out.println(countryInfo.size());
for (int i = 0; i < countryInfo.size(); i++) {
JSONObject key = (JSONObject) countryInfo.get(i);
String rank = (String) key.get("rank");
String countryid = (String) key.get("countryid");
String count = (String) key.get("count");
String gold = (String) key.get("gold");
String silver = (String) key.get("silver");
String bronze = (String) key.get("bronze");
System.out.println("rank" + rank + ":" + countryid);
System.out.println("gold:" + gold);
System.out.println("silver:" + silver);
System.out.println("bronze:" + bronze);
System.out.println("count:" + count);
System.out.println("-----");
}
}
public static void writeDateSchedule(String path) throws FileNotFoundException{
String s = Lib.readJsonFile(path);
JSONObject jobj = JSON.parseObject(s);
JSONArray matchList = jobj.getJSONObject("data").getJSONArray("matchList");//构建JSONArray数组
for (int i = 0; i < matchList.size(); i++) {
JSONObject key = (JSONObject) matchList.get(i);
//将时分从时刻表中分割出来
String startdatecn = (String) key.get("startdatecn");
String[] DataAndClock=startdatecn.split(" ");
String[] hourMinuteSecond=DataAndClock[1].split(":");
String itemcodename=(String)key.get("itemcodename");
String title=(String)key.get("title");
String homename=(String)key.get("homename");
String awayname=(String)key.get("awayname");
String venuename=(String)key.get("venuename");
System.out.println("time" + ":" + hourMinuteSecond[0]+":"+hourMinuteSecond[1]);
System.out.println("sport:"+itemcodename);
if((!homename.equals(""))&&(!awayname.equals("")))
System.out.println("name:"+title+" "+homename+"VS"+awayname);
else
System.out.println("name:"+title);
System.out.println("venue:"+venuename);
System.out.println("-----");
}
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class OlympicSearch {
public static void main (String[]args) throws IOException {
//Lib.readTxt("src/data/"+args[0]);//读取input.txt的每行的内容
Lib.readTxt(args[0]);//读取input.txt的每行的内容
//设置流将数据输出至output.txt
//PrintStream ps = new PrintStream("src/data/"+args[1],"utf-8");
PrintStream ps = new PrintStream(args[1],"utf-8");
System.setOut(ps);
for (int i = 0; i < Lib.count; i++) {
String temp=Lib.contentOfPerLine[i];//temp是input.txt中每行的内容
String temp1=temp;//保存temp的副本
temp=temp.trim();
if (temp.equals("total")) {//输出奖牌榜
Lib.writeTotalRank();
}
else if (temp.isEmpty()) {//忽略空指令
}
else if (temp1.length()>8) {
if (temp1.startsWith("schedule")) {//字符串为schedule******
temp1 = temp1.substring(8);//去除前面的schedule
if (temp1.startsWith(" ")&&!temp1.isEmpty()) {//剩余部分以空格打头且非空
temp1 = temp1.trim();//去除空格
int date=0;
try {
date=Integer.parseInt(temp1);//对schedule后的字符串进行异常处理,防止解析数字失败
} catch (Exception e) {
System.out.println("N/A");
System.out.println("-----");
continue;
}
if (date>=202&&date<=220) {//时间在0202-0220之间
/*if (date>=216) {//0216-0220使用爬虫
String crawlerContent=Lib.getDateAfterFifteen(temp1);//获得需要的json
Lib.writeDateSchedule(crawlerContent);
}*/
//0202-0215直接读取本地文件
String path="src/data/schedule/"+temp1+".json";
String jsonContent = Lib.readJsonFile(path);
Lib.writeDateSchedule(jsonContent);
}
else if (date<202||date>220) {
System.out.println("N/A");
System.out.println("-----");
}
}
else {
System.out.println("Error");
System.out.println("-----");
}
}
else {
System.out.println("Error");
System.out.println("-----");
}
}
else {
System.out.println("Error");
System.out.println("-----");
}
}
String result = Lib.readJsonFile(args[1]);
result = result.substring(0,result.length()-2);
FileWriter fileOut = new FileWriter(args[1]);
fileOut.write("");
fileOut.append(result);
fileOut.close();
}
}
{
"data": {
"total": 4,
"matchList": [
{
"homeid": "CURXTEAM2---SWE01",
"h5pageid": "",
"statusname": "结束",
"itemcode": "CUR-------------------------------",
"subitemname": "混合双人",
"id": "OGMM164249724307010092",
"vrtotalurl": "",
"enddatecn": "2022-02-02 22:00:00",
"albumurl": "",
"title": "混双循环赛第1轮",
"vrlivecode": "",
"deletedflag": "0",
"documentcode": "CURXTEAM2-------------PREL000101--",
"totaltitle": "",
"imageurl": "",
"vrliveurl": "",
"pageid": "curling01",
"startdatecn": "2022-02-02 20:05:00",
"venuename": "国家游泳中心",
"subitemcode": "CURXTEAM2-------------------------",
"awayid": "CURXTEAM2---GBR01",
"totalguid": "",
"status": "FINISHED",
"combatflag": "0",
"lockflag": "0",
"mvlivecode": "",
"homename": "瑞典",
"liveurl": "",
"awayname": "英国",
"reserve3": "",
"venue": "NAC",
"awayscore": "9",
"reserve2": "",
"homecode": "SWE",
"reserve1": "startlist",
"vrtotalcode": "",
"homescore": "5",
"livecode": "",
"itemcodename": "冰壶",
"totalurl": "",
"mvliveurl": "",
"awaycode": "GBR",
"adcode": "",
"medal": ""
},
{
"homeid": "CURXTEAM2---AUS01",
"h5pageid": "",
"statusname": "结束",
"itemcode": "CUR-------------------------------",
"subitemname": "混合双人",
"id": "OGMM164249724310010093",
"vrtotalurl": "",
"enddatecn": "2022-02-02 22:00:00",
"albumurl": "",
"title": "混双循环赛第1轮",
"vrlivecode": "",
"deletedflag": "0",
"documentcode": "CURXTEAM2-------------PREL000102--",
"totaltitle": "",
"imageurl": "",
"vrliveurl": "",
"pageid": "curling01",
"startdatecn": "2022-02-02 20:05:00",
"venuename": "国家游泳中心",
"subitemcode": "CURXTEAM2-------------------------",
"awayid": "CURXTEAM2---USA01",
"totalguid": "",
"status": "FINISHED",
"combatflag": "0",
"lockflag": "0",
"mvlivecode": "",
"homename": "澳大利亚",
"liveurl": "",
"awayname": "美国",
"reserve3": "",
"venue": "NAC",
"awayscore": "6",
"reserve2": "",
"homecode": "AUS",
"reserve1": "startlist",
"vrtotalcode": "",
"homescore": "5",
"livecode": "",
"itemcodename": "冰壶",
"totalurl": "",
"mvliveurl": "",
"awaycode": "USA",
"adcode": "",
"medal": ""
},
{
"homeid": "CURXTEAM2---NOR01",
"h5pageid": "",
"statusname": "结束",
"itemcode": "CUR-------------------------------",
"subitemname": "混合双人",
"id": "OGMM164249724312910094",
"vrtotalurl": "",
"enddatecn": "2022-02-02 22:00:00",
"albumurl": "",
"title": "混双循环赛第1轮",
"vrlivecode": "",
"deletedflag": "0",
"documentcode": "CURXTEAM2-------------PREL000103--",
"totaltitle": "",
"imageurl": "",
"vrliveurl": "",
"pageid": "curling01",
"startdatecn": "2022-02-02 20:05:00",
"venuename": "国家游泳中心",
"subitemcode": "CURXTEAM2-------------------------",
"awayid": "CURXTEAM2---CZE01",
"totalguid": "",
"status": "FINISHED",
"combatflag": "0",
"lockflag": "0",
"mvlivecode": "",
"homename": "挪威",
"liveurl": "",
"awayname": "捷克",
"reserve3": "",
"venue": "NAC",
"awayscore": "7",
"reserve2": "",
"homecode": "NOR",
"reserve1": "startlist",
"vrtotalcode": "",
"homescore": "6",
"livecode": "",
"itemcodename": "冰壶",
"totalurl": "",
"mvliveurl": "",
"awaycode": "CZE",
"adcode": "",
"medal": ""
},
{
"homeid": "CURXTEAM2---CHN01",
"h5pageid": "",
"statusname": "结束",
"itemcode": "CUR-------------------------------",
"subitemname": "混合双人",
"id": "OGMM164249724315910095",
"vrtotalurl": "",
"enddatecn": "2022-02-02 22:00:00",
"albumurl": "",
"title": "混双循环赛第1轮",
"vrlivecode": "",
"deletedflag": "0",
"documentcode": "CURXTEAM2-------------PREL000104--",
"totaltitle": "",
"imageurl": "",
"vrliveurl": "",
"pageid": "curling01",
"startdatecn": "2022-02-02 20:05:00",
"venuename": "国家游泳中心",
"subitemcode": "CURXTEAM2-------------------------",
"awayid": "CURXTEAM2---SUI01",
"totalguid": "",
"status": "FINISHED",
"combatflag": "0",
"lockflag": "0",
"mvlivecode": "",
"homename": "中国",
"liveurl": "",
"awayname": "瑞士",
"reserve3": "",
"venue": "NAC",
"awayscore": "6",
"reserve2": "CHN",
"homecode": "CHN",
"reserve1": "startlist",
"vrtotalcode": "",
"homescore": "7",
"livecode": "",
"itemcodename": "冰壶",
"totalurl": "",
"mvliveurl": "",
"awaycode": "SUI",
"adcode": "",
"medal": ""
}
]
}
}
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
{
"data": {
"total": 29,
"medalsList": [
{
"bronze": "13",
"rank": "1",
"count": "37",
"silver": "8",
"countryname": "挪威",
"gold": "16",
"countryid": "NOR"
},
{
"bronze": "5",
"rank": "2",
"count": "27",
"silver": "10",
"countryname": "德国",
"gold": "12",
"countryid": "GER"
},
{
"bronze": "2",
"rank": "3",
"count": "15",
"silver": "4",
"countryname": "中国",
"gold": "9",
"countryid": "CHN"
},
{
"bronze": "7",
"rank": "4",
"count": "25",
"silver": "10",
"countryname": "美国",
"gold": "8",
"countryid": "USA"
},
{
"bronze": "5",
"rank": "5",
"count": "18",
"silver": "5",
"countryname": "瑞典",
"gold": "8",
"countryid": "SWE"
},
{
"bronze": "4",
"rank": "6",
"count": "17",
"silver": "5",
"countryname": "荷兰",
"gold": "8",
"countryid": "NED"
},
{
"bronze": "4",
"rank": "7",
"count": "18",
"silver": "7",
"countryname": "奥地利",
"gold": "7",
"countryid": "AUT"
},
{
"bronze": "5",
"rank": "8",
"count": "14",
"silver": "2",
"countryname": "瑞士",
"gold": "7",
"countryid": "SUI"
},
{
"bronze": "14",
"rank": "9",
"count": "32",
"silver": "12",
"countryname": "俄罗斯奥运队",
"gold": "6",
"countryid": "ROC"
},
{
"bronze": "2",
"rank": "10",
"count": "14",
"silver": "7",
"countryname": "法国",
"gold": "5",
"countryid": "FRA"
},
{
"bronze": "14",
"rank": "11",
"count": "26",
"silver": "8",
"countryname": "加拿大",
"gold": "4",
"countryid": "CAN"
},
{
"bronze": "9",
"rank": "12",
"count": "18",
"silver": "6",
"countryname": "日本",
"gold": "3",
"countryid": "JPN"
},
{
"bronze": "8",
"rank": "13",
"count": "17",
"silver": "7",
"countryname": "意大利",
"gold": "2",
"countryid": "ITA"
},
{
"bronze": "2",
"rank": "14",
"count": "9",
"silver": "5",
"countryname": "韩国",
"gold": "2",
"countryid": "KOR"
},
{
"bronze": "2",
"rank": "15",
"count": "7",
"silver": "3",
"countryname": "斯洛文尼亚",
"gold": "2",
"countryid": "SLO"
},
{
"bronze": "4",
"rank": "16",
"count": "8",
"silver": "2",
"countryname": "芬兰",
"gold": "2",
"countryid": "FIN"
},
{
"bronze": "0",
"rank": "17",
"count": "3",
"silver": "1",
"countryname": "新西兰",
"gold": "2",
"countryid": "NZL"
},
{
"bronze": "1",
"rank": "18",
"count": "4",
"silver": "2",
"countryname": "澳大利亚",
"gold": "1",
"countryid": "AUS"
},
{
"bronze": "0",
"rank": "19",
"count": "2",
"silver": "1",
"countryname": "英国",
"gold": "1",
"countryid": "GBR"
},
{
"bronze": "2",
"rank": "20",
"count": "3",
"silver": "0",
"countryname": "匈牙利",
"gold": "1",
"countryid": "HUN"
},
{
"bronze": "1",
"rank": "21",
"count": "2",
"silver": "0",
"countryname": "捷克",
"gold": "1",
"countryid": "CZE"
},
{
"bronze": "1",
"rank": "21",
"count": "2",
"silver": "0",
"countryname": "斯洛伐克",
"gold": "1",
"countryid": "SVK"
},
{
"bronze": "1",
"rank": "21",
"count": "2",
"silver": "0",
"countryname": "比利时",
"gold": "1",
"countryid": "BEL"
},
{
"bronze": "0",
"rank": "24",
"count": "2",
"silver": "2",
"countryname": "白俄罗斯",
"gold": "0",
"countryid": "BLR"
},
{
"bronze": "0",
"rank": "25",
"count": "1",
"silver": "1",
"countryname": "西班牙",
"gold": "0",
"countryid": "ESP"
},
{
"bronze": "0",
"rank": "25",
"count": "1",
"silver": "1",
"countryname": "乌克兰",
"gold": "0",
"countryid": "UKR"
},
{
"bronze": "1",
"rank": "27",
"count": "1",
"silver": "0",
"countryname": "波兰",
"gold": "0",
"countryid": "POL"
},
{
"bronze": "1",
"rank": "27",
"count": "1",
"silver": "0",
"countryname": "拉脱维亚",
"gold": "0",
"countryid": "LAT"
},
{
"bronze": "1",
"rank": "27",
"count": "1",
"silver": "0",
"countryname": "爱沙尼亚",
"gold": "0",
"countryid": "EST"
}
]
}
}
\ No newline at end of file