开发喵影院
根据要求完成开发喵影院的开发。
- 定义用户类
User
,包含以下属性:用户的名称(username)、用户登录手机号(phone)、用户的密码(password)、用户的身份(role int类型 1 表示管理员 2 表示普通用户)
- 定义电影类
Movie
,包含以下属性:电影的名称(name)、电影的价格(price)、电影的导演(director)、上映时间(date int类型,比如:2023年1月1日 写成20230101)。
- 定义电影院类
MovieTheater
,包含以下属性:电影集合 Movie[] movies; 用户集合 User[] users 用来存储多部电影对象及用户对象。
- 完成以下功能展示
- 重写 toString 方法,输出对象的所有信息。
- 重写 equals 方法
- 当电影院类中存储电影集合或存储用户的数组超过指定容量时,要进行扩容。
- 进入系统后可看到如下功能列表:
-
- 查询电影信息
-
- 显示所有电影信息 (按照评分降序显示)
-
- 管理员登录
-
- 用户登录
-
- 用户注册
-
- 退出
- 用户登录之后可看到如下功能列表:
- 管理员登录可以看到如下功能列表:
-
- 新增电影
-
- 删除电影(输入电影名称,删除对应电影)
-
- 修改电影(输入电影名称,展示电影信息,选择要修改的内容(价格/上映时间),输入内容,修改已存储的电影信息)
-
- 查看所有用户信息
-
- 退出
-
- 界面要求
- 注释要求
代码实现
1. User 类
package kfm.bases.OOPMovie;
public class User {
private String username;
private String phone;
private String password;
private Integer role;
public User(String username, String phone, String password, int role) {
this.setUsername(username);
this.setPhone(phone);
this.setPassword(password);
this.setRole(role);
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setRole(Integer role) {
this.role = role;
}
public Integer getRole() {
return role;
}
@Override
public String toString() {
return ((role == 1)?"管理员":"普通用户 ") + username + " 手机号:" + phone;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof User user) {
if (!user.username.equals(this.username)) {
return false;
}
if (!user.phone.equals(this.phone)) {
return false;
}
if (!user.password.equals(this.password)) {
return false;
}
if (user.role != this.role) {
return false;
}
return true;
}
return false;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + (username != null ? username.hashCode() : 0);
result = 31 * result + (phone != null ? phone.hashCode() : 0);
result = 31 * result + (password != null ? password.hashCode() : 0);
result = 31 * result + role;
return result;
}
}
2. Movie 类
package kfm.bases.OOPMovie;
public class Movie {
private String name;
private Double price;
private String director;
private Integer date;
public Movie(String name, double price, String director, int date) {
this.name = name;
this.price = price;
this.director = director;
this.date = date;
}
public void setPrice(double price) {
this.price = price;
}
public void setDate(int date) {
this.date = date;
}
public String getName() {
return name;
}
public Double getPrice() {
return price;
}
public String getDirector() {
return director;
}
public Integer getDate() {
return date;
}
@Override
public String toString() {
return "《" + name + "》\t" + date + "上映\t" + "导演:" + director + "\t价格:" + price;
}
}
3. MovieTheater 类
package kfm.bases.OOPMovie;
import java.util.Arrays;
public class MovieTheater {
private Movie[] movies = new Movie[10];
private User[] users = new User[10];
private int movNum = 0;
private int userNum = 0;
{
Movie movie1 = new Movie("流浪地球", 45.0, "郭帆", 20190205);
addMovie(movie1);
Movie movie2 = new Movie("战狼2", 38.0, "吴京", 20170727);
addMovie(movie2);
Movie movie3 = new Movie("复仇者联盟4:终局之战", 58.0, "安东尼·罗素", 20190424);
addMovie(movie3);
Movie movie4 = new Movie("你好,李焕英", 42.0, "贾玲", 20210212);
addMovie(movie4);
Movie movie5 = new Movie("红海行动", 39.0, "林超贤", 20180216);
addMovie(movie5);
Movie movie6 = new Movie("我和我的祖国", 46.0, "陈凯歌", 20190930);
addMovie(movie6);
User user1 = new User("小羊呆呆", "123520","5201314", 2);
addUser(user1);
User user2 = new User("犯困桃子", "123123","5201314", 1);
addUser(user2);
}
public void addMovie(Movie movie) {
movExpansion();
movies[movNum] = movie;
movNum++;
}
public void movExpansion() {
if (movNum == movies.length) {
movies = Arrays.copyOf(movies,movNum * 2);
}
}
public void addUser(User user) {
userExpansion();
users[userNum] = user;
userNum ++;
}
public void userExpansion() {
if (userNum == users.length) {
users = Arrays.copyOf(users,userNum * 2);
}
}
public void show() {
if (movNum == 0) {
System.out.println("\u001B[31m 目前电影无在映!\u001B[0m");
} else {
for (int i = 0; i < movNum; i++) {
System.out.println(movies[i].toString());
}
}
}
public void showUser() {
for (int i = 0; i < userNum; i++) {
System.out.println((i + 1) + '\t' + users[i].toString());
}
}
public User searchUser(String phone, String password, Integer role) {
for (int i = 0; i < userNum; i++) {
if (users[i].getPhone().equals(phone) && users[i].getPassword().equals(password) && users[i].getRole() == role) {
return users[i];
}
}
return null;
}
public Movie search(String name) {
for (int i = 0; i < movNum; i++) {
if (movies[i].getName().equals(name)) {
return movies[i];
}
}
return null;
}
public void deleteMovie(String name) {
for (int i = 0; i < movNum; i++) {
if (movies[i].getName().equals(name)) {
if (i != movNum - 1) {
movies[i] = movies[i + 1];
} else {
movies[i] = null;
}
}
}
movNum --;
}
}
4. Main
package kfm.bases.OOPMovie;
import java.util.Scanner;
public class Main {
static Scanner input = new Scanner(System.in);
static MovieTheater movieTheater = new MovieTheater();
public static void start() {
System.out.println("欢迎使用开发喵影院系统");
showMenu();
}
private static void showMenu() {
System.out.println();
System.out.println("开发喵影院");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("请选择功能:");
System.out.println("1. 查询电影信息");
System.out.println("2. 显示所有电影信息");
System.out.println("3. 管理员登录");
System.out.println("4. 用户登录");
System.out.println("5. 用户注册");
System.out.println("6. 退出");
System.out.print("请输入:");
int menu = input.nextInt();
selectMenu(menu);
}
private static void selectMenu(int menu) {
switch (menu) {
case 1 -> searchMovie();
case 2 -> showMovie();
case 3 -> adminlogin();
case 4 -> userlogin();
case 5 -> userRegistr();
default -> exit();
}
showMenu();
}
private static void showUser(User user) {
System.out.println("开发喵影院");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("欢迎光临!" + user.getUsername());
System.out.println("请选择功能:");
System.out.println("1. 修改用户信息");
System.out.println("2. 查询用户信息");
System.out.println("3. 退出");
System.out.print("请输入:");
int menu = input.nextInt();
selectUser(menu, user);
}
private static void selectUser(int menu, User user) {
switch (menu) {
case 1 -> modifyUserInformation(user);
case 2 -> earchUser(user);
default -> showMenu();
}
showUser(user);
}
private static void earchUser(User user) {
System.out.println(user.toString());
}
private static void modifyUserInformation(User user) {
System.out.println("修改信息");
System.out.println();
System.out.println("请输入你的用户名:");
user.setUsername(input.next());
System.out.println("请输入你的电话号码:");
user.setPhone(input.next());
user.setPassword(password());
System.out.println("修改成功!你现在的信息为:");
earchUser(user);
}
private static void searchMovie() {
System.out.println("请输入你要查找的影片名称:");
String name = input.next();
Movie movie = movieTheater.search(name);
if (movie != null) {
System.out.println(movie.toString());
} else {
System.out.println("\u001B[31m 未找到匹配电影!\u001B[0m");
}
}
private static void showMovie() {
movieTheater.show();
}
private static void adminlogin() {
System.out.println("管理员登录");
System.out.println();
System.out.println("请输入你的电话号码:");
String phone = input.next();
System.out.println("请输入密码:");
String password = input.next();
User user = movieTheater.searchUser(phone, password, 1);
if (user != null) {
showAdminMenu(user);
} else {
System.out.println("\u001B[31m 请检查你的输入!\u001B[0m");
adminlogin();
}
}
private static void showAdminMenu(User user) {
System.out.println();
System.out.println("开发喵影院\t欢迎光临" + user.getUsername());
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("请选择功能:");
System.out.println("1. 新增电影");
System.out.println("2. 删除电影");
System.out.println("3. 修改电影");
System.out.println("4. 查看所有用户的信息");
System.out.println("5. 退出");
System.out.print("请输入:");
int menu = input.nextInt();
selectAdminMenu(menu, user);
}
private static void selectAdminMenu(int menu, User user) {
switch (menu) {
case 1 -> addMovie();
case 2 -> deleteMovie();
case 3 -> modifyMovie();
case 4 -> showUsers();
default -> showMenu();
}
showAdminMenu(user);
}
private static void addMovie() {
System.out.println("请输入电影名称:");
String name = input.next();
System.out.println("请输入票价:");
double price = input.nextDouble();
System.out.println("请输入导演:");
String director = input.next();
System.out.println("请输入上映日期");
int date = input.nextInt();
Movie movie = new Movie(name, price, director, date);
movieTheater.addMovie(movie);
System.out.println("《" + name + "》" + "添加成功!");
}
private static void deleteMovie() {
System.out.println("请输入要删除的电影名称:");
String name = input.next();
movieTheater.deleteMovie(name);
System.out.println("删除《" + name + "》成功!");
}
private static void modifyMovie() {
System.out.println("请输入要修改电影名称:");
String name = input.next();
Movie movie = movieTheater.search(name);
movie.toString();
System.out.println("请选择要修改的内容:1.价格 2.上映时间");
int i = input.nextInt();
if (i == 1) {
System.out.println("请输入新价格:");
movie.setPrice(input.nextDouble());
} else if (i == 2) {
System.out.println("请输入上映时间");
movie.setDate(input.nextInt());
}
System.out.println("更改过后的信息为" + movie.toString());
}
private static void showUsers() {
movieTheater.showUser();
}
private static void userlogin() {
System.out.println("用户登录");
System.out.println();
System.out.println("请输入你的电话号码:");
String phone = input.next();
System.out.println("请输入密码:");
String password = input.next();
User user = movieTheater.searchUser(phone, password, 2);
if (user != null) {
showUser(user);
} else {
System.out.println("\u001B[31m 请检查你的输入!\u001B[0m");
showMenu();
}
}
private static void userRegistr() {
System.out.println("用户注册");
System.out.println();
System.out.println("请输入你的用户名:");
String username = input.next();
System.out.println("请输入你的电话号码:");
String phone = input.next();
String password = password();
User user = new User(username, phone,password, 2);
movieTheater.addUser(user);
}
private static String password() {
System.out.println("请输入密码:");
String password = input.next();
System.out.println("请再次输入密码:");
String passwordtwo = input.next();
if (password.equals(passwordtwo)) {
return password;
} else {
System.out.println("\u001B[两次输入的密码不同,请重新输入密码!\u001B[0m");
password();
}
return null;
}
private static void exit() {
System.out.println("欢迎下次光临");
System.exit(0);
}
}
5. Test
package kfm.bases.OOPMovie;
public class Test {
public static void main(String[] args) {
Main.start();
}
}