需求:
1、展示系统中的全部电影(每部电影展示:名称、价格)。
2、允许用户根据电影编号(id)查询出某个电影的详细信息。
//设计电影类,用来创建movie对象
//设计电影类,用于创建电影对象
public class Movie {
private int id;
private String name;
private double price;
private double score;
private String director;
private String actor;
private String info;
//无参构造器 必须要有无参构造器
public Movie() {
}
//手动输入一个有参构造器,用来收集储存数据
public Movie(int id, String name, double price, double score, String director, String actor, String info) {
this.id = id;
this.name = name;
this.price = price;
this.score = score;
this.director = director;
this.actor = actor;
this.info = info;
}
//各个成员变量的get、set方法
public 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 getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
//创建操作类,用于对电影类中接收到的数据或储存的数据进行处理
//创建操作类,用于对电影类中的数据进行处理
public class MovieOperator {
private Movie[] movies;
public MovieOperator(Movie[] files){
this.movies = files;
}
//用来展示全部电影的信息
public void printFiles(){
System.out.println("-------展示全部电影信息如下-----");
for (int i = 0; i < movies.length; i++) {
Movie m = movies[i];
System.out.println("编号:" + m.getId());
System.out.println("名称:" + m.getName());
System.out.println("价格:" + m.getPrice());
System.out.println("---------------------");
}
}
//通过id来获取电影的全部详情信息
public void searchFilesById(int id){
for (int i = 0; i < movies.length; i++) {
Movie m = movies[i];
if (m.getId() == id){
System.out.println("----电影详情如下----");
System.out.println("编号" + m.getId());
System.out.println("名称" + m.getName());
System.out.println("价格" + m.getPrice());
System.out.println("评分" + m.getScore());
System.out.println("导演" + m.getDirector());
System.out.println("演员" + m.getActor());
System.out.println("其他" + m.getInfo());
return;//已经找到了电影信息不需要继续找了
}
}
System.out.println("没有电影信息");
}
}
//测试类,用于测试操作类和电影类中的方法和数据是否有误
import java.util.Scanner;
//测试类,用于测试操作类和电影类对数据的处理是否正确
public class Test {
public static void main(String[] args) {
//创建数据容器,来存储接收到的数据,或像其中传入数据
Movie[] movies = new Movie[4];
movies[0] = new Movie(1, "水门桥", 38.9, 9.8, "徐克", "吴京", "12万人想看");
movies[1] = new Movie(2, "出拳吧", 39, 7.8, "唐晓白", "田雨", "3.5万人想看");
movies[2] = new Movie(3, "月球陨落", 42, 7.9, "罗兰", "贝瑞", "17.9万人想看");
movies[3] = new Movie(4, "一点就到家", 35, 8.7, "许宏宇", "刘昊然", "10.8万人想看");
MovieOperator operator = new MovieOperator(movies);
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("==电影信息系统==");
System.out.println("1、查询全部电影信息");
System.out.println("2、根据id查询某个电影的详细信息");
System.out.println("请您输入操作命令:");
int command = sc.nextInt();
switch (command){
case 1 :
operator.printFiles();
break;
case 2 :
System.out.println("请您输入您要查询的电影");
int id = sc.nextInt();
operator.searchFilesById(id);
break;
default:
System.out.println("您输入的命令有问题");
}
}
}
}