From 82e5cb6befc422eaa46a76e04669084f735d13a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=95=85=E6=98=A5?= <3481369387@qq.com> Date: Thu, 30 Apr 2026 19:33:46 +0800 Subject: [PATCH] W9 --- W9/Article.java | 56 ++++++++++++++++++++++++++++++++ W9/HistoryCommand.java | 26 +++++++++++++++ W9/Test.java | 72 ++++++++++++++++++++++++++++++++++++++++++ W9/W9.txt | 0 4 files changed, 154 insertions(+) create mode 100644 W9/Article.java create mode 100644 W9/HistoryCommand.java create mode 100644 W9/Test.java create mode 100644 W9/W9.txt diff --git a/W9/Article.java b/W9/Article.java new file mode 100644 index 0000000..217995c --- /dev/null +++ b/W9/Article.java @@ -0,0 +1,56 @@ +import java.time.LocalDate; + +public class Article { + private String title; + private String content; + private String author; + private LocalDate publishDate; + + public Article(String title, String content, String author, LocalDate publishDate) { + this.title = title; + this.content = content; + this.author = author; + this.publishDate = publishDate; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public LocalDate getPublishDate() { + return publishDate; + } + + public void setPublishDate(LocalDate publishDate) { + this.publishDate = publishDate; + } + + @Override + public String toString() { + return "Article{" + + "title='" + title + '\'' + + ", author='" + author + '\'' + + ", publishDate=" + publishDate + + '}'; + } +} \ No newline at end of file diff --git a/W9/HistoryCommand.java b/W9/HistoryCommand.java new file mode 100644 index 0000000..ca28c45 --- /dev/null +++ b/W9/HistoryCommand.java @@ -0,0 +1,26 @@ +import java.util.ArrayList; +import java.util.List; + +public class HistoryCommand { + private final List commandHistory = new ArrayList<>(); + + public void addCommand(String command) { + commandHistory.add(command); + } + + // 返回副本,避免外部直接修改内部列表 + public List getHistory() { + return new ArrayList<>(commandHistory); + } + + public void printHistory() { + System.out.println("=== 命令历史 ==="); + for (int i = 0; i < commandHistory.size(); i++) { + System.out.printf("%d: %s%n", i + 1, commandHistory.get(i)); + } + } + + public void clearHistory() { + commandHistory.clear(); + } +} \ No newline at end of file diff --git a/W9/Test.java b/W9/Test.java new file mode 100644 index 0000000..e30861d --- /dev/null +++ b/W9/Test.java @@ -0,0 +1,72 @@ +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +public class Test { + // 暗色主题常量(修改一处常量示例) + public static final String BACKGROUND_COLOR = "#1E1E1E"; // 深灰(暗色) + public static final String TEXT_COLOR = "#FFFFFF"; // 白色 + + // 命令别名映射 + private static final Map ALIASES = new HashMap<>(); + static { + ALIASES.put("c", "crawl"); + ALIASES.put("h", "help"); + } + + // URL 格式验证正则 + private static final Pattern URL_PATTERN = + Pattern.compile("^(https?://)?[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)+(/[a-zA-Z0-9-./]*)*$"); + + public static boolean isValidUrl(String url) { + if (url == null || url.isBlank()) { + return false; + } + return URL_PATTERN.matcher(url).matches(); + } + + public static String resolveAlias(String inputCommand) { + String[] parts = inputCommand.split(" ", 2); + String command = parts[0]; + String args = parts.length > 1 ? parts[1] : ""; + + String realCommand = ALIASES.getOrDefault(command, command); + return args.isEmpty() ? realCommand : realCommand + " " + args; + } + + public static void main(String[] args) { + // 1. 测试 Article 类 + Article article = new Article( + "Java MVC 入门", + "这是一篇关于MVC模式的文章...", + "张三", + LocalDate.now() + ); + System.out.println("=== 测试 Article 类 ==="); + System.out.println(article); + + // 2. 测试 HistoryCommand + HistoryCommand history = new HistoryCommand(); + history.addCommand("c https://example.com"); + history.addCommand("help"); + history.addCommand("exit"); + System.out.println("\n=== 测试 HistoryCommand ==="); + history.printHistory(); + + // 3. 测试命令别名 + System.out.println("\n=== 测试命令别名 ==="); + System.out.println("输入:c https://test.com → 解析后:" + resolveAlias("c https://test.com")); + System.out.println("输入:h → 解析后:" + resolveAlias("h")); + + // 4. 测试 URL 验证 + System.out.println("\n=== 测试 URL 验证 ==="); + System.out.println("https://example.com → " + (isValidUrl("https://example.com") ? "有效" : "无效")); + System.out.println("invalid-url → " + (isValidUrl("invalid-url") ? "有效" : "无效")); + + // 5. 测试暗色主题常量 + System.out.println("\n=== 测试暗色主题常量 ==="); + System.out.println("背景色:" + BACKGROUND_COLOR); + System.out.println("文字色:" + TEXT_COLOR); + } +} diff --git a/W9/W9.txt b/W9/W9.txt new file mode 100644 index 0000000..e69de29