面对对象基础案例
模仿电影信息系统
需求:
- 展示系统中的全部电影(每部电影展示:名称,价格)。
- 允许用户根据电影编号(id)查询出某个电影的详细信息。
Movie类
public class Movie {
private int id; //id
private String namel; //电影名
private double price; //价格
private double score; //评分
private String director;//导演
private String actor; //主演
private String info; //观看人数
public Movie() {
}
public Movie(int id, String namel, double price, double score, String director, String actor, String info) {
this.id = id;
this.namel = namel;
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 getNamel() {
return namel;
}
public void setNamel(String namel) {
this.namel = namel;
}
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;
}
}
MovieOperator类
public class MovieOpertor {
private Movie[] movies;
public MovieOpertor(Movie[] movies) {
this.movies = movies;
}
/** 1.展示系统全部电影信息 **/
public void printAllMovies(){
System.out.println("----------系统全部电影信息如下:-----------");
for (int i = 0; i < movies.length ; i++) {
Movie m = movies[i];
System.out.println("编号:"+m.getId());
System.out.println("名称:"+m.getNamel());
System.out.println("价格:"+m.getPrice());
System.out.println("-------------------------------------");
}
}
/** 2.根据电影编号查询电影详细信息 **/
public void searchMovieById(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.getNamel());
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("没有该电影信息");
}
}
Test类
public class Test {
public static void main(String[] args) {
//1.设计一个电影类\
//2.设计一个电影操作类
//3.准备全部电影数据
Movie[] movies = new Movie[3];
movies[0] = new Movie(1,"水门桥",38.9,9.8,"徐克","吴京","13万人想看");
movies[1] = new Movie(2,"出拳把",39,8.8,"唐晓白","田宇","3.5万人想看");
movies[2] = new Movie(3,"月球陨落",42,7.9,"罗兰","贝瑞","17万人想看");
//4.创建一个电影操作类
MovieOpertor opertor = new MovieOpertor(movies);
while (true) {
System.out.println("==电影信息系统==");
System.out.println("1.查询电影全部信息");
System.out.println("2.根据id查询某个电影详细信息");
System.out.println("请您输入操作命令");
Scanner s = new Scanner(System.in);
int sc = s.nextInt();
switch (sc) {
case 1:
opertor.printAllMovies();
break;
case 2:
System.out.println("请输入您查询的电影id:");
int id = s.nextInt();
opertor.searchMovieById(id);
break;
default:
System.out.println("您输入的操作有问题");
}
}
}
}