oop案例1
电影类
package com.item.huang.demo01;
//电影类
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 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;
}
}
电影操作类
package com.item.huang.demo01;
//电影操作类
public class MovieOperator {
private Movie[] movies;
//有参构造(赋值)
public MovieOperator(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.getName());
System.out.println("价格:"+m.getPrice());
}
}
//2.根据电影的编号查询电影的详细信息并展示
public void searchMovieById(int id){
for (int i = 0; i < movies.length; i++) {
Movie m = movies[i];
if(id == m.getId()){
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("没有电影信息!");
}
}
测试类
package com.item.huang.demo01;
/*
*电影数据:
* 1,"水门桥",38.9,9.8,"徐克","吴京","12万人想看"
* 2,"出拳吧",39,7.8,"唐晓白","田雨","3.5万人想看"
* 3,"月球陨落",42,7.9,"罗兰","贝瑞","17.9万人想看"
* 4,"一点就到家",35,8.7,"许宏宇","刘昊然","10.8万人想看"
*
* 需求:
* 1.展示系统中的全部电影(每部电影展示:名称,价格)
* 2.允许用户根据电影编号(id)查询某部电影的详细信息
*
* 1.设计一个电影类
* 2.设计一个电影操作类
*/
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//1.给电影赋值
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万人想看");
//2.展示系统中的全部电影
MovieOperator movieOperator = new MovieOperator(movies);
movieOperator.printAllMovies();
//3.用户根据电影编号(id)查询某部电影的详细信息
System.out.println("===================================");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入想要查询的电影编号:");
sc.hasNextInt();
int id = sc.nextInt();
if(id<1||id>4){
System.out.println("输入的编号有误");
}else {
movieOperator.searchMovieById(id);
break;//成功查询到电影
}
}
}
}
标签:String,movies,System,案例,oop,println,public,out
From: https://www.cnblogs.com/qingandji/p/18085898