面向对象综合案例
需求:
展示系统中的全部电影(每部电影展示:名称,价格)
允许用户根据电影编号(id)查询出某个电影的详细信息
分析
一、设计一个实体类:电影类(Movie)
成员变量有:id(编号)、电影名字(name)、电影价格(price)、电影评分(score)、导演(director)、主演(actor)、其他信息(info)
getset方法
有参构造器和无参构造器
二、设计电影的操作类(MovieOperator)
Demo.java
import java.util.Random;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
/*
"1","电影1","11","90","导演1","演员1","其他信息1"
"2","电影2","22","80","导演2","演员2","其他信息2"
"3","电影3","33","70","导演3","演员3","其他信息3"
"4","电影4","44","60","导演4","演员4","其他信息4"
*/
Movie[] movies = new Movie[4];
movies[0] = new Movie(1,"电影1",11,90,"导演1","演员1","其他信息1");
movies[1] = new Movie(2,"电影2",22,80,"导演2","演员2","其他信息2");
movies[2] = new Movie(3,"电影3",33,70,"导演3","演员3","其他信息3");
movies[3] = new Movie(4,"电影4",44,60,"导演4","演员4","其他信息4");
//创建一个电影操作对象,进行业务处理
MovieOperator movieOperator = new MovieOperator(movies);
Scanner sc = new Scanner(System.in);
//操作
while (true) {
System.out.println("=======================================电影系统============================================");
System.out.println("1、查询电影信息");
System.out.println("2、根据电影编号查询所有信息");
System.out.println("3、退出系统");
System.out.println("进行的操作:");
int command = sc.nextInt();
switch (command){
case 1:
movieOperator.show(movies);
break;
case 2:
System.out.println("输入编号1-4:");
int id = sc.nextInt();
movieOperator.searchMovieById(id);
break;
case 3:
System.out.println("退出系统...");
return; // 退出程序
default:
System.out.println("命令有问题,重新选择");
}
}
}
}
Movie.java
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;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return this.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;
}
}
MovieOperator.java
public class MovieOperator {
private Movie[] movie;
public MovieOperator(Movie[] movie) {
this.movie = movie;
}
public MovieOperator() {
}
//展示系统中全部电影的编号、名称、价格
public void show(Movie[] movie){
System.out.println("=================电影信息======================");
for(int i = 0 ; i < movie.length ; i++){
//获取电影信息
Movie m = movie[i];
System.out.println("电影编号:" + m.getId() + " 电影名字:" + m.getName() + " 电影价格:" + m.getPrice());
}
System.out.println("=================电影信息======================");
}
//根据编号查询电影所有信息
public void searchMovieById(int id){
System.out.println("=======================================电影信息============================================");
for(int i = 0 ; i < movie.length ; i++){
//获取电影信息
Movie m = movie[i];
if(id == m.getId()) {
System.out.println("电影编号:" + m.getId() + " 电影名字:" + m.getName() +
" 电影价格:" + m.getPrice() + "电影评分:" + m.getScore() + " 电影导演:" +
m.getDirector() + " 电影主演:" + m.getActor() + " 其他信息:" + m.getInfo());
System.out.println("=======================================电影信息============================================");
return;
}
}
System.out.println("没有该电影信息");
}
}
运行结果
=======================================电影系统============================================
1、查询电影信息
2、根据电影编号查询所有信息
3、退出系统
进行的操作:
1
=================电影信息======================
电影编号:1 电影名字:电影1 电影价格:11.0
电影编号:2 电影名字:电影2 电影价格:22.0
电影编号:3 电影名字:电影3 电影价格:33.0
电影编号:4 电影名字:电影4 电影价格:44.0
=================电影信息======================
=======================================电影系统============================================
1、查询电影信息
2、根据电影编号查询所有信息
3、退出系统
进行的操作:
2
输入编号1-4:
1
=======================================电影信息============================================
电影编号:1 电影名字:电影1 电影价格:11.0电影评分:90.0 电影导演:导演1 电影主演:演员1 其他信息:其他信息1
=======================================电影信息============================================
=======================================电影系统============================================
1、查询电影信息
2、根据电影编号查询所有信息
3、退出系统
进行的操作:
2
输入编号1-4:
5
=======================================电影信息============================================
没有该电影信息
=======================================电影系统============================================
1、查询电影信息
2、根据电影编号查询所有信息
3、退出系统
进行的操作:
3
退出系统...
总结