1. 程式人生 > >Java實現wc部分功能

Java實現wc部分功能

def planning lin 改進 hub \n personal earch https

GitHub倉庫:https://github.com/TaoTaoLv1/WcProject

一、開發前PSP表格預估*

PSP2.1Personal Software Process Stages預估耗時(分鐘)實際耗時(分鐘)
Planning 計劃 30 30
· Estimate · 估計這個任務需要多少時間 30 30
Development 開發 464 655
· Analysis · 需求分析 (包括學習新技術) 20 30
· Design Spec · 生成設計文檔 30 60
· Design Review · 設計復審 (和同事審核設計文檔) 60 60
· Coding Standard · 代碼規範 (為目前的開發制定合適的規範) 30 40
· Design · 具體設計 30 60
· Coding · 具體編碼 240 300
· Code Review · 代碼復審 30 60
· Test · 測試(自我測試,修改代碼,提交修改) 24 45
Reporting 報告 80 80
· Test Report · 測試報告 20 40
· Size Measurement · 計算工作量 30 20
· Postmortem & Process Improvement Plan · 事後總結, 並提出過程改進計劃 30 20
合計 574 765

二、項目思路

  • 基本要求

    • -c 統計文件字符數 (實現)

    • -w 統計文件詞數 (實現)

    • -l 統計文件行數(實現)

  • 擴展功能

    • -s 遞歸處理目錄下符合條件得文件(實現)
    • -a 返回文件代碼行 / 空行 / 註釋行(實現)
  • 高級功能

    • [ ] -x 圖形化界面(未實現)

  

參數實現部分:

  • -c:每讀入一個字符,計數器加一。
  • -w:每讀入一個不屬於單詞的字符,並且之前出現過屬於單詞的字符,計數器加一。單詞字符的限定初步設想以“A”~“Z”和“a”~“z”為準。
  • -l:每讀入一個換行符,計數器加一。
  • -a(空行\註釋行\代碼行):每讀入一行,一次性統計
  • -s:輸入文件路徑,繼續輸入查找要文件的名字,支持模糊搜索。

三、設計實現過程

技術分享圖片

代碼

啟動類main.java

public class main {
    public static void main(String[] args) {
        CommandController commandController = new CommandController();
        while (true) {
            System.out.println("\n*********************************************");
            System.out.println("**** -c [文件名]  返回文件字符數         ****");
            System.out.println("**** -w [文件名]  返回文件詞的數目       ****");
            System.out.println("**** -l [文件名]  返回文件行數           ****");
            System.out.println("**** -s [文件夾]  搜索文件名             ****");
            System.out.println("**** -a [文件名]  統計代碼行/空行/註釋行 ****");
            System.out.println("*********************************************");
            System.out.print("請輸入命令:");
            Scanner s = new Scanner(System.in);
            String m =s.nextLine();
            String arr[]=m.split("\\s");
            CommandServer server = commandController.SearchControlsCommand(arr[0]);
            server.command(arr[1]);
        }
    }
}

  

CommandController

public class CommandController {

    public CommandServer SearchControlsCommand(String command){
        CommandServer server = null;
        switch (command){
            case "-c": server = new CharacterCountServer();break; //返回文件字符數
            case "-w": server = new WordCountServer();break;      //返回文件詞的數目
            case "-l": server = new RowCountServer();break;       //返回文件行數
            case "-s": server = new ConditionFileServer();break;  //搜索文件名
            case "-a": server = new ComplexCountServer();break;   //統計代碼行 / 空行 / 註釋行
            default:
                System.out.println("參數輸入不正確");
        }
        return server;
    }
}

測試

測試文件:

技術分享圖片

測試結果:

技術分享圖片

代碼覆蓋率

進行代碼覆蓋率測試

技術分享圖片

技術分享圖片

整體代碼覆蓋是86%

Java實現wc部分功能