WordCount (Java implementation)

 

Github project Address: https://github.com/linzworld/word-count

Project requirements

Title Description

Word Count

  1. Achieve a simple and complete software tools (statistical characteristics of the source program).

  2. Unit test, regression test, performance test, the use of associated tools in the course of achieving the above program.

  3. To practice Personal Software Process (PSP), and gradually record their own time spent in each part of software engineering.

WC project requirements

  1. wc.exe is a common tool, it can count the number of characters of text files, words, and lines. The project asked to write a command line program, to mimic the function of the existing wc.exe, and be expanded, given the number of characters in a programming language source files, words, and lines.

  2. Implement a statistical program, the number of characters it can correct statistics program file, the number of words, lines, and also has other extended functions, and can handle multiple files quickly. Specific functional requirements: mode handles user needs to:

    wc.exe [parameter] [file_name]

  3. The basic list of features:

    wc.exe -c file.c // returns the number of characters in the file file.c of (complete)

    wc.exe -w file.c // Returns the number of files file.c word of (complete)

    wc.exe -l file.c // file file.c the number of rows returned (completed)

  4. extensions:

    -s recursive processing files that meet the conditions of the directory. (Unfinished) -a return more complex data (line / space lines / comment lines). (carry out)

    Blank line: Bank format all control characters or spaces, if included code, no more than a displayable character, such as "{."

    Line: Bank codes comprises more than one character.

    Comment lines: The Bank is not a line of code, and the Bank includes comments. An interesting example is that some programmers will add a comment behind a single character:

    } // NOTE In this case, the line belongs to a comment line.

  5. Advanced Features:

-x parameter. This parameter is used alone. If you have the command line parameters, the program will display a graphical interface, the user can select a single file through the interface, the program will display the file number of characters, lines, etc. All statistics.

Demand For example:   wc.exe -s -a * .c (unfinished)

The number of lines of code returns the current directory and all subdirectories * .c files, the number of blank lines, comments, number of rows.

Completion of the statistical work

The following lists the functions of all kinds of detailed requirements.

basic skills

Support -c (completion) support -w (completion) support -l (complete)

extensions

Support -s parameter (unfinished) supports -a parameter (completed) supports a variety of file wildcard (* ,?)

Advanced Features

The basic operation of Windows GUI program (unfinished) support through a graphical interface to select the file information support through a graphical interface to show files

 


Problem-solving ideas and precautions

  1. The entire project is divided into two parts, one is a console application, is a graphical page JavaFX (unfinished), share the same set of process performance function.

  2. Just beginning to see the title of the time, the idea has always been in their minds, not too much to practice with the code;

  3. Good package features functions

  4. Wildcard match

  5. Use IO streams


The key code or design specifications


//获取文件行数
public static void lineCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  line = 0;
  while(reader.readLine()!=null) {
      line ++;
  }
  reader.close();
}
//获取单词数目,单词只能匹配连续的英文字母
public static void wordCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  word=0;

  String currentLine;
  Pattern pattern = Pattern.compile(RegexConst.WORD_REGEX);
  Matcher matcher;
  while((currentLine=reader.readLine())!=null) {
      currentLine=currentLine.trim();
      matcher= pattern.matcher(currentLine);
      while(matcher.find()) {
          word++;
      }
  }
  reader.close();
}
// Get the number of characters, the characters comprising blank characters here, so the length of strings to be used directly for accumulating each read 
public static void charCount (String fileName) throws IOException {
  the BufferedReader Reader = FileUtil.fileWrapper (fileName) ;
  Character = 0;
  String currentLine;
  the while (! (currentLine = reader.readLine ()) = null) {
      Character currentLine.length + = ();
  }
  reader.Close ();
}
@ Blank lines: All Bank format control character is a space or, if included code, no more than a displayable character, such as "{." 
static void nullLineCount public (String fileName) throws IOException {
  the BufferedReader Reader = FileUtil.fileWrapper (fileName);
  nullLine = 0;
  String currentLine;
  // use regular expressions
  while ((currentLine = reader.readLine () ) = null!) {
      IF (currentLine.matches (RegexConst.nullLineRegex)) {
          nullLine ++;
      }
  }
  reader.Close ();
} // comment line statistics public static void commentLineCount (String fileName) throws IOException {   the BufferedReader Reader = FileUtil.fileWrapper (fileName );   commentLine = 0;   String currentLine;







  while((currentLine=reader.readLine())!=null) {
      if(currentLine.matches(RegexConst.Single_Line_Note_REGEX)) {
          commentLine++;
      }else if(currentLine.matches(RegexConst.Block_Note_Start_REGEX)) {
          commentLine++;
          while((currentLine=reader.readLine())!=null) {
              if(currentLine.matches(RegexConst.Block_Note_End_REGEX)) {
                  commentLine++;
                  break;
              }else {
                  commentLine++;
              }
          }
      }
  }
}

