JAVA面向对象基础:案例--电影系统 成员变量与局部变量区别
package com.itheima.DuiXianganli; public class Movie { private int id; private String name; private double price; private double score; private String director; private String 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 getInfo() { return info; } public void setInfo(String info) { this.info = info; } public Movie(int id, String name, double price, double score, String director, String info) { this.id = id; this.name = name; this.price = price; this.score = score; this.director = director; this.info = info; } public Movie() { } }
package com.itheima.DuiXianganli; public class MovieOperater { private Movie[] movies; public MovieOperater(Movie[] movies){ this.movies=movies; } 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()); System.out.println("-------------------------------"); } } 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.getName()); System.out.println("价格"+m.getPrice()); System.out.println("得分"+m.getScore()); System.out.println("导演"+m.getDirector()); System.out.println("详细信息:"+m.getInfo()); System.out.println("-------------------------------"); return;//查找到了直接结束了,没有一直执行到输出”无“ } System.out.println("无"); } } }
package com.itheima.DuiXianganli; import java.util.Scanner; public class DuiXianganli { public static void main(String[] args) { Movie[] movie = new Movie[4]; Movie m1 = new Movie(1, "544", 56, 56, "vvv", "sdfdsfdsfsdf"); Movie m2 = new Movie(2, "544", 56, 56, "vvv", "sdfdsfdsfsdf"); Movie m3 = new Movie(3, "544", 56, 56, "vvv", "sdfdsfdsfsdf"); Movie m4 = new Movie(4, "544", 56, 56, "vvv", "sdfdsfdsfsdf"); for (int i = 0; i < movie.length; i++) { switch (i) { case 0: movie[i] = new Movie(1, "544", 56, 56, "vvv", "sdfdsfdsfsdf"); break; case 1: movie[i] = m2; break; case 2: movie[i] = m3; break; case 3: movie[i] = m4; break; } } MovieOperater movieOperater = new MovieOperater(movie); while (true) { System.out.println("==电影系统=="); System.out.println("1.查询电影所有信息"); System.out.println("2.根据id查询某个电影的详细信息展示"); System.out.println("3.退出系统"); System.out.println("请您输入操作指令:"); Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); switch (num) { case 1: movieOperater.printAllMovies(); break; case 2: System.out.println("请您输入查询电影编号:"); int id = scanner.nextInt(); movieOperater.searchMovieByid(id); break; case 3: return; default: System.out.println("您输入的内容有误"); } } } }