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

影院售票系统

大致思路

技术分享

本项目共有十个类:
---------------
看电影类(Cimema)-->电影类(Movie)--->枚举lei(MovieType)
                  
                  放映计划类(Schedule)-->放映场次(ScheduleItem)
座位类(Seat)

电影票(Ticket)-->免费票(FreeTicket)-->学生票(StudentTicket)-->工厂类(TicketFactor

  技术分享

类图框架

 

 

 技术分享

类中的关系上面有说到

开始码代码

先上十大类

1.Cimema看电影类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace D_017{    //看电影  保存  放映计划(Schedule)和座位类(Seat)(顶级类)    //放映计划类里面有放映场次累    [Serializable]    public class Cimema    {        #region 初始化数据        //无参构造(当任何时候调用Cimema时都把 座位集合  票集合  放映计划初始化)        public Cimema()        {            Seats = new Dictionary<string, Seat>();            list = new List<Ticket>();            Schedule = new Schedule();        }        #endregion        #region 保存信息        //放映计划(释放以保存的放映计划)        public Schedule Schedule { get; set; }        //座位  座位集合属性(把20个座位的颜色和文本那拿到放到到集合中将来要做对比)        public Dictionary<string, Seat> Seats { get; set; }        //以出售电影飘得集合(把用户每点一次的购买结果保存到票的集合中将来要反序列化出来)        public List<Ticket> list { get; set; }         #endregion              #region 持久化到硬盘        //保存票的情况(会调用list中的值形成IO文本)        public void Save()        {        }        //读取售票情况(会把IO/序列中的值反馈到票的集合中读取)        public void Load()        {        }         #endregion           }}

2.Movie电影类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace D_017{    //电影类用来存放电影的属性 ,标上可被序列化的期望    [Serializable]  public   class Movie    {        #region 单机数状节点时在详情里面初始化        //电影名字        public string MovieName { get; set; }        //电影海报的名字        public string Postor { get; set; }        //导演        public string Director { get; set; }        //主演        public string Actor { get; set; }        //电影类型(枚举)将来要涉及到给枚举赋值的方法        public MovieType MovieType { get; set; }        //定价        public int Price { get; set; }         #endregion        #region 两个初始化构造        public Movie()        {        }        public Movie(string movieName, string poster, string director, string actor, MovieType movieType, int price)        {            this.MovieName = movieName;            this.Postor = poster;            this.Director = director;            this.Actor = actor;            this.MovieType = movieType;            this.Price = price;        }         #endregion    }}

3.Schedule放映计划

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;namespace D_017{    //放映 计划,保存影院当天的放映计划    [Serializable]    public class Schedule    {        #region 描述放映计划        //放映场次(时间   和  放映场次-->电影(Movie))        public Dictionary<string, ScheduleItem> Item { get; set; }        #endregion        #region 初始化放映计划        public Schedule()        {            // 放映计划中的放映列表            Item = new Dictionary<string, ScheduleItem>();        }        #endregion        #region 解析放映计划(每一个时间段对应一个放映计划)        //读取xml的方法        public void LoadItems()        {         //开始手打            XmlDocument doc = new XmlDocument();            doc.Load("ShowList.xml");            XmlNode root = doc.DocumentElement;            foreach (XmlNode item in root.ChildNodes)            {                //一个item就是一个放映计划和放映计划的名字                Movie movie = new Movie();//xml中转站到详细Movie类里面的属性                movie.MovieName = item["Name"].InnerText;                movie.Postor = item["Poster"].InnerText;                movie.Director = item["Director"].InnerText;                movie.Actor = item["Actor"].InnerText;                //前面解释道要给枚举赋值的方法手打一遍                movie.MovieType = (MovieType)Enum.Parse(typeof(MovieType), item["Type"].InnerText);                 movie.Price = Convert.ToInt32(item["Price"].InnerText);                //如果你仔细看xml文件会发现 有两个节点的名字相同                //Analysis:用三个foreach循环遍历出来                //Analysis:下面这种方法                foreach (XmlNode it in item["Schedule"].ChildNodes)                {                   //一个it就是一个放映场次  new ScheduleItem();                    ScheduleItem sh=new ScheduleItem();                    sh.Time=it.InnerText;                    sh.Movie=movie;                    Item.Add(sh.Time,sh);//放映计划集合               }            }        }        #endregion    }}

4.ScheduleItem放映场次

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace D_017{    //影院每天放映计划的场次 ,保存每场电影的信息(时间 和  电影--->属性)    [Serializable]    public class ScheduleItem    {        #region 属性        //放映时间        public string Time { get; set; }        //本场所放映的电影        public Movie Movie { get; set; }         #endregion        #region 两个构造        public ScheduleItem()        {        }        public ScheduleItem(string time, Movie movie)        {            this.Time = time;            this.Movie = movie;        }         #endregion    }}

5.Seat座位

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace D_017{    //保存影院的座位信息    [Serializable]   public   class Seat    {        #region 单个座位信息        //座位卖出的状态颜色        public System.Drawing.Color Color { get; set; }        //座位号        public string SeatNum { get; set; }         #endregion        #region 两个构造        public Seat()        {        }        public Seat(string seatnum, System.Drawing.Color color)        {            this.SeatNum = seatnum;            this.Color = color;        }         #endregion    }}

6.MovieType枚举

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace D_017{    //枚举类 电影类型  英文的    [Serializable]    public enum MovieType    {        Comedy,//喜剧片        War,//战争片        Romance,//浪漫篇        Action,//动作片        Cartoon,//卡通片        Thriller,//惊悚片        Adventure //冒险片    }}

7.Ticket电影票父类

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace D_017{    //电影票父类。保存电影票信息    [Serializable]   public class Ticket    {        #region 售出票的属性(工厂调用)        //放映场次        public ScheduleItem ScheduleItem { get; set; }        //座位对象        public Seat Seat { get; set; }        //优惠价        public int Price { get; set; }         #endregion        #region 父类玩票的方法        //计算票价的方法        public virtual void CalcPrice()        {            this.Price = ScheduleItem.Movie.Price;        }        //打印售票信息的虚方法        public virtual void Print()        {        }        //显示当前售出票的虚方法        public virtual void Show()        {        }         #endregion        #region 两个构造        public Ticket()        {        }        public Ticket(ScheduleItem scheduleItem, Seat seat, int price)        {            this.ScheduleItem = scheduleItem;            this.Seat = seat;            this.Price = price;        }         #endregion    }}

8.FreeTicket赠票:Ticket

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace D_017{    //免费票子类.继承父类Ticket,保存特殊的增票信息    [Serializable]   public  class FreeTicket:Ticket    {        #region 子类特有的名字属性(工厂调用)        //获得赠票者的名字属性        public string CustomerName { get; set; }         #endregion        #region 重写        //重写父类计算票价的方法        public override void CalcPrice()        {            this.Price = ScheduleItem.Movie.Price;        }        //重写父类打印售票信息的方法        public override void Print()        {            base.Print();        }        //重写父类显示当前售出票的信息        public override void Show()        {            base.Show();        }         #endregion        #region 两个构造         public FreeTicket()        {        }        public FreeTicket(ScheduleItem scheduleItem,Seat seat,string customername)        {            this.ScheduleItem = scheduleItem;            this.Seat = seat;            this.CustomerName = customername;        }        #endregion    }}

9.StudentTicket学生票:Ticket

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace D_017{    //学生票子类,继承父类 Ticket  保存特殊的学生票信息    [Serializable]    public class StudentTicket:Ticket    {        #region 子类特有的属性折扣(工厂调用)        //学生票的折扣        public int Discount { get; set; }         #endregion        #region 重写        //重写父类计算票价的方法        public override void CalcPrice()        {            this.Price = ScheduleItem.Movie.Price;        }        //重写父类打印售票信息的方法        public override void Print()        {            base.Print();        }        //重写父类显示当前售出票的信息        public override void Show()        {            base.Show();        }         #endregion        #region 两个构造        public StudentTicket()        {        }        public StudentTicket(ScheduleItem scheduleItem, Seat seat, int discount)        {            this.ScheduleItem = scheduleItem;            this.Seat = seat;            this.Discount = discount;        }         #endregion    }}

10.TicketFactory工厂类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace D_017{    //工厂类    [Serializable]    public class TicketFactory    {        //创建电影票        public static Ticket CreateTicket(ScheduleItem scheduitem, Seat seat, int discount, string CustomeName, string type)        {            Ticket tick = null;            switch (type)            {                case "student":                    tick = new StudentTicket(scheduitem, seat, discount);                    break;                case "free":                    tick = new FreeTicket(scheduitem, seat, CustomeName);                    break;                case "":                    tick = new Ticket(scheduitem, seat, discount);                    break;            }            return tick;        }    }

 

代码有些还没有完成不过最难的部分已经概括了(有注释)

主窗体中的代码

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 D_017{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        //用来存放lab的集合左边是座位号,右边是lab(用来刷新)        Dictionary<string, Label> labs = new Dictionary<string, Label>();        Cimema cimema = new Cimema();        private void Form1_Load(object sender, EventArgs e)        {            comboBox1.Enabled = false;            textBox1.Enabled = false;            InitSeats();        }        //动态加载lab控件        private void InitSeats()        {            //逻辑循环创建Lable            for (int i = 1; i <= 5; i++)            {                for (int j = 1; j <= 4; j++)                {                    Label lab = new Label();                    lab.Text = j + "-" + i;                    lab.BackColor = Color.Yellow;                    lab.Width = 60;                    lab.Font = new Font("楷体", 14);                    lab.TextAlign = ContentAlignment.MiddleCenter;                    //以上的用来设置字体                    //这个Location需要想一会                    lab.Location = new Point(20 + (i - 1) * 90, 20 + (j - 1) * 55);                    //+=注册事件  -=注销事件                    lab.Click += lab_Click;                    //把每一个Seat中的   座位号  和  颜色  赋值                    Seat seat = new Seat(lab.Text, BackColor);                    //把20个lable 不同的座位和颜色 保存到 Cimema 中的Seat集合中                    //现在的集合中的                     颜色都一样                     cimema.Seats.Add(lab.Text, seat);  //保存用户单机改变颜色的lable                    //又把20个lable存到了labs集合当中    颜色也都是一样的  存了两便                    labs.Add(lab.Text, lab);                    //绑定到tabPage2控件上                    tabPage2.Controls.Add(lab);                }            }        }        //四大模块里面最难的一块(每选中一个lable就把当前的售票信息添加到Cimame 中的list单列集合中  list = new List<Ticket>();)        string type = "";        void lab_Click(object sender, EventArgs e)        {                     //判断是那种票            if (radioButton2.Checked)            {                type = "free";            }            else if (radioButton3.Checked)            {                type = "student";             }           //首先获取当前lable的文本值            Label l = (Label)sender;            //调用工厂函数  根据时间  座位号  价格 折扣 类型  拿到对应的场次 票               Ticket tick = TicketFactory.CreateTicket(cimema.Schedule.Item[label14.Text], cimema.Seats[l.Text], int.Parse(label16.Text), textBox1.Text, type);            //消息框            DialogResult result = MessageBox.Show("是否购买?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);           //如果选择了是            if (result == DialogResult.Yes)            {                //持久化到硬盘中改变用户选中lable的颜色                cimema.Seats[l.Text].Color = Color.Red;                //更新座位                foreach (string item in cimema.Seats.Keys)                {                    labs[item].BackColor = cimema.Seats[item].Color;                }                //计算价格                tick.CalcPrice();                cimema.list.Add(tick);//把卖出的加到集合中                label16.Text = tick.Price.ToString();//把价格加上去                tick.Print();//打印售票的方法            }        }                        //获取放映场次        private void 获取ToolStripMenuItem_Click(object sender, EventArgs e)        {            //每次调用都清除一遍            treeView1.Nodes.Clear();            //调用以初始化好放映列表            cimema.Schedule.LoadItems();            //给treeview添加子节点的声明  xxx.Nodes.Add();            TreeNode tr=null;            //制空一个字符串            string name="";            //遍历看电影(Cimema)中的放映场次(看我里面有初始化吧(相当于调用了SchedulItem)中的集合..Item(以付值))            foreach (ScheduleItem item in cimema.Schedule.Item.Values)            {                //如果Movie中的电影名字不是空的(不相等)                //如果名字不相等只绑定名字                if (item.Movie.MovieName != name)                {                    //把item.MovieName添加到TreeNode中                    tr = new TreeNode(item.Movie.MovieName);                    //绑定到treeView1                    treeView1.Nodes.Add(tr);                }                //如果名字相等就绑定时间                TreeNode tree = new TreeNode(item.Time);                //偷偷的把放映场次绑定到Tag中                tree.Tag = item;                tr.Nodes.Add(tree);                name = item.Movie.MovieName;            }                  }        //treeView选中事件改变详细框里面的值        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)        {                //获取treeNode选中的Tag偷偷隐藏得值                TreeNode node = treeView1.SelectedNode;                //强转成ScheduleItem类型 不要转成Movie 要不然时间不好拿到                ScheduleItem sh = (ScheduleItem)node.Tag;                //一次给详细信息里面的控件赋值                label2.Text = sh.Movie.MovieName;                pictureBox1.Image = Image.FromFile(sh.Movie.Postor);                label11.Text = sh.Movie.Director;                label12.Text = sh.Movie.Actor;                label13.Text = sh.Movie.MovieType.ToString();                label14.Text = Convert.ToString(sh.Time);                label15.Text = sh.Movie.Price.ToString();                //清空座位                foreach (Seat item in cimema.Seats.Values)                {                    item.Color = Color.Yellow;                }                foreach (Ticket it in cimema.list)                {                    foreach (Seat seat in cimema.Seats.Values)                    {                        if (it.ScheduleItem.Time == label14.Text && it.Seat.SeatNum == seat.SeatNum)                        {                            seat.Color = Color.Red;                        }                    }                }               //更新座位                foreach (string item in cimema.Seats.Keys)                {                    labs[item].BackColor = cimema.Seats[item].Color;                }        }        //RadioButton1选中事件改变特殊票框的值        private void radioButton1_CheckedChanged(object sender, EventArgs e)        {            comboBox1.Text = "0";            textBox1.Text = "0";            comboBox1.Enabled = false;            textBox1.Enabled = false;            label16.Text = "0";        }        //RadioButton2选中事件改变特殊票框的值        private void radioButton2_CheckedChanged(object sender, EventArgs e)        {            comboBox1.Enabled = false;            comboBox1.Text = "0";            textBox1.Enabled = true;            textBox1.Text = "0";            label16.Text = "0";        }        //RadioButton3选中事件改变特殊票框的值        private void radioButton3_CheckedChanged(object sender, EventArgs e)        {            comboBox1.Enabled = true;            comboBox1.Text = "7";            textBox1.Enabled = false;            textBox1.Text = "";            int _1 = int.Parse(comboBox1.Text);            int _2 = int.Parse(label15.Text);            label16.Text = (_1 * _2 / 10).ToString();        }    }}

 

影院售票系统