package com.redred.run;
import com.redred.bean.Business;
import com.redred.bean.Customer;
import com.redred.bean.Movie;
import com.redred.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.ToDoubleFunction;
public class MovieBuy {
/**
存储所有用户信息
*/
public static final List<User> ALL_USERS = new ArrayList<>();
/**
存储所有商家-电影信息
*/
public static final Map<Business, List<Movie>> ALL_MOVIES = new HashMap<>();
/**
定义一个静态的Map集合存储电影的评分
*/
public static final Map<String , List<Double>> MOVIES_SCORE = new HashMap<>();
public static final Scanner SYS_SC = new Scanner(System.in);
/**
存储当前登录用户名
*/
public static User loginUser;
/**
解析时间格式
*/
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
/**
日志
*/
public static final Logger LOGGER = LoggerFactory.getLogger("MovieBuy.class");
/**
定义初始数据
*/
static {
Customer c = new Customer();
c.setLoginName("zs888");
c.setPassWord("123456");
c.setName("张三");
c.setSex('男');
c.setMoney(1000);
c.setPhone("110110");
ALL_USERS.add(c);
Customer c1 = new Customer();
c1.setLoginName("ls888");
c1.setPassWord("123456");
c1.setName("李四");
c1.setSex('女');
c1.setMoney(2000);
c1.setPhone("111111");
ALL_USERS.add(c1);
Business b = new Business();
b.setLoginName("we888");
b.setPassWord("123456");
b.setName("王二");
b.setMoney(0);
b.setSex('男');
b.setPhone("110110");
b.setAddress("荧惑16号2A二层");
b.setShopName("皮尔冲国际影城");
ALL_USERS.add(b);
// 注意,商家一定需要加入到店铺排片信息中去
List<Movie> movies = new ArrayList<>();
ALL_MOVIES.put(b , movies); // b = []
Business b2 = new Business();
b2.setLoginName("mz888");
b2.setPassWord("123456");
b2.setName("麻子");
b2.setMoney(0);
b2.setSex('女');
b2.setPhone("110110");
b2.setAddress("荧惑82号1B八层");
b2.setShopName("卡路里国际影城");
ALL_USERS.add(b2);
// 注意,商家一定需要加入到店铺排片信息中去
List<Movie> movies3 = new ArrayList<>();
ALL_MOVIES.put(b2 , movies3); // b2 = []
}
public static void main(String[] args) {
showMain();
}
private static void showMain() {
while (true) {
System.out.println("---------欢迎光临,愚钝电影城----------");
System.out.println("1.登录");
System.out.println("2.客户注册");
System.out.println("3.商家注册");
System.out.println("4.退出");
System.out.println("请您选择将进行的操作:");
String command = SYS_SC.nextLine();
switch (command){
case "1":
// 登录
login();
break;
case "2":
// 客户注册
break;
case "3":
// 商家注册
break;
case "4":
// 退出
System.out.println("您即将退出,欢迎下次光临~~");
return;
default:
System.out.println("操作有误,请重新选择:");
}
}
}
// 登录
private static void login() {
while (true) {
System.out.println("-------欢迎您的到来,请登录--------");
System.out.println("请输入您的登录名:");
String userName = SYS_SC.nextLine();
System.out.println("请输入您的登录密码:");
String passWord = SYS_SC.nextLine();
User user = loginByUserName(userName);
/**
当成员对象存在时,确认密码是否正确,通过后判定成员对象真实类,进入相应界面
*/
if (user != null) {
if (user.getPassWord().equals(passWord)) {
if (user instanceof Business){
/**
商家信息
*/
showBusinessMain();
}else {
/**
客户信息
*/
showCustomerMain();
}
return;
}else {
System.out.println("您的密码输入错误,请重新输入~~");
}
}else {
System.out.println("您的登录名错误,请确认,再次登录~~");
}
}
}
private static void showCustomerMain() {
System.out.println("-------用户界面--------");
System.out.println(loginUser.getName() + (loginUser.getSex() == '男' ? "先生" : "女士") + "欢迎您进入系统~~" + "您的余额是" + loginUser.getMoney());
while (true) {
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("请输入您的操作:");
String command = SYS_SC.nextLine();
switch (command){
case "1":
// 展示全部排片信息
showAllMovies();
break;
case "2":
queryMovie();
break;
case "3":
// 评分功能
scoreMovie();
sortByScoreToMovie();
showAllMovies();
break;
case "4":
// 购票功能
buyMovie();
break;
case "5":
return; // 干掉方法
default:
System.out.println("不存在该命令!!");
break;
}
}
}
// 电影评分
private static void scoreMovie() {
// 1、查询当前登录成功的用户历史购买记录,看哪些电影是可以评分的。
Customer customer = (Customer) loginUser;
Map<String, Boolean> movies = customer.getBuyMovies();
if(movies.size() == 0 ){
System.out.println("当前您没有看过电影,不能评价!");
return;
}
// 已购买电影 ,看哪些电影是可以评分的。
movies.forEach((name, flag) -> {
if(flag){
System.out.println(name +"此电影已评价");
}else {
// 可添加判断条件哦,更细节
System.out.println("请您对:" + name + "进行打分(0-10):");
double score = Double.valueOf(SYS_SC.nextLine());
// 先根据电影名称拿到评分数据,排序阶段重要数据,用于计算平均值
List<Double> scores = MOVIES_SCORE.get(name); // MOVIES_SCORE = [ 名称=[10] , ... ]
if(scores == null){
// 说明此电影是第一次评价
scores = new ArrayList<>();
scores.add(score);
MOVIES_SCORE.put(name, scores);
}else {
scores.add(score);
}
// 已评价设值为true,下次就不需要再次进行评价了哦
movies.put(name, true);
}
});
}
// 电影评分降序排序
private static void sortByScoreToMovie() {
// 取评分集合所有键
Set<String> strings = MOVIES_SCORE.keySet();
for (User user : ALL_USERS) {
if (user instanceof Business){
List<Movie> movies = ALL_MOVIES.get(user);
// 遍历 评分键名与电影名是否匹配,匹配对齐进行求平均,并将评分修改
for (String string : strings) {
for (Movie movie : movies) {
if (string.equals(movie.getName())){
// double ave = MOVIES_SCORE.get(string).stream().mapToDouble((new ToDoubleFunction<Double>() {
// @Override
// public double applyAsDouble(Double value) {
// return value;
// }
// })).average().getAsDouble();
// 上式简化 stream流--流水线式
double ave = MOVIES_SCORE.get(string).stream().mapToDouble(value->value).average().getAsDouble();
movie.setScore(ave);
}
}
}
// 降序排序,double型使用Double.compare(o1, o2)(升序,反过来降序)
// movies.sort(new Comparator<Movie>() {
// @Override
// public int compare(Movie o1, Movie o2) {
// return Double.compare(o1.getScore(), o2.getScore()); // 升序
// }
// });
// 上式简化
movies.sort((o1, o2) -> Double.compare(o2.getScore(), o1.getScore()));
}
}
}
// 展示所有商家及其电影信息
private static void showAllMovies() {
System.out.println("-------店铺及其影片信息展示--------");
for (User user : ALL_USERS) {
if (user instanceof Business){
System.out.println("店铺名称" + "\t\t" + "店铺地址" + "\t\t" + "店铺老板");
System.out.println(((Business) user).getShopName() + "\t\t" + ((Business) user).getAddress() + "\t\t" + user.getName());
System.out.println("---------影片信息------");
List<Movie> movies = ALL_MOVIES.get(user);
System.out.println("片名" + "\t\t主演" + "\t\t评分" + "\t\t时长" + "\t\t票价" + "\t\t余票" + "\t\t开始时间");
if (movies.size() > 0) {
for (Movie movie : movies) {
System.out.println(movie.getName() + "\t\t" + movie.getActor() + "\t\t" + movie.getScore() + "\t\t"
+ movie.getTime() + "\t\t" + movie.getPrice() + "\t\t" + movie.getNumber() + "\t\t"
+ movie.getStartTime());
}
}else {
System.out.println("影院暂时无影片放映~~");
}
}
}
}
// 查询电影
private static void queryMovie() {
System.out.println("-----------查询界面--------------");
// 临时集合存储商家与查询的电影集合(临时键值对集合)
Map<Business, List<Movie>> temporaryBusinessAndMovieMap = new HashMap<>();
showAllMovies();
while (true) {
System.out.println("请输入您想要看的电影名:");
String name = SYS_SC.nextLine();
// 查询匹配 匹配每一个商家内容信息
for (User user : ALL_USERS) {
// 临时电影集合
List<Movie> temporaryMovie = new ArrayList<>();
if (user instanceof Business){
List<Movie> movies = ALL_MOVIES.get(user);
if (movies.size() > 0) {
for (Movie movie : movies) {
// 当在该商家电影集合中寻找到类似名称将其加入临时集合中去,contains()模糊查询
if (movie.getName().contains(name)){
temporaryMovie.add(movie);
}
}
}
// 当本次查询结果大于0,将商家与查询电影集合存储至临时键值对集合中
if (temporaryMovie.size() > 0){
temporaryBusinessAndMovieMap.put((Business) user, temporaryMovie);
}
}
}
// 将临时键值对集合遍历
if (temporaryBusinessAndMovieMap.size() > 0) {
System.out.println("---------查询结果------------");
temporaryBusinessAndMovieMap.forEach((business, movies) -> {
System.out.println("店铺名称" + "\t\t" + "店铺地址" + "\t\t" + "店铺老板");
System.out.println(business.getShopName() + "\t\t" + business.getAddress() + "\t\t" + business.getName());
System.out.println("---------影片信息------");
System.out.println("片名" + "\t\t主演" + "\t\t评分" + "\t\t时长" + "\t\t票价" + "\t\t余票" + "\t\t开始时间");
for (Movie movie : movies) {
System.out.println(movie.getName() + "\t\t" + movie.getActor() + "\t\t" + movie.getScore() + "\t\t"
+ movie.getTime() + "\t\t" + movie.getPrice() + "\t\t" + movie.getNumber() + "\t\t"
+ movie.getStartTime());
}
}
);
System.out.println("是否需要进行购票呢y/n");
String command = SYS_SC.nextLine();
switch (command){
case "y":
buyMovie();
break;
default:
System.out.println("好滴~~");
return;
}
}else {
System.out.println("查无信息~~~");
System.out.println("是否需要进行查询呢y/n");
String command = SYS_SC.nextLine();
switch (command){
case "y":
break;
default:
System.out.println("好滴~~");
return;
}
}
}
}
// 买票
private static void buyMovie() {
showAllMovies();
Customer customer = (Customer) loginUser;
System.out.println("=============用户购票功能=================");
while (true) {
System.out.println("请您输入需要买票的门店:");
String shopName = SYS_SC.nextLine();
// 1、查询是否存在该商家。
Business business = getBusinessByShopName(shopName);
if(business == null){
System.out.println("对不起,没有该店铺!请确认");
}else {
// 2、此商家全部的排片
List<Movie> movies = ALL_MOVIES.get(business);
// 3、判断是否存在上映的电影
if(movies.size() > 0) {
// 4、开始进行选片购买
while (true) {
System.out.println("请您输入需要购买电影名称:");
String movieName = SYS_SC.nextLine();
// 去当前商家下,查询该电影对象。
Movie movie = getMovieByShopAndName(business, movieName);
if(movie != null){
// 开始购买
while (true) {
System.out.println("请您输入要购买的电影票数:");
String number = SYS_SC.nextLine();
int buyNumber = Integer.valueOf(number);
// 判断电影是否购票
if(movie.getNumber() >= buyNumber){
// 可以购买了
// 当前需要花费的金额
// 高精度计算避免误差
double money = BigDecimal.valueOf(movie.getPrice()).multiply(BigDecimal.valueOf(buyNumber))
.doubleValue();
if(loginUser.getMoney() >= money){
// 终于可以买票了
System.out.println("您成功购买了"+ movie.getName() + " " + buyNumber +
"张票!总金额是:" + money + ",您的余额还剩:" + (loginUser.getMoney() - money));
// 更新自己的金额 更新商家的金额
loginUser.setMoney(loginUser.getMoney() - money);
business.setMoney(business.getMoney() + money);
movie.setNumber(movie.getNumber() - buyNumber);
// 记录购买电影的信息
// 第一个参数是购买的电影,第二个参数是没有评价的标记!
// 评分阶段重要数据内容
customer.getBuyMovies().put(movie.getName(), false);
return;// 结束方法
}else {
// 钱不够!
System.out.println("是否继续~~");
System.out.println("是否继续买票?y/n");
String command = SYS_SC.nextLine();
switch (command) {
case "y":
break;
default:
System.out.println("好的!");
return;
}
}
}else {
// 票数不够
System.out.println("您当前最多可以购买:" + movie.getNumber());
System.out.println("是否继续买票?y/n");
String command = SYS_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 = SYS_SC.nextLine();
switch (command) {
case "y":
break;
default:
System.out.println("好的!");
return;
}
}
}
}
}
private static Movie getMovieByShopAndName(Business business, String name) {
List<Movie> movies = ALL_MOVIES.get(business);
for (Movie movie : movies) {
if(movie.getName().contains(name)){
return movie;
}
}
return null;
}
private static Business getBusinessByShopName(String shopName) {
Set<Business> businesses = ALL_MOVIES.keySet();
for (Business business : businesses) {
if(business.getShopName().equals(shopName)){
return business;
}
}
return null;
}
private static void showBusinessMain() {
System.out.println("--------企业的基本信息,请查看-------");
System.out.println(loginUser.getName() + (loginUser.getSex() == '男' ? "先生" : "女士") + "欢迎您进入系统~~");
while (true) {
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("请输入您的操作:");
String command = SYS_SC.nextLine();
switch (command){
case "1":
// 展示全部排片信息
showBusinessInfos();
break;
case "2":
// 上架电影信息
addMovie();
break;
case "3":
// 下架电影信息
deleteMovie();
break;
case "4":
// 修改电影信息
updateMovie();
break;
case "5":
System.out.println(loginUser.getLoginName() +"请您下次再来啊~~~");
return; // 干掉方法
default:
System.out.println("不存在该命令!!");
break;
}
}
}
// 商家电影信息展示
private static void showBusinessInfos() {
System.out.println("---------影片信息------");
// 强转可获取商家都有行为
Business business = (Business) loginUser;
List<Movie> movies = ALL_MOVIES.get(business);
System.out.println("片名" + "\t\t主演" + "\t\t评分" + "\t\t时长" + "\t\t票价" + "\t\t余票" + "\t\t开始时间");
/**
假如商家无电影上映,电影集合为空,提醒商家;反之将其打印
*/
if (movies.size() > 0) {
for (Movie movie : movies) {
System.out.println(movie.getName() + "\t\t" + movie.getActor() + "\t\t" + movie.getScore() + "\t\t"
+ movie.getTime() + "\t\t" + movie.getPrice() + "\t\t" + movie.getNumber() + "\t\t"
+ movie.getStartTime());
}
}else {
System.out.println("您的影院暂时无影片放映,请先添加~~~");
}
}
// 添加电影
private static void addMovie() {
System.out.println("--------添加影片界面-----------");
Business business = (Business) loginUser;
// 通过商家键获取对应电影集合,进行数据添加
List<Movie> movies = ALL_MOVIES.get(business);
while (true) {
System.out.println("请您输入必要的信息:");
System.out.println("请您输入片名:");
String name = SYS_SC.nextLine();
System.out.println("请您输入主演:");
String actor = SYS_SC.nextLine();
System.out.println("请您输入时长:");
String time = SYS_SC.nextLine();
System.out.println("请您输入票价:");
String price = SYS_SC.nextLine();
System.out.println("请您输入余票:");
String number = SYS_SC.nextLine();
// 时间异常,日志记录,防止异常导致运行结束,可最终输入正确,程序继续运行
while (true) {
try {
System.out.println("请您输入开始时间:");
String startTime = SYS_SC.nextLine();
Movie movie = new Movie(name, actor, Double.valueOf(time), Double.valueOf(price), Integer.valueOf(number), sdf.parse(startTime));
movies.add(movie);
System.out.println("您已经成功上架了" + movie.getName());
return;
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("时间解析出错");
}
}
}
}
// 删除电影
private static void deleteMovie() {
Business business = (Business)loginUser;
List<Movie> movies = ALL_MOVIES.get(business);
System.out.println("---------删除界面----------");
if (movies.size() > 0){
while (true) {
System.out.println("请输入您需要进行删除的影片名:");
String name = SYS_SC.nextLine();
Movie movie = getMovieByName(name);
if (movie != null) {
showBusinessInfos();
System.out.println("是否删除本影片y/n");
String select = SYS_SC.nextLine();
switch (select){
case "y":
// 从电影集合中删除寻找的电影即可
movies.remove(movie);
System.out.println("您当前店铺已经成功下架了:" + movie.getName());
showBusinessInfos();
return;
default:
System.out.println("好滴呢。");
return;
}
}else {
System.out.println("无此片信息,是否输入错误~~");
System.out.println("请问继续下架吗?y/n");
String command = SYS_SC.nextLine();
switch (command) {
case "y":
break;
default:
System.out.println("好的!");
return;
}
}
}
}else {
System.out.println("影院暂时无影片,请先添加再确认是否需要删除~~");
return;
}
}
// 通过电影名寻找是否存在该电影,字符串应使用equals()方法
public static Movie getMovieByName(String name){
Business business = (Business) loginUser;
List<Movie> movies = ALL_MOVIES.get(business);
for (Movie movie : movies) {
if(movie.getName().equals(name)) {
return movie;
}
}
return null;
}
// 过于啰嗦了,可以只使用操作7 修改电影信息
private static void updateMovie() {
Business business = (Business)loginUser;
List<Movie> movies = ALL_MOVIES.get(business);
System.out.println("---------修改界面----------");
if (movies.size() > 0){
while (true) {
System.out.println("请输入您需要进行修改的影片名:");
String name = SYS_SC.nextLine();
for (Movie movie : movies) {
if (movie.getName().equals(name)) {
while (true) {
try {
showBusinessInfos();
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("8.退出");
System.out.println("请输入您需要修改的操作或全部修改:");
String command = SYS_SC.nextLine();
switch (command) {
case "1":
System.out.println("1.修改片名");
movie.setName(SYS_SC.nextLine());
break;
case "2":
System.out.println("2.修改主演");
movie.setActor(SYS_SC.nextLine());
break;
case "3":
System.out.println("3.修改时长");
movie.setTime(Double.valueOf(SYS_SC.nextLine()));
break;
case "4":
System.out.println("4.修改票价");
movie.setPrice(Double.valueOf(SYS_SC.nextLine()));
break;
case "5":
System.out.println("5.修改余票");
movie.setNumber(Integer.valueOf(SYS_SC.nextLine()));
break;
case "6":
System.out.println("6.修改开始时间");
movie.setStartTime(sdf.parse(SYS_SC.nextLine()));
break;
case "7": {
System.out.println("1.修改片名");
movie.setName(SYS_SC.nextLine());
System.out.println("2.修改主演");
movie.setActor(SYS_SC.nextLine());
System.out.println("3.修改时长");
movie.setTime(Double.valueOf(SYS_SC.nextLine()));
System.out.println("4.修改票价");
movie.setPrice(Double.valueOf(SYS_SC.nextLine()));
System.out.println("5.修改余票");
movie.setNumber(Integer.valueOf(SYS_SC.nextLine()));
System.out.println("6.修改开始时间");
movie.setStartTime(sdf.parse(SYS_SC.nextLine()));
}
break;
case "8":
System.out.println("退出去咯~~");
return;
default:
System.out.println("操作失误,请重新选择~~");
}
} catch (ParseException e) {
e.printStackTrace();
LOGGER.error("修改时间解析错误");
}
}
} else {
System.out.println("无此片信息,是否输入错误~~");
}
}
}
}else {
System.out.println("影院暂时无影片,请先上映再来修改哦~~");
return;
}
}
/**
通过登录名寻找ALL_USERS中是否存在该成员,是:返回成员对象,否:返回null
*/
private static User loginByUserName(String userName) {
for (User user : ALL_USERS) {
if (user.getLoginName().equals(userName)){
loginUser = user;
return user;
}
}
return null;
}
}
购票 Java
最新推荐文章于 2023-02-22 17:04:25 发布