深入.NET 青鸟影院系统

按照要求创建类    
    /// <summary>
    /// 创建电影票工具类
    /// 使用简单工厂模式创建票
    /// </summary>
    public class TicketUtil
    {
        public static Ticket CreateTicket(ScheduleItem item,Seat seat,string customerName,double discount,string type)
        {
            Ticket ticket = null;
            switch (type)
            {
                case "normal":
                    ticket = new Ticket(item, seat);
                    break;
                case "free":
                    ticket = new FreeTicket(customerName, item, seat);
                    break;
                case "student":
                    ticket = new StudentTicket(item, seat, discount);
                    break;
            }
            return ticket;
        }



    /// 电影票父类
    /// </summary>
    public class Ticket
    {
        public Ticket() { }
        public Ticket(ScheduleItem scheduleItem,Seat seat)
        {
            this.ScheduleItem = scheduleItem;
            this.Seat = seat;
            this.Price = CalcPrice();
        }

        //价格
        public double Price { get; set; }
        //放映场次
        public ScheduleItem ScheduleItem { get; set; }
        //座位
        public Seat Seat { get; set; }


        /// <summary>
        /// 计算价格
        /// </summary>
        /// <returns></returns>
        public virtual double CalcPrice()
        {
            return this.ScheduleItem.Movie.Price;
        }

        /// <summary>
        /// 打印电影票
        /// </summary>
        public virtual void Print()
        {
            string path = (this.ScheduleItem.Time + " " + this.Seat.SeatNum).Replace(":", "-") + ".txt";
            using (FileStream fs = new FileStream(path,FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs,Encoding.Default))
                {
                    sw.WriteLine("*******************************");
                    sw.WriteLine("          青鸟影院             ");
                    sw.WriteLine("-------------------------------");
                    sw.WriteLine("电影名:"+ScheduleItem.Movie.MovieName);
                    sw.WriteLine("时间:"+this.ScheduleItem.Time);
                    sw.WriteLine("座位号:"+this.Seat.SeatNum);
                    sw.WriteLine("价格:"+this.Price);
                    sw.WriteLine("*******************************");
                }
            }
        }



    /// 学生票
    /// </summary>
    public class StudentTicket:Ticket
    {
        public StudentTicket() { }
        public StudentTicket(ScheduleItem scheduleItem,Seat seat,double discount):base(scheduleItem,seat)
        {
            this.Discount = discount;
            this.Price = CalcPrice();
        }

        //折扣
        public double Discount { get; set; }

        /// <summary>
        /// 计算学生票价格
        /// </summary>
        /// <returns></returns>
        public override double CalcPrice()
        {
            return this.ScheduleItem.Movie.Price * this.Discount /10;
        }

        /// <summary>
        /// 打印学生票
        /// </summary>
        public override void Print()
        {
            string path = (this.ScheduleItem.Time + " " + this.Seat.SeatNum).Replace(":", "-") + ".txt";
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                {
                    sw.WriteLine("*******************************");
                    sw.WriteLine("      青鸟影院(学生票)         ");
                    sw.WriteLine("-------------------------------");
                    sw.WriteLine("电影名:" + ScheduleItem.Movie.MovieName);
                    sw.WriteLine("时间:" + this.ScheduleItem.Time);
                    sw.WriteLine("座位号:" + this.Seat.SeatNum);
                    sw.WriteLine("折扣:"+this.Discount);
                    sw.WriteLine("价格:" + this.Price);
                    sw.WriteLine("*******************************");
                }
            }



    /// 座位类
    /// </summary>
    public class Seat
    {
        public Seat() { }
        public Seat(string seatNum,Color color)
        {
            this.SeatNum = seatNum;
            this.Color = color;
        }

        //座位号
        public string SeatNum { get; set; }
        //颜色
        public Color Color { get; set; }
    }


    /// 放映场次
    /// </summary>
    public class ScheduleItem
    {
        public ScheduleItem() { }
        public ScheduleItem(Movie movie,string time)
        {
            this.Movie = movie;
            this.Time = time;
        }

        //电影
        public Movie Movie { get; set; }
        //放映时间
        public string Time { get; set; }
    }



    /// 放映计划类
    /// </summary>
    public class Schedule
    {
        public Schedule() { Items = new Dictionary<string, ScheduleItem>(); }
        public Schedule(Dictionary<string, ScheduleItem> items)
        {
            this.Items = items;
        }

        public Dictionary<string, ScheduleItem> Items { get; set; }

        //加载XML中的放映场次信息
        public void LoadItems()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(@"Data\ShowList.xml");
                XmlNode root = doc.DocumentElement;

                foreach (XmlNode var in root.ChildNodes)//Movie
                {
                    Movie movie = new Movie();
                    foreach (XmlNode var2 in var.ChildNodes)//Name
                    {
                        switch (var2.Name)
                        {
                            case "Name":
                                movie.MovieName = var2.InnerText;
                                break;
                            case "Poster":
                                movie.Poster = var2.InnerText;
                                break;
                            case "Director":
                                movie.Director = var2.InnerText;
                                break;
                            case "Actor":
                                movie.Actor = var2.InnerText;
                                break;
                            case "Price":
                                movie.Price = Convert.ToDouble(var2.InnerText);
                                break;
                            case "Type":
                                movie.MovieType = (MovieType)Enum.Parse(typeof(MovieType), var2.InnerText);
                                break;
                            case "Schedule":
                                foreach (XmlNode var3 in var2.ChildNodes)//Item
                                {
                                    ScheduleItem item = new ScheduleItem(movie, var3.InnerText);
                                    this.Items.Add(var3.InnerText, item);
                                }
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }



    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmCinema());
        }
    }


    /// 电影类
    /// </summary>
    public class Movie
    {
        public Movie() { }
        public Movie(string actor, string director, string movieName, MovieType movieType, string poster, double price)
        {
            this.Actor = actor;
            this.Director = director;
            this.MovieName = movieName;
            this.MovieType = movieType;
            this.Poster = poster;
            this.Price = price;
        }

        //导演
        public string Actor { get; set; }
        //演员
        public string Director { get; set; }
        //电影名
        public string MovieName { get; set; }
        //电影类型
        public MovieType MovieType { get; set; }
        //海报路径
        public string Poster { get; set; }
        //电影价格
        public double Price { get; set; }
    }



    /// 赠票
    /// </summary>
    public class FreeTicket:Ticket
    {
        public FreeTicket() { }
        public FreeTicket(string customerName,ScheduleItem scheduleItem,Seat seat):base(scheduleItem,seat)
        {
            this.CustomerName = customerName;
            this.Price = CalcPrice();
        }

        //赠票人姓名
        public string CustomerName { get; set; }


        /// <summary>
        /// 计算赠票票价
        /// </summary>
        /// <returns></returns>
        public override double CalcPrice()
        {
            return 0;
        }

        /// <summary>
        /// 打印赠票
        /// </summary>
        public override void Print()
        {
            string path = (this.ScheduleItem.Time + " " + this.Seat.SeatNum).Replace(":", "-") + ".txt";
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                {
                    sw.WriteLine("*******************************");
                    sw.WriteLine("      青鸟影院(赠票)         ");
                    sw.WriteLine("-------------------------------");
                    sw.WriteLine("电影名:" + ScheduleItem.Movie.MovieName);
                    sw.WriteLine("时间:" + this.ScheduleItem.Time);
                    sw.WriteLine("座位号:" + this.Seat.SeatNum);
                    sw.WriteLine("赠送人:"+this.CustomerName);
                    sw.WriteLine("价格:" + this.Price);
                    sw.WriteLine("*******************************");
                }
            }
        }
    }



    /// 电影院类
    /// </summary>
    public class Cinema
    {
        public Cinema()
        {

            SoldTickets = new List<Ticket>();
            Schedule = new Schedule();
            Seats = new Dictionary<string, Seat>();
        }


        public Schedule Schedule { get; set; }

        public Dictionary<string, Seat> Seats { get; set; }

        public List<Ticket> SoldTickets { get; set; }

        /// <summary>
        /// 加载放映场次
        /// </summary>
        public void Load()
        {
            using (FileStream fs = new FileStream("student.dat",FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
                this.SoldTickets = bf.Deserialize(fs) as List<Ticket>;
            }
        }

        /// <summary>
        /// 保存销售信息
        /// </summary>
        public void Save()
        {
            //
            using (FileStream fs = new FileStream("student.dat",FileMode.Create))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, SoldTickets);
            }

        }
    }




  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值