新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
使用C#怎么实现一个影院售票系统?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
蓬安网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联建站于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。
1.首先是将解析完的XML文件绑定到TreeView上
2.用代码动态生成座位的label,生成触发事件Label_Click,俩组放映时间的售出座位是不同的
3.用序列化与反序列化实现代码的多次利用
4.打印票务
创建与本项目相关的10个类
一、首先在放映计划类中创建LoadItems()方法来解析ShowList.xml文件
XmlDocument dic=new XmlDocument(); dic.Load("ShowList.xml"); XmlNode xml = dic.DocumentElement; foreach (XmlNode item in xml.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; movie.MovieType = (MovieType)(Enum.Parse(typeof(MovieType), item["Type"].InnerText)); movie.Price = Convert.ToInt32(item["Price"].InnerText);
//因为放映时间不在电影类中,所以经过第二次遍历,找到时间,最后将所有解析完的文件添加到Dic的双列集合中
foreach (XmlNode itemm in item["Schedule"]) { ScheduleItem scheduleItem=new ScheduleItem(); scheduleItem.Time = itemm.InnerText; scheduleItem.Movie = movie; Dic.Add(scheduleItem.Time, scheduleItem);
二、将座位用代码填入放映厅中
int seatRow = 6; int seatLine = 5; for (int i = 0; i利用双循环自动生成6行5列的座位表,其中将控件的大小和颜色以及位置进行固定
三、将解析完的文件绑定到Dic的集合中后再主窗体上进行绑定TreeView
Schedule s=new Schedule(); treeView1.BeginUpdate(); treeView1.Nodes.Clear(); string movieName = null; TreeNode movieNode = null; s.LoadItems(); foreach (KeyValuePairitem in s.Dic) { if (movieName!=item.Value.Movie.MovieName) { movieNode=new TreeNode(item.Value.Movie.MovieName); treeView1.Nodes.Add(movieNode); } TreeNode timeNode=new TreeNode(item.Value.Time); movieNode.Nodes.Add(timeNode); movieName = item.Value.Movie.MovieName; } treeView1.EndUpdate(); 之后的成功如下图所示
五、完成影片详情的绑定
将TreeView上的文件信息以单机的形式绑定到详情上
foreach (KeyValuePairitem in s.Dic) { if (treeView1.SelectedNode.Text==item.Key) { this.label2.Text = item.Value.Movie.MovieName; this.label9.Text = item.Value.Movie.Director; this.label10.Text = item.Value.Movie.Actor; this.label11.Text = item.Value.Movie.MovieType.ToString(); this.label12.Text = item.Value.Time; this.label13.Text = item.Value.Movie.Price.ToString(); this.pictureBox1.Image =Image.FromFile(item.Value.Movie.Poster); this.label14.Text = ""; } } ClearSeat(); foreach (Ticket ticket in cinema.SoldTickets) { foreach (Seat seat in cinema.Seats.Values) { if ((ticket.ScheduleItem.Time == key) && (ticket.Seet.SeatNum == seat.SeatNum)) { seat.Color = Color.Red; } } } UpdateSeat(); } public void ClearSeat() { foreach (Seat seat in cinema.Seats.Values) { seat.Color = Color.Yellow; } } 六、选择特殊票系列特殊票分为学生票,普通票和赠票学生票类和赠票类分别继承它门的父类售票类学生票子类有独立的属性Discount(学生票折扣)赠票子类也有独立的属性CustomerName(赠送者的名字)都继承父类的票价的计算方法
当触发普通票时,折扣下拉框以及赠票者框关闭
当触发赠票时,折扣下拉框关闭,赠票者框打开
当触发学生票时,赠票者框关闭,折扣下来框打开
private void radioButton1_CheckedChanged(object sender, EventArgs e) { this.cmbDisCount.Enabled = false; this.txtCustomer.Text = ""; this.txtCustomer.Enabled = false; this.cmbDisCount.Text = ""; this.label14.Text = ""; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { this.txtCustomer.Enabled = true; this.cmbDisCount.Enabled = false; this.cmbDisCount.Text = ""; this.label14.Text = "0"; } private void radioButton3_CheckedChanged(object sender, EventArgs e) { this.txtCustomer.Enabled = false; this.txtCustomer.Text = ""; this.cmbDisCount.Enabled = true; this.cmbDisCount.Text = "7"; if (this.label13.Text!="") { label14.Text = (Convert.ToInt32(label13.Text) * Convert.ToInt32(cmbDisCount.Text) / 10).ToString(); } }七、售票售票当中我们会用到一种设计模式,工厂模式,建立工厂类
public class TicketUitl { public static Ticket CreateTicket(ScheduleItem item,Seat seat,string csName,double discount,string type) { Ticket ticket = null; switch (type) { case "normal": ticket=new Ticket(item,seat); break; case "free": ticket=new FreeTicket(csName,item,seat); break; case "student": ticket=new StudentTicket(discount,item,seat); break; } return ticket; } }在工厂类中我们以票类当作类型声明一个带参方法来判断三次不同的售票类型
之后所调用他们各子重写父类的CalcPrice()方法进行票务的结束工作
if (String.IsNullOrEmpty(this.label2.Text)) { MessageBox.Show("您还没选好电影!", "提示"); return; } try { ticket++; Schedule s=new Schedule(); s.LoadItems(); cinema.Schedule = s; string time = this.treeView1.SelectedNode.Text; ScheduleItem item = cinema.Schedule.Dic[time]; string seatNum = ((Label) sender).Text.ToString(); string customerName = this.txtCustomer.Text.ToString(); int discount = 0; string type = ""; if (this.radioButton3.Checked) { type = "student"; if (this.cmbDisCount.Text == null) { MessageBox.Show("请输入折扣数!", "提示"); return; } else { discount = int.Parse(this.cmbDisCount.Text); } } else if (this.radioButton2.Checked) { if (String.IsNullOrEmpty(this.txtCustomer.Text)) { MessageBox.Show("请输入增票者姓名!", "提示"); return; } else { type = "free"; } } else { type = "normal"; } //调用工具类创建票 Ticket newTickey = TicketUitl.CreateTicket(item, cinema.Seats[seatNum], customerName,Convert.ToInt32(discount), type); if (cinema.Seats[seatNum].Color == Color.Yellow) { //打印 DialogResult result; result = MessageBox.Show("是否购买!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { cinema.Seats[seatNum].Color = Color.Red; UpdateSeat(); newTickey.CalcPrice(); cinema.SoldTickets.Add(newTickey); label14.Text = newTickey.Price.ToString(); newTickey.Print(); } else if (result == DialogResult.No) { return; } } else { //显示当前售出票的信息 foreach (Ticket tickets in cinema.SoldTickets) { //判断是否为同场次、同电影、同座位号 if (tickets.Seet.SeatNum == seatNum && tickets.ScheduleItem.Time == treeView1.SelectedNode.Text && tickets.ScheduleItem.Movie.MovieName == treeView1.SelectedNode.Parent.Text) { tickets.Show(); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void UpdateSeat() { foreach (string item in cinema.Seats.Keys) { list[item].BackColor = cinema.Seats[item].Color; } }八、进行当前售票的存储以及显示
在影院类中写俩个方法进行存储和显示
分别利用序列化和反序列化的功能序列化:save()
public void Save() { FileStream fs = new FileStream("soldTickets.txt", FileMode.OpenOrCreate); StreamWriter writer = new StreamWriter(fs, Encoding.Default); for (int i = 0; i < SoldTickets.Count; i++) { Ticket ticket = SoldTickets[i]; 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.Seet.SeatNum + "|" + ticket.Seet.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"); //MessageBox.Show("保存成功"); writer.Close(); fs.Close(); }反序列化:Load()
public void Load() { try { StreamReader reader = new StreamReader("soldTickets.txt", Encoding.GetEncoding("GB2312")); string line = reader.ReadLine(); string[] propertyValues; Ticket ticket = null; while (line.Trim() != "The End") { propertyValues = line.Split('|'); string type = propertyValues[0]; Movie movie = new Movie(propertyValues[1], propertyValues[2], propertyValues[3], propertyValues[4], (MovieType) Enum.Parse(typeof (MovieType), propertyValues[5]), int.Parse(propertyValues[6])); ScheduleItem scheduleItem = new ScheduleItem(); scheduleItem.Time = propertyValues[7]; scheduleItem.Movie = movie; string color = propertyValues[9]; string endColor = color.Substring(color.IndexOf("[") + 1, color.Length - 1 - color.IndexOf("[") - 1); Seat seat = new Seat(propertyValues[8], Color.FromName(endColor)); int discount = 10; switch (type) { case "student": discount = 7; ticket = TicketUitl.CreateTicket(scheduleItem, seat, "", Convert.ToInt32(discount), type); break; case "free": discount = 0; ticket = TicketUitl.CreateTicket(scheduleItem, seat, propertyValues[11], Convert.ToInt32(discount), type); break; default: discount = 10; ticket = TicketUitl.CreateTicket(scheduleItem, seat, "", Convert.ToInt32(discount), ""); break; } this.SoldTickets.Add(ticket); line = reader.ReadLine(); } reader.Close(); } catch (Exception ex) { Console.WriteLine("网络连接异常:" + ex.Message); SoldTickets = new List(); throw; } } 看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
当前名称:使用C#怎么实现一个影院售票系统
标题网址:http://wjwzjz.com/article/ijches.html