diff --git a/w7/InvalidInputException.java b/w7/InvalidInputException.java new file mode 100644 index 0000000..4309062 --- /dev/null +++ b/w7/InvalidInputException.java @@ -0,0 +1,9 @@ +public class InvalidInputException extends Exception { + InvalidInputException() { + super("Cannot handle empty input"); + } + + InvalidInputException(String input, Throwable cause) { + super(String.format("Cannot handle input: \"%s\"", input), cause); + } +} diff --git a/w7/MainSeven.java b/w7/MainSeven.java new file mode 100644 index 0000000..f8be922 --- /dev/null +++ b/w7/MainSeven.java @@ -0,0 +1,43 @@ +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +public class MainSeven { + + private static int parseLine(String line) throws InvalidInputException { + String trimmed = line.trim(); + if (trimmed.isEmpty()) { + throw new InvalidInputException(); + } + + try { + return Integer.parseInt(trimmed); + } catch (NumberFormatException e) { + throw new InvalidInputException(trimmed, e); + } + } + + public static void main(String[] args) { + try (FileReader fr = new FileReader("scores.txt")) { + BufferedReader br = new BufferedReader(fr); + + int sum = 0; + String line; + while ((line = br.readLine()) != null) { + try { + sum += parseLine(line); + } catch (InvalidInputException e) { + System.out.println(e.getMessage()); + } + } + + br.close(); + System.out.println(sum); + } catch (FileNotFoundException e) { + System.out.println("File not found: " + e.getMessage()); + } catch (IOException e) { + System.out.println("IOException: " + e.getMessage()); + } + } +} diff --git a/w7/MainSevenBefore.java b/w7/MainSevenBefore.java new file mode 100644 index 0000000..5b72238 --- /dev/null +++ b/w7/MainSevenBefore.java @@ -0,0 +1,16 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class MainSevenBefore { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new FileReader("scores.txt")); + int sum = 0; + String line; + while ((line = br.readLine()) != null) { + sum += Integer.parseInt(line); + } + br.close(); + System.out.println(sum); + } +} diff --git a/w7/refactor.diff b/w7/refactor.diff new file mode 100644 index 0000000..449cc2a --- /dev/null +++ b/w7/refactor.diff @@ -0,0 +1,57 @@ +diff --git "a/.\\MainSevenBefore.java" "b/.\\MainSeven.java" +index 5b72238..f8be922 100644 +--- "a/.\\MainSevenBefore.java" ++++ "b/.\\MainSeven.java" +@@ -1,16 +1,43 @@ + import java.io.BufferedReader; ++import java.io.FileNotFoundException; + import java.io.FileReader; + import java.io.IOException; + +-public class MainSevenBefore { +- public static void main(String[] args) throws IOException { +- BufferedReader br = new BufferedReader(new FileReader("scores.txt")); +- int sum = 0; +- String line; +- while ((line = br.readLine()) != null) { +- sum += Integer.parseInt(line); ++public class MainSeven { ++ ++ private static int parseLine(String line) throws InvalidInputException { ++ String trimmed = line.trim(); ++ if (trimmed.isEmpty()) { ++ throw new InvalidInputException(); ++ } ++ ++ try { ++ return Integer.parseInt(trimmed); ++ } catch (NumberFormatException e) { ++ throw new InvalidInputException(trimmed, e); ++ } ++ } ++ ++ public static void main(String[] args) { ++ try (FileReader fr = new FileReader("scores.txt")) { ++ BufferedReader br = new BufferedReader(fr); ++ ++ int sum = 0; ++ String line; ++ while ((line = br.readLine()) != null) { ++ try { ++ sum += parseLine(line); ++ } catch (InvalidInputException e) { ++ System.out.println(e.getMessage()); ++ } ++ } ++ ++ br.close(); ++ System.out.println(sum); ++ } catch (FileNotFoundException e) { ++ System.out.println("File not found: " + e.getMessage()); ++ } catch (IOException e) { ++ System.out.println("IOException: " + e.getMessage()); + } +- br.close(); +- System.out.println(sum); + } + } diff --git a/w7/scores.txt b/w7/scores.txt new file mode 100644 index 0000000..079c5d8 --- /dev/null +++ b/w7/scores.txt @@ -0,0 +1,7 @@ +96 +85 +74 +62 + +58 +y76 diff --git a/w7/usage.md b/w7/usage.md new file mode 100644 index 0000000..b8c3169 --- /dev/null +++ b/w7/usage.md @@ -0,0 +1,24 @@ +# 运行记录 + +## 正常情况 + +```shell +$ javac MainSeven.java && java MainSeven +Cannot handle empty input +Cannot handle input: "y76" +375 +``` + +## 文件移动或删除 + +```shell +$ javac MainSeven.java && java MainSeven +File not found: scores.txt (系统找不到指定的文件。) +``` + +## 删除文件读取权限 + +```shell +$ javac MainSeven.java && java MainSeven +File not found: scores.txt (拒绝访问。) +```