diff --git a/w9/Article.java b/w9/Article.java new file mode 100644 index 0000000..e2b0402 --- /dev/null +++ b/w9/Article.java @@ -0,0 +1,67 @@ +package com.example.datacollect.model; + +public class Article { + private String title; + private String url; + private String content; + private String author; + private String publishdate; + + public Article(String title, String url, String content, String author, String publishdate) { + this.title = title; + this.url = url; + this.content = content; + this.author = author; + this.publishdate = publishdate; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + 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 String getPublishdate() { + return publishdate; + } + + public void setPublishdate(String publishdate) { + this.publishdate = publishdate; + } + + @Override + public String toString() { + return "Article{" + + "title='" + title + '\'' + + ", url='" + url + '\'' + + ", author='" + author + '\'' + + ", publishdate='" + publishdate + '\'' + + '}'; + } +} diff --git a/w9/Command.java b/w9/Command.java new file mode 100644 index 0000000..f6e49b5 --- /dev/null +++ b/w9/Command.java @@ -0,0 +1,9 @@ +package com.example.datacollect.command; + +import com.example.datacollect.model.Article; +import java.util.List; + +public interface Command { + String getName(); + void execute(String[] args, List
articles); +} diff --git a/w9/CrawlCommand.java b/w9/CrawlCommand.java new file mode 100644 index 0000000..6f9e4e7 --- /dev/null +++ b/w9/CrawlCommand.java @@ -0,0 +1,27 @@ +package com.example.datacollect.command; + +import com.example.datacollect.model.Article; +import com.example.datacollect.view.ConsoleView; +import java.util.List; + +public class CrawlCommand implements Command { + private final ConsoleView view; + + public CrawlCommand(ConsoleView view) { + this.view = view; + } + + @Override + public String getName() { + return "crawl"; + } + + @Override + public void execute(String[] args, List
articles) { + if (args.length < 2) { + view.printError("Usage: crawl "); + return; + } + view.printInfo("Stub: would crawl " + args[1]); + } +} diff --git a/w9/CrawlerController.java b/w9/CrawlerController.java new file mode 100644 index 0000000..f921c65 --- /dev/null +++ b/w9/CrawlerController.java @@ -0,0 +1,54 @@ +package com.example.datacollect.controller; + +import com.example.datacollect.command.Command; +import com.example.datacollect.command.ClearCommand; +import com.example.datacollect.command.CrawlCommand; +import com.example.datacollect.command.ExitCommand; +import com.example.datacollect.command.HelpCommand; +import com.example.datacollect.command.HistoryCommand; +import com.example.datacollect.command.ListCommand; +import com.example.datacollect.model.Article; +import com.example.datacollect.view.ConsoleView; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.ArrayList; + +public class CrawlerController { + private final Map commands = new HashMap<>(); + private final ConsoleView view; + private final List
articles; + private final List commandHistory = new ArrayList<>(); + + public CrawlerController(ConsoleView view, List
articles) { + this.view = view; + this.articles = articles; + register(new HelpCommand(view)); + register(new ListCommand(view)); + register(new CrawlCommand(view)); + register(new ExitCommand(view)); + register(new ClearCommand(view)); + register(new HistoryCommand(view, commandHistory)); + } + + private void register(Command command) { + commands.put(command.getName(), command); + } + + public void handle(String input) { + String text = input == null ? "" : input.trim(); + if (text.isEmpty()) { + return; + } + + String[] args = text.split("\\s+"); + String cmdName = args[0].toLowerCase(); + Command command = commands.get(cmdName); + if (command == null) { + view.printError("Unknown command: " + cmdName); + return; + } + commandHistory.add(text); + command.execute(args, articles); + } +} diff --git a/w9/ExitCommand.java b/w9/ExitCommand.java new file mode 100644 index 0000000..e0eff23 --- /dev/null +++ b/w9/ExitCommand.java @@ -0,0 +1,24 @@ +package com.example.datacollect.command; + +import com.example.datacollect.model.Article; +import com.example.datacollect.view.ConsoleView; +import java.util.List; + +public class ExitCommand implements Command { + private final ConsoleView view; + + public ExitCommand(ConsoleView view) { + this.view = view; + } + + @Override + public String getName() { + return "exit"; + } + + @Override + public void execute(String[] args, List
articles) { + view.printSuccess("Bye!"); + System.exit(0); + } +}