文章目录
- 电影信息系统
- 1. 将JavaBean数据类封装好
- 2.业务逻辑类
- 3.测试类(main函数类)
电影信息系统
需求:
- 展示系统中的全部电影(每部电影展示、名称、平分)。
- 允许用户根据电影编号(id)查询出某个电影的详细信息。
1. 将JavaBean数据类封装好
package com.liujintao.domain;/*** JavaBean*/
public class Movie {private int id;private String title;private String time;private double score;private String area;private String type;private String director;private String starring;public Movie() {}public Movie(int id, String title, String time, double score, String area, String type, String director, String statting) {this.id = id;this.title = title;this.time = time;this.score = score;this.area = area;this.type = type;this.director = director;this.starring = statting;}/*** 获取* @return id*/public int getId() {return id;}/*** 设置* @param id*/public void setIdl(int id) {this.id = id;}/*** 获取* @return title*/public String getTitle() {return title;}/*** 设置* @param title*/public void setTitle(String title) {this.title = title;}/*** 获取* @return time*/public String getTime() {return time;}/*** 设置* @param time*/public void setTime(String time) {this.time = time;}/*** 获取* @return score*/public double getScore() {return score;}/*** 设置* @param score*/public void setScore(double score) {this.score = score;}/*** 获取* @return area*/public String getArea() {return area;}/*** 设置* @param area*/public void setArea(String area) {this.area = area;}/*** 获取* @return type*/public String getType() {return type;}/*** 设置* @param type*/public void setType(String type) {this.type = type;}/*** 获取* @return director*/public String getDirector() {return director;}/*** 设置* @param director*/public void setDirector(String director) {this.director = director;}/*** 获取* @return statting*/public String getStarring() {return starring;}/*** 设置* @param starring*/public void setStarring(String starring) {this.starring = starring;}}
2.业务逻辑类
package com.liujintao.test;import com.liujintao.domain.Movie;import java.util.Scanner;public class MovieService {// 提升为成员变量,并合理隐藏private Movie[] movies;private Scanner sc;/*** 带参构造器:获取JavaBean中数据**/public MovieService(Movie[] movies) {this.movies = movies;}/*** 启动电影信息管理系统*/public void start() {this.sc = new Scanner(System.in);lo:while(true) {System.out.println("----------电影信息系统----------");System.out.println("请输入您的选择");System.out.println("1.查询全部电影信息");System.out.println("2.根据id查询电影信息");System.out.println("3.退出");int choice = sc.nextInt();switch(choice) {case 1:queryMoviesInfo();break;case 2:queryMoviesById();break;case 3:System.out.println("退出");break lo;default:System.out.println("您的输入有误!");break;}}}/*** 通过编号:展示电影所有信息*/private void queryMoviesById() {System.out.println("请输入电影的id号:");int id = sc.nextInt();for (int i = 0; i < movies.length; i++) {if (movies[i].getId() == id) {System.out.println("编号:" + movies[i].getId() + "-电影名:-" + movies[i].getTitle() + "-上映时间:-" + movies[i].getTime() + "-评分:-" + movies[i].getScore() + "-地区-" + movies[i].getArea() + "-电影类型-" + movies[i].getType() + "-导演-" + movies[i].getDirector() + "-主演-" + movies[i].getStarring());return;}}// 没找到!System.out.println("您输入的编号不存在!");}/*** 展示电影系统中全部的电影(名称、评分)*/private void queryMoviesInfo() {for (int i = 0; i < movies.length; i++) {Movie movie = movies[i];System.out.println("电影名:" + movie.getTitle() + "---评分:" + movie.getScore());}}
}
3.测试类(main函数类)
package com.liujintao.test;import com.liujintao.domain.Movie;public class MovieTest {public static void main(String[] args) {// 构造器初始数据Movie movie1 = new Movie(1, "东八区的先生们", "2022", 2.1, "中国大陆", "剧情 喜剧", "夏睿", "张翰 王晓晨");Movie movie2 = new Movie(2, "上海堡垒", "2019", 2.9, "中国大陆", "爱情 战争 科幻", "滕华涛", "鹿晗 舒淇");Movie movie3 = new Movie(3, "纯洁心灵·逐梦演艺圈", "2015", 2.2, "中国大陆", "剧情 喜剧", "毕志飞", "朱一文 李彦漫");// 将数据存到数组中Movie[] movies = {movie1, movie2, movie3};MovieService movieservice = new MovieService(movies);movieservice.start();}
}
运行结果展示: