深入.NET编程 第十一章 影院售票系统

深入.NET编程 第十一章 影院售票系统_第1张图片 深入.NET编程 第十一章 影院售票系统_第2张图片 深入.NET编程 第十一章 影院售票系统_第3张图片 深入.NET编程 第十一章 影院售票系统_第4张图片 深入.NET编程 第十一章 影院售票系统_第5张图片 深入.NET编程 第十一章 影院售票系统_第6张图片 深入.NET编程 第十一章 影院售票系统_第7张图片
主窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using Microsoft.SqlServer.Server;
using Microsoft.Win32;

namespace 影院售票系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string key = null;
        Dictionary labels = new Dictionary();
        private Cinema cinma = new Cinema();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.label10.Text = "";
            this.label11.Text = "";
            this.label12.Text = "";
            this.label13.Text = "";
            this.label14.Text = "";
            this.label15.Text = "";
            this.label16.Text = "";
            this.textBox1.Enabled = false;
            this.comboBox1.Enabled = false;
            this.radioButton1.Checked = true;

            //cinema = new Cinema();
            //初始化放映厅座位

            Intile();
       
        }

        //初始化TreeView控件

        //treeview 绑定
        public void InitTreeView()
        {

            this.treeView1.Nodes.Clear();
            string movieName = null;
            TreeNode tn = null;
            Schedule s = new Schedule();
            s.LoadItems();
           
            foreach (KeyValuePair item in s.Items)
            {

                //如果不存在此电影节点,创建电影节点 
                if (item.Value.Movie.MovieName != movieName)
                {
                    tn = new TreeNode(item.Value.Movie.MovieName);
                    //tn.Tag = item.Movie;
                    this.treeView1.Nodes.Add(tn);
                }
                //增加场次时间节点 
                TreeNode time = new TreeNode(item.Value.Time);
                tn.Nodes.Add(time);
                //获取当前场次的电影名字,重新遍历 
                movieName = item.Value.Movie.MovieName;

            }

        }


        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {


        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

       private ScheduleItem sch = null;

        //点击座位(购票 变色)
        private void label_Click(object sender, EventArgs e)
        {

                Label lbl = sender as Label;
                string customerName = this.textBox1.Text.ToString();
                int discount = 0;
                string type = "";
              //如果是学生票
                if (this.radioButton3.Checked)
                {
                    type = "StudentTickey";
                   
                        if (comboBox1.Text == null)
                        {
                            MessageBox.Show("请输入折扣数!", "提示");
                            return;
                        }
                       else
                    {
                        discount = int.Parse(this.comboBox1.Text);
                    }
                    
                }
                    //如果是赠票
                else if (this.radioButton2.Checked)
                {
                    if (string.IsNullOrEmpty(this.textBox1.Text))
                    {
                        MessageBox.Show("请输入赠票者姓名!", "提示");
                        return;
                    }
                    type = "FreeTicket";
                }
                key = treeView1.SelectedNode.Text;
            MessageBox.Show(key.ToString());
                Ticket newTicket = TicketUtil.CreareTicket(sch, cinma.Seats[lbl.Text], discount,
                    customerName, type);
            if (cinma.Seats[lbl.Text].Color == Color.Yellow )
            {
                DialogResult result = MessageBox.Show("是否购买?", "提示", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Information
                    );
               if (result == DialogResult.OK)
                {
                    newTicket.CalcPrice();
                    cinma.soldTickets.Add(newTicket);
                    //UpdateSeat();
                    label16.Text = newTicket.Price.ToString();
                    newTicket.Print();
                    cinma.Seats[lbl.Text].Color = Color.Red;
                    lbl.BackColor = Color.Red;
                }
            }
            else
            {
                foreach (Ticket tickets in cinma.soldTickets)
                {
                    if (tickets.Seat.SeatNum == lbl.Text && tickets.ScheduleItem.Time == treeView1.SelectedNode.Text &&
                        tickets.ScheduleItem.Movie.MovieName == treeView1.SelectedNode.Parent.Text)
                    {
                        tickets.Show();
                    }
                }
            }

        }




//private void 刷新座位ToolStripMenuItem_Click(object sender, EventArgs e)
//        {
            

//        }
      //更新调用座位的方法
        //public void UpdateReadSeat(List list )
        //{
        //    if (list.Count != 0)
        //    {
        //        foreach (Ticket item in list)
        //        {
        //            foreach (string str in labels.Keys)
        //            {
        //                if (item.Seat.SeatNum.Equals(str))
        //                {
        //                    foreach (Control my in this.tabPage2.Controls)
        //                    {
        //                        if (my.Text.Equals(item.Seat.SeatNum))
        //                        {
        //                            my.BackColor = Color.Red;
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //    }
        //}

        public void UpdateYellow()
        {
            foreach (Control item in this.tabPage2.Controls)
            {
                item.BackColor = Color.Yellow;
            }
        }
        //public void ClearSeat()
        //{
        //     foreach (Seat seat in cinma.Seats.Values)
        //    {
        //        seat.Color = Color.Green;
        //    }
            
        //}

        //加载座位+变色
        public void Intile()
        {
            Label label;
            Seat seat;

            for (int j = 1; j <= 6; j++)
            {
                for (int i = 1; i <= 8; i++)
                {
                    label = new Label();
                   
                    label.Text = j.ToString() + "-" + i.ToString();
                    label.AutoSize = false;
                    label.Size = new System.Drawing.Size(50, 25);
                    label.Font = new Font("楷书", 15, FontStyle.Bold); 
                    label.BackColor = Color.Yellow;
                    label.Click += label_Click;
                    label.Location = new Point(-50+(i * 70), -20+(j * 40));
                    tabPage2.Controls.Add(label);
                    labels.Add(label.Text,label);
                    seat = new Seat((j).ToString() + "-" + (i).ToString(), Color.Yellow);
                    cinma.Seats.Add(seat.SeatNum, seat);
                }

            }
        }
        //清空控件
        public void Clear()
        {
            this.textBox1.Text = "";
            this.comboBox1.Text = "";

        }
      //赠票
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox1.Enabled = true;
            this.comboBox1.Enabled = false;
            this.comboBox1.Text = "";                                                                                                                                                                                                                                                                                                                                                                                 
            this.label16.Text = "0";
        }
        //学生票
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox1.Enabled = false;
            this.textBox1.Text = "";
            this.comboBox1.Enabled = true;
            this.comboBox1.Text = "7";
            if (this.label15.Text != "")
            {
                int price = int.Parse(this.label15.Text);
                int discount = int.Parse(this.comboBox1.Text);
                this.label16.Text=(price*discount/10).ToString();
             
            }
         
        }

      
        private void 获取ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitTreeView();
        }

        //public void ClearSeat1()
        //{
        //    foreach (Seat seat in cinma.Seats.Values)
        //    {
        //        seat.Color = Color.Green;
        //    }
        //}

        //右侧绑定的传值
        private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
        {
            Schedule a = new Schedule();
            a.LoadItems();
            string time = this.treeView1.SelectedNode.Text;
            string name = this.treeView1.SelectedNode.Parent.Text;
            cinma.Schedule = a;
            foreach (KeyValuePair item in a.Items)
            {
                if (treeView1.SelectedNode.Text == item.Key)
                {
                    label10.Text = item.Value.Movie.MovieName;
                    label11.Text = item.Value.Movie.Director;
                    label12.Text = item.Value.Movie.Actor;
                    label13.Text = item.Value.Movie.MovieType.ToString();
                    label14.Text = item.Key;
                    label15.Text = item.Value.Movie.Price.ToString();
                    this.pictureBox1.Image = Image.FromFile(item.Value.Movie.Poster);
                }
                if (time.Equals(item.Value.Time))
                {
                    sch = item.Value;
                }
            }
            //将所有座位设置为黄色
           
                 foreach (Control control in tabPage2.Controls)
                 {

                         control.BackColor = Color.Yellow;
                  
                 }
           
                
            
            foreach (ScheduleItem item in cinma.Schedule.Items.Values)
            {
                if (item.Movie.MovieName.Equals(name))
                {
                    foreach (Ticket ticket in cinma.soldTickets)
                    {
                        if (ticket.ScheduleItem.Time.Equals(time))
                        {
                            foreach (string num in labels.Keys)
                            {
                                if (num.Equals(ticket.Seat.SeatNum))
                                {
                                    labels[num].BackColor = Color.Red;
                                }
                            }
                        }
                    }
                }
            }

           
            //在已售出的票中循环判断
            //foreach (Ticket ticket in cinma.soldTickets)
            //{
            //    foreach (Seat seat in this.cinma.Seats.Values)
            //    {
            //        //场次相同且座位号相同
            //        if (ticket.ScheduleItem.Time == key && ticket.Seat.SeatNum == seat.SeatNum)
            //        {
            //            //更新座位颜色
            //            seat.Color = Color.Red;
            //        }


            //    }
            //}
            //// 将座位颜色更新到Label上显示
            //foreach (Seat seat in cinma.Seats.Values)
            //{
            //    foreach (Label lbl in labels.Values)
            //    {
            //        // 座位号相同证明是对应Label
            //        if (lbl.Text == seat.SeatNum)
            //        {
            //            lbl.BackColor = seat.Color;
            //        }
            //    }
            //}


        }




        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void 继续购票ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyLoad();
        }
        //继续购票
        public void MyLoad()
        {
            //StreamReader reader = new StreamReader("soldTickets.txt", Encoding.Default);
            //string line = reader.ReadLine();
            //String[] pv;
            //Ticket ticket = null;
            //while (line.Trim() != "The End")
            //{
            //    pv = line.Split('|');
            //    string index = pv[1];
            //    ticket = TicketUtil.CreareTicket(cinma.Schedule.Items[index], cinma.Seats[pv[2]], int.Parse(pv[4]), pv[5], pv[6]);

            //}

            InitTreeView();
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream("Ying.bin", FileMode.Open);
            List list = (List)bf.Deserialize(fs);
            cinma.soldTickets = list;
            fs.Close();

        }

        private void 保存ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
           Save();
        }

        //保存
        public void Save()
        {
            //FileStream fs = new FileStream("soldTickets.txt", FileMode.OpenOrCreate);
            //StreamWriter sw = new StreamWriter(fs, Encoding.Default);

            //for (int i = 0; i < cinma.soldTickets.Count; i++)
            //{
            //    Ticket ticket = cinma.soldTickets[i];
            //    string type = "";
            //    int discount = 0;
            //    string name = "";
            //    if (cinma.soldTickets[i] is StudentTicket)
            //    {
            //        type = "stu";
            //        discount = ((StudentTicket)cinma.soldTickets[i]).Discount;

            //    }
            //    else if (cinma.soldTickets[i] is FreeTicket)
            //    {
            //        type = "free";
            //        name = ((FreeTicket)cinma.soldTickets[i]).Discount;
            //    }
            //    else
            //    {
            //        type = "normal";
            //    }
            //    string info = string.Format("{0}{1}{2}{3}{4}{5}{6}", cinma.soldTickets[i].ScheduleItem.Movie.MovieName,
            //        cinma.soldTickets[i].ScheduleItem.Time, cinma.soldTickets[i].Seat.SeatNum,
            //        cinma.soldTickets[i].Price, discount, type, name);
            //    sw.WriteLine(info);
            //}
            //sw.WriteLine("The End");
            //sw.Close();
            //fs.Close();
            //MessageBox.Show("保存成功!!!");

            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream("Ying.bin", FileMode.OpenOrCreate);
            bf.Serialize(fs, cinma.soldTickets);
            fs.Close();
            MessageBox.Show("success");

        }
        }


       

    }

