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 movies = crawler.start(); // 2. 数据清洗 List 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 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 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 scoreList = new ArrayList<>(); for (Movie movie : cleanData) { scoreList.add(Double.parseDouble(movie.getScore())); } DrawChart drawing = new DrawChart(); drawing.drawByOrder(scoreList); } }