电影购票系统的简单实现
实现目标
简单的电影院购票系统。
一、技术点分析
1.面向对象编程
系统包含了电影对象,商家对象,客户对象,需要用到继承、多态等语法知识。
2.程序流程控制
需要结合分支、循环、跳转关键字等相关操作控制程序的业务逻辑
3、使用集合容器
系统需要提供不同的容器分别存储系统注册的用户,以及当前商家发布的电影信息
4、使用常见API
登录信息的内容比较,业务数据的分析、处理,日期时间的处理等。
二、使用步骤
1.日志框架搭建、系统角色设计
2、首页、登录、商家界面、用户界面实现
showMain():
private static void showMain() {
while (true) {
System.out.println("================电影首页=================");
System.out.println("1、登录");
System.out.println("2、用户注册");
System.out.println("3、商家注册");
while (true) {
System.out.println("请输入操作命令:");
String command = SYS_SC.nextLine();
switch (command) {
case "1":
// 登录
login();
break;
case "2":
break;
case "3":
break;
default:
System.out.println("命令有误,请确认!");
}
}
}
}
showCustomerMain():
private static void showCustomerMain() {
while (true) {
System.out.println("================电影客户界面=================");
System.out.println(loginUser.getUserName() + (loginUser.getSex()=='男'?"先生":"女士")+"欢迎您进入系统" +
"\t余额:" + loginUser.getMoney());
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":
//
break;
case "3":
//
break;
case "4":
//购票功能
buyMovie();
break;
case "5":
return;
default:
System.out.println("不存在该命令!");
break;
}
}
}
showBusinessMain():
private static void showBusinessMain() {
while (true) {
System.out.println("================电影商家界面=================");
System.out.println(loginUser.getUserName() + (loginUser.getSex()=='男'?"先生":"女士"+"欢迎您进入系统"));
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.getUserName()+"请您下次再来啊~~~");
return;
default:
System.out.println("不存在该命令!");
break;
}
}
}
3、商家功能
1、展示详情
showBusinessInfos():
private static void showBusinessInfos() {
System.out.println("================商家详情界面======================");
LOGGER.info(loginUser.getUserName()+"商家,正在看自己的详情~~");
Business business = (Business) loginUser;
System.out.println(business.getShopName()+"\t\t电话:"+business.getPhone()+"\t\t地址:"+business.getAddress());
List<Movie> movies = ALL_MOVIES.get(loginUser);
if(movies.size() > 0){
System.out.println("片名\t\t\t主演\t\t时长\t\t评分\t\t票价\t\t余票数量\t\t放映时间");
for (Movie movie : movies) {
System.out.println(movie.getName()+"\t\t\t"+movie.getActor()+"\t\t"+movie.getTime()+
"\t\t"+movie.getScore()+"\t\t"+movie.getPrice()+"\t\t"+movie.getName()+"\t\t"
+ sdf.format(movie.getStartTime()));
}
}else{
System.out.println("您的店铺当前无片可播");
}
}
2、影片上架
addMovie():
private static void addMovie() {
Business business = (Business) loginUser;
List<Movie> movies = ALL_MOVIES.get(loginUser);
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 totalNumber = SYS_SC.nextLine();
while (true) {
try{
System.out.println("请输入影片放映时间:");
String stime = SYS_SC.nextLine();
Movie movie = new Movie(name, actor, Double.valueOf(time), Double.valueOf(price)
,Integer.valueOf(totalNumber), sdf.parse(stime));
movies.add(movie);
System.out.println("您已成功上架了:《"+movie.getName()+"》");
return;
} catch (ParseException e) {
e.printStackTrace();
LOGGER.error("时间解析出了毛病");
}
}
}
3、影片下架
deleteMovie():
private static void deleteMovie() {
System.out.println("==============下架电影=================");
Business business = (Business) loginUser;
List<Movie> movies = ALL_MOVIES.get(loginUser);
if(movies.size() == 0){
System.out.println("当前无片下架");
return;
}
// 2、用户选择需要下架的电影
while (true) {
System.out.println("请您输入需要下架的电影名称:");
String movieName = SYS_SC.nextLine();
// 3、查询有没有
Movie movie = getMovieByName(movieName);
if(movie != null){
movies.remove(movie);
showBusinessInfos();
System.out.println("您当前店铺已经成功下架了:"+movie.getName());
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;
}
}
}
}
/**
去查询当前商家下的影片
*/
public static Movie getMovieByName(String movieName){
Business business = (Business) loginUser;
List<Movie> movies = ALL_MOVIES.get(loginUser);
for (Movie movie : movies) {
if(movie.getName().contains(movieName)){
return movie;
}
}
return null;
}
4、影片修改
updateMovie():
private static void updateMovie() {
System.out.println("==============修改电影=================");
Business business = (Business) loginUser;
List<Movie> movies = ALL_MOVIES.get(loginUser);
if(movies.size() == 0){
System.out.println("当前无片下架");
return;
}
// 2、用户选择需要修改的电影
while (true) {
System.out.println("请您输入需要修改的电影名称:");
String movieName = SYS_SC.nextLine();
// 3、查询有没有
Movie movie = getMovieByName(movieName);
if(movie != null){
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 totalNumber = SYS_SC.nextLine();
while (true) {
try{
System.out.println("请输入影片放映时间:");
String stime = SYS_SC.nextLine();
movie.setName(name);
movie.setActor(actor);
movie.setPrice(Double.valueOf(price));
movie.setTime(Double.valueOf(time));
movie.setNumber(Integer.valueOf(totalNumber));
movie.setStartTime(sdf.parse(stime));
System.out.println("恭喜您修改了该影片");
showBusinessInfos();
return;
} catch (ParseException e) {
e.printStackTrace();
LOGGER.error("时间解析出了毛病");
}
}
}else{
System.out.println("您的店铺没有商家该影片!");
System.out.println("请问继续修改吗?y/n");
String command = SYS_SC.nextLine();
switch (command){
case "y":
break;
default:
System.out.println("好的!");
return;
}
}
}
}
4、用户功能
1、展示全部影片
showAllMovies():
private static void showAllMovies() {
System.out.println("================= 展示全部商家和其排片信息=======================");
ALL_MOVIES.forEach((business, movies) -> {
System.out.println(business.getShopName()+"\t\t电话:"+business.getPhone()+"\t\t地址:"+business.getAddress());
System.out.println("\t\t\t片名\t\t\t主演\t\t时长\t\t评分\t\t票价\t\t余票数量\t\t放映时间");
for (Movie movie : movies) {
System.out.println("\t\t\t"+movie.getName()+"\t\t\t"+movie.getActor()+"\t\t"+movie.getTime()+
"\t\t"+movie.getScore()+"\t\t"+movie.getPrice()+"\t\t"+movie.getName()+"\t\t"
+ sdf.format(movie.getStartTime()));
}
});
}
2、购票功能
buyMovie():
private static void buyMovie() {
showAllMovies();
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.setMoney(loginUser.getMoney() - money);
business.setMoney(business.getMoney() - money);
movie.setNumber(movie.getNumber() - buyNumber);
return;
}else{
// 钱不够
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;
}
}
}
}
}