一、xml列表

<?xml version="1.0" encoding="UTF-8"?>
<movies>
	<movie>
	    <name>卧虎藏龙</name>
	    <director>李安</director>
		<actor>周润发</actor>	
		<price>120</price>
		<schedule>
		   <item>09:00</item>
		   <item>13:00</item>
		</schedule>
	</movie>
	<movie>
	    <name>盗墓笔记</name>
	    <director>南派三叔</director>
		<actor>鹿晗</actor>	
		<price>80</price>
		<schedule>
		   <item>11:00</item>
		</schedule>
	</movie>
</movies>

二、电影类

package cn.com.cinema;

/*
 * 影片类
 */
public class Movie {

	private String name; // 影片名称
	private String director; // 影片导演
	private String actor; // 影片主演
	private Integer price; // 影片价格
	private String scheduleTime;// 影片放映时间

	public Movie() {
	}

	public Movie(String name, String director, String actor, Integer price,
			String scheduleTime) {
		super();
		this.name = name;
		this.director = director;
		this.actor = actor;
		this.price = price;
		this.scheduleTime = scheduleTime;
	}

	public String getName() {
		return name;
	}

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

	public String getDirector() {
		return director;
	}

	public void setDirector(String director) {
		this.director = director;
	}

	public String getActor() {
		return actor;
	}

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

	public Integer getPrice() {
		return price;
	}

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

	public String getScheduleTime() {
		return scheduleTime;
	}

	public void setScheduleTime(String scheduleTime) {
		this.scheduleTime = scheduleTime;
	}

}

三、电影管理类。操作都在里面

package cn.com.cinema;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/*
 * 影院管理类
 */
public class CinemaMgr {
	Scanner in = new Scanner(System.in);
	// 所有文件生成的目录
	private String dir = "D:/ticket";
	private String sellFile = "D:/ticket/ticket.txt";