影院类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using  System.IO;
using System.Windows.Forms;

namespace 影院售票系统
{
    [Serializable]
   public class Cinema
    {
       public Dictionary Seats=new Dictionary(); 
       public Schedule Schedule { get; set; }
       public  List soldTickets { get; set; }
       private Cinema cinma=null;
       public  Cinema()
           {
               soldTickets=new List();
           }
       
       public void Save()
       {
           //FileStream fs = new FileStream("soldTickets.txt", FileMode.OpenOrCreate);
           //StreamWriter sw = new StreamWriter(fs, Encoding.Default);

           //for (int i = 0; i < cinma.soldTickets.Count; i++)
           //{
           //    Ticket ticket = cinma.soldTickets[i];
           //    string type = "";
           //    int discount = 0;
           //    string name = "";
           //    if (cinma.soldTickets[i] is StudentTicket)
           //    {
           //        type = "stu";
           //        discount = ((StudentTicket)cinma.soldTickets[i]).Discount;

           //    }
           //    else if (cinma.soldTickets[i] is FreeTicket)
           //    {
           //        type = "free";
           //        name = ((FreeTicket)cinma.soldTickets[i]).Discount;
           //    }
           //    else
           //    {
           //        type = "normal";
           //    }
           //    string info = string.Format("{0}{1}{2}{3}{4}{5}{6}", cinma.soldTickets[i].ScheduleItem.Movie.MovieName,
           //        cinma.soldTickets[i].ScheduleItem.Time, cinma.soldTickets[i].Seat.SeatNum,
           //        cinma.soldTickets[i].Price, discount, type, name);
           //    sw.WriteLine(info);
           //}
           //sw.WriteLine("The End");
           //sw.Close();
           //fs.Close();
           //MessageBox.Show("保存成功!!!");

           BinaryFormatter bf = new BinaryFormatter();
           FileStream fs = new FileStream("Ying.bin", FileMode.OpenOrCreate);
           bf.Serialize(fs, cinma.soldTickets);
           fs.Close();
           MessageBox.Show("success");

       }

      
    }
}

