https://github.com/misterchaos/WordCount
Second, problem-solving ideas
2.1 Basic Requirements Analysis
After carefully reading the title analysis reveals the basic needs of the project are as follows:
wc.exe -c // returns the number of characters in the file file.c |
wc.exe -c // returns the number of characters in the file file.c |
Wc.exe -w // Returns the file number of words file.c |
wc.exe -l // returns the number of rows of file file.c |
wc.exe -a // return more complex data (line / space lines / comment lines) |
wc.exe -s // recursive processing files that meet the conditions of the directory. |
wc.exe -x // display a graphical interface |
2.2 realization of ideas
Because this project need to make a graphical interface, and the need to use more string manipulation, considering the development efficiency and process efficiency, and ultimately choose Java as a development language, using Javafx graphical interface development. Because not done before Javafx project, so it is necessary to find a Javafx use of tutorials to learn. Overall project requires parsing command console input function, file reading function, function, function graphical interface statistics text messages and so on.
Third, the design program
3.1 System Architecture
Use basic MVC structure:
System flowchart 3.2
The system flow chart is as follows:
Fourth, the project difficulty
4.1 How to file statistics accurately
Traversal using progressive way, statistics the number of characters per line, words, and finally get a summary total number of rows, information on the total number of characters, words, and so on.
4.2 How to build a graphical interface
Use scenebuilder build a basic graphical interface.
4.3 How to recursively traverse to find files in the folder in line with wildcards.
The wildcard user input converted to regular expressions, recursive get all the files in the current directory, file name matches one by one, to get eligible files.
V. Code Description
1 /** 2 * 核心统计方法 3 * 4 * @param reader 5 * @return 6 * @throws IOException 7 */ 8 private static CountResult count(BufferedReader reader) throws IOException { 9 CountResult result = new CountResult(); 10 String character = "\\w"; 11 String word = "[a-zA-Z]+"; 12 String line; 13 //Multi-line comments 14 Boolean isAnnotation = to false ; 15 the while (! (= Reader.readLine Line ()) = null ) { 16 // statistics that characters . 17 result.character + = CountUtil.count (Line, Character); 18 // number of words in a row statistics . 19 result.word + = CountUtil.count (line, Word); 20 is // statistics line 21 is result.line ++ ; 22 is // statistical blank lines, lines of code, the comment line 23 is IF ( line.trim (). isEmpty ()) { 24 // empty line 25 ++ result.emptyLine ; 26 is // begin multi-line comment 27 } the else IF (isAnnotation) { 28 result.annotationLine ++ ; 29 // multi-line comment end 30 IF (line.trim () endsWith ( "* /." {)) 31 is = isAnnotation to false ; 32 } 33 is } the else IF (.. line.trim () the contains ( "//") || line.trim () the contains ( "*" )) { 34 is // comment line 35 ++ result.annotationLine ; 36 // if the line comment 37 [ IF (!. (line.trim () startsWith ( "/") || line.trim () startsWith ( "*." ))) { 38 is result.codeLine ++ ; 39 } 40 // multi-line comments start 41 is IF (line.trim () startsWith ( "/ *." )) { 42 is isAnnotation = to true ; 43 is } 44 is } the else { 45 // line 46 is result.codeLine ++ ; 47 } 48 } 49 return Result; 50 } 51 is 52 is 53 is / ** 54 is * performed according to the command line parameters 55 * 56 is * @param args 57 is * / 58 public static void WC (String [] args) { 59 Long the begin = System.currentTimeMillis (); 60 the try { 61 is // plurality file 62 is IF (Constant.OPS_S.equals (args [0 ])) { 63 is IF (args.length ==. 3 ) { 64 List <File> Files = FileUtil.listFileByRegex (RegexUtil.toRegex (args [2 ])); 65 for (File F: Files) { 66 countFile (args [. 1 ], F); 67 } 68 } the else { 69 the throw new new Exception ( "lack of the necessary parameters, exemplary instructions: .c -s -a *" ); 70 } 71 is } the else { 72 // single file 73 is countFile (args [ 0],new new File (args [. 1 ])); 74 } 75 System.out.println ( "Run Processed:" + (System.currentTimeMillis () - the begin) + "MS" ); 76 } the catch (Exception E) { 77 System.out.println ( "error run:" + e.getMessage ()); 78 } 79 80 } 81 82 83 / ** 84 * statistics outputting a file 85 * 86 * @param OPS to be counted information 87 * @param file 文件名 88 * @throws Exception 89 */ 90 public static void countFile(String ops, File file) throws Exception { 91 System.out.println("文件信息:" + BeanUtil.getFileInfo(file)); 92 switch (ops) { 93 case Constant.OPS_C: 94 System.out.println("字符数:" + countService.countCharacter(file)); 95 break; 96 case Constant.OPS_W: 97 System.out.println("单词数:" + countService.countWord(file)); 98 break; 99 case Constant.OPS_L: 100 System.out.println("行数:" + countService.countLine(file)); 101 break; 102 case Constant.OPS_A: 103 CountResult result = countService.countAll(file); 104 System.out.println("字符数:" + result.character); 105 System.out.println("单词数:" + result.word); 106 System.out.println("行数:" + result.line); 107 System.out.println ( "blank line number:" + result.emptyLine); 108 System.out.println ( "lines of code:" + result.codeLine); 109 System.out.println ( "comment line number:" + result.annotationLine); 110 BREAK ; 111 default : 112 the throw new new Exception ( "unrecognized command types:" + OPS); 113 } 114 }
Sixth, the test results
6.1 single-file test
Test file:
Test Results
More than 6.2 file test
Test file:
Test Results:
6.3 graphical interface test
Select the file:
statistical results:
Seven, PSP form
PSP2.1 | Personal Software Process Stages | Estimated time consuming (minutes) | The actual time-consuming (minutes) |
---|---|---|---|
Planning | plan | 5 | 5 |
· Estimate | • Estimate how much time this task requires | 5 | 5 |
Development | Develop | 180 | 265 |
· Analysis | · Needs analysis (including learning new technologies) | 30 | 60 |
· Design Spec | Generate design documents | 5 | 10 |
· Design Review | · Design Review (and his colleagues reviewed the design documents) | 5 | 5 |
· Coding Standard | · Code specifications (development of appropriate norms for the current development) | 5 | 5 |
· Design | · Specific design | 5 | 5 |
· Coding | · Specific coding | 120 | 150 |
· Code Review | · Code Review | 5 | 10 |
· Test | · Test (self-test, modify the code, submit modifications) | 5 | 20 |
Reporting | report | 15 | 25 |
· Test Report | · testing report | 5 | 10 |
· Size Measurement | · Computing workload | 5 | 10 |
· Postmortem & Process Improvement Plan | · Hindsight, and propose process improvement plan | 5 | 5 |
total | 200 | 295 |
Eight, Project Summary
This project gives me the biggest difference is the use of the PSP table to manage their own time, from the actual results, the actual time-consuming slightly more than some of his plans will mainly have some problems are not expected within and, in the process of writing code also continued to generate new ideas, will generate more demand. The place is more time-consuming forms graphical interface, beginning just do a single interface file information display, but then I want to do more than file before he went to learn to use javafx table, Javafx do indeed form a little trouble, it took some time to learn its use, in general, the harvest is still a lot of.