影院售票
做一个小程序(影院售票)
效果如下
主要功能有:影院每天更新放映列表,系统支持实时查看,包括电影放映场次时间,电影概况
影院提供三类票有不同的价位
主持购票,允许选座
系统可以保存销售情况并允许恢复
首先我们需要10个类()具体关系如下
Seat类:保存影院座位信息 Movie:电影类 Ticket:电影父票类 StudentTickrt:学生子票类 FreeTicket:增票子类
ScheduItem:影院每天放映计划的场次 Cinema:影院类:保存放映计划和座位类 Tickuntli:工具类
主要功能一:实现座位的动态添加(代码如下)
for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { Label lb = new Label(); lb.BackColor = Color.Yellow; lb.Location = new Point(20 + j * 100, 50 + i * 70); lb.Font = new Font("Courier New", 11); lb.Name = (i + 1) + "-" + (j + 1); lb.Size = new Size(80, 30); lb.TabIndex = 0; lb.Text = (i + 1) + "-" + (j + 1); lb.TextAlign = ContentAlignment.MiddleCenter; lb.Click += lb_Click; tpCinema.Controls.Add(lb); //添加座位对象到CInema的Seats集合中 Seat seat = new Seat(lb.Text, Color.Yellow); cinema.Seats.Add(seat.SeatNum, seat); } }
效果如下:
功能二实现TreeView的绑定
首先需要解析XML文件(代码如下)
//加载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); } }
然后在主窗体调用方法并且绑定(代码如下)
this.tvMovies.Nodes.Clear(); //加载XML cinema.Schedule.LoadItems(); //绑定到TreeView TreeNode root = null; foreach (ScheduleItem var in cinema.Schedule.Items.Values) { if (root == null || root.Text != var.Movie.MovieName) { //根节点 root = new TreeNode(var.Movie.MovieName); this.tvMovies.Nodes.Add(root); } //子节点 root.Nodes.Add(var.Time); }
效果如下
功能三具体信息的显示
string time = this.tvMovies.SelectedNode.Text; ScheduleItem item = cinema.Schedule.Items[time]; this.lblActor.Text = item.Movie.Actor; this.lblDirector.Text = item.Movie.Director; this.lblMovieName.Text = item.Movie.MovieName; this.lblPrice.Text = item.Movie.Price.ToString(); this.lblTime.Text = item.Time; this.lblType.Text = item.Movie.MovieType.ToString(); this.picMovie.Image = Image.FromFile(@"Image\" + item.Movie.Poster); this.lblCalcPrice.Text = item.Movie.Price.ToString();
选择不同票时
#region 点击普通票 private void rdoNormal_CheckedChanged(object sender, EventArgs e) { this.cmbDisCount.Enabled = false; this.txtCustomer.Enabled = false; this.lblCalcPrice.Text = lblPrice.Text; } #endregion #region 点击赠票 private void rdoFree_CheckedChanged(object sender, EventArgs e) { this.txtCustomer.Enabled = true; this.cmbDisCount.Enabled = false; this.lblCalcPrice.Text = lblPrice.Text; } #endregion #region 点击学生票 private void rdoStudent_CheckedChanged(object sender, EventArgs e) { if (this.lblPrice.Text != "") { this.cmbDisCount.Enabled = true; this.txtCustomer.Enabled = false; this.lblCalcPrice.Text = (Convert.ToDouble(this.lblPrice.Text) * Convert.ToDouble(this.cmbDisCount.Text) / 10).ToString(); } }
售票功能
if (this.tvMovies.Nodes.Count == 0 || this.tvMovies.SelectedNode.Level == 0) { return; } lbl = sender as Label; if (lbl.BackColor == Color.Red) { MessageBox.Show("已售出"); } else { if (DialogResult.OK == MessageBox.Show("是否购买", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) { //取得放映时间 string time = this.tvMovies.SelectedNode.Text; //使用时间作为键找到放映场次对象 ScheduleItem item = cinema.Schedule.Items[time]; string type = string.Empty; type = rdoNormal.Checked ? "normal" : rdoFree.Checked ? "free" : "student"; Ticket ticket = TicketUtil.CreateTicket(item, cinema.Seats[lbl.Text], txtCustomer.Text, Convert.ToDouble(cmbDisCount.Text), type); //添加到已销售的集合中 cinema.SoldTickets.Add(ticket); //出票 ticket.Print(); //更新颜色 lbl.BackColor = Color.Red; cinema.Seats[lbl.Text].Color = Color.Red; } }
保存销售信息
using (FileStream fs = new FileStream("student.dat",FileMode.Create)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, SoldTickets); }
继续销售
foreach (Seat seat in cinema.Seats.Values) { foreach (Label lbl in tpCinema.Controls) { // 座位号相同证明是对应Label if (lbl.Text == seat.SeatNum) { lbl.BackColor = seat.Color; } } }
这个小程序还有很多不足