赠票

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using  System.IO;
using System.Windows.Forms;

namespace 影院售票系统
{
    [Serializable]
   public class FreeTicket:Ticket
    {
       public FreeTicket()
       {
       }
       public string CustomerName{ get; set; }
       public string Discount { get; set; }

      
       public FreeTicket(ScheduleItem sch, Seat seat, string name)
       {
           this.Seat = seat;
           this.CustomerName = name;
           this.ScheduleItem = sch;
       }
       public override void CalcPrice()
       {
           this.Price = 0;
       }

       public override void Print()
       {
           string info = string.Format("************************************************\n\t青鸟影院(赠票)\n------------------------------------------------\n电影名:\t{0}\n时间:\t{1}\n座位号:\t{2}\n姓名:\t{3}\n************************************************", this.ScheduleItem.Movie.MovieName, this.ScheduleItem.Time, this.Seat.SeatNum, this.CustomerName);
           MessageBox.Show(info);
           //存到文件中
           string fileName = this.ScheduleItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";
           FileStream fs = new FileStream(fileName, FileMode.Create);
           StreamWriter sw = new StreamWriter(fs);
           sw.Write(info);
           sw.Close();
           fs.Close();
       }

       public override void Show()
       {
           MessageBox.Show("已售出!\n赠票!");
       }
    }
}

