Which course does this assignment belong to | Soft worker |
---|---|
Where are the requirements for this assignment | Operational requirements |
The goal of this job | Design and develop a program for epidemic statistics, learn how to optimize the program, learn how to use GitHub, learn how to use PSP (personal software development process), and learn how to build |
Job text | task |
Other references | Eclipse unit test Jpprofiler using github blog park related articles |
1, GitHub warehouse address
2, Learning achievements of construction method
2.PSP form
PSP2.1 | Personal Software Process Stages | Estimated time (minutes) | Actual time (minutes) |
---|---|---|---|
Planning | plan | 30 | 45 |
Estimate | Estimate how long this task will take | 30 | 45 |
Development | Development | 450 | 500 |
Analysis | Needs analysis (including learning new technologies) | 30 | 15 |
Design Spec | Generate design documents | 30 | 25 |
Design Review | design review | 30 | 35 |
Coding Standard | Code specifications (develop appropriate specifications for current development) | 45 | 60 |
Design | Concrete design | 30 | 50 |
Coding | Specific encoding | 300 | 450 |
Code Review | Code Review | 60 | 60 |
Test | Test (self test, modify code, submit modification) | 150 | 40 |
Reporting | Presentation | 60 | 135 |
Test Repor | Test report | 60 | 90 |
Size Measurement | Calculate workload | 15 | 15 |
Postmortem & Process Improvement Plan | Summarize afterwards, and propose process improvement plan | 30 | 40 |
Total | 1350 | 1605 |
3, Description of problem solving ideas
1. Required knowledge points
In this task, I intend to use java language. After seeing this topic, I think of the following knowledge points that need to be used
Getting command line parameters
The parameters entered by the user on the command line, i.e. the list command and its options, need to be read in by the program and handled correctly
So I looked for relevant knowledge points How to get command line parameters of java program
And a test program is written to ensure that the command line parameters can be obtained correctly
Read in and output of files
We need to output the result by reading the file in the / log directory and processing it correctly
This involves java's management of file input and output as well as some details that need to be noted (such as coding problems)
java file reading and writing
2. Select the right data structure
Read the topic carefully, and you can find that each Province can be defined as an object, and you can define the Province class, which has the following data members
/*Record the data of a province*/ class Province{ String name; /*Provincial name*/ int infect; /*Number of infected persons*/ int seeming; /*Suspected number*/ int dead; /*death toll*/ int cured; /*Cure number*/ public Province() { infect = seeming = dead = cured = 0; }
}
3. Define document structure
221701126 (catalog name is student number) \--src \--InfectStatistic.java \--Lib.java \--README.md Describe your project, including how to run, function introduction, job link, blog link, etc \--codestyle.md Describe the code style you set before
4. Code style development
Before writing the code, you must standardize your own code style, which is related to the code style in the programming process. Therefore, referring to the recommended java code style of Alibaba, you have formulated your own code style
5. Demand analysis
Be able to count the information in the statistics file and print out the content to be understood into the target file
1. Process parameters according to the command line
2. Select the appropriate documents in the target directory
3. Process each line string of each file and record the data
4. Output the required line to the target file
6. String type in file
It can be divided into the following categories:
n new infected patients
n new suspected patients
Infected patients flow into n people
Suspected patients flow into n people
Death n person
Cure n people
Suspected patients confirmed to be infected with n people
Exclude n suspected patients
7. Handling documents
When you see the file structure, you can think of line by line processing. Then you need to divide the string into arrays with spaces, which is more convenient. Then we can know that the first string is the province, which can be saved, and the last string is the number of people, which can be taken out. The middle string is the behavior. We can judge one by one. If we encounter a string that can be judged directly, we will process it directly. Otherwise, we will judge the next string and judge its behavior comprehensively, so that the judgment of the whole logic is very clear Now.
8. Output file
First, all provinces are stored in an array in alphabetical order, and then the array is traversed to determine whether to output each province. If output is needed, then it is passed into the corresponding method as a parameter, and then specific judgment is made. Then the method will determine the type province option parameter.
4, Design implementation process
Code flow
Specific development process (gradual realization)
5, Code description
1. Data structure
The data structure uses Province to encapsulate the Province name and various index data, such as the number of infected people, suspected patients, etc., which can be used to represent the situation of a single Province.
class Province{ String name; /*Provincial name*/ int infect; /*Number of infected persons*/ int seeming; /*Suspected number*/ int dead; /*death toll*/ int cured; /*Cure number*/ public Province() { infect = seeming = dead = cured = 0; } }
2. Processing command line parameters
Get args parameters and process args array data
If it is the - log command, the input path is stored
If the - out command, the output path is stored
If it is the - date command, the date is stored
If the - type command, the type option is stored
If it is the - province command, the province option is stored
public static void solveArgs(String[] args) { int i = 0; int pos = 1; while(pos < args.length) { String arg = args[pos]; // System.out.println(pos + "-" + arg); if(arg.indexOf('-') == 0) {//This is an order if(arg.equals("-log")) {//Process input path inputPath = args[pos + 1] + "\\"; pos+=2; } else if(arg.equals("-out")) {//Process output path outputPath = args[pos + 1] + "\\"; pos+=2; } else if(arg.equals("-date")) {//Date of processing targetDate = args[pos + 1]; pos+=2; } else if(arg.equals("-type")) { for(i = pos + 1; i < args.length; i++) { String param = args[i]; if(param.indexOf('-') != 0) {//This is the parameter. typeItem.add(param); } else { pos = i; break; } } } else if(arg.equals("-province")) {//Handling the province command for(i = pos + 1; i < args.length; i++) { String param = args[i]; if(param.indexOf('-') != 0) {//This is the parameter. provinceItem.add(param); } else { pos = i; break; } } } if(i == args.length) { break; } } } }
The following shows the data members used to store each item of data
public static String inputPath = "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log\\"; public static String outputPath = "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt"; public static String targetDate = ""; public static ArrayList<String> typeItem = new ArrayList<String>();/*Record the options with the type command*/ public static ArrayList<String> provinceItem = new ArrayList<String>();/*Record the options with the province command*/
3. Search for documents to be processed
A vector is used to store the files to be processed, and each file in the directory is judged. If its date is less than the date parameter obtained from the command line, it will be stored in the vector
The comparison algorithm uses the compareTo of string, that is, the filename is converted into string, and then the judgment is made
Finally put it into "the whole country"
public static void solveDateOrder(String targetDate) { String maxDate = getMaxDate(); if(targetDate.compareTo(maxDate) > 0) { System.out.println("Date out of range"); return; } //Get all files under the input path File file = new File(inputPath); if(file.isDirectory()) { Vector<String> toHandleDate = new Vector<String>();//Get the date file that meets the requirements to be processed String[] fileNames = file.list(); // Get the file names of all files in the directory for(String fileName : fileNames) { fileName = fileName.substring(0, fileName.indexOf('.'));//Truncate suffix //Date comparison if(fileName.compareTo(targetDate) <= 0) { toHandleDate.add(fileName); System.out.println(fileName); } else { break; } //System.out.println(fileName); } if(toHandleDate.size() > 0) { solveEveryFile(toHandleDate); } map.put("Whole country", country); } }
4. Process each document
Get each file line by line, and then use the split function of string to divide it by space to get the information array
String[] information = str.split("\\s+");
Key algorithm: determine what the second string is
If it is newly added, judge whether the third string is infected or suspected, and make data statistics
switch (information[1]) { case "Newly added": if(information[2].equals("Infected patients")) {//Situation of infected patients p.infect += number; country.infect += number; //System.out.println(num); } else {//Suspected patients p.seeming += number; country.seeming += number; } break;
If "infected patients", it is determined as the inflow of infected people, and the number of people in the provinces that have been inflow is taken for data processing
case "Infected patients": String p2 = information[3];//Get the name of the province in the flow if(map.get(p2) != null) {//If the province has already appeared Province anotherProvince = map.get(p2); anotherProvince.infect += number; p.infect -= number; } else { Province province2 = new Province(); province2.name = p2; province2.infect += number; p.infect -= number; map.put(p2, province2); } break;
If it is "suspected patient", judge whether it is confirmed or inflow. If it is confirmed, reduce the number of suspected patients and increase the number of infected patients. If it is inflow, handle as above
case "Suspected patients": //Judge whether it is inflow or diagnosis if(information[2].equals("inflow")) { String p3 = information[3];//Get the name of the province in the flow if(map.get(p3) != null) {//If the province has already appeared Province anotherProvince = map.get(p3); anotherProvince.seeming += number; p.seeming -= number; } else { Province province3 = new Province(); province3.name = p3; province3.infect += number; p.infect -= number; map.put(p3, province3); } } else {//Diagnosis p.infect += number; p.seeming -= number; country.infect += number; country.seeming -= number; } break;
In case of "death", reduce the number of infections and increase the number of deaths
case "death": p.infect -= number; p.dead += number; country.infect -= number; country.dead += number; break;
In case of "cure", reduce the number of infections and increase the number of cures
case "Cure": p.infect -= number; p.cured += number; country.infect -= number; country.cured += number; break;
In case of "exclusion", reduce the number of suspects
case "Exclude": p.seeming -= number; country.seeming -= number; break;
5. Get the number of people in each line
Use substring to intercept the substring before the subscript of "person"
public static int getNumber(String[] information) { //Number of people getting String numString = information[information.length - 1]; int index = numString.indexOf("people"); numString = numString.substring(0, index); int number = Integer.parseInt(numString); return number; }
6. Print results
Store all provinces in order in advance
public static String[] province = {"Whole country", "Anhui", "Beijing", "Chongqing", "Fujian", "Gansu", "Guangdong", "Guangxi", "Guizhou", "Hainan", "Hebei", "Henan", "Heilongjiang", "Hubei", "Hunan", "Jilin", "Jiangsu", "Jiangxi", "Liaoning", "Inner Mongolia", "Ningxia", "Qinghai", "Shandong", "Shanxi", "Shaanxi", "Shanghai", "Sichuan", "Tianjin", "Tibet", "Xinjiang", "Yunnan", "Zhejiang" };
Judge each province through for loop to see if output is needed
public static void printResult() { try { File output = new File(outputPath); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(output)); if(provinceItem.size() == 0) { for(String provinceName : province) { if(map.get(provinceName) != null) { printTheProvince(provinceName, osw); } } } else { for(String provinceName : province) { if(provinceItem.contains(provinceName)) { printTheProvince(provinceName, osw); } } } osw.flush(); osw.close(); } catch (IOException e) { } }
7. Judgment of each province
First, determine whether there is a type command. If there is one, output in the specified order with for and switch. Otherwise, output in the original order
if(map.get(provinceName) != null) { Province province = map.get(provinceName); if(typeItem.size() != 0) { for(String item : typeItem) { switch (item) { case "ip": System.out.print(" Infected patients" + province.infect + "people"); osw.write(" Infected patients" + province.infect + "people"); break; case "sp": System.out.print(" Suspected patients" + province.seeming + "people"); osw.write(" Suspected patients" + province.seeming + "people"); break; case "cure": System.out.print(" Cure" + province.cured + "people"); osw.write(" Cure" + province.cured + "people"); break; case "dead": System.out.print(" death" + province.dead + "people"); osw.write(" death" + province.dead + "people"); break; default: break; } } } else { osw.write(" Infected patients" + province.infect + "Suspected patient" + province.seeming + "Human cure" + province.cured + "Human death" + province.dead + "people"); System.out.print(" Infected patients" + province.infect + "Suspected patient" + province.seeming + "Human cure" + province.cured + "Human death" + province.dead + "people"); }
}
6, Unit test screenshots and descriptions
@Test1 tests whether command line parameters can be obtained correctly
@Test public void testSolveArgs() { String[] order = {"list", "-date", "2020-01-23", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt", "-type", "ip", "sp", "dead", "-province", "Fujian", "Zhejiang"}; InfectStatistic.main(order); }
@Test2 test prints a province of information
@Test public void testPrintTheProvince() throws IOException { File output = new File("C:\\\\Users\\\\Peter\\\\Documents\\\\GitHub\\\\InfectStatistic-main\\\\221701126\\\\result\\\\out.txt"); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(output)); InfectStatistic.printTheProvince("Fujian", osw); osw.flush(); osw.close(); }
[](https://images.cnblogs.com/cnblogs_com/SeinoNana/1650130/o_200219095419~_%60(%60_IQPT$$NE]%7D1HHTZ~M.png)
Since the province has not yet given any data, the number is zero.
@Test3 tests whether all provinces to be printed can be printed
@Test public void testPrintResult() { InfectStatistic.outputPath = "C:\\\\Users\\\\Peter\\\\Documents\\\\GitHub\\\\InfectStatistic-main\\\\221701126\\\\result\\\\out.txt"; InfectStatistic.provinceItem.add("Whole country"); InfectStatistic.provinceItem.add("Fujian"); InfectStatistic.provinceItem.add("Inner Mongolia"); InfectStatistic.printResult(); }
All provinces can be printed out. Since there is no data, the number of people is 0
@Test4 tests whether you can get the number of strings per line
@Test public void testGetNumber() { String[] testStr1 = {"Fujian", "Newly added", "Suspected patients", "5 people"}; System.out.println(InfectStatistic.getNumber(testStr1)); String[] testStr2 = {"Hubei", "Infected patients", "inflow", "Fujian", "23 people"}; System.out.println(InfectStatistic.getNumber(testStr2)); String[] testStr3 = {"Hubei", "Exclude", "Suspected patients", "15 people"}; System.out.println(InfectStatistic.getNumber(testStr3)); }
Can get it right
@Test5 tests the processing of each file and prints out the file name to be processed
@Test public void testSolveEveryFile() { Vector<String> toHandleFile = new Vector<String>(); toHandleFile.add("2020-01-22"); toHandleFile.add("2020-01-23"); toHandleFile.add("2020-01-27"); InfectStatistic.solveEveryFile(toHandleFile); }
@Test6 test obtains the maximum date in the directory and prints out the maximum date in the directory
@Test public void testGetMaxDate() { System.out.println(InfectStatistic.getMaxDate()); }
@Test7 test gets all the files in the directory that meet the requirements and prints out their file names
@Test public void testSolveDateOrder() { System.out.println("Test 1"); InfectStatistic.targetDate = "2020-01-29"; InfectStatistic.solveDateOrder(InfectStatistic.targetDate); System.out.println("Test 2"); InfectStatistic.targetDate = "2020-01-24"; InfectStatistic.solveDateOrder(InfectStatistic.targetDate); System.out.println("Test 3"); InfectStatistic.targetDate = "2020-01-27"; InfectStatistic.solveDateOrder(InfectStatistic.targetDate); }
@The Test8 test does not have the command of date option. It prints out the log of the latest date. The test command is: List - log "C: \ users \ Peter \ documents \ GitHub \ infectstatistic main \ 221701126 \ log" "- out" "C: \ users \ Peter \ documents \ GitHub \ infectstatistic main \ 221701126 \ result \ out. TXT" "- type" "IP" "SP" "dead" "- provision" "Fujian" "Zhejiang"
@Test public void testNoDate() { String[] order = {"list", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt", "-type" , "ip", "sp", "dead", "-province", "Fujian", "Zhejiang"}; InfectStatistic.main(order); }
@Test9 test date option, when the parameter is 2020-01-23
@Test public void testDate1() { String[] order = {"list", "-date", "2020-01-23", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt"}; InfectStatistic.main(order); }
@Test10 test date option, when the parameter is 2020-01-25, the result is the same as that of 2020-01-23
@Test public void testDate2() { String[] order = {"list", "-date", "2020-01-25", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt"}; InfectStatistic.main(order); }
@Test11 test date option, when the parameter is 2020-01-27
@Test public void testDate3() { String[] order = {"list", "-date", "2020-01-27", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt"}; InfectStatistic.main(order); }
@Test12 test date option, when the parameter is 2020-01-28, print "date out of range"
@Test public void testDate4() { String[] order = {"list", "-date", "2020-01-28", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt"}; InfectStatistic.main(order); }
@Test13 test type option, with curve IP as the parameter
@Test public void testType1() { String[] order = {"list", "-date", "2020-01-22", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt", "-type", "cure", "ip"}; InfectStatistic.main(order); }
@Test14 test type option, with the parameter curve dead IP sp
@Test public void testType2() { String[] order = {"list", "-date", "2020-01-23", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt", "-type", "cure", "dead", "ip", "sp", "-province", "Whole country", "Zhejiang", "Fujian"}; InfectStatistic.main(order); }
@Test15 test - province option, the parameter is Zhejiang and Fujian
@Test public void testProvince1() { String[] order = {"list", "-date", "2020-01-23", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt", "-province", "Whole country", "Zhejiang", "Fujian"}; InfectStatistic.main(order); }
@Test16 test - province option. The province to be printed in the test does not appear in the file. The parameter is Shaanxi and Yunnan
@Test public void testProvince2() { String[] order = {"list", "-date", "2020-01-23", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt", "-province", "Shaanxi", "Yunnan"}; InfectStatistic.main(order); }
@Test17 test - province option, the parameter is Fujian, Shandong, Guangxi
@Test public void testProvince3() { String[] order = {"list", "-date", "2020-01-23", "-log", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\log", "-out", "C:\\Users\\Peter\\Documents\\GitHub\\InfectStatistic-main\\221701126\\result\\out.txt", "-province", "Fujian", "Shandong", "Whole country", "Guangxi"}; InfectStatistic.main(order); }
7, Unit test coverage optimization and performance testing
Unit test coverage is relatively high due to comprehensive testing, covering almost all code. There are also some areas not covered, that is, the test case is not in place or some exception catching statements, with little room for improvement
8, Code specification
9, Journey and harvest
Before this assignment, I haven't had the experience of team development, so I won't use version control tools like git and open-source warehouse tools like github, but in this development process, I try to query related articles step by step to use github.
The coding task of this time is epidemic statistics program. I think it's not difficult to develop this program on the basis of learning java. It's only necessary to review the previous knowledge of java carefully. But in the development process, it's more important to learn to record your own code version. Although it's still a small job of personal development, it's good to develop the habit of storing code version It's used to be necessary for every developer, so I used GitHub destop to record my own perfect project for this project. I divided this project into many modules. When I finished the development of one module, I would commit it. So even if there is any problem in the subsequent code, I can also trace back to the previous normal version according to the historical records. It can be said that I learned this skill is ten It's convenient and necessary.
After the whole task, I also learned unit testing. What is unit testing? It is to encapsulate many test functions into a class, so that all the tests needed to be carried out in a one-time run can be completed without testing one example at the command line, which improves the efficiency of testing. Testing is also an essential process in development. With testing, I can clearly know what shortcomings and bug s exist in my code , after the repair, you can carry out a commit, so the stability of the code is constantly improving, and it is also a developer's responsibility to learn to use unit tests to test your own code. The code you write will be tested by yourself, so that if the code needs to be modified and maintained in the future, you can fix the problem without rushing.
After the completion of the test, I tested the code coverage and performance through the development tool eclipse, and found that I had some imperfect syntax processing in the process of writing code, some methods that would not be used, or the order or nesting of some statements was very complicated, which would reduce the coverage and performance. Therefore, I followed the example of coverage Methods to improve, delete unnecessary statements, adjust the statement structure, and improve some coverage and performance. In the development process, developers should not only pay attention to the completion of tasks, but also pay attention to the performance of the system, because only we know our own code, so it is necessary to make some simple Optimization on this A good habit, an excellent project should not only fulfill the needs of users, but also have significant advantages in convenient performance. Even if the performance is improved a little, it is also a huge progress in the computer world.
The rest of the work, such as code specification and document writing, is also very important. It not only inspects the programming ability of a developer, but also tests the normalization and order of its programs. A developer should not be disordered, but should be orderly.
Through this task, I have a deep understanding of the development process of software engineering, and I am looking forward to the cooperation of multiple people in the future. But before the cooperation of multiple people, each of us should first standardize ourselves, so that the whole team can be orderly United.
10, Five related warehouses
1.vue.js
A large single page application with 45 pages is built based on vue2 + vuex, which imitates a shopping demo created by hungry Yao. It is suitable for learners who can't find demo consolidation to practice.
2.ant design pro
It is based on the framework of Ant Design, which can be used to build the ant d framework quickly, which is convenient for development and saves time.
3.react select
React selection control, originally built for keystone JS, is a method for developing react components, which can be used out of the box or customized.
4.h5 template
Tencent wechat's optimized H5 dynamic template helps you quickly build a full screen scrolling H5 page.
5.css.animation
A cross browser CSS animation library, simple and easy to use, with many controls can be used directly.