故春 2 months ago
parent
commit
82e5cb6bef
  1. 56
      W9/Article.java
  2. 26
      W9/HistoryCommand.java
  3. 72
      W9/Test.java
  4. 0
      W9/W9.txt

56
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 +
'}';
}
}

26
W9/HistoryCommand.java

@ -0,0 +1,26 @@
import java.util.ArrayList;
import java.util.List;
public class HistoryCommand {
private final List<String> commandHistory = new ArrayList<>();
public void addCommand(String command) {
commandHistory.add(command);
}
// 返回副本,避免外部直接修改内部列表
public List<String> 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();
}
}

72
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<String, String> 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);
}
}

0
W9/W9.txt

Loading…
Cancel
Save