JavaSE實現電影院系統
本文實例為大傢分享瞭JavaSE實現電影院系統的具體代碼,供大傢參考,具體內容如下
利用Java的面向對象進階知識,結合日志,創建一個簡單電影院系統
一.定義一個User作為父類,減少代碼冗餘
public class User { //用戶類,充當父類 private String loginName; //假名(昵稱),不重復 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 { //商傢角色,獨有屬性 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{ //客戶角色 }
三.定義一個電影類
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; //放映時間 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即可,如果設置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個字符寬度%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個字符寬度%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 { //定義系統的數據容器,用於存儲數據 // 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"); /* * 準備一些測試數據 * * */ 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("北京市海淀區中關村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("北京市海淀區中關村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("請再次輸入密碼:"); 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("輸入手機號碼:"); 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("手機號輸入不合法!"); } } } } } 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("請再次輸入密碼:"); 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("輸入手機號碼:"); 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("手機號輸入不合法!"); } } } } } 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(); //根據登錄名查詢用戶對象 User u = getUserByLoginName(loginName); //判斷用戶對象是否存在 if (u != null) { if (u.getPassWord().equals(passWord)) { System.out.println("登錄成功!"); loginUser = u; LOGGER.info(u.getUsername() + "登錄系統"); //判斷登錄為用戶是商傢還是顧客 if (u instanceof Customer) // 對象運算符(instanceof)用來判斷一個對象是否屬於某個指定的類或其子類的實例,如果是,返回真(true),否則返回假(false)。 { //登錄為顧客 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.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("退出系統成功!"); 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("您的系統內無影視資源!無法修改"); 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("請輸入修改後的時長:"); String time = sc.nextLine(); System.out.println("請輸入修改後的票價:"); String price = sc.nextLine(); System.out.println("請輸入修改後的票數:"); String totalNumber = sc.nextLine(); while (true) { try { System.out.println("請輸入影片修改後的放映時間:"); 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("您已經成功修改!"); System.out.println("==========影片詳情=========="); showBusinessInfos(); return; } catch (ParseException e) { e.printStackTrace(); LOGGER.error("時間解析錯誤!"); } } } else { System.out.println("不存在該影片!"); System.out.println("繼續修改按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("您的系統內無影視資源!無法下架"); 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("繼續下架按1,退出按其他任意鍵"); String command = sc.nextLine(); switch (command) { case "1": break; default: return; } } } } //根據影片名查找影片 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隻能判斷兩個變量的值是否相等。 contains常用與集合中判斷某個對象是否含有這個元素 { return movie; } } return null; } //展示當前商傢詳細信息 private static void showBusinessInfos() { System.out.println("==========商傢詳情=========="); LOGGER.info(loginUser.getUsername() + "查看詳情"); //根據商傢對象提取相應的值 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" + "時長" + "\t\t" + "評分" + "\t\t" + "票價" + "\t\t" + "餘票" + "\t\t" + "放映時間"); 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("您的店鋪當前沒有影視資源!"); } } //上架電影 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("請輸入時長:"); String time = sc.nextLine(); System.out.println("請輸入票價:"); String price = sc.nextLine(); System.out.println("請輸入票數:"); String totalNumber = sc.nextLine(); while (true) { try { System.out.println("請輸入影片放映時間:"); 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("您已經成功上架《" + movie.getName() + "》"); return; } catch (ParseException e) { e.printStackTrace(); LOGGER.error("時間解析錯誤!"); } } } //顧客操作 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.根據電影名稱查詢電影信息:"); System.out.println("3.購票系統:"); System.out.println("4.退出系統:"); String command = sc.nextLine(); switch (command) { case "1": showAllMovies(); break; case "2": NameMovie(); break; case "3": buyMovie(); break; case "4": System.out.println("退出系統成功!"); return; default: System.out.println("您輸入的命令有誤!請重新輸入"); break; } } } private static void NameMovie() { System.out.println("===========根據影片名查詢電影==========="); 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" + "時長" + "\t\t" + "評分" + "\t\t" + "票價" + "\t\t" + "餘票" + "\t\t" + "放映時間"); 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("輸入購買電影票數:"); 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("當前需要花費的金額為" + 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("繼續購買按1,退出按其他任意鍵"); String command = sc.nextLine(); switch (command) { case "1": break; default: return; } } } else { System.out.println("餘票不足!還剩" + movie.getNumber() + "張票"); System.out.println("繼續購買按1,退出按其他任意鍵"); String command = sc.nextLine(); switch (command) { case "1": break; default: return; } } } } else { System.out.println("該電影不存在!"); } } } else { System.out.println("該店鋪無影視資源!"); System.out.println("繼續購買按1,退出按其他任意鍵"); String command = sc.nextLine(); switch (command) { case "1": break; default: return; } } return; } else { System.out.println("不存在該店鋪!"); System.out.println("繼續購買按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" + "時長" + "\t\t" + "評分" + "\t\t" + "票價" + "\t\t" + "餘票" + "\t\t" + "放映時間"); 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())); } }); } //根據商傢店鋪名稱查詢商傢對象 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; //查無此人 } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。