首页 > 代码库 > 影院售票系统

影院售票系统

首先运行结果如图:

技术分享

需要一些辅助类

技术分享

cinema 影院类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace FrmMain{    /// <summary>    /// 影院类    /// </summary>    ///     [Serializable]    public class Cinema    {        //座位集合        public Dictionary<string,Seat> Seats { get; set; }        //放映计划        public Schedule Schedule { get; set; }        //已售出电影票的集合        public List<Ticket> SoldTickets{ get; set; }        //保存售票情况的Save()方法        public void Save(List<Ticket> SoldTickets)        {            FileStream fs = new FileStream("售票情况.din",FileMode.Create);            BinaryFormatter bf = new BinaryFormatter();            bf.Serialize(fs,SoldTickets);            fs.Close();        }        //读取情况的Load()方法        public List<Ticket> Load()        {            FileStream fs = new FileStream("售票情况.din", FileMode.Open);            BinaryFormatter bf = new BinaryFormatter();            List<Ticket> List = ((List<Ticket>)bf.Deserialize(fs));            fs.Close();            return List;        }        public Cinema()        {        }        public Cinema(Schedule Schedule)        {            this.Schedule = Schedule;            Seats = new Dictionary<string, Seat>();            SoldTickets = new List<Ticket>();        }    }}

  

Tickets父类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace FrmMain{    /// <summary>    /// 电影票父类    /// </summary>    ///     [Serializable]    public class Ticket    {        //放映场次        public ScheduleItem ScheduleItem { get; set; }        //所属座位对象        public Seat Seat { get; set; }        //票价        public int price { get; set; }        public Ticket()        {        }        public Ticket(ScheduleItem ScheduleItem, Seat Seat, int price)        {            this.ScheduleItem = ScheduleItem;            this.Seat = Seat;            this.price = price;        }        //计算票价的虚方法        public virtual void CalcPrice()        {        }        //打印售票信息的虚方法        public virtual void Print()        {            StringBuilder str = new StringBuilder();            str.AppendLine("**************************\n");            str.AppendLine("\t" + "青鸟影院" + "\n");            str.AppendLine("--------------------------\n");            str.AppendLine("    电影名:" + ScheduleItem.Movie.MovieName + "\n");            str.AppendLine("    时间:" + ScheduleItem.Time + "\n");            str.AppendLine("    座位号:" + Seat.SeatName + "\n");            str.AppendLine("    价格:" + this.price + "\n");            str.AppendLine("**************************");            FileStream file = new FileStream(ScheduleItem.Movie.MovieName + " " + Seat.SeatName + ".txt", FileMode.Create);            StreamWriter writer = new StreamWriter(file);            writer.WriteLine(str);            writer.Close();            file.Close();        }        //显示当前售出票信息的虚方法        public virtual string Show()        {            string str = "已售出:\n普通票";            return str;        }    }}

  

FreeTickets增票子类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace FrmMain{    /// <summary>    /// 赠票子类    /// </summary>    ///     [Serializable]    public class FreeTicket:Ticket    {        //获得增票者的名字        public string CustomerName { get; set; }        public FreeTicket(ScheduleItem ScheduleItem, Seat Seat, int price, string CustomerName)            : base(ScheduleItem, Seat, price)        {            this.CustomerName = CustomerName;        }        //计算票价的方法        public override void CalcPrice()        {            this.price = 0;        }        //打印售票信息的方法        public override void Print()        {            StringBuilder str = new StringBuilder();            str.AppendLine("**************************\n");            str.AppendLine("\t" + "青鸟影院(赠票)" + "\n");            str.AppendLine("--------------------------\n");            str.AppendLine("    电影名:" + ScheduleItem.Movie.MovieName + "\n");            str.AppendLine("    时间:" + ScheduleItem.Time + "\n");            str.AppendLine("    座位号:" + Seat.SeatName + "\n");            str.AppendLine("    姓名:" + this.CustomerName + "\n");            str.AppendLine("**************************");            FileStream file = new FileStream(ScheduleItem.Movie.MovieName + " " + Seat.SeatName + ".txt", FileMode.Create);            StreamWriter writer = new StreamWriter(file);            writer.WriteLine(str);            writer.Close();            file.Close();        }        //显示当前售出票信息的方法        public override string Show()        {            string str = "已售出:\n赠送票";            return str;        }    }}

  

StudentTickets学生子类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace FrmMain{    /// <summary>    /// 学生票子类    /// </summary>    ///     [Serializable]    public class StudentTicket:Ticket    {        //学生票的折扣        public int Discount { get; set; }        public StudentTicket()        {        }        public StudentTicket(ScheduleItem ScheduleItem, Seat Seat, int price, int Discount)            : base(ScheduleItem, Seat, price)        {            this.Discount = Discount;        }        //计算票价的方法        public override void CalcPrice()        {            this.price = Convert.ToInt32(price * (Discount * 0.1));        }        //打印售票信息的方法        public override void Print()        {            StringBuilder str = new StringBuilder();            str.AppendLine("**************************\n");            str.AppendLine("\t" + "青鸟影院(学生)" + "\n");            str.AppendLine("--------------------------\n");            str.AppendLine("    电影名:" + ScheduleItem.Movie.MovieName + "\n");            str.AppendLine("    时间:" + ScheduleItem.Time + "\n");            str.AppendLine("    座位号:" + Seat.SeatName + "\n");            str.AppendLine("    价格:" + this.price + "\n");            str.AppendLine("**************************");            FileStream file = new FileStream(ScheduleItem.Movie.MovieName + " " + Seat.SeatName + ".txt", FileMode.Create);            StreamWriter writer = new StreamWriter(file);            writer.WriteLine(str);            writer.Close();            file.Close();        }        //显示当前售出票信息的方法        public override string Show()        {            string str = "已售出:\n" + Discount + "折学生票";            return str;        }    }}

  

Schedule放映计划类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;namespace FrmMain{    /// <summary>    /// 放映计划类    /// </summary>    [Serializable]   public  class Schedule    {        //放映场次属性        public Dictionary<string,ScheduleItem> Items { get; set; }        public Schedule()        {            Items = new Dictionary<string, ScheduleItem>();        }        //读取xml文件获取放映计划合集的方法        public void LoadItems()        {            XmlDocument doc = new XmlDocument();            doc.Load("ShowList.xml");            XmlNode root = doc.DocumentElement;            foreach (XmlNode item in root)            {                string type=item["Type"].InnerText;                Movie movie = new Movie(item["Name"].InnerText, item["Poster"].InnerText, item["Director"].InnerText, item["Actor"].InnerText, TicketUtil.type(type),Convert.ToInt32( item["Price"].InnerText));                    foreach (XmlNode item1 in item["Schedule"])                    {                        ScheduleItem sitem = new ScheduleItem(item1.InnerText,movie);                        this.Items.Add(item1.InnerText, sitem);                    }                            }        }        }}

  

ScheduleItem影院计划放映场次类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FrmMain{    /// <summary>    /// 影院每天放映计划的场次    /// </summary>    ///     [Serializable]    public class ScheduleItem    {        //放映时间        public string Time { get; set; }        //本场所放映电影        public Movie Movie { get; set; }        public ScheduleItem()        {        }        public ScheduleItem(string Time, Movie Movie)        {            this.Time = Time;            this.Movie = Movie;        }    }}

  

Seat座位类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Drawing;namespace FrmMain{    /// <summary>    /// 影院座位信息    /// </summary>    ///     [Serializable]    public class Seat    {        //座位号        public string SeatName { get; set; }        ///座位卖出状态颜色        public Color Color { get; set; }        public Seat()        {        }        public Seat(string SeatName, System.Drawing.Color Color)        {            this.SeatName = SeatName;            this.Color = Color;        }    }}

  

TicketUtil工具类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FrmMain{    /// <summary>    /// 工具类    /// </summary>    ///     [Serializable]   public class TicketUtil    {        //创建电影票的方法        public static Ticket CreateTicket(string TicketType, ScheduleItem ScheduleItem, Seat Seat, int Discount, string CustomerName)        {            Ticket ticket = null;            Seat seat = new Seat();            seat.SeatName = Seat.SeatName;            seat.Color = System.Drawing.Color.Red;            switch (TicketType)            {                case "普通票":                    ticket = new Ticket(ScheduleItem, seat, ScheduleItem.Movie.Price);                    break;                case "赠票":                    ticket = new FreeTicket(ScheduleItem, seat, ScheduleItem.Movie.Price, CustomerName);                    break;                case "学生票":                    ticket = new StudentTicket(ScheduleItem, seat, ScheduleItem.Movie.Price, Discount);                    ticket.CalcPrice();                    break;            }            return ticket;        }        //判断电影类型的方法        public static MovieType type(string Type)        {            MovieType movietype = MovieType.War;            switch (Type)            {                case "Comedy":                    movietype = MovieType.Comedy;                    break;                case "War":                    movietype = MovieType.War;                    break;                case "Romance":                    movietype = MovieType.Romance;                    break;                case "Action":                    movietype = MovieType.Action;                    break;                case "Cartoon":                    movietype = MovieType.Cartoon;                    break;                case "Thriller":                    movietype = MovieType.Thriller;                    break;                case "Adventure":                    movietype = MovieType.Action;                    break;            }            return movietype;        }        public static Cinema Cinema()        {            Schedule sd = new Schedule();            sd.LoadItems();            Cinema cinema = new Cinema(sd);            return cinema;        }    }}

  

Movie电影类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FrmMain{    /// <summary>    /// 电影类    /// </summary>    ///     [Serializable]    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)        {            this.MovieName = MovieName;            this.Poster = Poster;            this.Director = Director;            this.Actor = Actor;            this.MovieType = MovieType;            this.Price = Price;        }    }}

  

MovieType电影类型枚举类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FrmMain{    /// <summary>    /// 电影类型枚举类    /// </summary>    ///     [Serializable]    public enum MovieType    {        //喜剧,战争片,爱情片,动作片,卡通片,惊悚片,冒险片         Comedy,War,Romance,Action,Cartoon,Thriller,Adventure    }}

  

主窗体Main方法中的代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace FrmMain{    [Serializable]    public partial class FrmMain : Form    {        public FrmMain()        {            InitializeComponent();        }        //影院对象        Cinema cinema = null;        string TicketType = "普通票";        private void FrmMain_Load(object sender, EventArgs e)        {            cinema = TicketUtil.Cinema();            ShowSeat(7,5);        }        //生成座位        public void ShowSeat(int seatRow, int seatLine)        {            for (int i = 0; i < seatRow; i++)            {                for (int j = 0; j < seatLine; j++)                {                    Label label = new Label();                    label.BackColor = Color.Yellow;                    label.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(123)));                    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 * 81), 60 + (j * 51));                    label.Click += new System.EventHandler(lblSeat_Click);                    tabPage1.Controls.Add(label);                    Seat seat = new Seat((j + 1).ToString() + "-" + (i + 1).ToString(), Color.Yellow);                    cinema.Seats.Add(seat.SeatName, seat);                }            }        }        //更新座位的方法        public void Info()        {            if (tvList.SelectedNode!=null)            {                TreeNode tn = tvList.SelectedNode;                if (tn.Tag!=null)                {                    foreach (Control item in tabList.Controls)                    {                        item.BackColor = Color.Yellow;                    }                    foreach (Ticket tk in cinema.SoldTickets)                    {                        foreach (Control cl in tabList.Controls)                        {                            if (tk.ScheduleItem.Time==((ScheduleItem)tn.Tag).Time&&cl.Text==tk.Seat.SeatName)                            {                                cl.BackColor = tk.Seat.Color;                            }                        }                    }                }            }        }        private void lblSeat_Click(object sender, EventArgs e)        {            //购票            string seatNum=((Label)sender).Text.ToString();            if (tvList.SelectedNode!=null)            {                TreeNode tn = tvList.SelectedNode;                if (tn.Tag!=null)                {                    DialogResult result = MessageBox.Show("是否确认购买?","提示",MessageBoxButtons.YesNo);                    if (result==DialogResult.Yes)                    {                        foreach (Seat item in cinema.Seats.Values)                        {                            if (item.SeatName==seatNum)                            {                                if (((Label)sender).BackColor==Color.Yellow)                                {                                    int discount = cmbZk.SelectedItem == null ? 0 : Convert.ToInt32(cmbZk.SelectedItem);                                    Ticket tc = TicketUtil.CreateTicket(TicketType,(ScheduleItem)tn.Tag,item,discount,txtZs.Text);                                    if (TicketType=="赠票"&&txtZs.Text!=""||TicketType=="学生票"|| TicketType == "普通票")                                    {                                        cinema.SoldTickets.Add(tc);                                        MessageBox.Show(tc.Show(),"提示",MessageBoxButtons.OK);                                        tc.Print();                                        cinema.Save(cinema.SoldTickets);                                        Info();                                    }                                    else                                    {                                        MessageBox.Show("请输入赠票者姓名","提示",MessageBoxButtons.OK);                                    }                                }                                else                                {                                    MessageBox.Show("该座位已售出", "提示", MessageBoxButtons.OK);                                }                            }                        }                    }                }                else                {                    MessageBox.Show("请选择场次", "提示", MessageBoxButtons.OK);                }            }            else            {                MessageBox.Show("请选择电影", "提示", MessageBoxButtons.OK);            }        }        private void tvList_AfterSelect(object sender, TreeViewEventArgs e)        {            TreeNode tn = tvList.SelectedNode;            if (tn.Tag!=null)            {                lblName.Text = ((ScheduleItem)tn.Tag).Movie.MovieName;                lblDy.Text = ((ScheduleItem)tn.Tag).Movie.Director;                lblZy.Text = ((ScheduleItem)tn.Tag).Movie.Actor;                lblLx.Text = ((ScheduleItem)tn.Tag).Movie.MovieType.ToString();                lblYpj.Text = ((ScheduleItem)tn.Tag).Movie.Price.ToString();                lblSj.Text = ((ScheduleItem)tn.Tag).Time;                picZp.ImageLocation = ((ScheduleItem)tn.Tag).Movie.Poster;                Info();            }        }        private void rbZp_CheckedChanged(object sender, EventArgs e)        {            //赠票            txtZs.Text = "";            cmbZk.Text = null;            cmbZk.Enabled = false;            txtZs.Enabled = true;            if (rbZp.Checked)            {                TicketType = rbZp.Text;                lblYhj.Text = (0).ToString();            }        }        private void rbXs_CheckedChanged(object sender, EventArgs e)        {            //学生票            txtZs.Text = "";            cmbZk.Text=(7).ToString();            cmbZk.Enabled = true;            txtZs.Enabled = false;            if (rbXs.Checked)            {                TicketType = rbXs.Text;                if (tvList.SelectedNode!=null)                {                    TreeNode tn=tvList.SelectedNode;                    if (tn.Tag!=null)                    {                        lblYhj.Text = (((ScheduleItem)tn.Tag).Movie.Price * (Convert.ToInt32(cmbZk.SelectedItem) * 0.1)).ToString();                    }                }            }        }        private void cmbZk_SelectedIndexChanged(object sender, EventArgs e)        {            if (tvList.SelectedNode!=null)            {                TreeNode tn = tvList.SelectedNode;                if (tn.Tag!=null)                {                    (((ScheduleItem)tn.Tag).Movie.Price * (Convert.ToInt32(cmbZk.SelectedItem) * 0.1)).ToString();                }            }        }        private void 保存售票信息ToolStripMenuItem_Click(object sender, EventArgs e)        {            cinema.Save(cinema.SoldTickets);            MessageBox.Show("保存成功","提示",MessageBoxButtons.OK);        }        private void 继续购票ToolStripMenuItem_Click(object sender, EventArgs e)        {            //反序列化            tvList.Nodes.Clear();            cinema.SoldTickets = cinema.Load();            Infotable();            Info();        }        private void Infotable()        {            //更新treeView节点            string name = null;            TreeNode tn = null;            foreach (ScheduleItem item in cinema.Schedule.Items.Values)            {                if (name != item.Movie.MovieName)                {                    name = item.Movie.MovieName;                    tn = new TreeNode();                    tn.Text = item.Movie.MovieName;                    tvList.Nodes.Add(tn);                }                tn.Nodes.Add(item.Time).Tag = item;            }        }        private void 获取新放映列表ToolStripMenuItem_Click(object sender, EventArgs e)        {            //初始化售票出票集合            tvList.Nodes.Clear();            cinema.SoldTickets = new List<Ticket>();            Infotable();        }    }}

  

影院售票系统