//代码行数
public static void codeLineCount(String fileName) throws IOException {
  BufferedReader reader= FileUtil.fileWrapper(fileName);
  lineCount(fileName);
  nullLineCount(fileName);
  commentLineCount(fileName);
  codeLine = line - nullLine - commentLine;
}

Select the command

// Select command, and executing 
public static void selectCommand An (Command String, String fileName) throws IOException
{
    System.out.println ( "Current file name:" + fileName);
    // instantiate workCount
    Switch (Command) {// Analyzing command and performs
        Case "-C":
            BasicService.charCount (fileName);
            System.out.println ( "number of characters:" + BasicService.character);
            BREAK;
        Case "-l":
            BasicService.lineCount (fileName);
            the System .out.println ( "line number:" + BasicService.line);
            BREAK;
        Case "-w":
            BasicService.wordCount (fileName);
            System.out.println "number of words:" (+ BasicService.word);
            break;
        Case "-a":
            BasicService.codeLineCount (fileName);
            BasicService.nullLineCount (fileName);
            BasicService.commentLineCount (fileName);
            System.out.println ( "lines of code:" + BasicService.codeLine);
            System.out.println ( "empty rows:" + BasicService.nullLine);
            System.out.println ( "rows Note:" + BasicService.commentLine);
            BREAK;
        Case "-s":
            //BasicService.getAllFilefileName(fileName);
            BREAK;
        default:
            System.out.println ( "command format currently does not exist!" + command);
            BREAK;
    }
}

Test Run

 

 

 

 

 

 

 

 

 

 PSP

PSP2.1

Personal Software Process Stages

Estimated time consuming (minutes)

The actual time-consuming (minutes)

 

Planning

plan

 60

 70

 

· Estimate

• Estimate how much time this task requires

 60

 70

 

Development

Develop

 620

 1465

 

· Analysis

· Needs analysis (including learning new technologies)

 50

 60

 

· Design Spec

Generate design documents

 40

 400

 

· Design Review

· Design Review (and his colleagues reviewed the design documents)

 30

 40

 

· Coding Standard

· Code specifications (development of appropriate norms for the current development)

 10

 15

 

· Design

· Specific design

 150

 300

 

· Coding

· Specific coding

 240

 500

 

· Code Review

· Code Review

 40

 50

 

· Test

· Test (self-test, modify the code, submit modifications)

 60

100

 

Reporting

report

 130

 140

 

· Test Report

· testing report

 50

 60

 

· Size Measurement

· Computing workload

 40

 20

 

· Postmortem & Process Improvement Plan

· Hindsight, and propose process improvement plan

 40

 60

 

total

 

 910

 1675

 

 


  • Project Summary

    1. Through this java project, I realized that the actual development time and ideas are not the same kinds of problems exist in the actual development, and I shed this knowledge in IO grasp is not comprehensive enough, or for file operations less likely, resulting in a function do not back out.

    2. No time to do planning, development does not adequately focus, resulting in reduced efficiency.

    3. Too excellence, leading to lower high hand-eye situation.

     

Guess you like

Origin www.cnblogs.com/linzworld/p/12563547.html