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.
33 lines
1.0 KiB
33 lines
1.0 KiB
package com.example.moviecli.command;
|
|
|
|
import com.example.moviecli.repository.MovieRepository;
|
|
import com.example.moviecli.view.ConsoleView;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
import com.example.moviecli.model.Movie;
|
|
public class StatCommand implements Command {
|
|
private final ConsoleView view;
|
|
|
|
public StatCommand(ConsoleView view) {
|
|
this.view = view;
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "stat";
|
|
}
|
|
|
|
@Override
|
|
public void execute(String[] args, MovieRepository repository) {
|
|
var movies = repository.getAll();
|
|
if (movies.isEmpty()) {
|
|
view.printInfo("无数据,请先 crawl。");
|
|
return;
|
|
}
|
|
Map<String, Long> scoreCount = movies.stream()
|
|
.collect(Collectors.groupingBy(Movie::getScore, Collectors.counting()));
|
|
view.printInfo("评分分布:");
|
|
scoreCount.forEach((score, count) ->
|
|
System.out.println(score + " 分 -> " + count + " 条"));
|
|
}
|
|
}
|