电影
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 影院售票系统
{
    [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  string Time { get; set; }
       public MovieType MovieType { get; set; }
       public int Price { get; set; }
    }
}

类型  枚举
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 影院售票系统
{
    [Serializable]
    public enum MovieType
    {
        Comedy,War,Romance,Action,Cartoon,Thriller,Adventure
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace 影院售票系统
{
    [Serializable]
   public class Schedule
    {
       public Dictionary Items=new Dictionary();

       public void LoadItems()
       {
           Items.Clear();
           XmlDocument xml=new XmlDocument();
           xml.Load("ShowList.xml");
           XmlElement root = xml.DocumentElement;
           foreach (XmlNode item in root.ChildNodes)
           {
               
               Movie movie=new Movie();
               movie.MovieName = item["Name"].InnerText;
               movie.Poster = item["Poster"].InnerText;
               movie.Director = item["Director"].InnerText;
               movie.Actor = item["Actor"].InnerText;
               switch (item["Type"].InnerText)
               {
                   case "Action":
                       movie.MovieType = MovieType.Action;
                       break;
                   case "Comedy":
                       movie.MovieType = MovieType.Comedy;
                       break;
                   case "Romance":
                       movie.MovieType = MovieType.Romance;
                       break;
                   case "War":
                       movie.MovieType = MovieType.War;
                       break;
                   case "Cartoon":
                       movie.MovieType = MovieType.Cartoon;
                       break;
                   case "Thriller":
                       movie.MovieType = MovieType.Thriller;
                       break;
                   case "Adventure":
                       movie.MovieType = MovieType.Adventure;
                       break;
               }
               movie.Price = Convert.ToInt32(item["Price"].InnerText);
               if (item["Schedule"].HasChildNodes)
               {
                   foreach (XmlNode item2 in item["Schedule"].ChildNodes)
                   {
                     
                       if (item2.Name == "Item")
                       {
                           ScheduleItem schItem = new ScheduleItem();
                           schItem.Time = item2.InnerText;
                          // MessageBox.Show(schItem.Time);
                           schItem.Movie = movie;
                           Items.Add(schItem.Time, schItem);
                       }
                     
                   }
               }
           }

       }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Server;

namespace 影院售票系统
{
    [Serializable]
    public class ScheduleItem
    {
        public string Time { get; set; }
        public Movie Movie { get; set; }
        private List _soldTickets=new List();
        private Dictionary _seats=new Dictionary();

        public Dictionary Seats
        {
            get { return _seats; }
            set { _seats = value; }
        }
    }
}

座位
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace 影院售票系统
{
    [Serializable]
   public class Seat
    {
       public Seat()
       {
           
       }  
       public Seat(string seatNum, Color color )
       {
           this.SeatNum = seatNum ;
           this.Color = color;
       }
      private string _seatNum;
 /// 
 /// 座位号
 /// 
 public string SeatNum
 {
 get { return _seatNum; }
 set { _seatNum = value; }
 }
 private Color _color;
 /// 
 /// 座位卖出状态颜色
 /// 
 public Color Color
 {
 get { return _color; }
 set { _color = value; }
    }
   }

    
    }


学生票
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 影院售票系统
{
    [Serializable]
    public class StudentTicket : Ticket
    {
        public StudentTicket()
        {

        }

        public StudentTicket(ScheduleItem sch, Seat seat, int discount) : base(sch, seat)
        {
            this.Discount = discount;
        }

        public int Discount { get; set; }

        public override void CalcPrice()
        {
            this.Price = this.ScheduleItem.Movie.Price*Discount/10;
        }

        public override void Print()
        {
            string info =
                string.Format(
                    "************************************************\n\t青鸟影院(学生)\n-" +
                    "-----------------------------------------------\n电影名:\t{0}\n时间:\t{1}\n" +
                    "座位号:\t{2}\n价格:\t{3}\n************************************************",
                    this.ScheduleItem.Movie.MovieName, this.ScheduleItem.Time, this.Seat.SeatNum, this.Price);
            MessageBox.Show(info);
            string fileName = this.ScheduleItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";
            FileStream fs = new FileStream(fileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(info);
            sw.Close();
            fs.Close();
        }
    



     public override void Show()
     {
         string info = string.Format("已售出!\n{0}折学生票!!!", this.Discount);
         MessageBox.Show(info)
             ;
     }
    }
}

普通票
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 影院售票系统
{
    [Serializable]
   public class Ticket
    {
       public Ticket()
       {
           
       }

       public Ticket(ScheduleItem sch, Seat seat)
       {
           this.ScheduleItem = sch;
           this.Seat = seat;
       }
        public ScheduleItem ScheduleItem { get; set; }
        public Seat Seat { get; set; }
        public int Price { get; set; }

        public virtual void CalcPrice()
        {
            this.Price = ScheduleItem.Movie.Price;
        }

        public virtual void Print()
        {
            string info = string.Format("*******************************\n\t青鸟影院\n--" +
                                        "----------------------------------------------\n电影名:" +
                                        "\t{0}\n时间:\t{1}\n座位号:\t{2}\n价格:\t{3}\n****" +
                                        "********************************************", 
                                        this.ScheduleItem.Movie.MovieName, this.ScheduleItem.Time,
                                        this.Seat.SeatNum, this.Price);
            MessageBox.Show(info);
            //存到文件中
           // MessageBox.Show(info);
            string fileName=this.ScheduleItem.Time.Replace(":","-")+" "+this.Seat.SeatNum+this.Seat.SeatNum+".txt";
                FileStream fs=new FileStream(fileName,FileMode.Create);
            StreamWriter sw=new StreamWriter(fs);
            sw.Write(info);
            sw.Close();
            fs.Close();
        }
        public virtual void Show()
        {
            string info = string.Format("已售出!\n普通票!!!");
            MessageBox.Show(info);
        }
    }
}

工具类
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 影院售票系统
{
    [Serializable]
   public class TicketUtil
    {

       public static Ticket CreareTicket(ScheduleItem sch,Seat seat,int discount,string customerName,string type)
       {
           Ticket ticket = null;
           switch (type)
           {
               case "StudentTickey":
                   ticket = new StudentTicket(sch, seat, discount);
                   break;
               case "FreeTicket":
                   ticket=new FreeTicket(sch,seat,customerName);
                   break;
               default:
                   ticket=new Ticket(sch,seat);
                   break;
           }
           return ticket;
       }
    }
}

你可能感兴趣的:(深入.NET编程 第十一章 影院售票系统)