影院售票系统-----一个让你有成就感的小项目,只有一丢丢哦
Posted 刘二雄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了影院售票系统-----一个让你有成就感的小项目,只有一丢丢哦相关的知识,希望对你有一定的参考价值。
先来点废话,刚拿到这个项目的时候你是怎么想的,上来就闷头写,还是构想思路......
项目需求:
影院售票系统可以销售每天不同时段的电影票,不同时段的电影票有不同的优惠政策,还有使用兑换券的免费票。选择某个时段的一场电影,
单击放映厅未售出的座位并选择一个种类的电影票,创建电影票,计算价格并打印,座位设置为红色表示已经售出。
思想:
使用面向对象思想编写影院售票系统
看完这个你有没有点思路呢,我想你是有的 ......
下面先来点图片,先刺激一下视觉:
窗体搭建:
程序运行时发生的,来看清楚了:
是不是该选择你要看的电影列表了
好了,该你选择电影了,
挑选你喜欢的座位:
掏钱吧,要不你选择的就是别人的了
这个座位是你的了,还想看别的么?
选择赠票吧,免费的:
再来张学生票:
这样看是不是特别不爽,我感觉也是,看图片没感觉,接下来代码吧!
先把我们所需要的几个类创建出来吧!
/// <summary> /// 电影票父类 /// </summary> public class Ticket { public Ticket() { } public Ticket(ScheduleItem scheduleItem, Seat seat) { ScheduleItem = scheduleItem; Seat = seat; } //播放场次 public ScheduleItem ScheduleItem { get; set; } //所属座位对象 public Seat Seat { get; set; } //票价 public int Price { get; set; } //计算票价的虚方法 public virtual void CalcPrice() { Price =ScheduleItem.Movie.Price; } //打印售票信息的虚方法 public virtual void Print() { string fileName = ScheduleItem.Time.Replace(":", "-") + " " + Seat.SeatNum + ".txt"; FileStream fs = new FileStream(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("***************************"); sw.WriteLine(" 青鸟影院(普通) "); sw.WriteLine("---------------------------"); sw.WriteLine(" 电影名:\\t{0}", ScheduleItem.Movie.MovieName); sw.WriteLine(" 时间: \\t{0}", ScheduleItem.Time); sw.WriteLine(" 座位号:\\t{0}", Seat.SeatNum); sw.WriteLine(" 价格: \\t{0}", Price.ToString()); sw.WriteLine("***************************"); sw.Close(); fs.Close(); } //显示当前售出票信息的虚方法 public virtual void Show() { MessageBox.Show("已售出"); } }
/// <summary> /// 学生票子类 /// </summary> public class StudentTicket : Ticket { //折扣 public int Discount { get; set; } public StudentTicket() { } public StudentTicket(ScheduleItem scheduleItem, Seat seat, int discount) : base(scheduleItem, seat) { Discount = discount; } //重写计算票价的方法 public override void CalcPrice() { Price = ScheduleItem.Movie.Price * Discount / 10; } //重写打印售票信息的方法 public override void Print() { string fileName = ScheduleItem.Time.Replace(":", "-") + " " + Seat.SeatNum + ".txt"; FileStream fs = new FileStream(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("***************************"); sw.WriteLine(" 青鸟影院 (学生) "); sw.WriteLine("---------------------------"); sw.WriteLine(" 电影名:\\t{0}", ScheduleItem.Movie.MovieName); sw.WriteLine(" 时间: \\t{0}", ScheduleItem.Time); sw.WriteLine(" 座位号:\\t{0}", Seat.SeatNum); sw.WriteLine(" 价格: \\t{0}", Price.ToString()); sw.WriteLine("***************************"); sw.Close(); fs.Close(); } //重写当前显示出售票信息的方法 public override void Show() { MessageBox.Show("已售出!\\n\\n " + Discount + "折学生票!"); } }
/// <summary> /// 赠票类 /// </summary> public class FreeTicket : Ticket { public FreeTicket() { } public FreeTicket(ScheduleItem scheduleItem, Seat seat, string customerName) : base(scheduleItem, seat) { CustomerName = customerName; } //赠票者名字 public string CustomerName { get; set; } //重写计算票价的方法 public override void CalcPrice() { Price = 0; } //重写打印售票信息的方法 public override void Print() { string fileName = ScheduleItem.Time.Replace(":", "-") + " " + Seat.SeatNum + ".txt"; FileStream fs = new FileStream(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("***************************"); sw.WriteLine(" 青鸟影院 (赠票)"); sw.WriteLine("---------------------------"); sw.WriteLine(" 电影名:\\t{0}", ScheduleItem.Movie.MovieName); sw.WriteLine(" 时间: \\t{0}", ScheduleItem.Time); sw.WriteLine(" 座位号:\\t{0}", Seat.SeatNum); sw.WriteLine(" 姓名: \\t{0}", CustomerName); sw.WriteLine("***************************"); sw.Close(); fs.Close(); } //重写当前显示出售票信息的方法 public override void Show() { MessageBox.Show("已售出!\\n\\n 赠送者:" + CustomerName); } }
/// <summary> /// 座位信息类 /// </summary> public class Seat { //座位号 public string SeatNum { get; set; } //卖出座位颜色 public Color Color { get; set; } public Seat() { } public Seat(string seatNum,System.Drawing.Color color) { this.SeatNum = seatNum; this.Color = color; } }
/// <summary> /// 保存放映计划信息类 /// </summary> public class ScheduleItem { public ScheduleItem() { Movie = new Movie(); } //播放时间 public string Time { get; set; } //播放电影属性 public Movie Movie { get; set; } }
/// <summary> /// 放映计划类 /// </summary> public class Schedule { public Schedule() { items = new Dictionary<string, ScheduleItem>(); } //放映场次 private Dictionary<string, ScheduleItem> items; public Dictionary<string, ScheduleItem> Items { get { return items; } set { items = value; } } //读取Xml文件的方法 public void LoadItems() { XmlDocument doc = new XmlDocument(); doc.Load("ShowList.xml"); XmlNode root=doc.DocumentElement; foreach (XmlNode item in root.ChildNodes) { foreach (XmlNode child in item["Schedule"]) { ScheduleItem sch = new ScheduleItem(); sch.Movie.MovieName = item["Name"].InnerText; sch.Movie.Poster = item["Poster"].InnerText; sch.Movie.Director = item["Director"].InnerText; sch.Movie.Price = Convert.ToInt32(item["Price"].InnerText); sch.Movie.Actor = item["Actor"].InnerText; sch.Movie.MovieType = (MovieType)Enum.Parse(typeof(MovieType), item["Type"].InnerText); sch.Time = child.InnerText; items.Add(sch.Time, sch); } } } }
/// <summary> /// 电影类 /// </summary> public class Movie { //电影名 public string MovieName { get; set; } //海报图片名 public string Poster { get; set; } //导演名 public string Director { get; set; } //主演 public string Actor { get; set; } //电影类型 public MovieType MovieType { get; set; } //定价 public int Price { get; set; } public Movie() { } public Movie(string movieName, string poster, string director, string actor, MovieType movieType, int price) { MovieName = movieName; Poster = poster; Director = director; Actor = actor; MovieType = movieType; Price = price; } } public enum MovieType { //喜剧 Comedy, //战争 War, //爱情 Romance, //动作 Action, //卡通 Cartoon, //恐怖 Thriller, //冒险 Adventure }
/// <summary> /// 影院类 /// </summary> public class Cinema { public Cinema() { Seats = new Dictionary<string, Seat>(); SoldTickets = new List<Ticket>(); Schedule = new Schedule(); } //座位集合 public Dictionary<string,Seat> Seats { get; set; } //放映计划 public Schedule Schedule { get; set; } //已出售电影票的集合 public List<Ticket> SoldTickets { get; set; } //保存售票信息的方法 public void Say() { FileStream fs = new FileStream("soldTickets.txt", FileMode.OpenOrCreate); StreamWriter writer = new StreamWriter(fs, Encoding.Default); for (int index = 0; index < SoldTickets.Count; index++) { Ticket ticket = SoldTickets[index]; string info = "|" + ticket.ScheduleItem.Movie.MovieName + "|" + ticket.ScheduleItem.Movie.Poster + "|" + ticket.ScheduleItem.Movie.Director + "|" + ticket.ScheduleItem.Movie.Actor + "|" + ticket.ScheduleItem.Movie.MovieType.ToString() + "|" + ticket.ScheduleItem.Movie.Price + "|" + ticket.ScheduleItem.Time + "|" + ticket.Seat.SeatNum + "|" + ticket.Seat.Color + "|" + ticket.Price + "|"; if (ticket is FreeTicket) { string customerName = ((FreeTicket)ticket).CustomerName; writer.WriteLine("free" + info + customerName); } else if (ticket is StudentTicket) { writer.WriteLine("student" + info + ""); } else { writer.WriteLine("" + info + ""); } } writer.WriteLine("The End"); writer.Close(); fs.Close(); } //读取售票信息的方法 public void Load() { try { //创建一个读取对象 //路径 //转换格式 StreamReader reader = new StreamReader("soldTickets.txt", Encoding.GetEncoding("GB2312")); string line = reader.ReadLine(); //声明一个数组 存储 soldTickets.txt 的数据 string[] values; //声明一个电影票对象 Ticket ticket = null; while (line.Trim() != "The End") { values = line.Split(\'|\'); string type = values[0]; Movie movie = new Movie(values[1], values[2], values[3], values[4], (MovieType)Enum.Parse(typeof(MovieType), values[5]), int.Parse(values[6])); ScheduleItem scheduleItem = new ScheduleItem(); scheduleItem.Time = values[7]; scheduleItem.Movie = movie; string color = values[9]; string endColor = color.Substring(color.IndexOf("[") + 1, color.Length - 1 - color.IndexOf("[") - 1); Seat seat = new Seat(values[8], Color.FromName(endColor)); int discount = 10; switch (type) { case "student": discount = 7; ticket = TicketUtil.CreateTicket(scheduleItem, seat, discount, "", type); break; case "free": discount = 0; ticket = TicketUtil.CreateTicket(scheduleItem, seat, discount, values[11], type); break; default: discount = 10; ticket = TicketUtil.CreateTicket(scheduleItem, seat, discount, "", ""); break; } this.SoldTickets.Add(ticket); line = reader.ReadLine(); } reader.Close(); } catch (Exception ex) { Console.WriteLine("出错了:" + ex.Message); SoldTickets = new List<Ticket>(); } } }
/// <summary> /// 工具类 /// </summary> public class TicketUtil { public static Ticket CreateTicket(ScheduleItem scheduleItem, Seat seat, int discount, string customerName, string type) { Ticket ticket = null; switch (type) { case "student": ticket = new StudentTicket(scheduleItem, seat, discount); break; case "free": ticket = new FreeTicket(scheduleItem, seat, customerName); break; case "putong": ticket = new Ticket(scheduleItem, seat); break; } return ticket; } }
我去,这么多类,你有没有这个感觉,反正我是有的,这些类的创建不是瞎弄的,要把自己的思路理清楚了!
具体的代码实现:
public Cinema cinema; //加载座位 Dictionary<string, Label> labels = new Dictionary<string, Label>(); int tickets = 0; string key = null; /// <summary> /// 程序刚启动时 窗体加载的数据 /// </summary> private void FrmMain_Load(object sender, EventArgs e) { label1.Text = ""; label2.Text = ""; label3.Text = ""; label4.Text = ""; label5.Text = ""; label6.Text = ""; label8.Text = ""; rb01.Checked = true; //初始化一个影院类对象 cinema = new Cinema(); InitInfo(); //调用显示售票信息的方法 cinema.Load(); } /// <summary> /// 点击新播放列表时触发 /// </summary> private void 新放映列表ToolStripMenuItem_Click(object sender, EventArgs e) { cinema.Schedule.LoadItems(); cinema.SoldTickets.Clear(); InitTreeView(); } /// <summary> /// 绑定ListView /// </summary> private void InitTreeView() { tvList.Nodes.Clear(); string movieName = ""; TreeNode tn = null; foreach (ScheduleItem item in cinema.Schedule.Items.Values) { if (movieName != item.Movie.MovieName) { tn = new TreeNode(); tn.Text = item.Movie.MovieName; tvList.Nodes.Add(tn); } TreeNode node = new TreeNode(item.Time); tn.Nodes.Add(node); movieName = item.Movie.MovieName; } } Label label; /// <summary> /// 加载座位 /// </summary> private void InitInfo() { int seatRow = 8; int seatLine = 5; for (int i = 0; i < seatRow; i++) { for (int j = 0; j < seatLine; j++) { label = new Label(); //设置背景颜色 label.BackColor = Color.Yellow; //设置字体 label.Font = new System.Drawing.Font("宋体",14.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point, 134); //设置尺寸 label.AutoSize = false; label.Size = new System.Drawing.Size(50, 25); label.Text = (j + 1).ToString() + "-" + (i + 1).ToString(); label.TextAlign = ContentAlignment.MiddleCenter; //设置位置 label.Location = new Point(60+(i*70),40+(j*影院在线售票云平台(仿猫眼电影,附SpringBoot项目源码) 系统功能实现JavaWeb Springmvc 电影院售票管理系统(数据库+源码+论文可运行《精品毕设》)主要实现了用户管理电影管理影片浏览选择票务管理订单通知公告影院模拟支付个人信息等功能