Java学习-模拟电影购票系统

目录

1 日志框架搭建、系统角色分析

2 首页设计、登录、商家界面、用户界面实现

3 商家详情页设计、影片上架、退出

4 商家-影片下架、影片修改

5 用户-展示全部影片

6 用户-购票功能

7 用户-评分功能

8 用户-根据片名查询全部影片信息

9 MovieSystem类全部代码

1 日志框架搭建、系统角色分析

日志框架搭建在上次已经搭建好,见上个文档

系统角色分析

①集成日志框架、用于后期记录日志信息。
②定义一个电影类Movie类,Movie类包含: 片名、主演、评分、时长、票价、余票
③系统包含2个用户角色:客户、商家。存在大量相同属性信息。
④定义User类作为父类,属性:登录名称、密码、真实名称、性别、电话、账户金额
⑤定义Business类代表商家角色, 属性:店铺名称、地址。
⑥定义Customer类代表客户角色,属性:
⑦定义集合List 用户存放系统注册的用户对象信息。
⑧定义集合Map>存放商家和其排片信息。
Java学习-模拟电影购票系统_第1张图片

Movie类

package com.zwit.bean;

import javax.xml.crypto.Data;
import java.util.Date;

public class Movie {
    private String name;
    private String actor;
    private double score;
    private double time;
    private double price;
    private int number;//余票
    private Date startTime;//上映时间

    public Movie() {
    }

