You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
37 lines
1.3 KiB
package com.example.datacollect.command;
|
|
import com.example.datacollect.model.Article;
|
|
import com.example.datacollect.model.DataService;
|
|
import com.example.datacollect.view.ConsoleView;
|
|
import java.util.List;
|
|
public class SearchCommand implements Command{
|
|
private final ConsoleView view;
|
|
public SearchCommand(ConsoleView view){
|
|
this.view=view;
|
|
}
|
|
@Override
|
|
public String getName(){
|
|
return "search";
|
|
}
|
|
public void execute(String[] args, DataService dataService){
|
|
if (args.length < 2) {
|
|
view.printError("Usage: search <keyword>");
|
|
return;
|
|
}
|
|
String keyword=args[1].toLowerCase();
|
|
List<Article> allArticles=dataService.getAllArticles();
|
|
int count=0;
|
|
view.printInfo("Searching for..."+keyword);
|
|
for(Article article: allArticles){
|
|
if(article.getTitle().toLowerCase().contains(keyword)||article.getUrl().toLowerCase().contains(keyword)){
|
|
count++;
|
|
System.out.println((count) + ". " + article.getTitle() + " | " + article.getUrl());
|
|
}
|
|
}
|
|
if(count==0){
|
|
view.printInfo("not search matched article");
|
|
}
|
|
else{
|
|
view.printSuccess("Successfully search "+count+" articles");
|
|
}
|
|
}
|
|
}
|
|
|