- (客户和商店)登录
- 商店
- 展示详情
- 上架电影
- 下架电影
- 修改电影
- 退出
- 客户
- 展示全部影片功能
- 根据电影名称查询
- 评分功能
- 购票功能
- 显示评分
- 退出系统
- 商店
- 商店注册
- 客户注册
代码详情
public static void main(String[] args) { deal(); } // 页面详情 private static void deal() { Scanner sc = new Scanner(System.in); while (true) { menu(); String type = sc.nextLine(); switch (type) { case "1" -> { // 登录 login(); } case "2" -> { // 商家入驻 BusinessRegister(); } case "3" -> { // 客户注册 CustomerRegister(); } default -> System.out.println("选择错误,请重新选择:>"); } } } // 用户注册 private static void CustomerRegister() { System.out.println("==================== 用户注册界面 ===================="); Customer c = new Customer(); String name = RandomName(6); c.setName(name); System.out.println("请输入用户名"); String UserName = SC.nextLine(); c.setUserName(UserName); while (true) { System.out.println("请输入密码"); String password = SC.nextLine(); System.out.println("请再次输入密码"); String okPassword = SC.nextLine(); if (password.equals(okPassword)) { c.setPassword(password); break; } else { System.out.println("两次密码不一致,请重新输入"); } } System.out.println("请输入性别"); String sex = SC.nextLine(); c.setGender(sex); System.out.println("请输入电话号码"); String tel = SC.nextLine(); c.setTel(tel); System.out.println("请输入存款的金额"); Double money = Double.valueOf(SC.nextLine()); c.setMoney(money); boolean flag = ALL_USER.add(c); if (!flag) { System.out.println("注册失败,请重新输入"); return; } System.out.println("注册成功,你的登录名称为:" + c.getName()); } // 商家注册 private static void BusinessRegister() { System.out.println("==================== 商家注册界面 ===================="); Business b = new Business(); String name = RandomName(6); b.setName(name); System.out.println("请输入用户名"); String UserName = SC.nextLine(); b.setUserName(UserName); while (true) { System.out.println("请输入密码"); String password = SC.nextLine(); System.out.println("请再次输入密码"); String okPassword = SC.nextLine(); if (password.equals(okPassword)) { b.setPassword(password); break; } else { System.out.println("两次密码不一致,请重新输入"); } } System.out.println("请输入性别"); String sex = SC.nextLine(); b.setGender(sex); System.out.println("请输入电话号码"); String tel = SC.nextLine(); b.setTel(tel); System.out.println("请输入该影院地址"); String address = SC.nextLine(); b.setAddress(address); System.out.println("请输入该影院名称"); String businessName = SC.nextLine(); b.setBusinessName(businessName); boolean flag = ALL_USER.add(b); if (!flag) { System.out.println("注册失败,请重新输入"); return; } List<Movie> movies = new ArrayList<>(); ALL_MOVIES.put(b, movies); System.out.println("注册成功,你的登录名称为:" + b.getName()); } //随机登录名称 private static String RandomName(int n) { Random r = new Random(); ArrayList<Character> list = new ArrayList<>(); for (int i = 0; i < 26; i++) { list.add((char) ('a' + i)); list.add((char) ('A' + i)); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n - 2; i++) { int index = r.nextInt(list.size()); sb.append(list.get(index)); } for (int i = 0; i < 2; i++) { int index = r.nextInt(9) + 1; sb.append(index); } return sb.toString(); } // 商家菜单 private static void BusinessMenu() { System.out.println("1. 展示详情:>"); System.out.println("2. 上架电影:>"); System.out.println("3. 下架电影:>"); System.out.println("4. 修改电影:>"); System.out.println("5. 退出:>"); System.out.println("请输入你要操作的命令:>"); } // 客户菜单 private static void CustomerMenu() { 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.println("请输入你要操作的命令:>"); } // 登录 private static void login() { while (true) { System.out.println("==================== 电影用户登录 ======================"); System.out.println("请输入你的登录名称"); String loginName = SC.nextLine(); User u = contains(loginName); if (u == null) { System.out.println("用户名不存在,请重新输入"); } else { while (true) { System.out.println("请输入你的登录密码"); String password = SC.nextLine(); if (u.getPassword().equals(password)) { System.out.println("登录成功,欢迎" + u.getUserName() + "进入系统"); if (u instanceof Business b) { loginBusiness(b); return; } else if (u instanceof Customer c) { loginCustomer(c); return; } } else { System.out.println("密码错误,请重新登录"); } } } } } // 用户登录 private static void loginCustomer(Customer c) { List<Movie> list = new ArrayList<>(); while (true) { System.out.println("==================== 电影用户界面 ===================="); System.out.println(c.getUserName() + (c.getGender() == "男" ? "男士" : "女士") + "你好,请你选择用户操作的功能:"); while (true) { CustomerMenu(); String type = SC.nextLine(); switch (type) { case "1" -> { // 展示全部影片西信息 showAll(c); } case "2" -> { // 根据电影名称查询 checkName(); } case "3" -> { // 评分功能 Grade(list); } case "4" -> { // 购票功能 // System.out.println("购票功能"); buyTrick(c, list); } case "5" -> { showScore(list); } case "6" -> { System.out.println("系统退出成功"); return; } default -> System.out.println("选择错误,请重新选择:>"); } } } } // 显示电影评分 private static void showScore(List<Movie> list) { System.out.println("==================== 电影评分显示详情 ===================="); if (list.size() == 0) { System.out.println("没有任何一部电影被观看,请先去观看"); return; } double sum = 0; for (Movie movie : list) { List<Double> doubles = ALL_SCORE.get(movie); for (Double d : doubles) { sum += d; } System.out.println(movie.getName() + "评分为:" + (sum / doubles.size()) + "分"); return; } } // 评分 private static void Grade(List<Movie> list) { if (list.size() == 0) { System.out.println("没有任何一部电影被观看"); return; } while (true) { System.out.println("==================== 电影评分详情 ===================="); System.out.println("请输入你要评分的电影名称"); String name = SC.nextLine(); Movie movie = containMovie(name, list); if (movie != null) { // 找到已经观看完的电影 while (true) { System.out.println("请输入你的分数(1-10)"); Double score = Double.valueOf(SC.nextLine()); List<Double> all = new ArrayList<>(); if (score >= 1 && score <= 10) { // 正确格式 all.add(score); ALL_SCORE.put(movie, all); System.out.println("评分成功"); return; } else { System.out.println("评分有误,请重新输入"); } } } else { System.out.println("你还没有观看过此电影,无法对此电影进行评分"); return; } } } // 购票 private static void buyTrick(Customer c, List<Movie> list) { System.out.println("==================== 电影购票详情 ===================="); while (true) { showAll(c); System.out.println("请输入你要购买的商家的名称"); String businessName = SC.nextLine(); Business b = contain(businessName); if (b != null) { while (true) { List<Movie> movies = ALL_MOVIES.get(b); // 判断该电影院有无影片 if (movies.size() > 0) { while (true) { System.out.println("请输入购买的电影信息"); String movieName = SC.nextLine(); Movie movie = containMovie(movieName, movies); if (movie != null) { // 开始购买 while (true) { System.out.println("请输入购买的票数"); Integer count = Integer.valueOf(SC.nextLine()); if (movie.getCount() >= count) { // 票数够 double money = BigDecimal.valueOf(movie.getPrice()).multiply(BigDecimal.valueOf(count)).doubleValue(); if (c.getMoney() >= money) { // 钱够 c.setMoney(c.getMoney() - money); b.setMoney(b.getMoney() + money); movie.setCount(movie.getCount() - count); System.out.println("购买" + movie.getName() + "的电影,票数为:" + count); // 购买的电影 list.add(movie); return; } else { System.out.println("对不起,当前余额不足,无法购买"); System.out.println("是否进行买票(y/n)"); String command = SC.nextLine(); switch (command) { case "y": { break; } default: { System.out.println("好的"); return; } } } } else { System.out.println("你最多只能购买:" + movie.getCount() + "张票"); System.out.println("是否进行买票(y/n)"); String command = SC.nextLine(); switch (command) { case "y": { break; } default: { System.out.println("好的"); return; } } } } } else { System.out.println("电影名称有误"); } } } else { System.out.println("该电影院已关门"); System.out.println("是否进行买票(y/n)"); String command = SC.nextLine(); switch (command) { case "y": { break; } default: { System.out.println("好的"); return; } } } } } else { System.out.println("不存在该商家,请重新输入"); } } } private static Business contain(String businessName) { for (int i = 0; i < ALL_USER.size(); i++) { if (ALL_USER.get(i) instanceof Business b) if (b.getBusinessName().equals(businessName)) { return b; } } return null; } // 根据电影名称查找 private static void checkName() { while (true) { System.out.println("==================== 电影名称查找详情 ===================="); while (true) { System.out.println("请输入你要查找的电影名称"); String name = SC.nextLine(); List<User> users = containMovie(name); if (users.size() != 0) { showAll(users); return; } else { System.out.println("输入错误,请重新输入"); } } } } // 展示搜索的内容 private static void showAll(List<User> list) { for (int i = 0; i < list.size(); i++) { Business b = (Business) list.get(i); System.out.println(b.getBusinessName() + "\t电话:" + b.getTel() + "\t地址:" + b.getAddress() + "\t总营收:" + b.getMoney()); List<Movie> movies = ALL_MOVIES.get(b); System.out.println("影片名称:\t\t\t主演:\t\t时长:\t\t票价:\t\t余票:\t\t放映时间:"); for (int j = 0; j < movies.size(); j++) { Movie movie = movies.get(j); System.out.println(movie.getName() + "\t\t" + movie.getAuthor() + "\t\t" + movie.getTimeLong() + "\t\t" + movie.getPrice() + "\t\t" + movie.getCount() + "\t\t" + movie.getStartTime()); } } return; } // 查询电影名称 private static List<User> containMovie(String name) { List<User> list = new ArrayList<>(); Iterator<Map.Entry<User, List<Movie>>> it = ALL_MOVIES.entrySet().iterator(); while (it.hasNext()) { Map.Entry<User, List<Movie>> map = it.next(); List<Movie> movies = map.getValue(); for (int i = 0; i < movies.size(); i++) { if (name.matches(movies.get(i).getName())) { list.add(map.getKey()); } } } return list; } // 展示全部电影全部信息 private static void showAll(Customer c) { System.out.println("==================== 电影用户详情 ===================="); System.out.println("你当前信息如下"); System.out.println(c.getUserName() + "\t电话:" + c.getTel() + "\t钱包:" + c.getMoney()); Iterator<Map.Entry<User, List<Movie>>> iterator = ALL_MOVIES.entrySet().iterator(); System.out.println("影院排片信息如下:"); while (iterator.hasNext()) { Map.Entry<User, List<Movie>> map = iterator.next(); if (map == null) { System.out.println("你当前无任何排片信息,请先去上架影片~"); return; } else { Business b = (Business) map.getKey(); System.out.println(b.getBusinessName() + "\t电话:" + b.getTel() + "\t地址:" + b.getAddress() + "\t总营收:" + b.getMoney()); List<Movie> movies = map.getValue(); System.out.println("影片名称:\t\t\t主演:\t\t时长:\t\t票价:\t\t余票:\t\t放映时间:"); for (int i = 0; i < movies.size(); i++) { Movie movie = movies.get(i); System.out.println(movie.getName() + "\t\t" + movie.getAuthor() + "\t\t" + movie.getTimeLong() + "\t\t" + movie.getPrice() + "\t\t" + movie.getCount() + "\t\t" + movie.getStartTime()); } } } return; } // 商店登录 private static void loginBusiness(Business b) { while (true) { System.out.println("==================== 电影商家界面 ===================="); System.out.println(b.getUserName() + (b.getGender() == "男" ? "男士" : "女士") + "你好,请你选择商家操作的功能:"); BusinessMenu(); String type = SC.nextLine(); switch (type) { case "1" -> { // 展示详情 show(b); } case "2" -> { // 上架电影 addMovie(b); } case "3" -> { // 下架电影 deleteMovie(b); } case "4" -> { // 修改电影 updateMovie(b); } case "5" -> { System.out.println("系统退出成功"); return; } } } } private static void updateMovie(Business b) { System.out.println("==================== 电影修改详情 ===================="); List<Movie> movies = ALL_MOVIES.get(b); while (true) { System.out.println("请输入修改电影的名称"); String name = SC.nextLine(); Movie movie = containMovie(name, movies); if (movie == null) { System.out.println("该电影不存在,请重新输入"); } else { while (true) { updateMenu(); String type = SC.nextLine(); switch (type) { case "1" -> { // 修改电影名称 updateMovieName(b, movie, movies); } case "2" -> { // 修改主演名称 System.out.println("请输入新电影主演"); String author = SC.nextLine(); movie.setAuthor(author); updateMovie(b, movie, movies); } case "3" -> { // 修改时长 System.out.println("请输入新电影时长"); Integer timeLong = Integer.parseInt(SC.nextLine()); movie.setTimeLong(timeLong); updateMovie(b, movie, movies); } case "4" -> { // 修改票价 System.out.println("请输入新票价"); Double price = Double.parseDouble(SC.nextLine()); movie.setPrice(price); updateMovie(b, movie, movies); } case "5" -> { // 修改余票 System.out.println("请输入新的余票数"); Integer count = Integer.parseInt(SC.nextLine()); movie.setCount(count); updateMovie(b, movie, movies); } case "6" -> { // 修改放映时间 System.out.println("请输入新的放映时间"); String startTimeStr = SC.nextLine(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date startTime = sdf.parse(startTimeStr); movie.setStartTime(startTime); } catch (ParseException e) { e.printStackTrace(); } updateMovie(b, movie, movies); } case "7" -> { System.out.println("退出系统"); return; } default -> System.out.println("选择错误,请重新选择:>"); } } } } } // 修改电影 private static void updateMovie(Business b, Movie movie, List<Movie> list) { System.out.println("==================== 电影名称修改详情 ===================="); int index = checkIndex(movie, list); Movie set = list.set(index, movie); if (set != null) { ALL_MOVIES.put(b, list); System.out.println("修改成功"); return; } else { System.out.println("修改失败"); } } // 修改电影名称 private static void updateMovieName(Business b, Movie movie, List<Movie> list) { System.out.println("==================== 电影名称修改详情 ===================="); System.out.println("请输入新的电影名称"); String name = SC.nextLine(); movie.setName(name); int index = checkIndex(movie, list); Movie set = list.set(index, movie); if (set != null) { ALL_MOVIES.put(b, list); System.out.println("修改成功"); return; } else { System.out.println("修改失败"); } } // 查找电影的位置 private static int checkIndex(Movie movie, List<Movie> list) { for (int i = 0; i < list.size(); i++) { Movie m = list.get(i); if (m.equals(movie)) { return i; } } return -1; } // 修改电影菜单 private static void updateMenu() { 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.println("7. 退出系统"); System.out.println("请选择:>"); } // 下架电影 private static void deleteMovie(Business b) { System.out.println("==================== 电影下架详情 ===================="); List<Movie> movies = ALL_MOVIES.get(b); if (movies.size() == 0) { System.out.println("该商店没有任何电影,请先去上架"); return; } System.out.println("请输入下架电影的名称"); String name = SC.nextLine(); Movie movie = containMovie(name, movies); boolean flag = movies.remove(movie); if (flag) { System.out.println("下架成功"); return; } else { System.out.println("下架失败,该商家没有该电影,请重新输入"); } } // 查看电影是否存在 private static Movie containMovie(String name, List<Movie> list) { for (int i = 0; i < list.size(); i++) { Movie movie = list.get(i); if (movie.getName().equals(name)) { return movie; } } return null; } // 上架电影 private static void addMovie(Business b) { System.out.println("==================== 电影上架详情 ===================="); List<Movie> movies = new ArrayList<>(); Movie m = new Movie(); System.out.println("请输入新片名"); String name = SC.nextLine(); m.setName(name); System.out.println("请输入主演"); String author = SC.nextLine(); m.setAuthor(author); System.out.println("请输入时长"); Integer TimeLong = Integer.parseInt(SC.nextLine()); m.setTimeLong(TimeLong); System.out.println("请输入票价"); Double price = Double.parseDouble(SC.nextLine()); m.setPrice(price); System.out.println("请输入票数"); Integer count = Integer.valueOf(SC.nextLine()); m.setCount(count); System.out.println("请输入上映时间"); String startTimeStr = SC.nextLine(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date startTime = sdf.parse(startTimeStr); m.setStartTime(startTime); } catch (ParseException e) { e.printStackTrace(); } boolean flag = movies.add(m); if (flag) { ALL_MOVIES.put(b, movies); System.out.println("你已经成功上架电影:" + m.getName()); } else { System.out.println("上架失败"); } } // 展示电影信息 private static void show(Business b) { System.out.println("==================== 电影商家详情 ===================="); System.out.println("你当前信息如下"); System.out.println(b.getBusinessName() + "\t电话:" + b.getTel() + "\t地址:" + b.getAddress() + "\t总营收:" + b.getMoney()); System.out.println("你的影院排片信息如下:"); List<Movie> movies = ALL_MOVIES.get(b); if (movies.size() == 0) { System.out.println("你当前无任何排片信息,请先去上架影片~"); return; } else { System.out.println("影片名称:\t\t\t主演:\t\t时长:\t\t票价:\t\t余票:\t\t放映时间:"); for (int i = 0; i < movies.size(); i++) { Movie movie = movies.get(i); System.out.println(movie.getName() + "\t\t" + movie.getAuthor() + "\t\t" + movie.getTimeLong() + "\t\t" + movie.getPrice() + "\t\t" + movie.getCount() + "\t\t" + movie.getStartTime()); } return; } } // 查看用户是否存在 private static User contains(String name) { for (int i = 0; i < ALL_USER.size(); i++) { User u = ALL_USER.get(i); if (u.getName().equals(name)) { return u; } } return null; } // 菜单 public static void menu() { System.out.println("================== 电影系统主页 ======================="); System.out.println("1. 登录(客户,商家):>"); System.out.println("2. 商家入驻:>"); System.out.println("3. 客户注册:>"); System.out.println("请选择:>"); }
临时数据
// 所有用户信息
public static final List<User> ALL_USER = new ArrayList<>();
// 每家商店的电影信息
public static final Map<User, List<Movie>> ALL_MOVIES = new HashMap<>();
public static final Scanner SC = new Scanner(System.in);
// 每一部影片的分数
public static final Map<Movie, List<Double>> ALL_SCORE = new HashMap<>();
static {
Customer c = new Customer();
c.setName("zyf888");
c.setPassword("123456");
c.setUserName("黑马刘德华");
c.setGender("男");
c.setMoney(10000);
c.setTel("110110");
ALL_USER.add(c);
Customer c1 = new Customer();
c1.setName("gzl888");
c1.setPassword("123456");
c1.setUserName("黑马关之琳");
c1.setGender("女");
c1.setMoney(2000);
c1.setTel("111111");
ALL_USER.add(c1);
Business b = new Business();
b.setName("baozugong888");
b.setPassword("123456");
b.setUserName("黑马包租公");
b.setMoney(0);
b.setGender("男");
b.setTel("110110");
b.setAddress("火星6号2B二层");
b.setBusinessName("甜甜圈国际影城");
ALL_USER.add(b);
// 注意,商家一定需要加入到店铺排片信息中去
List<Movie> movies = new ArrayList<>();
ALL_MOVIES.put(b, movies); // b = []
Business b2 = new Business();
b2.setName("baozupo888");
b2.setPassword("123456");
b2.setUserName("黑马包租婆");
b2.setMoney(0);
b2.setGender("女");
b2.setTel("110110");
b2.setAddress("火星8号8B八层");
b2.setBusinessName("巧克力国际影城");
ALL_USER.add(b2);
// 注意,商家一定需要加入到店铺排片信息中去
List<Movie> movies3 = new ArrayList<>();
ALL_MOVIES.put(b2, movies3); // b2 = []
ArrayList<Movie> list = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = sdf.parse("2021-12-12 12:12:12");
list.add(new Movie("倚天屠龙记", "张无忌", 120, 120, 120, 12, date));
} catch (ParseException e) {
e.printStackTrace();
}
ALL_MOVIES.put(b, list);
}