思路:1、创建实体类(Movie.java),用于保存电影数据。2、创建数据处理类(MovieOperator.java),用于进行业务处理,使其与实体类分类,达到封装、数据存储和数据处理相分离的目的。3、创建测试类(MovieTest.java),用于模拟用户的情况,由于不是网页的形式,所以还是停留在用控制台进行操作的阶段。
package com.ctgu.Movie;
public class Movie {//实体类(保存电影数据)。
/* 需求
展示系统中的全部电影(每部电影展示:名称、价格)。
允许用户根据电影编号( id)查询出某个电影的详细信息。*/
private int movieId;//电影ID
private String movieName;//电影名称。
private double moviePrice;//电影价格。
private double score;//电影评分。
private String director;//电影导演。
private String actor;//电影主员。
private String info;//多少人想看。
public Movie() {
}
public Movie(int movieId, String movieName, double moviePrice, double score, String director, String actor, String info) {
this.movieId = movieId;
this.movieName = movieName;
this.moviePrice = moviePrice;
this.score = score;
this.director = director;
this.actor = actor;
this.info = info;
}
public int getMovieId() {
return movieId;
}
public void setMovieId(int movieId) {
this.movieId = movieId;
}
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public double getMoviePrice() {
return moviePrice;
}
public void setMoviePrice(double moviePrice) {
this.moviePrice = moviePrice;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
package com.ctgu.Movie;
import java.util.Scanner;
public class MovieOperator {//数据处理类。
private Movie[] movie;//由于电影数量多,所以应该用数组对象比较方便。
Scanner scanner = new Scanner(System.in);//创建扫描器对象。
public MovieOperator(Movie[] movie){//有参构造器。
this.movie = movie;
}
public void printAllMoviesGeneralInformation(){//展示资料库中电影的大概信息。
System.out.println("------资料库中所有电影如下------");
for (int i = 0; i < movie.length ; i++) {
System.out.println("电影名称:"+movie[i].getMovieName()+",评分:"+movie[i].getScore()+",想看:"+movie[i].getInfo());
System.out.println("======分割线======");
}
}
public void printMoviesDetails(Movie movie){//打印详情的方法。
System.out.println("------电影详情如下------");
System.out.println("电影编号:"+movie.getMovieId()+",电影名称:"+movie.getMovieName()+",票价:"+movie.getMoviePrice()+",评分:"+movie.getScore()+",导演:"+movie.getDirector()+",主演:"+movie.getActor()+",想看:"+movie.getInfo());
System.out.println("======分割线======");
}
public void searchMoviesById(int id){//按电影ID查找。
for (int i = 0; i < movie.length; i++) {
if (movie[i].getMovieId() == id){
printMoviesDetails(movie[i]);
return;
}
}
System.out.println("======无结果======");
}
public void searchMoviesByName(String name){//按照电影名称查找。
boolean movieFound = false;//标志位。
for (int i = 0; i < movie.length; i++) {
if(movie[i].getMovieName().equals(name)){
printMoviesDetails(movie[i]);
movieFound = true;
}
}
if (!movieFound){
System.out.println("======无结果======");
}
}
public void searchMoviesByPrice(double price){//按照价格列出低于该价格的电影。
boolean movieFound = false;//标志位。
for (int i = 0; i < movie.length; i++) {
if(movie[i].getMoviePrice() <= price){
printMoviesDetails(movie[i]);
movieFound = true;
}
}
if (!movieFound){
System.out.println("======无结果======");
}
}
public void searchByMoviesByDirector(String director){//按照导演查找。
boolean movieFound = false;//标志位。
for (int i = 0; i < movie.length; i++) {
if(movie[i].getDirector().equals(director)){
printMoviesDetails(movie[i]);
movieFound = true;
}
}
if (!movieFound){
System.out.println("======无结果======");
}
}
public void searchMoviesByActor(String actor){//按主演查找。
boolean movieFound = false;//标志位。
for (int i = 0; i < movie.length; i++) {
if(movie[i].getActor().equals(actor)){
printMoviesDetails(movie[i]);
movieFound = true;
}
}
if (!movieFound){
System.out.println("======无结果======");
}
}
public void searchMoviesByScore(double score){//按评分查找。
boolean movieFound = false;//标志位。
for (int i = 0; i < movie.length; i++) {
if (movie[i].getScore() >= score){
printMoviesDetails(movie[i]);
movieFound = true;
}
}
if (!movieFound){
System.out.println("======无结果======");
}
}
}
package com.ctgu.Movie;
import java.util.Scanner;
public class MovieTest {//测试类。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);//创建扫描器对象。
//1、存数据。
Movie[] movies = new Movie[6];
Movie movie1 = new Movie(1,"《惊奇队长2》",48,6.8,"妮娅·达科斯塔","布丽·拉尔森","224073人");
Movie movie2 = new Movie(2,"《星际穿越》",45,8.6,"克里斯托弗·诺兰","马修·麦康纳","320050人");
Movie movie3 = new Movie(3,"《盗梦空间》",40,8.8,"克里斯托弗·诺兰","莱昂纳多·迪卡普里奥","285340人");
Movie movie4 = new Movie(4,"《蝙蝠侠:黑暗骑士》",50,9.0,"克里斯托弗·诺兰","克里斯蒂安·贝尔","352010人");
Movie movie5 = new Movie(5,"《复仇者联盟》",55,8.5,"乔斯·韦登","小罗伯特·唐尼","398765人");
Movie movie6 = new Movie(6,"《银河护卫队》",38,7.9,"詹姆斯·古恩","克里斯·帕拉特","213450人");
//2、赋值给数组对象。
movies[0] = movie1;
movies[1] = movie2;
movies[2] = movie3;
movies[3] = movie4;
movies[4] = movie5;
movies[5] = movie6;
//3、创建电影业务处理类对象,接受存储的数据,并对数据进行业务处理。
MovieOperator operator = new MovieOperator(movies);
//3、测试。
while (true) {
System.out.println("======欢迎进入模拟电影信息系统======");
System.out.println("1、电影列表。2、按ID搜索。3、按名称搜索。4、按价格搜索。5、按导演搜索。6、按主演搜索。7、按评分搜索。8、按8退出系统。");//按1输出电影列表,按2进入按照条件搜索电影。
int press = scanner.nextInt();
if(press == 8 ) break;
switch (press){
case 1 : operator.printAllMoviesGeneralInformation();break;
case 2 : System.out.println("请输入ID:");int id = scanner.nextInt();operator.searchMoviesById(id);break;
case 3 : System.out.println("请输入电影名称:");String moviesName = scanner.next();operator.searchMoviesByName(moviesName);break;
case 4 : System.out.println("请输入价格:");double price = scanner.nextDouble();operator.searchMoviesByPrice(price);break;
case 5 : System.out.println("请输入导演姓名:");String directorsName = scanner.next();operator.searchByMoviesByDirector(directorsName);break;
case 6 : System.out.println("请输入主演姓名:");String actorsName = scanner.next();operator.searchMoviesByActor(actorsName);break;
case 7 : System.out.println("请输入评分:");double score = scanner.nextDouble();operator.searchMoviesByScore(score);break;
default:
System.out.println("======输入信息错误======");break;
}
}
}
}