@TOC
思路
代码
结构
具体代码
Movie.java
public class Movie {//一共七个private int id;private String name;private double price;private double score;private String director;private String actors;private String info;//get和setpublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}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 getActors() {return actors;}public void setActors(String actors) {this.actors = actors;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}//设计private就需要有构造器,方便访问//有参 : 为了创建对象时赋值public Movie(int id, String name, double price, double score, String director, String actors, String info) {this.id = id;this.name = name;this.price = price;this.score = score;this.director = director;this.actors = actors;this.info = info;}//无参: 实体类必须有public Movie(){}}
MoiveOperator.java
public class MovieOperator {//业务操作类private Movie[] movies;//有参构造 不太懂为什么public MovieOperator(Movie[] movies) {this.movies = movies;}//方法:有什么需求就写什么方法//需求1:展示出全部电影的信息//不需要传参数 返回值为voidpublic void printAllMovies(){System.out.println("以下是电影院所有电影信息:");for(int i = 0; i<movies.length; i++){System.out.println("-------------------------");System.out.println(movies[i].getId());System.out.println(movies[i].getName());System.out.println(movies[i].getActors());System.out.println(movies[i].getInfo());System.out.println(movies[i].getPrice());System.out.println(movies[i].getDirector());System.out.println(movies[i].getScore());}}//需求2:根据id查询电影//参数 id;返回值为voidpublic void selectMovie(int id){for(int i = 0;i < movies.length;i++){//id和数组下标不一样,不能直接拿接收的参数和数组下标进行比较if(id == movies[i].getId()){System.out.println(movies[i].getId());System.out.println(movies[i].getName());System.out.println(movies[i].getActors());System.out.println(movies[i].getPrice());return;//非常巧妙,找到了就不在执行循环,直接结束整个方法}}System.out.println("您输入的电影id有误");}
}
Test.java
import java.util.Scanner;public class Test {public static void main(String[] args) {//写一个电影数组Movie[] movies = new Movie[4];//每一个数组对象 都是一个movie类型的对象,所以要newmovies[0] = new Movie(1, "《热辣苦瓜》", 36.9, 8.8, "甄玲", "张三,里斯", "年度好片"){};movies[1] = new Movie(2, "《热辣西瓜》", 48.9, 9.8, "甄玲", "张三,瓦内", "年度超好片"){};movies[2] = new Movie(3, "《热辣甜瓜》", 25.3, 6.8, "甄玲", "张三,王五", "年度烂片"){};movies[3] = new Movie(4, "《热辣蜜瓜》", 36.9, 3.8, "甄玲", "张三,二狗", "年度动作片"){};//创建一个业务控制器类MovieOperator operator = new MovieOperator(movies);//operator.printAllMovies();//System.out.println("----------");// operator.selectMovie(1);//到上面结束已经完成了基本的功能,下面写一下交互 正规一点的System.out.println("----------欢迎来到电影院------------");Scanner sc = new Scanner(System.in);while(true) {System.out.println("输入1查看所有影片;输入2根据id查找电影;输入3退出程序~~~");int num = sc.nextInt();switch(num) {case 1:operator.printAllMovies();break;case 2:System.out.println("请输入想要查询的电影id:");int id = sc.nextInt();operator.selectMovie(id);break;case 3:System.out.println("退出程序。");return;default:System.out.println("您输入的命令有问题,请检查后再输入~~~");break;}}}
}
结果
反思
这个代码这是第二遍,写了思路以后比第一遍熟练一点,但是还是不太懂个别为什么要写,比如MovieOperator类中的有参构造,不懂为什么。
还有为什么是业务类对象operator打点调方法,我最开始以为是 直接数组对象打点调方法。
继续学习。