C#窗体项目————影院售票系统

影院售票系统(一)


现在学习到了C#的最后一本书,有一个窗体的结业案例。

  1. 先上图C#窗体项目————影院售票系统_第1张图片
    运行起来是这样的
    C#窗体项目————影院售票系统_第2张图片
    点击座位号弹出是否购票
    C#窗体项目————影院售票系统_第3张图片
    买了之后座位变红色C#窗体项目————影院售票系统_第4张图片
    对于老手来说可能so easy;但对我这菜鸟来说有点复杂,主要是面向对象思想与泛型集合的结合。
    话不多说,先上类图
    C#窗体项目————影院售票系统_第5张图片
    2.类介绍
    其中包含了10个类
    ticket电影票父类
  //电影票父类
  public  class Ticket
    {
      //放映场次
        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("      xx影院(普通)     ");
            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("已售出");
        }
        public Ticket()
        {

        }
        public Ticket(ScheduleItem scheduleItem, Seat seat)
        {
            ScheduleItem = scheduleItem;
            Seat = seat;
        }

  }

学生票子类

 //学生票
   public class StudentTicket:Ticket
    {
       //折扣
        public int Discount { get; set; }
        //计算票价
        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("     xx影院 (学生)    ");
            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 + "折学生票!");
        }
        public StudentTicket()
        {

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

免费票子类

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("     xx影院 (赠票)");
            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);
        }
    }

电影类

//电影类
    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 class Seat
    {
       //座位号
        public string SeatNum { get; set; }
       //座位卖出状态颜色
        public Color Color { get; set; }
        public Seat() { }
        public Seat(string seatnum,Color c) 
        {
            this.SeatNum = seatnum;
            this.Color = c;
        }
   }

放映计划类
这里包含了我注释掉的代码

//放映计划类
   public class Schedule
    {
       public Schedule() 
       {
            //放映场次属性
       Items = new Dictionary();
       }
       private Dictionary items;
       public Dictionary Items
       {
           get
           {
               return items;
           }

           set
           {
               items = value;
           }
       }
       //读取Xml文件的方法
       public void LoadItems()
       {
           XmlDocument doc = new XmlDocument();
           doc.Load(@"E:\c#\影院售票系统\影院售票系统\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);
               }
              
           }
       }

      //读取xml文件获取放映计划集合的方法
     /*  public void LoadItems() 
       {
           if (Items == null)
           {
               Items = new Dictionary();
           }
           XmlDocument xml = new XmlDocument();
           xml.Load(@"E:\c#\影院售票系统\影院售票系统\ShowList.xml");
           XmlElement xe = xml.DocumentElement;
           foreach(XmlNode xn in xe.ChildNodes)
           {
               
               ScheduleItem sc = new ScheduleItem();
               Movie m = new Movie();
               m.MovieName = xn["Name"].InnerText;
               m.Poster=xn["Poster"].InnerText;
               m.Diretor=xn["Director"].InnerText;
               m.Actor=xn["Actor"].InnerText;
               //foreach(MovieType mt in Enum.GetValues(typeof(MovieType)))
               //{
                   //if (mt.ToString().Equals(xn["Type"].InnerText)) 
                   //{
                      m.MovieType=xn["Type"].InnerText;
               //    }
               //}              
               m.Price=Convert.ToInt32(xn["Price"].InnerText);
               sc.Movie = m;         
               foreach(XmlNode x in xn.ChildNodes)
               {
                   if (x.Name == "Schedule")
                   {
                     sc.Time=x["Item"].InnerText;
                   }
               }
               Items.Add(m.MovieName,sc);
           }
       }
      */
   }

影院每天放映计划的场次,保存每场电影信息

  //每天放映场次的计划
  public  class ScheduleItem
    {
      //放映时间属性
        public string Time { get; set; }
      //本场所放映电影属性
        public Movie Movie { get; set; }
        public ScheduleItem()
        {
            Movie = new Movie();
        }
  }

影院类

 //影院类
   public class Cinema
    {
       public Cinema()
       {

           Seats = new Dictionary();
           SoldTickets = new List();
           Schedule = new Schedule();
       }
       public Dictionary Seats;

       //放映计划
       public Schedule Schedule { get; set; }
        
       //已售出电影票的集合
       public List SoldTickets;

       //保存售票情况
       public void Save() 
       {
           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();
            }
        
       }
   }

最后是工具类

//工具类
   public class TicketUtil
    {
       //创建电影票的方法
       public static Ticket CreateTicket(ScheduleItem item, Seat seat, int discount, string customer, string type) 
       {
           Ticket ticket = null;
           switch (type)
           {
               case "student":
                   ticket = new StudentTicket(item, seat, discount);
                   break;
               case "free":
                   ticket = new FreeTicket(item, seat, customer);
                   break;
               case "putong":
                   ticket = new Ticket(item, seat);
                   break;
           }
           return ticket;
       }
    }

你可能感兴趣的:(C#,C#窗体,面向对象,泛型集合,xml)