影院购票系统 C#源代码
生活随笔
收集整理的這篇文章主要介紹了
影院购票系统 C#源代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我希望能得到更多的建議,如果你覺得哪里能夠改進,請您花點時間告訴我,
QQ:1743703238
獨立網站:www.addoiles.com
電臺:《程序員的生活記錄》 目前在網易云音樂/網易云電臺,荔枝FM,喜馬拉雅,蘋果播客可獲得收聽影院購票系統實現的功能:
1.選擇電影
2.該電影有放映場次,可以選擇放映場次
3.電影中提供三種票:學生票、普通票、贈送票
4.電影選好了、時間選好了、票的種類選好了,就可以選座位了
5.有確認購票的功能,票才會買,而且你的票一旦買過就不能重復買
6.當你換到其他的時間場次,可以購票,再回來時,購票的信息還在,就是保存死了,不會丟失
7.當你關閉系統時,再次打開信息時票的信息還在
8.提供了系統重置的功能,一旦重置,所有的東西全部初始化
Movie的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<ShowList><Movie><Name>碟中諜5</Name><Poster>碟中諜5.jpg</Poster><Director>克里斯托夫·邁考利</Director><Actor>湯姆·克魯斯 / 杰瑞米·雷納</Actor><Type>動作、冒險、驚悚</Type><Price>60</Price><Schedule><item>9:00</item><item>12:50</item></Schedule></Movie><Movie><Name>地心引力</Name><Poster>地心引力.jpg</Poster><Director>阿方索·卡隆</Director><Actor>喬治·克魯尼 / 桑德拉·布洛克</Actor><Type>科幻 / 劇情 / 驚悚</Type><Price>80</Price><Schedule><item>12:00</item><item>15:00</item></Schedule></Movie><Movie><Name>極樂空間</Name><Poster>極樂空間.jpg</Poster><Director>尼爾·布洛姆坎普</Director><Actor>馬特·達蒙 / 朱迪·福斯特</Actor><Type>科幻 / 動作 / 劇情</Type><Price>60</Price><Schedule><item>7:00</item><item>12:20</item><item>15:30</item></Schedule></Movie><Movie><Name>星際穿越</Name><Poster>星際穿越.jpg</Poster><Director>克里斯托弗·諾蘭</Director><Actor>馬修·麥康納 / 安妮·海瑟薇</Actor><Type>冒險 / 科幻</Type><Price>90</Price><Schedule><item>1:00</item><item>2:23</item></Schedule></Movie><Movie><Name>遺落戰境</Name><Poster>遺落戰境.jpg</Poster><Director>約瑟夫·科辛斯基</Director><Actor>湯姆·克魯斯 / 摩根·弗里曼</Actor><Type>科幻 / 冒險 / 懸疑 / 動作</Type><Price>80</Price><Schedule><item>1:20</item><item>2:34</item></Schedule></Movie><Movie><Name>泰坦尼克號</Name><Poster>泰坦尼克號.jpg</Poster><Director>詹姆斯·卡梅隆</Director><Actor>迪卡普里奧/溫斯萊特</Actor><Type>災難,愛情</Type><Price>80</Price><Schedule><item>1:45</item><item>2:14</item></Schedule></Movie>
</ShowList>
Form1窗體中的源代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
namespace FetchMovie
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){LoadTreeView(); //加載TreeViewLoadLabel();//加載座位Labelthis.cboDiscount.Items.Add("今天全免費!!!");this.cboDiscount.SelectedIndex = 6;}/// <summary>/// 成員變量/// </summary>public string xmlPath = "C:\\Users\\YangRunkang\\Desktop\\FetchMovie\\FetchMovie\\Movie.xml";public int ticketsCount = 0;//定的座位的個數,及買票的個數,要保證每次換是時間時,就是選別的時間看電影,要歸0,但在歸0之前,要存起來public bool isBuy=false;//是否選擇電影(即是否觸發了TreeView的AfterSelect事件),如果觸發了該事件,truepublic string ticketType = "";//電影票的類型public double totalMoney = 0;//總票價public StringBuilder seatNumber = new StringBuilder();//記錄座位號public List<Label> LabelLists = new List<Label>();//記錄界面中的所有座位Labelpublic string otherPeople = "匿名";//增票的人的名字//public int isTreeViewClick = 0;/// <summary>/// 加載座位的Label標簽/// </summary>public void LoadLabel(){for (int i = 1; i <=5;i++)//控制行數{for (int n = 1; n <=7; n++){Label label = new Label();label.Location = new Point(60+(i*100),-20+(n*50));label.Text = (n.ToString() + "-" + i.ToString()).ToString();label.TextAlign = ContentAlignment.MiddleCenter;label.Size = new System.Drawing.Size(50,25);label.BackColor = Color.Wheat;label.Tag = (i.ToString() + n.ToString()).ToString();//只以此為唯一標示 labellabel.Click += new EventHandler(label18_Click);//綁定統一事件this.tabPage2.Controls.Add(label);LabelLists.Add(label);//將new出來的Label存入總的List<Class>中,以備不時之需}}}/// <summary>/// 加載TreeView/// </summary>public void LoadTreeView(){XmlDocument doc = new XmlDocument();doc.Load(xmlPath);XmlNode node = doc.DocumentElement;foreach (XmlNode no in node.ChildNodes){foreach (XmlNode n in no.ChildNodes){//電影名稱節點 父節點TreeNode MasterNode = new TreeNode();MasterNode.Text = no["Name"].InnerText;MasterNode.Tag = MasterNode;//Save Nodethis.treeView1.Nodes.Add(MasterNode);//時間節點,統一以電影名稱節點為父節點TreeNode StuNode = new TreeNode();StuNode.Text=no["Schedule"]["item"].InnerText;StuNode.Tag = StuNode;MasterNode.Nodes.Add(StuNode);//加載到父節點MasterNode下//時間節點,統一以電影名稱節點為父節點TreeNode StuNode2 = new TreeNode();StuNode2.Text = no["Schedule"]["item"].NextSibling.InnerText;//判斷點 下StuNode2.Tag = StuNode2;MasterNode.Nodes.Add(StuNode2);//加載到父節點MasterNode下break;//循環一次,跳一次}}}/// <summary>/// 統一公用的控件,此控件的點擊事件綁定了35個new出來的控件/// </summary>/// <param name="sender">點擊label控件時,一個對象就穿過來了</param>事件源/// <param name="e"></param>//定義一個Label集合,里面存入已經售出的票List<Label> labelList = new List<Label>();private void label18_Click(object sender, EventArgs e){Label clickLabel =(Label)sender;//售出的,不能再賣if (clickLabel.BackColor == Color.Red){MessageBox.Show("此票以售出"); return;}clickLabel.BackColor = Color.Red;//點擊后,變色string text = clickLabel.Text;//我點擊的Label上的Text值//座位號seatNumber.Append(text+"\t");//座位號如果有多個的話,他是追加的labelList.Add(clickLabel);ticketsCount++;//用加加,原來問題是處在這里,不是算你點了多少個label,而是算你實際的有效點擊次數//ticketsCount = labelList.Count;//記票數}//定義一個數組這個數組中存入電影的名字 然后用循環一次查找,找到即顯示相應的圖片 然后return //找不到,就什么都不干string[] filmNames = new string[] { "碟中諜5", "地心引力", "極樂空間", "星際穿越", "遺落戰境", "泰坦尼克號"};private void treeView1_AfterSelect(object sender, TreeViewEventArgs e){//每一次點擊保證所有的座位Label是最新的,新場次嘛,肯定要最新的;也要保證座位號(我是把它設為全局變量)是全新的,//不能在上一次的基礎上追加foreach(Label label in LabelLists){label.BackColor = Color.Wheat;seatNumber = new StringBuilder() ;this.lowPrice.Text = "無優惠";//將優惠價重置this.txtSender.Text = "匿名";//將贈送者的文本框重置ticketsCount = 0;//我換場次的時候,票數也要歸0}// 點擊相應的電影顯示相應的圖片for (int i = 0; i < filmNames.Length;i++){//一開始用 this.treeView1.SelectedNode.Tag.ToString().Substring(9) 用了Tag,其實弄麻煩了,直接用textif ((this.treeView1.SelectedNode.Text.ToString()).Equals(filmNames[i])){//MessageBox.Show("TestOK");測試成功,能實現this.pictureBox1.BackgroundImage=this.Poster.Images[i];this.pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;//return;不能再這里return,在這里return將結束這個方法,那么下面的將不會執行}}//進來就說明你點了電影,isBuy = true;TreeNode node= this.treeView1.SelectedNode;//你選中的節點,電影節點(第2次的想法)//node.Text;//節點上的文本信息string cinemaName = node.Text;//獲取具體的電影,然后就可以展示了showDetailsByName(node, cinemaName);//如果當前節點下面有字節點,那么我就打開if (node.Nodes.Count>0){node.Expand();//因為我是點擊節點,然后就順便,所以其子節點是可見的}//處理點擊時間,放映時間也會變得功能if (node.Nodes.Count == 0) //等于0說明我選的是時間{TreeNode childNode = this.treeView1.SelectedNode;//因為此子節點下面就沒有其他節點了,所以就是時間了this.Time.Text = childNode.Text;}///觸發條件 深度為1 次判斷,用于根據節點獲取相應的獲取文件,并從文件中獲取數據,最后將獲取到到的數據還原到窗體上if (this.treeView1.SelectedNode.Level == 1){DealTxtToForm();}}/// <summary>根電影的名字來展示電影的相關信息/// 根電影的名字來展示電影的相關信息/// </summary>/// <param name="node">選中的節點(電影名稱的節點)</param>/// <param name="cinemaName">選中節點上的文本,即電影的名字</param>public void showDetailsByName(TreeNode node,string cinemaName){XmlDocument doc = new XmlDocument();doc.Load(xmlPath);XmlNode no = doc.DocumentElement;foreach (XmlNode n in no.ChildNodes){if (n["Name"].InnerText.Equals(cinemaName)){// MessageBox.Show("1234");測試成功,所以下面就好辦了,給各個空間賦值this.FileName.Text=cinemaName;this.Director.Text = n["Director"].InnerText;this.Actor.Text = n["Actor"].InnerText;this.Type.Text = n["Type"].InnerText;this.Price.Text = n["Price"].InnerText;}}}/// <summary>/// 購票條件: 選票 選座位 根據票的種類計算價格,最后打印,打印前或后將數據存入文件/// 購票流程: 流程用字符串進行記錄,每記錄一個信息用一個%進行結尾,最后以%為標志,進行分割,然后存入數組,以供使用/// 1.選電影 public bool isBuy/// 2.選座位 public int ticketsCount/// 3.選票 rbtn/// 4.最后點擊“購票”按鈕/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void 購票ToolStripMenuItem_Click(object sender, EventArgs e){buyTicketBefore();//里面調用了購票方法buyTicket(),在調用這個方法之前,就是先判斷用戶是否買票seatNumber = new StringBuilder(); //賣完之后,我要將seatNumber座位號清空一下,調試的結果 測試成功}/// <summary>/// 買之前要進行一定的價檢查,價差好了之后再調用buyTicket()方法,進行買票/// </summary>public void buyTicketBefore(){//判斷用戶選擇,已提供良好的用戶體驗//Coolif (this.treeView1.SelectedNode == null)//判斷節點對象是否為空,就可以判斷有沒有選擇,不用憑借字符字符了,而且憑借字符效率也好低{MessageBox.Show("請選擇電影");return;}else if (this.treeView1.SelectedNode.Parent == null){MessageBox.Show("請選擇時間");return;}else if (ticketsCount == 0){MessageBox.Show("請選擇座位");return;}else if (this.rbtnNormal.Checked == false && this.rbtnSend.Checked == false && this.rbtnStu.Checked == false) //判斷 票的種類 有沒有選{MessageBox.Show("請選擇票的種類");return;}else{//先問是否購買DialogResult result = MessageBox.Show("是否確認購買", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);if (result == DialogResult.Yes){buyTicket(); //當一切ok,沒錯,那么我們進行購票 調用buyTicket()}else //不買,將選的座位空出來 {//定義一個Label的List集合,里面存入已經售出的票 List<Label> labelList = new List<Label>();//將存在集合里面的票顏色變過來foreach (Label label in labelList){label.BackColor = Color.Wheat;}ticketsCount = 0;}}}/// <summary>/// 購票的方法/// </summary>public void buyTicket(){string selectTime = this.treeView1.SelectedNode.Text.Replace(":","");//因為時間有:,而:作為文件名就不行就會從出錯string path = "d:\\" + this.treeView1.SelectedNode.Parent.Text + "%" + selectTime + ".txt";//文件流FileStream fis = new FileStream(path, FileMode.Create);//寫入器StreamWriter sw = new StreamWriter(fis);//寫////this.treeView1.SelectedNode.Parent.Text 電影名稱//this.treeView1.SelectedNode.Text 電影的放映時間//ticketsCount 電影的票數//StringBuilder sb = new StringBuilder();//電影清單sb.AppendLine("********************************");sb.AppendLine("*********影院售票系統*********");sb.AppendLine("電影名稱:《" + this.treeView1.SelectedNode.Parent.Text + "》");sb.AppendLine("電影放映時間:" + this.treeView1.SelectedNode.Text + "");sb.AppendLine("電影類型:" + this.Type.Text + "");sb.AppendLine("購買《" + this.treeView1.SelectedNode.Parent.Text + "》電影票數量:" + ticketsCount.ToString() + "張");//處理學生票類型string ticketType="";if(this.rbtnNormal.Checked==true){ticketType="普通票";}else if(this.rbtnSend.Checked==true){ticketType="贈送票";}else{ticketType="學生票";}sb.AppendLine("電影票類型:" + ticketType);if ((!this.txtSender.Text.Equals("匿名"))||!(this.txtSender.Text.Equals(string.Empty)))//表示有人送票,然后將贈送票的人打印出來{sb.AppendLine("贈送者:"+this.txtSender.Text);}//如果我選的是普通票,我算一下;贈送不用算0元,學生票已經處理過了//test OK全成功if( ticketType=="普通票"){totalMoney = Convert.ToInt32(this.Price.Text) * ticketsCount;ticketsCount = 0;//每次算完各自的賬后,就歸0,反正已經用過了}sb.AppendLine("總票價:"+totalMoney);totalMoney = 0;//用完之后就清0,給下一個操作留位子 //Test OK//**************************打印座位號sb.AppendLine("您的座位號是:"+seatNumber.ToString());sb.AppendLine("購票時間:" + System.DateTime.Now.ToString());sb.AppendLine("********************************"); sw.Write(sb.ToString());//向文件中寫入//關閉sw.Close();fis.Close(); MessageBox.Show(sb.ToString());}/// <summary>/// 當選的是學生票是,學生折扣的cbo能用/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void rbtnStu_CheckedChanged(object sender, EventArgs e){this.cboDiscount.Enabled = true;this.txtSender.Text = "";ticketType = "學生票";this.txtSender.Enabled = false;this.lowPrice.Text = "¥";}//普通票private void rbtnNormal_CheckedChanged(object sender, EventArgs e){this.cboDiscount.Enabled = false;this.txtSender.Enabled = false;this.txtSender.Text = "";this.lowPrice.Text = "無優惠";ticketType = "普通票";}//贈票 優惠的label改為0private void rbtnSend_CheckedChanged(object sender, EventArgs e){this.cboDiscount.Enabled = false;this.txtSender.Enabled = true;//當點中rbtnSend時,那么txtSender可用this.txtSender.Text = otherPeople;this.lowPrice.Text = "贈票,價格0元" + "贈送者:" + otherPeople;ticketType = "贈票";}private void txtSender_TextChanged(object sender, EventArgs e){this.lowPrice.Text ="無優惠"+" "+ "贈送者:"+this.txtSender.Text;}/// <summary>/// 專門處理學生票的,為學生票票計算錢的 獲取總金額totalMoney/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void cboDiscount_SelectedIndexChanged(object sender, EventArgs e){string disCountStr=this.cboDiscount.Text;//多個if判斷麻煩,用數組string[] dis = new string[] { "九折", "八折", "七折", "六折", "五折", "四折", "今天全免費!!!" };double[] ToDis = new double[] { 0.9,0.8,0.7,0.6,0.5,0.4,0};//對應上面的折扣for (int i = 0; i < dis.Length; i++){if (disCountStr == dis[i]){totalMoney = ticketsCount * Convert.ToDouble(this.Price.Text) * ToDis[i];this.lowPrice.Text = totalMoney.ToString();return;}}}//艱巨的任務來了,從文件中獲取數據,并且將獲取到到的數據還原到窗體上 --->文本信息獲取到了之后,轉到另一個方法中AccordingfileContentLoadLabelOfSeat()/// <summary>/// 從文件中獲取數據,并且將獲取到到的數據還原到窗體上/// /// 事件觸發條件:節點深度為1/// </summary>public string fileContent = "";public void DealTxtToForm(){//所有電影的文件的名稱(簡單點,就是買完票之后的清單) 沒有枚舉了,直接用數組,方便點string[] allFilesName = new string[] { "地心引力%1200.txt", "地心引力%1500.txt", "碟中諜5%900.txt", "碟中諜5%1250.txt", "極樂空間%700.txt", "極樂空間%1220.txt", "泰坦尼克號%145.txt", "泰坦尼克號%214.txt", "星際穿越%100.txt", "星際穿越%223.txt", "遺落戰境%120.txt", "遺落戰境%234.txt" };//我要獲取D盤下的所有文件的文件名,然后用for循環進行依次比對,比對成功打開文件,進行文件中數據的處理//到D盤下DirectoryInfo D = new DirectoryInfo("D:\\");//獲取D盤下的文件FileInfo[] D_files = D.GetFiles();//獲取每個文件的文件名 存到文件名數組中//文件名數組string[] getD_filesName=new string[D_files.Length];//文件全路徑數組string[] getD_filesFullName = new string[D_files.Length];for (int i = 0; i < D_files.Length; i++){getD_filesName[i] = D_files[i].Name;getD_filesFullName[i] = D_files[i].FullName;}//下面的驗證成功,可以獲取到文件的名稱//for (int i = 0; i < D_files.Length; i++)//{// MessageBox.Show(getD_filesName[i]);// MessageBox.Show(getD_filesFullName[i]);//}//就是根據用戶的點擊(此方法的觸發有一個條件,節點深度為1,所以不用擔心,所以可以直接用this.treeView1.SelectNode.Text),來調取相應的文件string treeViewSelectNodeText = this.treeView1.SelectedNode.Parent.Text + "%" + this.treeView1.SelectedNode.Text.Replace(":","") + ".txt";//時間要將:去掉//MessageBox.Show(treeViewSelectNodeText); OKfor (int i = 0; i < D_files.Length; i++){if (treeViewSelectNodeText == getD_filesName[i])//如果文件名稱相等{FileStream fis = new FileStream(getD_filesFullName[i],FileMode.Open);//文件的全路徑//打開文件獲取里面的內容StreamReader read = new StreamReader(fis);fileContent = read.ReadToEnd();//從頭讀到尾,將讀到的數據放入成員變量fileContent中//MessageBox.Show(fileContent); OKread.Close();fis.Close();AccordingfileContentLoadLabelOfSeat(fileContent);//專門處理座位的文本return;}}//如果if進去,下面的兩行代碼是執行不了的//MessageBox.Show(treeViewSelectNodeText);//MessageBox.Show(fileContent);}/// <summary>/// 文本信息獲取到了之后,根據文本的信息加載座位Label/// </summary>/// <param name="fileContent"></param>//LabelLists 所有的座位號,一直存在數組里,現在到用它的時候了public void AccordingfileContentLoadLabelOfSeat(string fileContent){//下面就是處理文本了,我主要是想獲取里面的座位號 X-Y 格式的//解決方法 以"-"為標志,取前取后,然后連接起來,存入新的數組(成員變量的位置),以供使用我現將所有的內容,分割,一個字符分一下 行不通//string[] words = fileContent.Split(' ');//for (int i = 0; i < words.Length; i++)//{// MessageBox.Show(words[i]);//}//正則表達式Regex obj = new Regex("[0-9][-][0-9]");MatchCollection co= obj.Matches(fileContent,0);foreach(Match maco in co) //MatchCollection 大, match 小{//MessageBox.Show(maco.ToString()); 測試成功,每個座位號都可以獲取foreach (Label label in LabelLists){if (label.Text.Equals(maco.ToString())){label.BackColor = Color.Red;}}}}private void 退出ToolStripMenuItem_Click(object sender, EventArgs e){Application.Exit();}private void 重置系統ToolStripMenuItem_Click(object sender, EventArgs e){DialogResult result = MessageBox.Show("是否重置系統?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);if (result == DialogResult.Yes){//所有電影的文件的名稱(簡單點,就是買完票之后的清單) 沒有枚舉了,直接用數組,方便點string[] allFilesName = new string[] { "地心引力%1200.txt", "地心引力%1500.txt", "碟中諜5%900.txt", "碟中諜5%1250.txt", "極樂空間%700.txt", "極樂空間%1220.txt", "泰坦尼克號%145.txt", "泰坦尼克號%214.txt", "星際穿越%100.txt", "星際穿越%223.txt", "遺落戰境%120.txt", "遺落戰境%234.txt" };//我要獲取D盤下的所有文件的文件名,然后用for循環進行依次比對,比對成功打開文件,進行文件中數據的處理//到D盤下DirectoryInfo D = new DirectoryInfo("D:\\");//獲取D盤下的文件FileInfo[] D_files = D.GetFiles();//獲取每個文件的文件名 存到文件名數組中//文件名數組string[] getD_filesName = new string[D_files.Length];//文件全路徑數組string[] getD_filesFullName = new string[D_files.Length];for (int i = 0; i < D_files.Length; i++){getD_filesName[i] = D_files[i].Name;getD_filesFullName[i] = D_files[i].FullName;}for (int i = 0; i < D_files.Length; i++){File.Delete(getD_filesFullName[i]);}this.Hide();//隱藏Form1 form = new Form1();form.Show();//showMessageBox.Show("重置成功");}}}
}我希望能得到更多的建議,如果你覺得哪里能夠改進,請您花點時間告訴我,手機號:13955195045(發信息給我) QQ:1743703238 微博:楊潤康Bla
?
總結
以上是生活随笔為你收集整理的影院购票系统 C#源代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: audio进度条(改进)
- 下一篇: 揭密迅雷BT式下载本质 [揭密迅雷]