java 开发练习 实现买电影票

xiaoxiao2021-03-01  21

 

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <movies> <movie> <xh>1</xh> <title>羞羞的铁拳</title> <dy>李乐</dy> <actor>马腾</actor> <lx>喜剧</lx> <price>60</price> <time>13:36</time> </movie> <movie> <lx>2</lx> <title>lol</title> <dy>gelun</dy> <xh>kate</xh> <price>madongmdongdondod25</price> <time>13:00</time> </movie> <movie> <xh>3</xh> <title>lol</title> <dy>卡特</dy> <actor>动漫</actor> <lx /> <price>45</price> <time>13:45</time> </movie> </movies>

 

package com.entriy; import java.io.Serializable; public class film implements Serializable { private String filename;//电影名 private String date;//观看时间 private String sno;//座位号 public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public film(String filename, String date, String sno) { super(); this.filename = filename; this.date = date; this.sno = sno; } public film() { super(); } } package com.entry; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.entry.film; public class FilmText { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String con = ""; do { System.out.println("==欢迎来到 幻影影院 =="); System.out.println("1.员工入口\n2.管理员入口\n3.退出"); System.out.println("请选择:"); Scanner sca = new Scanner(System.in); int num = sca.nextInt(); switch (num) { case 1: log(); yg(); break; case 2: admin(); gly(); break; case 3: System.out.println("退出系统成功!"); System.exit(0); break; } System.out.println("输入0返回:"); con = sca.next(); } while (con.equals("0")); } // 员工入口 public static void yg() throws Exception { Scanner sca = new Scanner(System.in); System.out.println("1.查询电影\n2.购票"); int nums = sca.nextInt(); switch (nums) { case 1: cat(); break; case 2: gp(); break; } } // 查看 public static void cat() throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse("src/login.xml"); // 获取所有节点 NodeList roots = doc.getChildNodes(); Node root = roots.item(0);// 获取根节点 // System.out.println(root.getNodeName()); NodeList movies = root.getChildNodes(); System.out.println("\t\t\t欢迎来到动感影城"); System.out.println("序号\t电影名称\t导演\t演员\t电影类型\t价格\t时间"); for (int i = 0; i < movies.getLength(); i++) { Node movie = movies.item(i); NodeList items = movie.getChildNodes(); for (int j = 0; j < items.getLength(); j++) { Node item = items.item(j); if (item.getNodeName().equals("xh")) { System.out.print(item.getTextContent() + "\t"); } if (item.getNodeName().equals("title")) { System.out.print(item.getTextContent() + "\t"); } if (item.getNodeName().equals("dy")) { System.out.print(item.getTextContent() + "\t"); } if (item.getNodeName().equals("actor")) { System.out.print(item.getTextContent() + "\t"); } if (item.getNodeName().equals("lx")) { System.out.print(item.getTextContent() + "\t"); } if (item.getNodeName().equals("price")) { System.out.print(item.getTextContent() + "\t"); } if (item.getNodeName().equals("time")) { System.out.print(item.getTextContent() + "\t"); } } System.out.println(); } } // 购票 public static void gp() throws Exception { Scanner sca = new Scanner(System.in); init(); String con = ""; do { cat(); System.out.println("下面为影院的座位结构图:"); System.out.println("\t\t\t屏幕"); for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 7; j++) { System.out.print(i + "-" + j + "\t"); } System.out.println(); } sell(); System.out.println("是否继续购票 y\\n "); con = sca.next(); } while (con.equals("y")); } public static void init() throws Exception { File file = new File("D:\\a\\t.txt"); if (!file.exists()) { file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream op = new ObjectOutputStream(fos); List<film> list = new ArrayList<film>(); op.writeObject(list); op.close(); } } public static void sell() throws Exception { // 先判断 再购买 // 购买成功后还要把最新的信息序列化到文本 File file = new File("D:\\a\\t.txt"); Scanner sca = new Scanner(System.in); System.out.println("请输入要观看的电影"); String name = sca.next(); System.out.println("请输入要观看的电影时间"); String date = sca.next(); System.out.println("请输入1.普通票2.学生票3.赠送票"); String price = sca.next(); System.out.println("请输入座位号 (几排)-(几号)"); String sno = sca.next(); film film1 = new film(name, date, sno); FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); List<film> list = (List<film>) ois.readObject(); ois.close(); boolean flag = false; if (list.size() == 0) { // 文本没内容 flag = false; } else { for (int j = 0; j < list.size(); j++) { if (((list.get(j).getTitle()).equals(name) && (list.get(j).getTime()).equals(date) && (list.get(j) .getSeat()).equals(sno))) { flag = true; System.out.println("此票已卖出,请重新选择"); sell(); break; } } if (flag == false) { System.out.println("购票成功"); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream op = new ObjectOutputStream(fos); list.add(film1); op.writeObject(list); System.out.println("list.size=" + list.size()); op.close(); } } } // 管理员 public static void gly() throws Exception { String con = ""; do { Scanner sca = new Scanner(System.in); System.out.println("1.删除\n2.添加\n3.修改"); int num = sca.nextInt(); switch (num) { case 1: delete(); break; case 2: add(); break; case 3: update(); break; } System.out.println("是否继续进行操作y\n "); con = sca.next(); } while (con.equals("y")); } public static void delete() { try { // 1、得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2、从DOM工厂获得DOM解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 3、解析XML文档,得到一个Document,即DOM树 Document doc = db.parse("src/login.xml"); Scanner sca = new Scanner(System.in); System.out.println("输入要删除的节点序号"); String a = sca.next(); // 找到删除的节点 // 获取所有节点 NodeList roots = doc.getChildNodes(); Node root = roots.item(0);// 获取根节点 // System.out.println(root.getNodeName()); NodeList movies = root.getChildNodes(); for (int i = 0; i < movies.getLength(); i++) { Node movie = movies.item(i); NodeList items = movie.getChildNodes(); for (int j = 0; j < items.getLength(); j++) { Node item = items.item(j); if (item.getNodeName().equals("xh")) { NodeList list = doc.getElementsByTagName("movie"); for (int u = 0; u < list.getLength(); u++) { if (item.getTextContent().equals(a)) { Element studentElement = (Element) list.item(u); studentElement.getParentNode().removeChild( studentElement); } // 删除 } } } } // 保存XML文件 TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(doc); // 设置编码类型 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); StreamResult result = new StreamResult(new FileOutputStream( "src/login.xml")); // 把DOM树转换为XML文件 transformer.transform(domSource, result); System.out.println("删除成功"); } catch (Exception e) { e.printStackTrace(); } } // 添加 public static void add() { try { // 1、得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2、从DOM工厂获得DOM解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 3、解析XML文档,得到一个Document,即DOM树 Document doc = db.parse("src/login.xml"); // 创建movie节点 cat(); Scanner input = new Scanner(System.in); System.out.println("请输入序号:"); String a = input.next(); System.out.println("请输入名字:"); String b = input.next(); System.out.println("请输入电影的导演:"); String c = input.next(); System.out.println("请输入电影的演员:"); String d = input.next(); System.out.println("请输入电影的类型:"); String e = input.next(); System.out.println("请输入电影票的价格:"); String f = input.next(); System.out.println("请输入电影播放的时间:"); String g = input.next(); Element bookElement = doc.createElement("movie"); // bookElement.setAttribute("5"); // 创建title节点 Element titleElement = doc.createElement("xh"); titleElement.setTextContent(a); // 添加父子关系 bookElement.appendChild(titleElement); // 创建author节点 Element nameElement = doc.createElement("title"); nameElement.setTextContent(b); // 添加父子关系 bookElement.appendChild(nameElement); Element dyElement = doc.createElement("dy"); dyElement.setTextContent(c); // 添加父子关系 bookElement.appendChild(dyElement); Element actorElement = doc.createElement("actor"); dyElement.setTextContent(d); // 添加父子关系 bookElement.appendChild(actorElement); Element lxElement = doc.createElement("lx"); actorElement.setTextContent(e); // 添加父子关系 bookElement.appendChild(lxElement); // 创建price节点 Element priceElement = doc.createElement("price"); priceElement.setTextContent(f); // 添加父子关系 bookElement.appendChild(priceElement); Element timeElement = doc.createElement("time"); timeElement.setTextContent(g); // 添加父子关系 bookElement.appendChild(timeElement); // 找到根节点 Element booksElement = (Element) doc.getElementsByTagName("movies") .item(0); booksElement.appendChild(bookElement); // 保存XML文件 TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(doc); // 设置编码类型 transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); StreamResult result = new StreamResult(new FileOutputStream( "src/login.xml")); // 把DOM树转换为XML文件 transformer.transform(domSource, result); System.out.println("添加成功"); } catch (Exception e) { e.printStackTrace(); } } public static void update() throws ParserConfigurationException, SAXException, IOException, TransformerException { // 1、得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2、从DOM工厂获得DOM解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 3、解析XML文档,得到一个Document,即DOM树 Document doc = db.parse("src/login.xml"); Scanner sca = new Scanner(System.in); System.out.println("输入要修改的节点序号"); String a = sca.next(); NodeList list = doc.getElementsByTagName("movie"); for (int i = 0; i < list.getLength(); i++) { Element bookElement = (Element) list.item(i); String scoreID = bookElement.getAttribute("xh"); System.out.println(scoreID); if (scoreID.equals(a)) { NodeList items = bookElement.getChildNodes(); for (int j = 0; j < items.getLength(); j++) { Node node = items.item(j); if (node.getNodeName().equals("price")) { node.setTextContent("75"); } } } } // 保存XML文件 TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(doc); // 设置编码类型 transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); StreamResult result = new StreamResult(new FileOutputStream( "src/login.xml")); // 把DOM树转换为XML文件 transformer.transform(domSource, result); System.out.println("修改成功"); } public static void log() throws Exception { Scanner sca = new Scanner(System.in); System.out.println("请输入员工账户:"); String str1 = sca.next(); System.out.println("请输入密码:"); String str2 = sca.next(); ArrayList<root> list = new ArrayList<root>(); // 1、得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2、从DOM工厂获得DOM解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 3、解析XML文档,得到一个Document,即DOM树 Document doc = db.parse("src/pw.xml"); // 找到删除的节点 // 获取所有节点 NodeList roots = doc.getChildNodes(); Node root = roots.item(0);// 获取根节点 // System.out.println(root.getNodeName()); NodeList movies = root.getChildNodes(); for (int i = 0; i < movies.getLength(); i++) { Node movie = movies.item(i); NodeList items = movie.getChildNodes(); root r = new root(); for (int j = 0; j < items.getLength(); j++) { Node item = items.item(j); if (item.getNodeName().equals("name")) { r.setName(item.getTextContent()); } if (item.getNodeName().equals("role")) { r.setRole(item.getTextContent()); } if (item.getNodeName().equals("pwd")) { r.setPwd(item.getTextContent()); } } if (r.getName() != null) { list.add(r); } } // for (int j = 0; j < list.size(); j++) { // System.out.println(list.get(j).getName()+list.get(j).getRole()+list.get(j).getPwd()); // } boolean flag = false; for (int j = 0; j < list.size(); j++) { if (list.get(j).getName().equals(str1) && (list.get(j).getPwd().equals(str2))) { flag = true; System.out.println("登录成功"); System.out.println("===================="); break; } } if (flag == false) { System.out.println("账号或密码不对,请重新输入"); log(); } } public static void admin() throws Exception { Scanner sca = new Scanner(System.in); System.out.println("请输入管理员账户:"); String a = sca.next(); System.out.println("请输入密码:"); String b = sca.next(); // 1、得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 2、从DOM工厂获得DOM解析器 DocumentBuilder db = dbf.newDocumentBuilder(); // 3、解析XML文档,得到一个Document,即DOM树 Document doc = db.parse("src/pw.xml"); // 找到删除的节点 // 获取所有节点 NodeList roots = doc.getChildNodes(); Node root = roots.item(0);// 获取根节点 // System.out.println(root.getNodeName()); NodeList movies = root.getChildNodes(); Boolean flag = false;// buzhenque Boolean flag1 = false; for (int i = 0; i < movies.getLength(); i++) { Node movie = movies.item(i); NodeList items = movie.getChildNodes(); for (int j = 0; j < items.getLength(); j++) { Node item = items.item(j); if (item.getNodeName().equals("name")) { if (item.getTextContent().equals("admin")) { if (a.equals(item.getTextContent())) { flag = true; System.out.println("管理员账户正确"); break; } } } // if (item.getNodeName().equals("pwd")) { if (item.getTextContent().equals("123")) { if (b.equals(item.getTextContent())) { flag1 = true; break; } } } } } if (flag == false) { System.out.println("账号不正确"); admin(); } if (flag1 == false) { System.out.println("密码不正确"); admin(); } else { System.out.println("管理员密码正确"); } } }

 

转载请注明原文地址: https://www.6miu.com/read-4049938.html

最新回复(0)