//主函数
package com.itheima.demo;
public class Test {
public static void main(String[] args) {
Movie []movies=new Movie[4];
movies[0]=new Movie(1,"大浪淘沙",30,9.4,"张艺谋","许印","7.8万人想看");
movies[1]=new Movie(2,"梦断蓝桥",30,9.8,"Angila","Surina","10.4万人想看");
movies[2]=new Movie(3,"流年似水",25,9.2,"陈佳丽","苏年华","5.6万人想看");
movies[3]=new Movie(4,"月梦",45,9.5,"诺兰","安格洛","14.7万人想看");
MovieOperator opperator=new MovieOperator(movies);
opperator.PrintInfo();
opperator.ChooseId(2);
}
}
//Movie类
package com.itheima.demo;
public class Movie {
int id;
private String name;
private int price;
private double score;
private String director;
private String actor;
private String info;
public Movie() {
}
public Movie(int id, String name, int 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;
}
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 int getPrice() {
return price;
}
public void setPrice(int 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;
}
}
//MovieOperator类
package com.itheima.demo;
import javax.swing.*;
public class MovieOperator {
Movie []movies;
public MovieOperator() {
}
public MovieOperator(Movie []movies) {
this.movies = movies;
}
public void PrintInfo(){
for (int i = 0; i < movies.length; i++) {
Movie m=movies[i];
System.out.println("名称: "+m.getName());
System.out.println("价格: "+m.getPrice());
System.out.println("------------------------");
}
}
public void ChooseId(int id){
while (true) {
System.out.println("请输入要查询的电影id");
for (int i = 0; i < movies.length; i++) {
Movie m=movies[i];
if(m.id==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());
System.out.println("电影查询结束");
return;//使用return结束死循环
}
System.out.println("没有该电影信息");
}
}
}
}
需求 展示系统中的全部电影(每部电影展示:名称、价格)。 允许用户根据电影编号(id)查询出某个电影的详细信息。 目标 使用所学的面向对象编程实现以上2个需求。