影院售票系统

    xiaoxiao2021-03-25  43

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Day_影院售票系统 { /// <summary> /// 影院类 /// </summary> public class Cinema { public static Dictionary<string, Seat> Seats=new Dictionary<string, Seat>(); public static Schedule Schedule = new Schedule(); public static List<Ticket> SoldTickets=new List<Ticket>(); public Cinema() { Seats=new Dictionary<string, Seat>(); Schedule=new Schedule(); SoldTickets=new List<Ticket>(); } public static void Load() { StreamReader reader = new StreamReader("soldTickets.txt", Encoding.Default); //一行一行的读取,先预读一行给while判断用 string line = reader.ReadLine(); //保存分割好的数据的数组 string[] pv; Ticket ticket = null; //当是The End时结束读取 while (line.Trim() != "The End") { pv = line.Split('|'); //读取文本信息 string index = pv[1]; //创建票对象 ticket = TicketUtil.CreateTicket(Cinema.Schedule.Items[index], Cinema.Seats[pv[2]], int.Parse(pv[4]), pv[6], pv[5]); //加入到售出票集合 Cinema.SoldTickets.Add(ticket); line = reader.ReadLine(); } reader.Close(); } public static void save() { FileStream fs = new FileStream("soldTickets.txt", FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(fs, Encoding.Default); for (int i = 0; i < Cinema.SoldTickets.Count; i++) { //创建售出票对象 Ticket ticket = Cinema.SoldTickets[i]; //票的类型 string type = ""; //票的折扣 int discount = 0; //赠送者的名字 string name = ""; //用is判断属于哪个类,并根据类设置类型 if (Cinema.SoldTickets[i] is StudentTicket) { type = "stu"; discount = ((StudentTicket)Cinema.SoldTickets[i]).Discount; } else if (Cinema.SoldTickets[i] is FreeTicket) { type = "free"; name = ((FreeTicket)Cinema.SoldTickets[i]).CustomerName; } else { type = "normal"; } //将信息写入文本文件0.电影名1.场次2.座位号3.票价4.折扣5.类型6.赠送者 string info = string.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}", Cinema.SoldTickets[i].ScheduItem.Movie.MovieName, Cinema.SoldTickets[i].ScheduItem.Time, Cinema.SoldTickets[i].Seat.SeatNum, Cinema.SoldTickets[i].Price, discount, type, name); sw.WriteLine(info); } sw.WriteLine("The End"); sw.Close(); fs.Close(); MessageBox.Show("保存成功!"); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Day_影院售票系统 { /// <summary> /// 电影类 /// </summary> public class Movie { public string MovieName;//电影名 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; }//定价 } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Day_影院售票系统 { public enum MovieType { Comedy=0,//爱情片 War=1,//战争片 Romance=2,//浪漫电影 Action=3,//动作片 Cartoon=4,//卡通片 Thriller = 5,// 惊悚片 AdvenTure = 6//冒险片 } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Day_影院售票系统 { /// <summary> /// 放映计划类 /// </summary> public class Schedule { public Dictionary<string, ScheduleItem> Items =new Dictionary<string, ScheduleItem>(); public void LoadItems() { Items.Clear(); XmlDocument xml = new XmlDocument(); xml.Load("ShowList.xml"); XmlElement root = xml.DocumentElement; foreach (XmlNode item in root.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; switch (item["Type"].InnerText) { case "Action": movie.MovieType = MovieType.Action; break; case "War": movie.MovieType = MovieType.War; break; case "Comedy": movie.MovieType = MovieType.Comedy; break; } movie.Price = Convert.ToInt32(item["Price"].InnerText); if (item["Schedule"].HasChildNodes) { foreach (XmlNode item2 in item["Schedule"].ChildNodes) { ScheduleItem schItem = new ScheduleItem(); schItem.Time = item2.InnerText; schItem.Movie = movie; Items.Add(schItem.Time, schItem); } } } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Day_影院售票系统 { /// <summary> /// 影院每天放映计划的场次,保存每场电影的信息 /// </summary> public class ScheduleItem { public string Time { get; set; }//放映时间属性 public Movie Movie{ get; set; }//本场所放映电影属性 } } using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Day_影院售票系统 { public class Seat { public Seat(string s, Color yellow) { this.SeatNum = s; this.Color = yellow; } public string SeatNum { get; set; }//座位号 public Color Color { get; set; }//座位卖出状态颜色 public Seat(){ } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Windows.Forms; namespace Day_影院售票系统 { /// <summary> /// 学生票子类 /// </summary> public class StudentTicket:Ticket { public StudentTicket(ScheduleItem sch, Seat seat, int discount) : base(sch, seat) { this.Discount = discount; } private int _discount; /// <summary> /// 学生票的折扣 /// </summary> public int Discount { get { return _discount; } set { _discount = value; } } public override void CalcPrice() { this.Price = this.ScheduItem.Movie.Price * Discount / 10; } public override void Print() { string info = string.Format("************************************************\n\t青鸟影院(学生)\n------------------------------------------------\n电影名:\t{0}\n时间:\t{1}\n座位号:\t{2}\n价格:\t{3}\n************************************************", this.ScheduItem.Movie.MovieName, this.ScheduItem.Time, this.Seat.SeatNum, this.Price); MessageBox.Show(info); //存到文件中 string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt"; FileStream fs = new FileStream(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.Write(info); sw.Close(); fs.Close(); } public override void Show() { string info = string.Format("已售出!\n{0}折学生票!", this.Discount); } } } using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; namespace Day_影院售票系统 { /// <summary> /// /// </summary> public class Ticket { public int Price { get; set; }//票价 // public ScheduleItem ScheduleItem { get; set; }//放映场次 public Seat Seat { get; set; }//所属座位对象 //计算票价的方法 public Ticket(ScheduleItem sch, Seat seat) { this.ScheduItem = sch; this.Seat = seat; } public ScheduleItem ScheduItem = new ScheduleItem(); // public object ScheduleItem { get; set; } public virtual void CalcPrice() { this.Price = ScheduItem.Movie.Price; } //打印售出票信息的虚方法 public virtual void Print() { string info = string.Format("************************************************\n\t青鸟影院\n------------------------------------------------\n电影名:\t{0}\n时间:\t{1}\n座位号:\t{2}\n价格:\t{3}\n************************************************", this.ScheduItem.Movie.MovieName, this.ScheduItem.Time, this.Seat.SeatNum, this.Price); MessageBox.Show(info); //存到文件中 string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt"; FileStream fs = new FileStream(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.Write(info); sw.Close(); fs.Close(); } public virtual void Show() { string info = string.Format("已售出!\n普通票!"); MessageBox.Show(info); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Windows.Forms; namespace Day_影院售票系统 { /// <summary> /// 赠票子类 /// </summary> public class FreeTicket:Ticket { public FreeTicket(ScheduleItem sch, Seat seat, string name) : base(sch,seat) { this.Seat = seat; this.CustomerName = name; this.ScheduItem = sch; } public string CustomerName { get; set; } public override void CalcPrice() { this.Price = 0; } public override void Print() { string info = string.Format("************************************************\n\t青鸟影院(赠票)\n------------------------------------------------\n电影名:\t{0}\n时间:\t{1}\n座位号:\t{2}\n姓名:\t{3}\n************************************************", this.ScheduItem.Movie.MovieName, this.ScheduItem.Time, this.Seat.SeatNum, this.CustomerName); MessageBox.Show(info); //存到文件中 string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt"; FileStream fs = new FileStream(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.Write(info); sw.Close(); fs.Close(); } public override void Show() { MessageBox.Show("已售出!\n赠票!"); ; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Day_影院售票系统 { class TicketUtil { public static Ticket CreateTicket(ScheduleItem sch,Seat seat,int discount,string customerName,string type) { Ticket newTicket=null; switch (type) { case "student": newTicket = new StudentTicket(sch, seat, discount); break; case "free": newTicket = new FreeTicket(sch, seat, customerName); break; default: newTicket = new Ticket(sch, seat); break; } return newTicket; } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; namespace Day_影院售票系统 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Dictionary<string,Label> labels=new Dictionary<string, Label>(); string key = null; private Seat seat = null; Cinema cinema =new Cinema(); public void SeatShow() { int row = 5; int Line = 7; for (int i = 0; i < Line; i++) { for (int j = 0; j < row; j++) { Label label = new Label(); label.Text = (j + 1) + "-" + (i + 1); //label.Text = "1-1"; label.ForeColor = Color.Red; label.BackColor = Color.Blue; label.AutoSize = false; label.Size = new Size(30, 20); label.Location = new Point(40 + (i*70), 30 + (j*50)); label.Click +=label_Click; seat=new Seat((j+1).ToString()+"-"+(i+1).ToString(),Color.Blue); Cinema.Seats.Add(seat.SeatNum,seat); labels.Add(label.Text,label); tabPage3.Controls.Add(label); } } } private string seatNum = ""; private void label_Click(object sender, EventArgs e) { seatNum = ((Label) sender).Text.ToString(); string customer = this.textBox1.Text.ToString(); key = treeView1.SelectedNode.Text; int discount = 0; string type = ""; if (this.radioButton3.Checked) { type = "student"; if (this.comboBox1.Text==null) { MessageBox.Show("请输入折扣数"); return; } else { discount = int.Parse(this.comboBox1.Text); } } else if (this.radioButton2.Checked) { if (String.IsNullOrEmpty(this.textBox1.Text)) { MessageBox.Show("请输入赠票者姓名"); return; } else { type = "free"; } } Ticket newTicket = TicketUtil.CreateTicket(Cinema.Schedule.Items[key], Cinema.Seats[seatNum], discount, customer, type); if (Cinema.Seats[seatNum].Color==Color.Blue) { DialogResult result; result = MessageBox.Show("是否购买","购买",MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { Cinema.Seats[seatNum].Color = Color.Red; UpdateSeat(); newTicket.CalcPrice(); Cinema.SoldTickets.Add(newTicket); label6.Text = newTicket.Price.ToString(); newTicket.Print(); } else if (result==DialogResult.No) { return; } } else { foreach (Ticket ticket0 in Cinema.SoldTickets) { if (ticket0.Seat.SeatNum==seatNum&& ticket0.ScheduItem.Time==treeView1.SelectedNode.Text&& ticket0.ScheduItem.Movie.MovieName==treeView1.SelectedNode.Parent.Text) { ticket0.Show(); } } } } private void UpdateSeat() { foreach (string key in Cinema.Seats.Keys) { labels[key].BackColor = Cinema.Seats[key].Color; } } private void Form1_Load(object sender, EventArgs e) { Cinema.Schedule.LoadItems(); SeatShow(); InitTreeView(); } public void InitTreeView() { this.treeView1.Nodes.Clear(); string movieName = ""; TreeNode tn = null; foreach (ScheduleItem item in Cinema.Schedule.Items.Values) { //如果不存在此电影节点,创建电影节点 if (item.Movie.MovieName != movieName) { tn = new TreeNode(item.Movie.MovieName); tn.Tag = item.Movie; treeView1.Nodes.Add(tn); } //增加场次时间节点 TreeNode time = new TreeNode(item.Time); tn.Nodes.Add(time); //获取当前场次的电影名字,重新遍历9 movieName = item.Movie.MovieName; } } private void tabPage3_Click (object sender, EventArgs e) { } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode node = treeView1.SelectedNode; if (node==null) { return; } if (node.Level!=1) { return; } Schedule s=new Schedule(); s.LoadItems(); foreach (KeyValuePair<string, ScheduleItem> item in s.Items) { if (treeView1.SelectedNode.Text == item.Key) { this.label9.Text = item.Value.Movie.MovieName; this.label3.Text = item.Value.Movie.Director; this.label4.Text = item.Value.Movie.Actor; this.label5.Text = item.Value.Movie.MovieType.ToString(); this.label8.Text = item.Value.Time; this.label7.Text = item.Value.Movie.Price.ToString(); this.pictureBox1.Image = Image.FromFile(item.Value.Movie.Poster); this.label6.Text = ""; } } foreach (Ticket ticket in Cinema.SoldTickets) { foreach (Seat seat in Cinema.Seats.Values) { if ((ticket.ScheduItem.Time == key) && (ticket.Seat.SeatNum == seat.SeatNum)) { seat.Color = Color.Red; } } } } public void ClearSeat() { foreach (Seat seat in Cinema.Seats.Values) { seat.Color = Color.Blue; } } private void radioButton1_CheckedChanged(object sender, EventArgs e) { this.textBox1.Enabled = false; this.comboBox1.Enabled = false; this.comboBox1.Text = ""; this.label6.Text = "0"; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { this.textBox1.Enabled = true; this.comboBox1.Enabled = false; this.comboBox1.Text = ""; this.label6.Text = "0"; } private void radioButton3_CheckedChanged(object sender, EventArgs e) { this.textBox1.Enabled = false; this.textBox1.Text = ""; this.comboBox1.Enabled = true; this.comboBox1.Text = "7"; if (this.label7.Text!="") { int Price = int.Parse(this.label7.Text); int discount = int.Parse(this.comboBox1.Text); this.label6.Text = (Price*discount/10).ToString(); } } private void pictureBox2_Click(object sender, EventArgs e) { } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } } }
    转载请注明原文地址: https://ju.6miu.com/read-34717.html

    最新回复(0)