JavaSE實(shí)現(xiàn)電影院系統(tǒng)
本文實(shí)例為大家分享了JavaSE實(shí)現(xiàn)電影院系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
利用Java的面向?qū)ο筮M(jìn)階知識,結(jié)合日志,創(chuàng)建一個(gè)簡單電影院系統(tǒng)
一.定義一個(gè)User作為父類,減少代碼冗余
public class User { ? ? //用戶類,充當(dāng)父類 ? ? private String loginName; ? ? ? //假名(昵稱),不重復(fù) ? ? private String username; ? ? ? ?//真名 ? ? private String passWord; ? ? private String sex; ? ? private String phone; ? ? private double money; ? ? ? ? public User() { ? ? } ? ? ? public User(String loginName, String username, String passWord, String sex, String phone) { ? ? ? ? this.loginName = loginName; ? ? ? ? this.username = username; ? ? ? ? this.passWord = passWord; ? ? ? ? this.sex = sex; ? ? ? ? this.phone = phone; ? ? } ? ? ? public String getLoginName() { ? ? ? ? return loginName; ? ? } ? ? ? public void setLoginName(String loginName) { ? ? ? ? this.loginName = loginName; ? ? } ? ? ? public String getUsername() { ? ? ? ? return username; ? ? } ? ? ? public void setUsername(String username) { ? ? ? ? this.username = username; ? ? } ? ? ? public String getPassWord() { ? ? ? ? return passWord; ? ? } ? ? ? public void setPassWord(String passWord) { ? ? ? ? this.passWord = passWord; ? ? } ? ? ? public String getSex() { ? ? ? ? return sex; ? ? } ? ? ? public void setSex(String 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; ? ? } }
二.定義顧客類,以及商家類
public class Business extends User { ? ? //商家角色,獨(dú)有屬性 ? ? private String shopName; ? ? ? ?//店鋪名稱 ? ? ? private String 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; ? ? } } ? ? public class Customer extends User{ ? ? //客戶角色 }
三.定義一個(gè)電影類
import java.util.*; ? public class Movie { ? ? private String name; ? ? private String actor; ? ? private double score; ? ? private double time; ? ? private double price; ? ? private int number; ? ? ? ? //余票 ? ? private Date startTime; ? ? //放映時(shí)間 ? ? ? public Movie() { ? ? } ? ? ? public Movie(String name, String actor, double time, double price, int number, Date startTime) { ? ? ? ? this.name = name; ? ? ? ? this.actor = actor; ? ? ? ? this.score = score; ? ? ? ? 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; ? ? } }
四.添加logback.xml配置文件
注意:輸出日志的格式為ERROR即可,如果設(shè)置ALL,不要在控制臺打印
<?xml version="1.0" encoding="UTF-8"?> <configuration> ? ? <!--定義日志文件的存儲地址 勿在 LogBack 的配置中使用相對路徑--> ? ? <property name="LOG_HOME" value="E:/log" /> ? ? <!-- 控制臺輸出 --> ? ? <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> ? ? ? ? <!-- 日志輸出編碼 --> ? ? ? ? <Encoding>UTF-8</Encoding> ? ? ? ? <layout class="ch.qos.logback.classic.PatternLayout"> ? ? ? ? ? ? <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個(gè)字符寬度%msg:日志消息,%n是換行符--> ? ? ? ? ? ? <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n ? ? ? ? ? ? </pattern> ? ? ? ? </layout> ? ? </appender> ? ? <!-- 按照每天生成日志文件 --> ? ? <appender name="FILE" ?class="ch.qos.logback.core.rolling.RollingFileAppender"> ? ? ? ? <Encoding>UTF-8</Encoding> ? ? ? ? <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> ? ? ? ? ? ? <!--日志文件輸出的文件名--> ? ? ? ? ? ? <FileNamePattern>${LOG_HOME}/myApp.log.%d{yyyy-MM-dd}.log</FileNamePattern> ? ? ? ? ? ? <MaxHistory>30</MaxHistory> ? ? ? ? </rollingPolicy> ? ? ? ? <layout class="ch.qos.logback.classic.PatternLayout"> ? ? ? ? ? ? <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個(gè)字符寬度%msg:日志消息,%n是換行符--> ? ? ? ? ? ? <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n ? ? ? ? ? ? </pattern> ? ? ? ? </layout> ? ? ? ? <!--日志文件最大的大小--> ? ? ? ? <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> ? ? ? ? ? ? <MaxFileSize>1MB</MaxFileSize> ? ? ? ? </triggeringPolicy> ? ? </appender> ? ? ? <!-- 日志輸出級別 --> ? ? <root level="ERROR"> ? ? ? ? <appender-ref ref="CONSOLE" /> ? ? ? ? <appender-ref ref="FILE" /> ? ? </root> ? </configuration>
五.測試類
import com.lll.bean.Business; import com.lll.bean.Customer; import com.lll.bean.Movie; import com.lll.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 { ? ? //定義系統(tǒng)的數(shù)據(jù)容器,用于存儲數(shù)據(jù) // ? ?1。存儲很多用戶(客戶,商家) ? ? ? public static final List<User> ALL_USERS = new ArrayList<>(); ? ? ? public static Map<List<Movie>,Double>MOVIE_SCORE=new HashMap<>(); ? ? ? ? /* ? ? ?* 存儲商家拍片信息 ? ? ?* 商家1=【p1,p2,p3】 ? ? ?* 商家2=【p1,p2,p3】 ? ? ?* */ ? ? public static final Map<Business, List<Movie>> ALL_MOVIES = new HashMap<>(); ? ? ? public static final Scanner 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("MovieSystem.class"); ? ? ? /* ? ? ?* 準(zhǔn)備一些測試數(shù)據(jù) ? ? ?* ? ? ?* */ ? ? static { ? ? ? ? Customer c = new Customer(); ? ? ? ? c.setLoginName("lzb123"); ? ? ? ? c.setPassWord("123456"); ? ? ? ? c.setUsername("劉六"); ? ? ? ? c.setSex("男"); ? ? ? ? c.setMoney(1000); ? ? ? ? c.setPhone("110011"); ? ? ? ? ALL_USERS.add(c); ? ? ? ? ? Customer c1 = new Customer(); ? ? ? ? c1.setLoginName("w123"); ? ? ? ? c1.setPassWord("123456"); ? ? ? ? c1.setUsername("老王"); ? ? ? ? c1.setSex("男"); ? ? ? ? c1.setMoney(0); ? ? ? ? c1.setPhone("110111"); ? ? ? ? ALL_USERS.add(c1); ? ? ? ? ? Business b = new Business(); ? ? ? ? b.setLoginName("馬化騰66"); ? ? ? ? b.setPassWord("123456"); ? ? ? ? b.setUsername("馬化騰"); ? ? ? ? b.setMoney(0); ? ? ? ? b.setSex("男"); ? ? ? ? b.setPhone("8888"); ? ? ? ? b.setShopName("騰訊影視"); ? ? ? ? b.setAddress("北京市海淀區(qū)中關(guān)村1號"); ? ? ? ? ALL_USERS.add(b); ? ? ? ? List<Movie> movies = new ArrayList<>(); ? ? ? ? ALL_MOVIES.put(b, movies); ? ? ? ? ? Business b1 = new Business(); ? ? ? ? b1.setLoginName("馬云66"); ? ? ? ? b1.setPassWord("123456"); ? ? ? ? b1.setUsername("馬云"); ? ? ? ? b1.setMoney(10000); ? ? ? ? b1.setSex("男"); ? ? ? ? b1.setPhone("6666"); ? ? ? ? b1.setShopName("阿里影視"); ? ? ? ? b1.setAddress("北京市海淀區(qū)中關(guān)村2號"); ? ? ? ? ALL_USERS.add(b1); ? ? ? ? List<Movie> movies1 = new ArrayList<>(); ? ? ? ? ALL_MOVIES.put(b1, movies1); ? ? ? } ? ? ? ? public static void main(String[] args) { ? ? ? ? shouMain(); ? ? ? } ? ? ? public static void NewCustomer() ? ? { ? ? ? ? System.out.println("==========用戶注冊=========="); ? ? ? ? System.out.println("輸入用戶名:"); ? ? ? ? String loginName=sc.nextLine(); ? ? ? ? System.out.println("輸入姓名:"); ? ? ? ? String username=sc.nextLine(); ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("輸入密碼:"); ? ? ? ? ? ? String passWord=sc.nextLine(); ? ? ? ? ? ? System.out.println("請?jiān)俅屋斎朊艽a:"); ? ? ? ? ? ? String passWord2=sc.nextLine(); ? ? ? ? ? ? if(passWord.equals(passWord2)==false) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? System.out.println("兩次密碼輸入不一致!"); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? System.out.println("輸入性別:"); ? ? ? ? ? ? ? ? String sex=sc.nextLine(); ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? System.out.println("輸入手機(jī)號碼:"); ? ? ? ? ? ? ? ? ? ? String phone=sc.nextLine(); ? ? ? ? ? ? ? ? ? ? if(phone.matches("^1[3|4|5|7|8][0-9]\\d{4,8}$")) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("注冊成功!"); ? ? ? ? ? ? ? ? ? ? ? ? User user=new User(loginName,username,passWord,sex,phone); ? ? ? ? ? ? ? ? ? ? ? ? ALL_USERS.add(user); ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("手機(jī)號輸入不合法!"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? public static void NewBusiness() ? ? { ? ? ? ? System.out.println("==========商家入駐=========="); ? ? ? ? System.out.println("輸入用戶名:"); ? ? ? ? String loginName=sc.nextLine(); ? ? ? ? System.out.println("輸入姓名:"); ? ? ? ? String username=sc.nextLine(); ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("輸入密碼:"); ? ? ? ? ? ? String passWord=sc.nextLine(); ? ? ? ? ? ? System.out.println("請?jiān)俅屋斎朊艽a:"); ? ? ? ? ? ? String passWord2=sc.nextLine(); ? ? ? ? ? ? if(passWord.equals(passWord2)==false) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? System.out.println("兩次密碼輸入不一致!"); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? System.out.println("輸入性別:"); ? ? ? ? ? ? ? ? String sex=sc.nextLine(); ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? System.out.println("輸入手機(jī)號碼:"); ? ? ? ? ? ? ? ? ? ? String phone=sc.nextLine(); ? ? ? ? ? ? ? ? ? ? if(phone.matches("^1[3|4|5|7|8][0-9]\\d{4,8}$")) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("請輸入店鋪名稱:"); ? ? ? ? ? ? ? ? ? ? ? ? String shopName=sc.nextLine(); ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("請輸入店鋪地址:"); ? ? ? ? ? ? ? ? ? ? ? ? String address=sc.nextLine(); ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("注冊成功!"); ? ? ? ? ? ? ? ? ? ? ? ? Business b=new Business(); ? ? ? ? ? ? ? ? ? ? ? ? b.setLoginName(loginName); ? ? ? ? ? ? ? ? ? ? ? ? b.setPassWord(passWord); ? ? ? ? ? ? ? ? ? ? ? ? b.setUsername(username); ? ? ? ? ? ? ? ? ? ? ? ? b.setSex(sex); ? ? ? ? ? ? ? ? ? ? ? ? b.setPhone(phone); ? ? ? ? ? ? ? ? ? ? ? ? b.setShopName(shopName); ? ? ? ? ? ? ? ? ? ? ? ? b.setAddress(address); ? ? ? ? ? ? ? ? ? ? ? ? ALL_USERS.add(b); ? ? ? ? ? ? ? ? ? ? ? ? List<Movie> movies = new ArrayList<>(); ? ? ? ? ? ? ? ? ? ? ? ? ALL_MOVIES.put(b, movies); ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("手機(jī)號輸入不合法!"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? ? private static void shouMain() { ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("==========ZB電影首頁=========="); ? ? ? ? ? ? System.out.println("1.登錄"); ? ? ? ? ? ? System.out.println("2.用戶注冊"); ? ? ? ? ? ? System.out.println("3.商家入駐"); ? ? ? ? ? ? System.out.println("請輸入操作命令"); ? ? ? ? ? ? String command = sc.nextLine(); ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? case "1": ? ? ? ? ? ? ? ? ? ? //登錄 ? ? ? ? ? ? ? ? ? ? longin(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "2": ? ? ? ? ? ? ? ? ? ? //注冊 ? ? ? ? ? ? ? ? ? ? NewCustomer(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "3": ? ? ? ? ? ? ? ? ? ? NewBusiness(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? System.out.println("命令有誤!請重新輸入!"); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? private static void longin() { ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("請輸入登錄名稱:"); ? ? ? ? ? ? String loginName = sc.nextLine(); ? ? ? ? ? ? System.out.println("請輸入登錄密碼:"); ? ? ? ? ? ? String passWord = sc.nextLine(); ? ? ? ? ? ? //根據(jù)登錄名查詢用戶對象 ? ? ? ? ? ? User u = getUserByLoginName(loginName); ? ? ? ? ? ? //判斷用戶對象是否存在 ? ? ? ? ? ? if (u != null) { ? ? ? ? ? ? ? ? if (u.getPassWord().equals(passWord)) { ? ? ? ? ? ? ? ? ? ? System.out.println("登錄成功!"); ? ? ? ? ? ? ? ? ? ? loginUser = u; ? ? ? ? ? ? ? ? ? ? LOGGER.info(u.getUsername() + "登錄系統(tǒng)"); ? ? ? ? ? ? ? ? ? ? //判斷登錄為用戶是商家還是顧客 ? ? ? ? ? ? ? ? ? ? if (u instanceof Customer) ? ? // ?對象運(yùn)算符(instanceof)用來判斷一個(gè)對象是否屬于某個(gè)指定的類或其子類的實(shí)例,如果是,返回真(true),否則返回假(false)。 ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //登錄為顧客 ? ? ? ? ? ? ? ? ? ? ? ? showCustomerMain(); ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? //登錄為商家 ? ? ? ? ? ? ? ? ? ? ? ? showBusinessMain(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? System.out.println("密碼錯(cuò)誤!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.println("登錄名稱錯(cuò)誤,請重新輸入!"); ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? } ? ? ? ? //商家操作 ? ? ? private static void showBusinessMain() { ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("==========商家界面=========="); ? ? ? ? ? ? System.out.println(loginUser.getUsername() + (loginUser.getSex() .equals("男") ? "先生" : "女士") + "您好," + "請選擇商家操作的功能:"); ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? 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 = 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("退出系統(tǒng)成功!"); ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("您輸入的命令有誤!請重新輸入"); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? private static void updateMovie() { ? ? ? ? Business business = (Business) loginUser; ? ? ? ? List<Movie> movies = ALL_MOVIES.get(business); ? ? ? ? if (movies.size() == 0) { ? ? ? ? ? ? System.out.println("您的系統(tǒng)內(nèi)無影視資源!無法修改"); ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("請您輸入需要修改的電影名稱:"); ? ? ? ? ? ? String movieName = sc.nextLine(); ? ? ? ? ? ? Movie movie = getMovieByName(movieName); ? ? ? ? ? ? if (movie != null) { ? ? ? ? ? ? ? ? System.out.println("請輸入修改后的片名:"); ? ? ? ? ? ? ? ? String name = sc.nextLine(); ? ? ? ? ? ? ? ? System.out.println("請輸入修改后的主演:"); ? ? ? ? ? ? ? ? String actor = sc.nextLine(); ? ? ? ? ? ? ? ? System.out.println("請輸入修改后的時(shí)長:"); ? ? ? ? ? ? ? ? String time = sc.nextLine(); ? ? ? ? ? ? ? ? System.out.println("請輸入修改后的票價(jià):"); ? ? ? ? ? ? ? ? String price = sc.nextLine(); ? ? ? ? ? ? ? ? System.out.println("請輸入修改后的票數(shù):"); ? ? ? ? ? ? ? ? String totalNumber = sc.nextLine(); ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("請輸入影片修改后的放映時(shí)間:"); ? ? ? ? ? ? ? ? ? ? ? ? String startTime = sc.nextLine(); ? ? ? ? ? ? ? ? ? ? ? ? movie.setName(name); ? ? ? ? ? ? ? ? ? ? ? ? movie.setActor(actor); ? ? ? ? ? ? ? ? ? ? ? ? movie.setTime(Double.valueOf(time)); ? ? ? ? ? ? ? ? ? ? ? ? movie.setPrice(Double.valueOf(price)); ? ? ? ? ? ? ? ? ? ? ? ? movie.setNumber(Integer.valueOf(totalNumber)); ? ? ? ? ? ? ? ? ? ? ? ? movie.setStartTime(sdf.parse(startTime)); ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("您已經(jīng)成功修改!"); ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("==========影片詳情=========="); ? ? ? ? ? ? ? ? ? ? ? ? showBusinessInfos(); ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } catch (ParseException e) { ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? ? ? LOGGER.error("時(shí)間解析錯(cuò)誤!"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.println("不存在該影片!"); ? ? ? ? ? ? ? ? System.out.println("繼續(xù)修改按1,退出按其他任意鍵"); ? ? ? ? ? ? ? ? String command = sc.nextLine(); ? ? ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? ? ? case "1": ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? ? //影片下架功能 ? ? private static void deleteMovie() { ? ? ? ? System.out.println("==========下架電影==========="); ? ? ? ? Business business = (Business) loginUser; ? ? ? ? List<Movie> movies = ALL_MOVIES.get(business); ? ? ? ? if (movies.size() == 0) { ? ? ? ? ? ? System.out.println("您的系統(tǒng)內(nèi)無影視資源!無法下架"); ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("請您輸入需要下架的電影名稱:"); ? ? ? ? ? ? String movieName = sc.nextLine(); ? ? ? ? ? ? Movie movie = getMovieByName(movieName); ? ? ? ? ? ? if (movie != null) { ? ? ? ? ? ? ? ? movies.remove(movie); ? ? ? ? ? ? ? ? System.out.println(movieName + "下架成功!"); ? ? ? ? ? ? ? ? System.out.println("==========影片詳情=========="); ? ? ? ? ? ? ? ? showBusinessInfos(); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.println("不存在該影片!"); ? ? ? ? ? ? ? ? System.out.println("繼續(xù)下架按1,退出按其他任意鍵"); ? ? ? ? ? ? ? ? String command = sc.nextLine(); ? ? ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? ? ? case "1": ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? //根據(jù)影片名查找影片 ? ? public static Movie getMovieByName(String movieName) { ? ? ? ? Business business = (Business) loginUser; ? ? ? ? List<Movie> movies = ALL_MOVIES.get(business); ? ? ? ? for (Movie movie : movies) { ? ? ? ? ? ? if (movie.getName().contains(movieName)) ? ? ? ? ? ? //equals只能判斷兩個(gè)變量的值是否相等。 contains常用與集合中判斷某個(gè)對象是否含有這個(gè)元素 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return movie; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? ? } ? ? ? ? //展示當(dāng)前商家詳細(xì)信息 ? ? private static void showBusinessInfos() { ? ? ? ? System.out.println("==========商家詳情=========="); ? ? ? ? LOGGER.info(loginUser.getUsername() + "查看詳情"); ? ? ? ? //根據(jù)商家對象提取相應(yīng)的值 ? Map<Business List<Movie>> ? ? ? ? Business business = (Business) loginUser; ? ? ? ? System.out.println(business.getShopName() + "\t\t電話:" + business.getPhone() + "\t\t地址:" + business.getAddress()); ? ? ? ? List<Movie> movies = ALL_MOVIES.get(business); ? ? ? ? if (movies.size() > 0) { ? ? ? ? ? ? System.out.println("片名" + "\t\t" + "主演" + "\t\t" + "時(shí)長" + "\t\t" + "評分" + "\t\t" + "票價(jià)" + "\t\t" + "余票" + "\t\t" + "放映時(shí)間"); ? ? ? ? ? ? for (Movie movie : movies) { ? ? ? ? ? ? ? ? System.out.println(movie.getName() + "\t\t" + movie.getActor() + "\t\t" + movie.getTime() + "\t\t" + movie.getScore() + "\t\t" + movie.getPrice() + "\t\t" + movie.getNumber() + "\t\t" + sdf.format(movie.getStartTime())); ? ? ? ? ? ? } ? ? ? ? } else { ? ? ? ? ? ? System.out.println("您的店鋪當(dāng)前沒有影視資源!"); ? ? ? ? } ? ? ? } ? ? ? //上架電影 ? ? ? private static void addMovie() { ? ? ? ? System.out.println("==========上架電影=========="); ? ? ? ? Business business = (Business) loginUser; ? ? ? ? List<Movie> movies = ALL_MOVIES.get(business); ?//鍵值對 ? ? ? ? System.out.println("請輸入新片名:"); ? ? ? ? String name = sc.nextLine(); ? ? ? ? System.out.println("請輸入主演:"); ? ? ? ? String actor = sc.nextLine(); ? ? ? ? System.out.println("請輸入時(shí)長:"); ? ? ? ? String time = sc.nextLine(); ? ? ? ? System.out.println("請輸入票價(jià):"); ? ? ? ? String price = sc.nextLine(); ? ? ? ? System.out.println("請輸入票數(shù):"); ? ? ? ? String totalNumber = sc.nextLine(); ? ? ? ? while (true) { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? System.out.println("請輸入影片放映時(shí)間:"); ? ? ? ? ? ? ? ? String startTime = sc.nextLine(); ? ? ? ? ? ? ? ? Movie movie = new Movie(name, actor, Double.valueOf(time), Double.valueOf(price), Integer.valueOf(totalNumber), sdf.parse(startTime)); ? ? ? ? ? ? ? ? movies.add(movie); ? ? ? ? ? ? ? ? System.out.println("您已經(jīng)成功上架《" + movie.getName() + "》"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } catch (ParseException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? LOGGER.error("時(shí)間解析錯(cuò)誤!"); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? ? //顧客操作 ? ? private static void showCustomerMain() { ? ? ? ? System.out.println("==========客戶界面=========="); ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println(loginUser.getUsername() + (loginUser.getSex().equals("男") ? "先生" : "女士") + "您好," + "請選擇客戶操作的功能:"); ? ? ? ? ? ? System.out.println("賬戶余額" + loginUser.getMoney()); ? ? ? ? ? ? System.out.println("1.展示全部影片信息功能:"); ? ? ? ? ? ? System.out.println("2.根據(jù)電影名稱查詢電影信息:"); ? ? ? ? ? ? System.out.println("3.購票系統(tǒng):"); ? ? ? ? ? ? System.out.println("4.退出系統(tǒng):"); ? ? ? ? ? ? String command = sc.nextLine(); ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? case "1": ? ? ? ? ? ? ? ? ? ? showAllMovies(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "2": ? ? ? ? ? ? ? ? ? ? NameMovie(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "3": ? ? ? ? ? ? ? ? ? ? buyMovie(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "4": ? ? ? ? ? ? ? ? ? ? System.out.println("退出系統(tǒng)成功!"); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? System.out.println("您輸入的命令有誤!請重新輸入"); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? ? } ? ? ? ? private static void NameMovie() { ? ? ? ? System.out.println("===========根據(jù)影片名查詢電影==========="); ? ? ? ? System.out.println("請輸入影片名:"); ? ? ? ? String Findname = sc.nextLine(); ? ? ? ? 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" + "時(shí)長" + "\t\t" + "評分" + "\t\t" + "票價(jià)" + "\t\t" + "余票" + "\t\t" + "放映時(shí)間"); ? ? ? ? ? ? for (Movie movie : movies) { ? ? ? ? ? ? ? ? if (movie.getName().equals(Findname)) ? ? ? ? ? ? ? ? ? ? System.out.println("\t\t\t" + movie.getName() + "\t\t" + movie.getActor() + "\t\t" + movie.getTime() + "\t\t" + movie.getScore() + "\t\t" + movie.getPrice() + "\t\t" + movie.getNumber() + "\t\t" + sdf.format(movie.getStartTime())); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? } ? ? ? private static void buyMovie() { ? ? ? ? showAllMovies(); ? ? ? ? System.out.println("==========用戶購票=========="); ? ? ? ? while (true) { ? ? ? ? ? ? System.out.println("請輸入需要購票的門店"); ? ? ? ? ? ? String shopName = sc.nextLine(); ? ? ? ? ? ? Business business = getUserByShopName(shopName); ? ? ? ? ? ? if (business != null) { ? ? ? ? ? ? ? ? List<Movie> movies = ALL_MOVIES.get(business); ? ? ? ? ? ? ? ? if (movies.size() > 0) { ? ? ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("請輸入需要購買的電影名稱"); ? ? ? ? ? ? ? ? ? ? ? ? String MovieName = sc.nextLine(); ? ? ? ? ? ? ? ? ? ? ? ? Movie movie = getMovieByShopName(business, MovieName); ? ? ? ? ? ? ? ? ? ? ? ? if (movie != null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? while (true) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("輸入購買電影票數(shù):"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String Number = sc.nextLine(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int buyNumber = Integer.valueOf(Number); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (movie.getNumber() >= buyNumber) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? double money = BigDecimal.valueOf(movie.getPrice()).multiply(BigDecimal.valueOf(buyNumber)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .doubleValue(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("當(dāng)前需要花費(fèi)的金額為" + money); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (loginUser.getMoney() >= money) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("購票成功!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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("賬戶余額不足!"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("繼續(xù)購買按1,退出按其他任意鍵"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String command = sc.nextLine(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "1": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("余票不足!還剩" + movie.getNumber() + "張票"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("繼續(xù)購買按1,退出按其他任意鍵"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String command = sc.nextLine(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? case "1": ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("該電影不存在!"); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? System.out.println("該店鋪無影視資源!"); ? ? ? ? ? ? ? ? ? ? System.out.println("繼續(xù)購買按1,退出按其他任意鍵"); ? ? ? ? ? ? ? ? ? ? String command = sc.nextLine(); ? ? ? ? ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? ? ? ? ? case "1": ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.println("不存在該店鋪!"); ? ? ? ? ? ? ? ? System.out.println("繼續(xù)購買按1,退出按其他任意鍵"); ? ? ? ? ? ? ? ? String command = sc.nextLine(); ? ? ? ? ? ? ? ? switch (command) { ? ? ? ? ? ? ? ? ? ? case "1": ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? } ? ? ? 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" + "時(shí)長" + "\t\t" + "評分" + "\t\t" + "票價(jià)" + "\t\t" + "余票" + "\t\t" + "放映時(shí)間"); ? ? ? ? ? ? for (Movie movie : movies) { ? ? ? ? ? ? ? ? System.out.println("\t\t\t" + movie.getName() + "\t\t" + movie.getActor() + "\t\t" + movie.getTime() + "\t\t" + movie.getScore() + "\t\t" + movie.getPrice() + "\t\t" + movie.getNumber() + "\t\t" + sdf.format(movie.getStartTime())); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? ? //根據(jù)商家店鋪名稱查詢商家對象 ? ? public static Business getUserByShopName(String shopName) { ? ? ? ? Set<Business> businesses = ALL_MOVIES.keySet(); ? ? ? ? for (Business business : businesses) { ? ? ? ? ? ? if (business.getShopName().equals(shopName)) { ? ? ? ? ? ? ? ? return business; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? ? } ? ? ? ? //查詢商家的電影 ? ? public static Movie getMovieByShopName(Business business, String name) { ? ? ? ? List<Movie> movies = ALL_MOVIES.get(business); ? ? ? ? for (Movie movie : movies) { ? ? ? ? ? ? if (movie.getName().contains(name)) { ? ? ? ? ? ? ? ? return movie; ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? ? } ? ? ? public static User getUserByLoginName(String loginName) { ? ? ? ? for (User user : ALL_USERS) { ? ? ? ? ? ? //判斷用戶登錄名 ? ? ? ? ? ? if (user.getLoginName().equals(loginName)) { ? ? ? ? ? ? ? ? return user; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return null; ? //查無此人 ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring在多線程環(huán)境下如何確保事務(wù)一致性問題詳解
這篇文章主要介紹了Spring在多線程環(huán)境下如何確保事務(wù)一致性問題詳解,說到異步執(zhí)行,很多小伙伴首先想到Spring中提供的@Async注解,但是Spring提供的異步執(zhí)行任務(wù)能力并不足以解決我們當(dāng)前的需求,需要的朋友可以參考下2023-11-11JavaWeb實(shí)現(xiàn)用戶登錄與注冊功能(服務(wù)器)
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)用戶登錄與注冊功能,服務(wù)器部分的關(guān)鍵代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java實(shí)現(xiàn)給微信群中定時(shí)推送消息
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)給微信群中定時(shí)推送消息的功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下2022-12-12聊聊@RequestBody和Json之間的關(guān)系
這篇文章主要介紹了@RequestBody和Json之間的關(guān)系,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot @PostConstruct原理用法解析
這篇文章主要介紹了SpringBoot @PostConstruct原理用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08如何在JDK 9中更簡潔使用 try-with-resources 語句
本文詳細(xì)介紹了自 JDK 7 引入的 try-with-resources 語句的原理和用法,以及介紹了 JDK 9 對 try-with-resources 的改進(jìn),使得用戶可以更加方便、簡潔的使用 try-with-resources 語句。,需要的朋友可以參考下2019-06-06