	// 声明一个已售出影票的集合(Map)
	private Map<String, List<String>> selledMap;
	// 初始化加载已经售出的所有座位
	{
		File selledFile = new File(sellFile);
		if (selledFile.exists()) {
			try {
				FileInputStream fis = new FileInputStream(selledFile);
				ObjectInputStream ois = new ObjectInputStream(fis);
				selledMap = (Map<String, List<String>>) ois.readObject();
				ois.close();
				fis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	// 定义影片列表
	private List<Movie> movieList;
	/*
	 * 加载影片列表 读取movies.xml文件获取所有影片信息 使用Dom4j解析XML
	 */
	private void loadMovies() {
		movieList = new ArrayList<Movie>();
		// 创建SAXReader
		SAXReader reader = new SAXReader();
		try {
			Document doc = reader.read("src/movies.xml");
			Element root = doc.getRootElement();
			List<Element> mList = root.elements("movie");
			for (Element m : mList) {
				String name = m.elementText("name");
				String director = m.elementText("director");
				String actor = m.elementText("actor");
				Integer price = Integer.parseInt(m.elementText("price"));
				List<Element> itemList = m.element("schedule").elements("item");
				for (Element item : itemList) {
					movieList.add(new Movie(name, director, actor, price, item
							.getText()));
				}
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 格式化打印影片列表
	 */
	private void showAllMovies() {
		System.out.println("序号\t电影名称\t导演\t主演\t价格\t时间");
		for (int i = 0; i < movieList.size(); i++) {
			Movie m = movieList.get(i);
			System.out.println((i + 1) + "\t" + m.getName() + "\t"
					+ m.getDirector() + "\t" + m.getActor() + "\t"
					+ m.getPrice() + "\t" + m.getScheduleTime());
		}
	}

	/*
	 * 判断是否已经售出该场影片的该座位
	 */
	private boolean isSelled(Movie movie, String seat) {
		if (null != selledMap) {
			// Map中key格式为“影片名称13:00”
			if (selledMap
					.containsKey(movie.getName() + movie.getScheduleTime())) {
				List<String> seatList = selledMap.get(movie.getName()
						+ movie.getScheduleTime());
				if (seatList.contains(seat)) {
					return true; // 已经售出
				}
			}
		}
		return false;
	}

	/*
	 * 购票
	 */
	private void choiceTicket() {
		System.out.print("请输入影片序号:");
		int moiveId = in.nextInt();
		Movie movie = movieList.get(moiveId - 1);// 获取选定影片
		// 调用打印座位号的方法,并获取选择座位号字符串
		String seat = getSeat();
		// 判断是否已经售出该场影片的该座位
		if (isSelled(movie, seat)) {
			System.out.println("该座位已经售出!");
			return;
		}
		System.out.println("请选择影票类型:(1.普通票    2.学生票   3.赠票)");
		int typeId = in.nextInt();
		switch (typeId) {
		case 1: // 普通票
			printTicket(movie, seat);
			break;
		case 2: // 学生票
			System.out.println("请选择折扣:(1~9折)");
			int zk = in.nextInt();
			printTicket(movie, seat, zk);
			break;
		case 3: // 赠票
			System.out.println("请输入姓名:");
			String customName = in.next();
			printTicket(movie, seat, customName);
			break;
		}
	}

	/*
	 * 文件输出(打印影票)
	 */
	private void writerTicket(String fileName, String content) {
		File ticketDir = new File(dir);
		if (!ticketDir.exists()) { // 若目录不存在,则创建
			ticketDir.mkdir();
		}
		try {
			FileWriter fw = new FileWriter(dir + File.separator + fileName);
			fw.write(content);
			fw.flush();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 添加售票记录
	 */
	private void addSelledTicket(Movie movie, String seat) {
		String key = movie.getName() + movie.getScheduleTime();
		// 判断是否首次运行
		if (selledMap == null) {
			selledMap = new HashMap<String, List<String>>();
		}
		// 判断本部影片本场是否卖出过票
		if (selledMap.containsKey(key)) {
			// 本场次本片已售过票
			List<String> seatList = selledMap.get(key);
			seatList.add(seat);
		} else {
			// 本场次本片未售过票
			List<String> seatList = new ArrayList<String>();
			seatList.add(seat);
			selledMap.put(key, seatList);
		}
	}

	/*
	 * 打印普通票
	 */
	private void printTicket(Movie movie, String seat) {
		StringBuffer sub = new StringBuffer();
		sub.append("**********************\n");
		sub.append("            青鸟影院\n");
		sub.append("----------------------\n");
		sub.append("     电影名:" + movie.getName() + "\n");
		sub.append("     时间:  " + movie.getScheduleTime() + "\n");
		sub.append("     座位号:" + seat + "\n");
		sub.append("     价格:  " + movie.getPrice() + "\n");
		sub.append("**********************");
		String content = sub.toString();
		System.out.println("购票成功!请拿好您的影票!");
		System.out.println(content);
		// 打印票(文件输出)
		String fileName = movie.getName() + " "
				+ movie.getScheduleTime().replace(":", "点") + "分  " + seat
				+ "座.txt";
		writerTicket(fileName, content.replaceAll("\n", "\r\n"));
		// 打票成功后,将售票记录添加到售票记录的Map中
		addSelledTicket(movie, seat);
	}

	/*
	 * 打印学生票
	 */
	private void printTicket(Movie movie, String seat, int zk) {
		StringBuffer sub = new StringBuffer();
		sub.append("***************************\n");
		sub.append("            青鸟影院 (学生)\n");
		sub.append("---------------------------\n");
		sub.append("     电影名:" + movie.getName() + "\n");
		sub.append("     时间:  " + movie.getScheduleTime() + "\n");
		sub.append("     座位号:" + seat + "\n");
		sub.append("     价格:  " + (movie.getPrice() * zk / 10) + "\n");
		sub.append("***************************");
		String content = sub.toString();
		System.out.println("购票成功!请拿好您的影票!");
		System.out.println(content);
		// 打印票(文件输出)
		String fileName = movie.getName() + " "
				+ movie.getScheduleTime().replace(":", "点") + "分  " + seat
				+ "座.txt";
		writerTicket(fileName, content.replaceAll("\n", "\r\n"));
		// 打票成功后,将售票记录添加到售票记录的Map中
		addSelledTicket(movie, seat);
	}

	/*
	 * 打印赠票
	 */
	private void printTicket(Movie movie, String seat, String customName) {
		StringBuffer sub = new StringBuffer();
		sub.append("*************************\n");
		sub.append("            青鸟影院(赠票)\n");
		sub.append("-------------------------\n");
		sub.append("     电影名:" + movie.getName() + "\n");
		sub.append("     时间:  " + movie.getScheduleTime() + "\n");
		sub.append("     座位号:" + seat + "\n");
		sub.append("     姓名:  " + customName + "\n");
		sub.append("*************************");
		String content = sub.toString();
		System.out.println("购票成功!请拿好您的影票!");
		System.out.println(content);
		// 打印票(文件输出)
		String fileName = movie.getName() + " "
				+ movie.getScheduleTime().replace(":", "点") + "分  " + seat
				+ "座.txt";
		writerTicket(fileName, content.replaceAll("\n", "\r\n"));
		// 打票成功后,将售票记录添加到售票记录的Map中
		addSelledTicket(movie, seat);
	}

	/*
	 * 打印并选择座位号
	 */
	private String getSeat() {
		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();
		}
		System.out.print("请输入选择的座位号:");
		return in.next();
	}

	/*
	 * 启动方法
	 */
	public void start() {
		// 启动影院
		loadMovies();// 初始化加载影片
		System.out.println("\t\t欢迎来到XXX影院观影");
		System.out.println("当天影片信息如下:");
		int flag = 0;
		do {
			// 格式化打印所有影片列表
			showAllMovies();
			// 开始购票
			choiceTicket();
			System.out.print("\n输入0继续购票,输入其他数字退出!");
			flag = in.nextInt();
		} while (flag == 0);
		// 将本次系统运行过程中售出的票的记录,记录到文件中
		writeRecodeToFile();
		System.out.println("\n谢谢光临!欢迎下次再来观影!");
	}
	/*
	 * 将售出的票的记录,记录到文件中
	 */
	private void writeRecodeToFile() {
		// 对象序列化写出到文件中
		try {
			FileOutputStream fos = new FileOutputStream(sellFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(selledMap);
			oos.flush();
			oos.close();
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

四、测试类

package cn.com.cinema;
public class Test {
	public static void main(String[] args) {
		new CinemaMgr().start();//启动
	}
}