首页 > 代码库 > 影院售票系统-----一个让你有成就感的小项目,只有一丢丢哦
影院售票系统-----一个让你有成就感的小项目,只有一丢丢哦
先来点废话,刚拿到这个项目的时候你是怎么想的,上来就闷头写,还是构想思路......
项目需求:
影院售票系统可以销售每天不同时段的电影票,不同时段的电影票有不同的优惠政策,还有使用兑换券的免费票。选择某个时段的一场电影,
单击放映厅未售出的座位并选择一个种类的电影票,创建电影票,计算价格并打印,座位设置为红色表示已经售出。
思想:
使用面向对象思想编写影院售票系统
看完这个你有没有点思路呢,我想你是有的 ......
下面先来点图片,先刺激一下视觉:
窗体搭建:
程序运行时发生的,来看清楚了:
是不是该选择你要看的电影列表了
好了,该你选择电影了,
挑选你喜欢的座位:
掏钱吧,要不你选择的就是别人的了
这个座位是你的了,还想看别的么?
选择赠票吧,免费的:
再来张学生票:
这样看是不是特别不爽,我感觉也是,看图片没感觉,接下来代码吧!
先把我们所需要的几个类创建出来吧!
/// <summary> /// 电影票父类 /// </summary> public class Ticket { public Ticket() { } public Ticket(ScheduleItem scheduleItem, Seat seat) { ScheduleItem = scheduleItem; Seat = seat; } //播放场次 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(" 青鸟影院(普通) "); 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("已售出"); } }
/// <summary> /// 学生票子类 /// </summary> public class StudentTicket : Ticket { //折扣 public int Discount { get; set; } public StudentTicket() { } public StudentTicket(ScheduleItem scheduleItem, Seat seat, int discount) : base(scheduleItem, seat) { Discount = discount; } //重写计算票价的方法 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(" 青鸟影院 (学生) "); 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 + "折学生票!"); } }
/// <summary> /// 赠票类 /// </summary> 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(" 青鸟影院 (赠票)"); 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); } }
/// <summary> /// 座位信息类 /// </summary> public class Seat { //座位号 public string SeatNum { get; set; } //卖出座位颜色 public Color Color { get; set; } public Seat() { } public Seat(string seatNum,System.Drawing.Color color) { this.SeatNum = seatNum; this.Color = color; } }
/// <summary> /// 保存放映计划信息类 /// </summary> public class ScheduleItem { public ScheduleItem() { Movie = new Movie(); } //播放时间 public string Time { get; set; } //播放电影属性 public Movie Movie { get; set; } }
/// <summary> /// 放映计划类 /// </summary> public class Schedule { public Schedule() { items = new Dictionary<string, ScheduleItem>(); } //放映场次 private Dictionary<string, ScheduleItem> items; public Dictionary<string, ScheduleItem> Items { get { return items; } set { items = value; } } //读取Xml文件的方法 public void LoadItems() { XmlDocument doc = new XmlDocument(); doc.Load("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); } } } }
/// <summary> /// 电影类 /// </summary> 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 enum MovieType { //喜剧 Comedy, //战争 War, //爱情 Romance, //动作 Action, //卡通 Cartoon, //恐怖 Thriller, //冒险 Adventure }
/// <summary> /// 影院类 /// </summary> public class Cinema { public Cinema() { Seats = new Dictionary<string, Seat>(); SoldTickets = new List<Ticket>(); Schedule = new Schedule(); } //座位集合 public Dictionary<string,Seat> Seats { get; set; } //放映计划 public Schedule Schedule { get; set; } //已出售电影票的集合 public List<Ticket> SoldTickets { get; set; } //保存售票信息的方法 public void Say() { 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<Ticket>(); } } }
/// <summary> /// 工具类 /// </summary> public class TicketUtil { public static Ticket CreateTicket(ScheduleItem scheduleItem, Seat seat, int discount, string customerName, string type) { Ticket ticket = null; switch (type) { case "student": ticket = new StudentTicket(scheduleItem, seat, discount); break; case "free": ticket = new FreeTicket(scheduleItem, seat, customerName); break; case "putong": ticket = new Ticket(scheduleItem, seat); break; } return ticket; } }
我去,这么多类,你有没有这个感觉,反正我是有的,这些类的创建不是瞎弄的,要把自己的思路理清楚了!
具体的代码实现:
public Cinema cinema; //加载座位 Dictionary<string, Label> labels = new Dictionary<string, Label>(); int tickets = 0; string key = null; /// <summary> /// 程序刚启动时 窗体加载的数据 /// </summary> private void FrmMain_Load(object sender, EventArgs e) { label1.Text = ""; label2.Text = ""; label3.Text = ""; label4.Text = ""; label5.Text = ""; label6.Text = ""; label8.Text = ""; rb01.Checked = true; //初始化一个影院类对象 cinema = new Cinema(); InitInfo(); //调用显示售票信息的方法 cinema.Load(); } /// <summary> /// 点击新播放列表时触发 /// </summary> private void 新放映列表ToolStripMenuItem_Click(object sender, EventArgs e) { cinema.Schedule.LoadItems(); cinema.SoldTickets.Clear(); InitTreeView(); } /// <summary> /// 绑定ListView /// </summary> private void InitTreeView() { tvList.Nodes.Clear(); string movieName = ""; TreeNode tn = null; foreach (ScheduleItem item in cinema.Schedule.Items.Values) { if (movieName != item.Movie.MovieName) { tn = new TreeNode(); tn.Text = item.Movie.MovieName; tvList.Nodes.Add(tn); } TreeNode node = new TreeNode(item.Time); tn.Nodes.Add(node); movieName = item.Movie.MovieName; } } Label label; /// <summary> /// 加载座位 /// </summary> private void InitInfo() { int seatRow = 8; int seatLine = 5; for (int i = 0; i < seatRow; i++) { for (int j = 0; j < seatLine; j++) { label = new Label(); //设置背景颜色 label.BackColor = Color.Yellow; //设置字体 label.Font = new System.Drawing.Font("宋体",14.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point, 134); //设置尺寸 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*70),40+(j*50)); //所有标签都绑定到同一个事件 label.Click += new System.EventHandler(labSeat_Click); tp01.Controls.Add(label); //labels集合存储以座位号为key值 labels.Add(label.Text, label); Seat seat = new Seat((j + 1).ToString() + "-" + (i + 1).ToString(), Color.Yellow); //保存的座位集合 cinema.Seats.Add(seat.SeatNum, seat); } } } /// <summary> /// 选择座位时做出的判断 /// </summary> private void labSeat_Click(object sender, EventArgs e) { if (label8.Text=="") { MessageBox.Show("您还没选择电影!", "提示"); return; } tickets++; try { string seatNum = ((Label)sender).Text.ToString(); string customerName =txt01.Text.ToString(); int discount = 0;//折扣 string type = "";//类型 if (rb03.Checked) { type = "student"; if (cbo01.Text == "") { MessageBox.Show("请输入折扣数!", "提示"); return; } else { discount = Convert.ToInt32(cbo01.Text); } } else if (rb02.Checked) { if (txt01.Text=="") { MessageBox.Show("请输入赠票者姓名!", "提示"); return; } else { type = "free"; } } else if(rb01.Checked) { type = "putong"; } //调用工具类创建票 Ticket Ticket = TicketUtil.CreateTicket(cinema.Schedule.Items[key], cinema.Seats[seatNum],discount, customerName, type); if (cinema.Seats[seatNum].Color == Color.Yellow) { //打印 DialogResult result = MessageBox.Show("是否购买?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { cinema.Seats[seatNum].Color = Color.Red; UpdateSeat(); Ticket.CalcPrice(); cinema.SoldTickets.Add(Ticket); label6.Text = Ticket.Price.ToString(); Ticket.Print(); } else if (result == DialogResult.No) { return; } } else { //显示当前售出票的信息 foreach (Ticket item in cinema.SoldTickets) { //判断是否为同场次、同电影、同座位号 if (item.Seat.SeatNum == seatNum && item.ScheduleItem.Time == tvList.SelectedNode.Text && item.ScheduleItem.Movie.MovieName == tvList.SelectedNode.Parent.Text) { item.Show(); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// Treeview选择时做出的判断 /// </summary> private void tvList_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode node = tvList.SelectedNode; if (node == null) { return; } if (node.Level != 1) { return; } key = node.Text; //将详细信息显示 label8.Text = cinema.Schedule.Items[key].Movie.MovieName; label1.Text = cinema.Schedule.Items[key].Movie.Director; label2.Text = cinema.Schedule.Items[key].Movie.Actor; label5.Text = cinema.Schedule.Items[key].Movie.Price.ToString(); label4.Text = cinema.Schedule.Items[key].Time; label3.Text = cinema.Schedule.Items[key].Movie.MovieType.ToString(); pic01.Image = Image.FromFile(cinema.Schedule.Items[key].Movie.Poster); label6.Text = ""; //清空座位 ClearSeat(); //遍历该场电影的座位销售情况 foreach (Ticket ticket in cinema.SoldTickets) { foreach (Seat seat in cinema.Seats.Values) { if ((ticket.ScheduleItem.Time == key) && (ticket.Seat.SeatNum == seat.SeatNum)) { seat.Color = Color.Red; } } } UpdateSeat(); } /// <summary> /// 清空座位 /// </summary> private void ClearSeat() { foreach (Seat seat in cinema.Seats.Values) { seat.Color = Color.Yellow; } } /// <summary> /// 更新座位状态 /// </summary> private void UpdateSeat() { //根据影院类里面的座位来更新座位的颜色 foreach (string key in cinema.Seats.Keys) { labels[key].BackColor = cinema.Seats[key].Color; } } /// <summary> /// 选择赠票 /// </summary> private void rb02_CheckedChanged(object sender, EventArgs e) { cbo01.Enabled = false; txt01.Enabled = true; cbo01.Text = ""; label6.Text = "0"; } /// <summary> /// 选择学生票 /// </summary> private void rb03_CheckedChanged(object sender, EventArgs e) { txt01.Enabled = false; txt01.Text = ""; cbo01.Enabled = true; } /// <summary> /// 选择普通票 /// </summary> private void rb01_CheckedChanged(object sender, EventArgs e) { cbo01.Enabled = false; txt01.Text = ""; txt01.Enabled = false; cbo01.Text = ""; label6.Text = "0"; } private void 继续销售ToolStripMenuItem_Click(object sender, EventArgs e) { //判断放映列表是否为空 if (cinema.Schedule.Items.Count == 0) { cinema.Schedule.LoadItems(); } InitTreeView(); } /// <summary> /// 选择下拉列表时框 /// </summary> private void cbo01_SelectedIndexChanged(object sender, EventArgs e) { //根据当前选中的电影,设置“优惠价” if (label5.Text != "") { int price = Convert.ToInt32(label5.Text); int discount = Convert.ToInt32(cbo01.Text); label6.Text = (price * discount / 10).ToString(); } } private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("是否保存当前销售状态?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { //退出时保存Cinema对象 cinema.Say(); } } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { cinema.Say(); Dispose(); }
这没有多少文字解说,思想在其中。
其实写这些都是枯燥的,但是写完的那一刻,运行的那一刻,你就会觉得这些都是值得的,虽然这是一个特小的项目。
人的信心就是从一点一滴积累起来的,我是这样告诉自己的,不要局限自己,编程是思想,不是Copy!
希望我的文章能给你提供一种方法,而不是让你作弊!
要想人前显贵,就要背后受罪,我觉得说的真好!啰嗦了,我只是想提醒自己!
影院售票系统-----一个让你有成就感的小项目,只有一丢丢哦