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.

71 lines
2.8 KiB

package com.example;
import com.example.bean.Movie;
import com.example.crawler.MovieCrawler;
import com.example.chart.DrawChart;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// 1. 爬取数据
MovieCrawler crawler = new MovieCrawler();
List<Movie> movies = crawler.start();
// 2. 数据清洗
List<Movie> cleanData = new ArrayList<>();
for (Movie movie : movies) {
if (movie.getTitle() != null && !movie.getTitle().isBlank()) {
cleanData.add(movie);
}
}
// ===================== CSV 保存功能 恢复!=====================
try (FileWriter writer = new FileWriter("movie_top100.csv")) {
writer.write("电影名称,评分,导演,上映年份\n");
for (Movie m : cleanData) {
writer.write(m.getTitle() + "," + m.getScore() + "," + m.getDirector() + "," + m.getYear() + "\n");
}
System.out.println("✅ CSV 文件已保存:movie_top100.csv");
} catch (Exception e) {
e.printStackTrace();
}
// ==============================================================
// 3. 评分统计
Map<String, Long> scoreCount = cleanData.stream()
.collect(Collectors.groupingBy(Movie::getScore, Collectors.counting()));
// 4. 控制台打印表格
System.out.println("\n==========================================");
System.out.println(" 电影评分统计表格 ");
System.out.println("==========================================");
System.out.printf("%-10s %-10s %-10s%n", "评分", "数量(部)", "占比(%)");
System.out.println("------------------------------------------");
double total = cleanData.size();
for (Map.Entry<String, Long> entry : scoreCount.entrySet()) {
String score = entry.getKey();
long count = entry.getValue();
double rate = (count * 100.0) / total;
System.out.printf("%-12s %-12d %-10.2f%n", score, count, rate);
}
System.out.println("------------------------------------------");
System.out.printf("总计:%d 部电影%n", (long) total);
System.out.println("==========================================");
// 5. 生成正常折线图:按排名 1→100 顺序连线
List<Double> scoreList = new ArrayList<>();
for (Movie movie : cleanData) {
scoreList.add(Double.parseDouble(movie.getScore()));
}
DrawChart drawing = new DrawChart();
drawing.drawByOrder(scoreList);
}
}