您好,登錄后才能下訂單哦!
使用C#怎么實現一個影院售票系統?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
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 <seatRow; i++) { for (int j = 0; j < seatLine; j++) { label17 = new Label(); label17.BackColor = Color.Yellow; label17.Font = new System.Drawing.Font("宋體", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (134))); label17.AutoSize = false; label17.Size = new System.Drawing.Size(50, 25); label17.Text = (j + 1).ToString() + "-" + (i + 1).ToString(); label17.TextAlign = ContentAlignment.MiddleCenter; label17.Location=new Point(30+(i*90),30+(j*60)); label17 .Click += new System.EventHandler(lblseat_Click); tabPage2.Controls.Add(label17); list.Add(label17.Text,label17); Seat seat=new Seat((j+1).ToString()+"-"+(i+1).ToString(),Color.Yellow); cinema.Seats.Add(seat.SeatNum,seat); } }
利用雙循環自動生成6行5列的座位表,其中將控件的大小和顏色以及位置進行固定
三、將解析完的文件綁定到Dic的集合中后再主窗體上進行綁定TreeView
Schedule s=new Schedule(); treeView1.BeginUpdate(); treeView1.Nodes.Clear(); string movieName = null; TreeNode movieNode = null; s.LoadItems(); foreach (KeyValuePair<string, ScheduleItem> item 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 (KeyValuePair<string, ScheduleItem> item 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<Ticket>(); throw; } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。