



首先创建Movie类,用来表示电影的各种属性,包括标题、导演、简介以及海报链接。此外,它还包含一个静态列表,用于模拟存储和操作电影数据的数据库。
Movie类的实现代码如下。
public class Movie {
private Long id; // 电影的唯一标识
private String title; // 电影标题
private String director; // 导演
private String description; // 电影简介
private String posterUrl; // 海报图片的 URL
// 静态列表模拟数据库
private static List<Movie> movies = new ArrayList<>();
// 省略构造函数、getters、setters
// 使用静态方法来模拟数据库操作
public static List<Movie> getAllMovies() {
return new ArrayList<>(movies);
}
public static void addMovie(Movie movie) {
movie.setId((long) (movies.size() + 1)); // 简单的 ID 赋值逻辑
movies.add(movie);
}
public static Movie getMovieById(Long id) {
return movies.stream()
.filter(movie -> movie.getId().equals(id))
.findFirst()
.orElse(null);
}
public static void deleteMovie(Long id) {
movies.removeIf(movie -> movie.getId().equals(id));
}
}
Movie类的功能如下。
getAllMovies():返回所有电影的列表。
addMovie(Movie movie):向列表中添加一个新电影。
getMovieById(Long id):根据ID查找并返回相应的电影。
deleteMovie(Long id):根据ID删除对应的电影。
Comment类用于表示用户对电影的评论和评分,代码如下。
public class Comment {
private Long id; // 评论的唯一标识
private Movie movie; // 关联的电影对象
private String content; // 评论内容
private Integer rating; // 用户评分
// 使用静态列表模拟数据库
private static List<Comment> comments = new ArrayList<>();
// 省略构造函数、getters、setters
// 使用静态方法来模拟数据库操作
public static List<Comment> getCommentsByMovieId(Long movieId) {
return comments.stream()
.filter(comment -> comment.getMovie().getId().equals(movieId))
.collect(Collectors.toList());
}
public static void addComment(Comment comment) {
comment.setId((long) (comments.size() + 1)); // 简单的ID赋值逻辑
comments.add(comment);
}
}
Comment类的功能如下。
getCommentsByMovieId():根据电影ID查找评论。
addComment():添加电影评论。