    public Movie(String name, String actor, double time, double price, int number, Date startTime) {
        this.name = name;
        this.actor = actor;
        this.time = time;
        this.price = price;
        this.number = number;
        this.startTime = startTime;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public double getTime() {
        return time;
    }

    public void setTime(double time) {
        this.time = time;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
}

 User类

package com.zwit.bean;

public class User {
    private String loginName;//登录名称
    private String passsword;
    private String name;//真实名称
    private char sex;
    private String phone;
    private double money;//账户余额

    public User() {
    }

    public User(String loginName, String passsword, String name, char sex, String phone, double money) {
        this.loginName = loginName;
        this.passsword = passsword;
        this.name = name;
        this.sex = sex;
        this.phone = phone;
        this.money = money;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPasssword() {
        return passsword;
    }

    public void setPasssword(String passsword) {
        this.passsword = passsword;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}

Business类

package com.zwit.bean;

public class Business extends User{
    private String shopName;
    private String address;

    public Business() {
    }

    public Business(String loginName, String passsword, String name, char sex, String phone, double money, String shopName, String address) {
        super(loginName, passsword, name, sex, phone, money);
        this.shopName = shopName;
        this.address = address;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

Customer类

package com.zwit.bean;

public class Customer extends User{
    public Customer() {
    }

    public Customer(String loginName, String passsword, String name, char sex, String phone, double money) {
        super(loginName, passsword, name, sex, phone, money);
    }
}


2 首页设计、登录、商家界面、用户界面实现

①首页需要包含登录,商家入驻,客户注册功能。
②商家和客户可以共用一一个登录功能。
③判断登录成功的用户的真实类型,根据用户类型完成对应的操作界面设计。
首页

    private static void showMain() {
        System.out.println("==========三国电影首页============");
        System.out.println("1.登录");
        System.out.println("2.用户注册");
        System.out.println("3.商家注册");
        while (true) {
            System.out.println("请输入操作命令:");
            String comond = SYS_SC.nextLine();//最好是一次扫描一行,避免出现bug
            switch (comond){
                case "1":
                    //登录
                    login();
                    break;
                case "2":
                    //用户注册
                    break;
                case "3":
                    //商家注册
                    break;
                default:
                    System.out.println("您输入信息有误,请重新输入!");
                    break;
            }
        }
    }

用户登录

    private static void login() {
        while (true) {
            System.out.println("请输入用户名称:");
            String name = SYS_SC.nextLine();
            System.out.println("请输入密码:");
            String password = SYS_SC.nextLine();
            //1.根据用户名查询用户
            User u = getUserByLoginName(name);
            //2.判断用户名是否存在
            if (u != null){
                //有此用户,判断密码是否正确
                if (u.getPasssword().equals(password)){
                    //密码正确,登录成功,判断是客户还是商家登录
                    loginUser = u;
                    if (u instanceof Customer){
                        //是客户登录的
                        showCustomerMain();
                    }else {
                        //是商家登录的
                        showBusinessMain();
                    }
                return;
                }else{
                    //密码错误
                    System.out.println("密码错误,请从新输入!");
                }
            }else {
                //无此用户
                System.out.println("用户名有毛病,请重新输入!");
            }
        }
    }

商家和用户登录界面

    private static void showBusinessMain() {
        while (true) {
            System.out.println("=========三国电影商家界面=========");
            System.out.println(loginUser.getName()+(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 commond = SYS_SC.nextLine();
            switch (commond){
                case "1":
                    //展示详情
                    break;
                case "2":
                    //上架电影
                    break;
                case "3":
                    //下架电影
                    break;
                case "4":
                    //修改电影
                    break;
                case "5":
                    //退出系统
                    return;
                default:
                    System.out.println("您输入选择有误,请重新输入!");
            }
        }
    }

    /**
     * 客户登录界面
     */
    private static void showCustomerMain() {
        while (true) {
            System.out.println("=========三国电影客户界面=========");
            System.out.println(loginUser.getName()+(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 commond = SYS_SC.nextLine();
            switch (commond){
                case "1":
                    //展示全部影片信息
                    break;
                case "2":
                    //根据电影名称查询电影信息
                    break;
                case "3":
                    //评分功能
                    break;
                case "4":
                    //购票功能
                    break;
                case "5":
                    //退出系统
                    return;
                default:
                    System.out.println("您输入选择有误,请重新输入!");
            }
        }
    }

查询用户方法

    private static User getUserByLoginName(String name) {
        for (User user : ALL_USER) {
            if (user.getLoginName().equals(name))
                return user;
        }
        return null;
    }

3 商家详情页设计、影片上架、退出

①展示本商家的信息和其排片情况。
②提供影片,上架功能:就是创建一 个影片对象,存入到商家的集合中去。
③退出,需要回到登录的首页。
 

展示商家信息

    private static void showBusinessInfo() {
        LOGGER.info(loginUser.getName() + "正在查看详情");
        //需要将loginUser转为Business才能得到Business的全部信息
        Business b = (Business) loginUser;
        System.out.println(b.getShopName()+"\t\t"+b.getPhone()+"\t\t"+b.getAddress());
        //得到该商家的所有电影
        List movies = ALL_MOVIES.get(b);
        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\t"+movie.getTime()+
                        "\t\t\t"+movie.getScore()+"\t\t\t"+movie.getPrice()+"\t\t\t"+movie.getNumber()
                        +"\t\t\t"+sdf.format(movie.getStartTime()));
            }
        }else{
            System.out.println("您的店铺没有片可播!去买点片吧。");
        }

    }

上架电影

    private static void addMovie() {
        Business b = (Business) loginUser;
        List movies = ALL_MOVIES.get(b);
        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) {
            //Movie(String name, String actor, double time, double price, int number, Data startTime)
            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 (ParseException e) {
                e.printStackTrace();
                System.out.println("您输入的日期有误!");
            }
        }
    }

4 商家-影片下架、影片修改

①提供影片下架功能:其实就是从商家的集合中删除影片对象。
②影片修改功能:拿到需要修改的影片对象,修改里面的数据。

下架电影

    private static void deleteMovie() {
        while (true) {
            System.out.println("=============下架电影==============");
            Business b = (Business) loginUser;
            List movies = ALL_MOVIES.get(b);
            //若集合为0则没有电影
            if (movies.size() == 0){
                System.out.println("没有电影可以下架!");
                return;
            }

            System.out.println("请输入要下架的电影名称:");
            String name = SYS_SC.nextLine();
            //查找有没有这部电影
            Movie m = getMovieByNamxe(name);
            if (m != null){
                //有这部电影
                System.out.println("确认下架吗?(y/n)");
                String commond = SYS_SC.nextLine();
                if (commond.equals("y")){
                    movies.remove(m);
                    System.out.println(m.getName() + "下架成功");
                    return;
                }else {
                    System.out.println("给您保留此电影");
                    return;
                }
            }else {
                System.out.println("没有这部电影。。请重新输入!");
            }
        }

    }

修改电影

    private static void updateMovie() {
        System.out.println("=============修改电影==============");
        Business b = (Business) loginUser;
        List movies = ALL_MOVIES.get(b);
        //若集合为0则没有电影
        if (movies.size() == 0){
            System.out.println("没有电影可以修改!");
            return;
        }

        System.out.println("请输入要修改的电影名称:");
        String name = SYS_SC.nextLine();
        //查找有没有这部电影
        Movie m = getMovieByNamxe(name);
        if (m != null){
            //有这部电影
            System.out.println("请修改电影信息:");
            //Movie(String name, String actor, double time, double price, int number, Date startTime)
            System.out.println("请输入片名:");
            String nname = 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) {
                //Movie(String name, String actor, double time, double price, int number, Data startTime)
                try {
                    System.out.println("请输入放映时间:");
                    String startTime = SYS_SC.nextLine();
                    m.setName(nname);
                    m.setActor(actor);
                    m.setTime(Double.valueOf(time));
                    m.setPrice(Double.valueOf(price));
                    m.setNumber(Integer.valueOf(number));
                    m.setStartTime(sdf.parse(startTime));
                    System.out.println("修改成功!");
                    return;
                } catch (ParseException e) {
                    e.printStackTrace();
                    System.out.println("您输入的日期有误!");
                }
            }
        }else {
            System.out.println("没有这部电影。。请重新输入!");
        }
    }

根据电影名称查询是否有这部电影方法

    private static Movie getMovieByNamxe(String name) {
        Business b = (Business) loginUser;
        List movies = ALL_MOVIES.get(b);
        for (Movie movie : movies) {
            if (movie.getName().equals(name))
                return movie;
        }
        return null;
    }


5 用户-展示全部影片

    private static void showAllMovies() {
        System.out.println("===============查询所有商家的所有电影信息=================");
        ALL_MOVIES.forEach((b, movies) -> {
            System.out.println(b.getShopName()+"\t\t电话:"+b.getPhone()+"\t\t地址:"+b.getAddress());
            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\t"+movie.getTime()+
                        "\t\t\t"+movie.getScore()+"\t\t\t"+movie.getPrice()+"\t\t\t"+movie.getNumber()
                        +"\t\t\t"+sdf.format(movie.getStartTime()));
            }
            System.out.println("================================================================");
        });
    }


6 用户-购票功能

①用户可以选择需要购买票的商家和其电影信息。
②可以选择购买的数量。
③购买成功后需要支付金额,并更新商家金额和客户金额
 

购票

    private static void byMovie() {
        showAllMovies();
        while (true) {
            System.out.println("==============用户购票===============");
            System.out.println("请输入店铺名称:");
            String shopName = SYS_SC.nextLine();
            //1.查找该店铺是否存在
            Business b = getBusinessByShopName(shopName);
            if (b != null){
                //店铺存在
                //输入电影名称,查找电影
                List movies = ALL_MOVIES.get(b);
                if (movies.size() > 0){
                    while (true){
                        //开始购票
                        System.out.println("请输入想看的电影:");
                        String movieName = SYS_SC.nextLine();
                        //查看是否存在这部电影
                        Movie movie = getMovieByShopName(b, movieName);
                        if (movie != null){
                            while (true) {
                                //有该电影
                                System.out.println("请输入票数:");
                                String number = SYS_SC.nextLine();
                                //票数不足
                                if (movie.getNumber() > Integer.valueOf(number)){
                                    //余票够
                                    //总共需要价格
                                    double sumPrice = BigDecimal.valueOf(movie.getPrice()).
                                            multiply(BigDecimal.valueOf(Integer.valueOf(number))).doubleValue();
                                    //判断用户余额是否够用
                                    if (loginUser.getMoney() >= sumPrice){
                                        //余额够用
                                        System.out.println("您已成功购买《"+movieName+"》"+Integer.valueOf(number)+"张"+"总金额为:"+sumPrice);
                                        //更新用户余额,店铺票数,店铺余额
                                        loginUser.setMoney(loginUser.getMoney()-sumPrice);
                                        movie.setNumber(movie.getNumber() - Integer.valueOf(number));
                                        b.setMoney(b.getMoney()+sumPrice);
                                        return;
                                    }else {
                                        //余额不足
                                        System.out.println("您的余额不足,是否继续购买?(y/n)您的余额为" + loginUser.getMoney());
                                        String commond = SYS_SC.nextLine();
                                        switch (commond){
                                            case "y":
                                                System.out.println("请继续购票!");
                                                break;
                                            default:
                                                System.out.println("为您退出购票功能!");
                                                return;
                                        }
                                    }
                                }else {
                                    //余票不够
                                    System.out.println("余票不足,是否继续购买?(y/n)余票为" + movie.getNumber());
                                    String commond = SYS_SC.nextLine();
                                    switch (commond){
                                        case "y":
                                            System.out.println("请继续购票!");
                                            break;
                                        default:
                                            System.out.println("为您退出购票功能!");
                                            return;
                                    }
                                }
                            }
                        }else {
                            //没有该电影
                            System.out.println("不存在该电影");
                            System.out.println("还要继续购买吗?(y/n)");
                            String commond = SYS_SC.nextLine();
                            switch (commond){
                                case "y":
                                    System.out.println("请继续购票!");
                                    break;
                                default:
                                    System.out.println("为您退出购票功能!");
                                    return;
                            }
                        }
                    }

                }else {
                    //没片
                    System.out.println("此店铺没有片可播,请重新选择!");
                    System.out.println("还要继续购买吗?(y/n)");
                    String commond = SYS_SC.nextLine();
                    switch (commond){
                        case "y":
                            System.out.println("请继续购票!");
                            break;
                        default:
                            System.out.println("为您退出购票功能!");
                            return;
                    }
                }
            }else{
                System.out.println("该店铺不存在,请重新输入!");
            }
        }

    }
    /**
     * 根据电影名称和店铺查询此店铺是否有该电影
     * @param b
     * @param movieName
     * @return
     */
    private static Movie getMovieByShopName(Business b, String movieName) {
        List movies = ALL_MOVIES.get(b);
        for (Movie movie : movies) {
            if (movie.getName().equals(movieName)){
                return movie;
            }
        }
        return null;
    }
    /**
     * 根据店铺名称查找该店铺是否存在
     * @param shopName
     * @return
     */
    private static Business getBusinessByShopName(String shopName) {
        Set businesses = ALL_MOVIES.keySet();//keySet方法返回所有的key集合
        for (Business business : businesses) {
            if (business.getShopName().equals(shopName))
                return business;
        }
        return null;
    }


7 用户-评分功能

    private static void scoreMovie() {
        while (true) {
            System.out.println("==============用户评分===============");
            System.out.println("请输入店铺名:");
            String shopName = SYS_SC.nextLine();
            //判断店铺是否存在
            Business b = getBusinessByShopName(shopName);
            if (b != null){
                while (true){
                    //存在
                    System.out.println("请输入想评分的电影:");
                    String movieName = SYS_SC.nextLine();
                    //查看是否存在这部电影
                    Movie movie = getMovieByShopName(b, movieName);
                    if (movie != null){
                        while (true) {
                            //存在
                            System.out.println("请评分:(满分10分)");
                            double score = Double.valueOf(SYS_SC.nextLine());
                            if (score <= 0 || score > 10){
                                System.out.println("您输入的分数有误,需要在0-10之间,请重新输入!");
                            }else{
                                movie.setScore(score);
                                System.out.println("评分成功,您给的分数为:"+score);
                                return;
                            }
                        }

                    }else{
                        //不存在
                        System.out.println("您输入的电影不存在,请重新输入电影名称");
                        System.out.println("还要继续评分吗?(y/n)");
                        String commond = SYS_SC.nextLine();
                        switch (commond){
                            case "y":
                                System.out.println("请继续购票!");
                                break;
                            default:
                                System.out.println("为您退出购票功能!");
                                return;
                        }
                    }
                }

            }else{
                //不存在
                System.out.println("您输入的店铺不存在,请重新输入!");
                System.out.println("还要继续评分吗?(y/n)");
                String commond = SYS_SC.nextLine();
                switch (commond){
                    case "y":
                        System.out.println("请继续购票!");
                        break;
                    default:
                        System.out.println("为您退出购票功能!");
                        return;
                }
            }
        }
    }


8 用户-根据片名查询全部影片信息

    private static void showMovieByName() {
        while (true) {
            System.out.println("==============查询电影===============");
            System.out.println("请输入店铺名:");
            String shopName = SYS_SC.nextLine();
            //判断店铺是否存在
            Business b = getBusinessByShopName(shopName);
            if (b != null){
                while (true){
                    //存在
                    System.out.println("请输入想查找的电影:");
                    String movieName = SYS_SC.nextLine();
                    //查看是否存在这部电影
                    Movie movie = getMovieByShopName(b, movieName);
                    if (movie != null){
                        System.out.println("片名\t\t\t主演\t\t时长\t\t评分\t\t票价\t\t余票数量\t\t放映时间");
                        System.out.println(movie.getName()+"\t\t\t"+movie.getActor()+"\t\t\t"+movie.getTime()+
                                "\t\t\t"+movie.getScore()+"\t\t\t"+movie.getPrice()+"\t\t\t"+movie.getNumber()
                                +"\t\t\t"+sdf.format(movie.getStartTime()));
                        System.out.println("是否继续查找电影信息?(y/n)");
                        String commond = SYS_SC.nextLine();
                        switch (commond){
                            case "y":
                                System.out.println("请继续查找!");
                                break;
                            default:
                                System.out.println("为您退出查找功能!");
                                return;
                        }
                    }else{
                        //不存在
                        System.out.println("您输入的电影不存在,请重新输入电影名称");
                        System.out.println("还要继续查找吗?(y/n)");
                        String commond = SYS_SC.nextLine();
                        switch (commond){
                            case "y":
                                System.out.println("请继续查找!");
                                break;
                            default:
                                System.out.println("为您退出查找功能!");
                                return;
                        }
                    }
                }

            }else{
                //不存在
                System.out.println("您输入的店铺不存在,请重新输入!");
                System.out.println("还要继续评分吗?(y/n)");
                String commond = SYS_SC.nextLine();
                switch (commond){
                    case "y":
                        System.out.println("请继续查找!");
                        break;
                    default:
                        System.out.println("为您退出查找功能!");
                        return;
                }
            }
        }
    }

9 MovieSystem类全部代码

package com.zwit.run;

import com.zwit.bean.Business;
import com.zwit.bean.Customer;
import com.zwit.bean.Movie;
import com.zwit.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.*;

public class MovieSystem {
    //1.List集合存放User信息,常量容器,不允许修改
    public static final List ALL_USER = new ArrayList<>();

    //2.Map集合存放商家和拍片信息
    public static final Map> ALL_MOVIES = new HashMap<>();

    //扫描器
    public static final Scanner SYS_SC = new Scanner(System.in);

    //定义一个静态的User类型的变量记住当前登录成功的用户对象,方便不同方法对当前对象的使用
    public static User loginUser;

    //定义一个日期对象,方便对日期相关数据操作
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    //记录日志
    public static Logger LOGGER = LoggerFactory.getLogger("MovieSystem");

    //静态代码块,在系统启动之前就把数据存在集合
    static {
        //3.准备测试数据
        //创建客户对象
        Customer c1 = new Customer("zw123","123456","吴彦祖",'男',"13658649",180.5);
        Customer c2 = new Customer("wzj123","123456","刘亦菲",'女',"143152526",180.5);
        Customer c3 = new Customer("yyy123","123456","张飞",'男',"13242356",180.5);
        //添加到List集合
        Collections.addAll(ALL_USER,c1,c2,c3);

        //创建商家对象
        Business b1 = new Business();
        b1.setLoginName("lb123");
        b1.setPasssword("123456");
        b1.setName("刘备");
        b1.setSex('男');
        b1.setPhone("136843567");
        b1.setShopName("蜀国影业");
        b1.setAddress("河北");
        //商家刘备的电影集合
        List movies1 = new ArrayList<>();

        Business b2 = new Business();
        b2.setLoginName("cc123");
        b2.setPasssword("123456");
        b2.setName("曹操");
        b2.setSex('男');
        b2.setPhone("136843321");
        b2.setShopName("魏国影业");
        b2.setAddress("山东");
        //商家曹操的电影集合
        List movies2 = new ArrayList<>();

        //商家也属于用户添加到List集合
        Collections.addAll(ALL_USER,b1,b2);
        //商家要添加到Map集合,商家要有电影上映
        ALL_MOVIES.put(b1,movies1);
        ALL_MOVIES.put(b2,movies2);

    }

    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("请输入操作命令:");
            String comond = SYS_SC.nextLine();//最好是一次扫描一行,避免出现bug
            switch (comond){
                case "1":
                    //登录
                    login();
                    break;
                case "2":
                    //用户注册
                    break;
                case "3":
                    //商家注册
                    break;
                default:
                    System.out.println("您输入信息有误,请重新输入!");
                    break;
            }
        }
    }

    /**
     * 用户登录
     */
    private static void login() {
        while (true) {
            System.out.println("请输入用户名称:");
            String name = SYS_SC.nextLine();
            System.out.println("请输入密码:");
            String password = SYS_SC.nextLine();
            //1.根据用户名查询用户
            User u = getUserByLoginName(name);
            //2.判断用户名是否存在
            if (u != null){
                //有此用户,判断密码是否正确
                if (u.getPasssword().equals(password)){
                    //密码正确,登录成功,判断是客户还是商家登录
                    loginUser = u;
                    LOGGER.info(u.getName() + "登录了系统");
                    if (u instanceof Customer){
                        //是客户登录的
                        showCustomerMain();
                    }else {
                        //是商家登录的
                        showBusinessMain();
                    }
                return;
                }else{
                    //密码错误
                    System.out.println("密码错误,请从新输入!");
                }
            }else {
                //无此用户
                System.out.println("用户名有毛病,请重新输入!");
            }
        }
    }

    /**
     * 商家登录界面
     */
    private static void showBusinessMain() {
        while (true) {
            System.out.println("=========三国电影商家界面=========");
            System.out.println(loginUser.getName()+(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 commond = SYS_SC.nextLine();
            switch (commond){
                case "1":
                    //展示详情
                    showBusinessInfo();
                    break;
                case "2":
                    //上架电影
                    addMovie();
                    break;
                case "3":
                    //下架电影
                    deleteMovie();
                    break;
                case "4":
                    //修改电影
                    updateMovie();
                    break;
                case "5":
                    //退出系统
                    System.out.println(loginUser.getName() + "欢迎下次再来");
                    return;
                default:
                    System.out.println("您输入选择有误,请重新输入!");
            }
        }
    }

    /**
     * 修改电影信息
     */
    private static void updateMovie() {
        System.out.println("=============修改电影==============");
        Business b = (Business) loginUser;
        List movies = ALL_MOVIES.get(b);
        //若集合为0则没有电影
        if (movies.size() == 0){
            System.out.println("没有电影可以修改!");
            return;
        }

        System.out.println("请输入要修改的电影名称:");
        String name = SYS_SC.nextLine();
        //查找有没有这部电影
        Movie m = getMovieByNamxe(name);
        if (m != null){
            //有这部电影
            System.out.println("请修改电影信息:");
            //Movie(String name, String actor, double time, double price, int number, Date startTime)
            System.out.println("请输入片名:");
            String nname = 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) {
                //Movie(String name, String actor, double time, double price, int number, Data startTime)
                try {
                    System.out.println("请输入放映时间:");
                    String startTime = SYS_SC.nextLine();
                    m.setName(nname);
                    m.setActor(actor);
                    m.setTime(Double.valueOf(time));
                    m.setPrice(Double.valueOf(price));
                    m.setNumber(Integer.valueOf(number));
                    m.setStartTime(sdf.parse(startTime));
                    System.out.println("修改成功!");
                    return;
                } catch (ParseException e) {
                    e.printStackTrace();
                    System.out.println("您输入的日期有误!");
                }
            }
        }else {
            System.out.println("没有这部电影。。请重新输入!");
        }
    }

    /**
     * 下架电影
     */
    private static void deleteMovie() {
        while (true) {
            System.out.println("=============下架电影==============");
            Business b = (Business) loginUser;
            List movies = ALL_MOVIES.get(b);
            //若集合为0则没有电影
            if (movies.size() == 0){
                System.out.println("没有电影可以下架!");
                return;
            }

            System.out.println("请输入要下架的电影名称:");
            String name = SYS_SC.nextLine();
            //查找有没有这部电影
            Movie m = getMovieByNamxe(name);
            if (m != null){
                //有这部电影
                System.out.println("确认下架吗?(y/n)");
                String commond = SYS_SC.nextLine();
                if (commond.equals("y")){
                    movies.remove(m);
                    System.out.println(m.getName() + "下架成功");
                    return;
                }else {
                    System.out.println("给您保留此电影");
                    return;
                }
            }else {
                System.out.println("没有这部电影。。请重新输入!");
            }
        }

    }

    /**
     * 根据电影名称查询是否有这部电影:当前登录用户
     * @param name
     */
    private static Movie getMovieByNamxe(String name) {
        Business b = (Business) loginUser;
        List movies = ALL_MOVIES.get(b);
        for (Movie movie : movies) {
            if (movie.getName().equals(name))
                return movie;
        }
        return null;
    }

    /**
     * 上架电影
     */
    private static void addMovie() {
        System.out.println("=============上架电影==============");
        Business b = (Business) loginUser;
        List movies = ALL_MOVIES.get(b);
        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) {
            //Movie(String name, String actor, double time, double price, int number, Data startTime)
            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 (ParseException e) {
                e.printStackTrace();
                System.out.println("您输入的日期有误!");
            }
        }
    }

    /**
     * 展示商家信息:当前用户信息
     */
    private static void showBusinessInfo() {
        LOGGER.info(loginUser.getName() + "正在查看详情");
        //需要将loginUser转为Business才能得到Business的全部信息
        Business b = (Business) loginUser;
        System.out.println(b.getShopName()+"\t\t电话:"+b.getPhone()+"\t\t地址:"+b.getAddress());
        //得到该商家的所有电影
        List movies = ALL_MOVIES.get(b);
        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\t"+movie.getTime()+
                        "\t\t\t"+movie.getScore()+"\t\t\t"+movie.getPrice()+"\t\t\t"+movie.getNumber()
                        +"\t\t\t"+sdf.format(movie.getStartTime()));
            }
        }else{
            System.out.println("您的店铺没有片可播!去买点片吧。");
        }

    }

    /**
     * 客户登录界面
     */
    private static void showCustomerMain() {
        while (true) {
            System.out.println("=========三国电影客户界面=========");
            System.out.println(loginUser.getName()+(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 commond = SYS_SC.nextLine();
            switch (commond){
                case "1":
                    //展示全部影片信息
                    showAllMovies();
                    break;
                case "2":
                    //根据电影名称查询电影信息
                    showMovieByName();
                    break;
                case "3":
                    //评分功能
                    scoreMovie();
                    break;
                case "4":
                    //购票功能
                    byMovie();
                    break;
                case "5":
                    //退出系统
                    return;
                default:
                    System.out.println("您输入选择有误,请重新输入!");
            }
        }
    }


    /**
     * 根据电影名称查询电影信息
     */
    private static void showMovieByName() {
        while (true) {
            System.out.println("==============查询电影===============");
            System.out.println("请输入店铺名:");
            String shopName = SYS_SC.nextLine();
            //判断店铺是否存在
            Business b = getBusinessByShopName(shopName);
            if (b != null){
                while (true){
                    //存在
                    System.out.println("请输入想查找的电影:");
                    String movieName = SYS_SC.nextLine();
                    //查看是否存在这部电影
                    Movie movie = getMovieByShopName(b, movieName);
                    if (movie != null){
                        System.out.println("片名\t\t\t主演\t\t时长\t\t评分\t\t票价\t\t余票数量\t\t放映时间");
                        System.out.println(movie.getName()+"\t\t\t"+movie.getActor()+"\t\t\t"+movie.getTime()+
                                "\t\t\t"+movie.getScore()+"\t\t\t"+movie.getPrice()+"\t\t\t"+movie.getNumber()
                                +"\t\t\t"+sdf.format(movie.getStartTime()));
                        System.out.println("是否继续查找电影信息?(y/n)");
                        String commond = SYS_SC.nextLine();
                        switch (commond){
                            case "y":
                                System.out.println("请继续查找!");
                                break;
                            default:
                                System.out.println("为您退出查找功能!");
                                return;
                        }
                    }else{
                        //不存在
                        System.out.println("您输入的电影不存在,请重新输入电影名称");
                        System.out.println("还要继续查找吗?(y/n)");
                        String commond = SYS_SC.nextLine();
                        switch (commond){
                            case "y":
                                System.out.println("请继续查找!");
                                break;
                            default:
                                System.out.println("为您退出查找功能!");
                                return;
                        }
                    }
                }

            }else{
                //不存在
                System.out.println("您输入的店铺不存在,请重新输入!");
                System.out.println("还要继续评分吗?(y/n)");
                String commond = SYS_SC.nextLine();
                switch (commond){
                    case "y":
                        System.out.println("请继续查找!");
                        break;
                    default:
                        System.out.println("为您退出查找功能!");
                        return;
                }
            }
        }
    }

    /**
     * 评分
     */
    private static void scoreMovie() {
        showAllMovies();
        while (true) {
            System.out.println("==============用户评分===============");
            System.out.println("请输入店铺名:");
            String shopName = SYS_SC.nextLine();
            //判断店铺是否存在
            Business b = getBusinessByShopName(shopName);
            if (b != null){
                while (true){
                    //存在
                    System.out.println("请输入想评分的电影:");
                    String movieName = SYS_SC.nextLine();
                    //查看是否存在这部电影
                    Movie movie = getMovieByShopName(b, movieName);
                    if (movie != null){
                        while (true) {
                            //存在
                            System.out.println("请评分:(满分10分)");
                            double score = Double.valueOf(SYS_SC.nextLine());
                            if (score <= 0 || score > 10){
                                System.out.println("您输入的分数有误,需要在0-10之间,请重新输入!");
                            }else{
                                movie.setScore(score);
                                System.out.println("评分成功,您给的分数为:"+score);
                                return;
                            }
                        }

                    }else{
                        //不存在
                        System.out.println("您输入的电影不存在,请重新输入电影名称");
                        System.out.println("还要继续评分吗?(y/n)");
                        String commond = SYS_SC.nextLine();
                        switch (commond){
                            case "y":
                                System.out.println("请继续评分!");
                                break;
                            default:
                                System.out.println("为您退出评分功能!");
                                return;
                        }
                    }
                }

            }else{
                //不存在
                System.out.println("您输入的店铺不存在,请重新输入!");
                System.out.println("还要继续评分吗?(y/n)");
                String commond = SYS_SC.nextLine();
                switch (commond){
                    case "y":
                        System.out.println("请继续评分!");
                        break;
                    default:
                        System.out.println("为您退出评分功能!");
                        return;
                }
            }
        }
    }

    /**
     * 购票
     */
    private static void byMovie() {
        showAllMovies();
        while (true) {
            System.out.println("==============用户购票===============");
            System.out.println("请输入店铺名称:");
            String shopName = SYS_SC.nextLine();
            //1.查找该店铺是否存在
            Business b = getBusinessByShopName(shopName);
            if (b != null){
                //店铺存在
                //输入电影名称,查找电影
                List movies = ALL_MOVIES.get(b);
                if (movies.size() > 0){
                    while (true){
                        //开始购票
                        System.out.println("请输入想看的电影:");
                        String movieName = SYS_SC.nextLine();
                        //查看是否存在这部电影
                        Movie movie = getMovieByShopName(b, movieName);
                        if (movie != null){
                            while (true) {
                                //有该电影
                                System.out.println("请输入票数:");
                                String number = SYS_SC.nextLine();
                                //票数不足
                                if (movie.getNumber() > Integer.valueOf(number)){
                                    //余票够
                                    //总共需要价格
                                    double sumPrice = BigDecimal.valueOf(movie.getPrice()).
                                            multiply(BigDecimal.valueOf(Integer.valueOf(number))).doubleValue();
                                    //判断用户余额是否够用
                                    if (loginUser.getMoney() >= sumPrice){
                                        //余额够用
                                        System.out.println("您已成功购买《"+movieName+"》"+Integer.valueOf(number)+"张"+"总金额为:"+sumPrice);
                                        //更新用户余额,店铺票数,店铺余额
                                        loginUser.setMoney(loginUser.getMoney()-sumPrice);
                                        movie.setNumber(movie.getNumber() - Integer.valueOf(number));
                                        b.setMoney(b.getMoney()+sumPrice);
                                        return;
                                    }else {
                                        //余额不足
                                        System.out.println("您的余额不足,是否继续购买?(y/n)您的余额为" + loginUser.getMoney());
                                        String commond = SYS_SC.nextLine();
                                        switch (commond){
                                            case "y":
                                                System.out.println("请继续购票!");
                                                break;
                                            default:
                                                System.out.println("为您退出购票功能!");
                                                return;
                                        }
                                    }
                                }else {
                                    //余票不够
                                    System.out.println("余票不足,是否继续购买?(y/n)余票为" + movie.getNumber());
                                    String commond = SYS_SC.nextLine();
                                    switch (commond){
                                        case "y":
                                            System.out.println("请继续购票!");
                                            break;
                                        default:
                                            System.out.println("为您退出购票功能!");
                                            return;
                                    }
                                }
                            }
                        }else {
                            //没有该电影
                            System.out.println("不存在该电影");
                            System.out.println("还要继续购买吗?(y/n)");
                            String commond = SYS_SC.nextLine();
                            switch (commond){
                                case "y":
                                    System.out.println("请继续购票!");
                                    break;
                                default:
                                    System.out.println("为您退出购票功能!");
                                    return;
                            }
                        }
                    }

                }else {
                    //没片
                    System.out.println("此店铺没有片可播,请重新选择!");
                    System.out.println("还要继续购买吗?(y/n)");
                    String commond = SYS_SC.nextLine();
                    switch (commond){
                        case "y":
                            System.out.println("请继续购票!");
                            break;
                        default:
                            System.out.println("为您退出购票功能!");
                            return;
                    }
                }
            }else{
                System.out.println("该店铺不存在,请重新输入!");
            }
        }

    }

    /**
     * 根据电影名称和店铺查询此店铺是否有该电影
     * @param b
     * @param movieName
     * @return
     */
    private static Movie getMovieByShopName(Business b, String movieName) {
        List movies = ALL_MOVIES.get(b);
        for (Movie movie : movies) {
            if (movie.getName().equals(movieName)){
                return movie;
            }
        }
        return null;
    }


    /**
     * 根据店铺名称查找该店铺是否存在
     * @param shopName
     * @return
     */
    private static Business getBusinessByShopName(String shopName) {
        Set businesses = ALL_MOVIES.keySet();//keySet方法返回所有的key集合
        for (Business business : businesses) {
            if (business.getShopName().equals(shopName))
                return business;
        }
        return null;
    }

    /**
     * 展示全部商家和电影信息
     */
    private static void showAllMovies() {
        System.out.println("===============查询所有商家的所有电影信息=================");
        ALL_MOVIES.forEach((b, movies) -> {
            System.out.println(b.getShopName()+"\t\t电话:"+b.getPhone()+"\t\t地址:"+b.getAddress());
            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\t"+movie.getTime()+
                        "\t\t\t"+movie.getScore()+"\t\t\t"+movie.getPrice()+"\t\t\t"+movie.getNumber()
                        +"\t\t\t"+sdf.format(movie.getStartTime()));
            }
            System.out.println("------------------------------------------------------------");
        });
    }

    /**
     * 根据用户名查询用户
     * @param name 用户名
     * @return
     */
    private static User getUserByLoginName(String name) {
        for (User user : ALL_USER) {
            if (user.getLoginName().equals(name))
                return user;
        }
        return null;
    }
}

总结:还有很多功能没有完成,算是个半成品。。

你可能感兴趣的:(Java,学习)