此篇文章為項目代碼,,,需要項目需求 ,思路分析與窗體效果請訪問:http://www.cnblogs.com/lsy131479/p/8367304.html ...
此篇文章為項目代碼,,,需要項目需求 ,思路分析與窗體效果請訪問:http://www.cnblogs.com/lsy131479/p/8367304.html
項目類圖:
影院類:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; namespace Theater_Ticket_Selling_System { /// <summary> /// 影院類 /// 保存放映計劃和座位類 /// </summary> [Serializable] public class Cinema { //座位集合 private Dictionary<string, Seat> seats; //放映計劃 private Schedule schedule; //已售出電影票的集合 private List<Ticket> soldTickets; public Cinema() { Schedule = new Schedule(); Schedule.LoadItems(); Seats = new Dictionary<string, Seat>(); SoldTickets = new List<Ticket>(); } public Cinema(Dictionary<string, Seat> seats, Schedule schedule, List<Ticket> soldTickets) { Seats = seats; Schedule = schedule; SoldTickets = soldTickets; } public Dictionary<string, Seat> Seats { get => seats; set => seats = value; } public Schedule Schedule { get => schedule; set => schedule = value; } public List<Ticket> SoldTickets { get => soldTickets; set => soldTickets = value; } //保存售票情況 public void Save(Dictionary<string, Cinema> dictionary) { //創建文件流派 Stream stream = new FileStream("save.bin", FileMode.Create); //二進位序列化 BinaryFormatter bf = new BinaryFormatter(); //將對象或具有指定頂級 (根)、 對象圖序列化到給定的流 bf.Serialize(stream, dictionary); //關閉流 stream.Close(); } //讀取售票情況 public Dictionary<string, Cinema> Load(Dictionary<string, Cinema> dictionary) { //創建文件流派 Stream stream = new FileStream("save.bin", FileMode.Open); //二進位序列化 BinaryFormatter bf = new BinaryFormatter(); //指定的流反序列化對象圖 dictionary = (Dictionary<string, Cinema>)bf.Deserialize(stream); //關閉流 stream.Close(); return dictionary; } } }
電影票類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Theater_Ticket_Selling_System { /// <summary> /// 電影票(父類) /// 保存電影信息 /// </summary> [Serializable] public class Ticket { //放映場次 private ScheduleItem scheduleItem; //所屬座位對象 private Seat seat; //票價 private int price; public Ticket() { } public Ticket(ScheduleItem scheduleItem, Seat seat, int price) { ScheduleItem = scheduleItem; Seat = seat; Price = price; } public ScheduleItem ScheduleItem { get => scheduleItem; set => scheduleItem = value; } public Seat Seat { get => seat; set => seat = value; } public int Price { get => price; set => price = value; } //計算票價 public virtual double CalcPrice() { return Price; } //列印售票 public virtual void Print() { } //顯示當前出票信息 public virtual string Show() { StringBuilder sb = new StringBuilder(); sb.AppendLine("*****************************************"); sb.AppendLine("\t " + "青鳥影院(普通票)"); sb.AppendLine("-----------------------------------------"); sb.AppendLine("電影名:" + ScheduleItem.Movie.MovieName); sb.AppendLine("時間:" + ScheduleItem.Time); string text = Seat.SeatNum.Remove(0, Seat.SeatNum.Length - 3); sb.AppendLine("座位號:" + text); sb.AppendLine("價格:" + Price); sb.AppendLine("*****************************************"); sb.AppendLine("\t " + "(a)普通票"); return sb.ToString(); } } }
贈票類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Theater_Ticket_Selling_System { /// <summary> /// 贈票(子類) /// 保存特殊的增票信息 /// </summary> public class FreeTicket:Ticket { //贈票者名字 private string customerName; public FreeTicket() { } public FreeTicket(ScheduleItem scheduleItem, Seat seat, int price, string customerName) : base(scheduleItem, seat, price) { this.CustomerName = customerName; } public string CustomerName { get => customerName; set => customerName = value; } //計算票價 public override double CalcPrice() { return 0; } //列印售票信息 public override void Print() { base.Print(); } //顯示當前出票信息 public override string Show() { StringBuilder sb = new StringBuilder(); sb.AppendLine("*****************************************"); sb.AppendLine("\t " + "青鳥影院(贈票)"); sb.AppendLine("-----------------------------------------"); sb.AppendLine("電影名:" + ScheduleItem.Movie.MovieName); sb.AppendLine("時間:" + ScheduleItem.Time); string text = Seat.SeatNum.Remove(0, Seat.SeatNum.Length - 3); sb.AppendLine("座位號:" + text); sb.AppendLine("價格:" + CalcPrice()); sb.AppendLine("*****************************************"); sb.AppendLine("\t " + "(a)贈票"); return sb.ToString(); } } }
學生票類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Theater_Ticket_Selling_System { /// <summary> /// 學生票(子類) /// 保存特殊的學生票信息 /// </summary> public class StudentTicket : Ticket { //學生折扣 private int discount; public StudentTicket() { } public StudentTicket(ScheduleItem scheduleItem, Seat seat, int price, int discount) : base(scheduleItem, seat, price) { this.Discount = discount; } public int Discount { get => discount; set => discount = value; } //計算票價 public override double CalcPrice() { return Price * Discount/10; } //列印售票信息 public override void Print() { base.Print(); } //顯示當前出票信息 public override string Show() { StringBuilder sb = new StringBuilder(); sb.AppendLine("*****************************************"); sb.AppendLine("\t " + "青鳥影院(學生票)"); sb.AppendLine("-----------------------------------------"); sb.AppendLine("電影名:" + ScheduleItem.Movie.MovieName); sb.AppendLine("時間:" + ScheduleItem.Time); string text = Seat.SeatNum.Remove(0, Seat.SeatNum.Length - 3); sb.AppendLine("座位號:" + text); sb.AppendLine("價格:" + CalcPrice()); sb.AppendLine("*****************************************"); sb.AppendLine("\t " + "(a)學生票"); return sb.ToString(); } } }
電影類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Theater_Ticket_Selling_System { /// <summary> /// 電影類 /// </summary> [Serializable] public class Movie { //電影名 private string movieName; //海報圖片名 private string poster; //導演名 private string director; //主演 private string actor; //電影類型 private MovieType movieType; //定價 private int price; public Movie() { } public Movie(string movieName, string poster, string director, string actor, MovieType movieType, int price) { MovieName = movieName; Poster = poster; Director = director; Actor = actor; MovieType = movieType; Price = price; } public string MovieName { get => movieName; set => movieName = value; } public string Poster { get => poster; set => poster = value; } public string Director { get => director; set => director = value; } public string Actor { get => actor; set => actor = value; } public MovieType MovieType { get => movieType; set => movieType = value; } public int Price { get => price; set => price = value; } } }
電影類型類(枚舉):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Theater_Ticket_Selling_System { /// <summary> /// 電影類型 /// </summary> public enum MovieType { //喜劇片 Comedy, //戰爭片 War, //浪漫片 Romance, //動作片 Action, //卡通片 Cartoon, //驚悚片 Thriller, //冒險 Adventure } }
放映計劃類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Theater_Ticket_Selling_System { /// <summary> /// 放映計劃 /// 保存影院當天的放映計劃集合 /// </summary> [Serializable] public class Schedule { //放映場次 private Dictionary<string, ScheduleItem> item; public Dictionary<string, ScheduleItem> Item { get => item; set => item = value; } public Schedule() { Item = new Dictionary<string, ScheduleItem>(); } public Schedule(Dictionary<string, ScheduleItem> item) { Item = item; } //讀取XML文件 public void LoadItems() { XmlDocument doc = new XmlDocument(); doc.Load("ShowList.xml"); XmlNode xml = doc.DocumentElement; ScheduleItem scheduleItem = null; 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); foreach (XmlNode items in item["Schedule"].ChildNodes) { scheduleItem = new ScheduleItem(); scheduleItem.Movie = movie; scheduleItem.Time = items.InnerText; Item.Add(scheduleItem.Time, scheduleItem); } } } } }
座位類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace Theater_Ticket_Selling_System { /// <summary> /// 保存影院座位信息 /// </summary> [Serializable] public class Seat { //座位號 private string seatNum; //座位賣出狀態顏色 private Color color; public Seat() { } public Seat(string seatNum, Color color) { SeatNum = seatNum; Color = color; } public string SeatNum { get => seatNum; set => seatNum = value; } public Color Color { get => color; set => color = value; } } }
工具類(設計模式:簡單工廠):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Theater_Ticket_Selling_System { /// <summary> /// 工具類 /// 根據輸入的值,判斷創建不同的電影票對象 /// </summary> public class TicketTtil { public static Ticket CreateTicket(string type) { Ticket ticket = null; switch (type) { case "贈票": ticket = new FreeTicket(); break; case "學生票": ticket = new StudentTicket(); break; default: ticket = new Ticket(); break; } return ticket; } } }
主窗體類:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace Theater_Ticket_Selling_System { /// <summary> /// 影院售票系統 /// </summary> public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } //實例化影院類(所有存儲都是以影院類為基礎) Cinema cinema = new Cinema(); //保存已售座位 Dictionary<string, Cinema> dictionary = new Dictionary<string, Cinema>(); //存儲所有lable控制項信息(方便改變控制項狀態) Dictionary<string, Label> labels = new Dictionary<string, Label>(); //清空所有控制項 public void Information_Emptying() { lblName.Text = null; lblDirector.Text = null; lblActor.Text = null; lblType.Text = null; lblTime.Text = null; lblPrice.Text = null; lblCalcPrice.Text = null; picMovie.Image = null; } //選中影片場次時,對所有控制項賦相應的值,顯示影片信息 public void Information_Filling() { ScheduleItem schedule = (ScheduleItem)tvMovies.SelectedNode.Tag; lblName.Text = schedule.Movie.MovieName; lblDirector.Text = schedule.Movie.Director; lblActor.Text = schedule.Movie.Actor; lblType.Text = schedule.Movie.MovieType.ToString(); lblTime.Text = tvMovies.SelectedNode.Text; lblPrice.Text = schedule.Movie.Price.ToString(); lblCalcPrice.Text = schedule.Movie.Price.ToString(); picMovie.Image = Image.FromFile(schedule.Movie.Poster); } private void FrmMain_Load(object sender, EventArgs e) { Information_Emptying(); Butten(); } //為TreeView控制項動態賦值 public void AddTree() { tvMovies.BeginUpdate(); tvMovies.Nodes.Clear(); TreeNode treeNode = null; string movieName = null; foreach (KeyValuePair<string, ScheduleItem> item in cinema.Schedule.Item) { if (movieName != item.Value.Movie.MovieName) { treeNode = new TreeNode(); treeNode.Text = item.Value.Movie.MovieName; treeNode.Tag = null; tvMovies.Nodes.Add(treeNode); } TreeNode tree = new TreeNode(); tree.Text = item.Key; tree.Tag = item.Value; treeNode.Nodes.Add(tree); movieName = item.Value.Movie.MovieName; } tvMovies.EndUpdate(); } //動態綁定所有lable控制項 public void Butten() { cinema.Seats.Clear(); labels.Clear(); tpCinema.Controls.Clear(); Seat seat = null; Label label = null; int seatRow = 7; int seatLine = 5; for (int i = 0; i < seatRow; i++) { for (int j = 0; j < seatLine; j++) { label = new Label(); label.BackColor = Color.Yellow; label.Font = new Font("宋體", 14.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)134)); label.AutoSize = false; label.Size = new Size(50, 25); label.Text = (j + 1).ToString() + "-" + (i + 1).ToString(); label.TextAlign = ContentAlignment.MiddleCenter; label.Location = new Point(60 + (i * 90), 60 + (j * 60)); label.Click += new EventHandler(lblSeat_Click); labels.Add(label.Text, label); tpCinema.Controls.Add(labels[label.Text]); seat = new Seat(label.Text, Color.Yellow); cinema.Seats.Add(seat.SeatNum, seat); } } } //當確認買票時為已售票集合添加相應場次,座位的信息 public void AddSold(object sender) { Label label = (Label)sender; Ticket ticket = TicketTtil.CreateTicket(tvMovies.SelectedNode.Parent.Text); ticket.ScheduleItem = (ScheduleItem)tvMovies.SelectedNode.Tag; ticket.Seat = cinema.Seats[label.Text]; ticket.Seat.Color = Color.Red; ticket.Price = Convert.ToInt32(this.lblCalcPrice.Text); dictionary[tvMovies.SelectedNode.Text].SoldTickets.Add(ticket); } private void lblSeat_Click(object sender, EventArgs e) { if (!Not_Null()) { return; } string print = Print(sender); if (string.IsNullOrEmpty(print)) { return; } MessageBox.Show(print); } public string Print(object sender) { /* * 買票 * */ Label label = (Label)sender; if (label.BackColor == Color.Red) { MessageBox.Show("已售!"); return null; } DialogResult result = MessageBox.Show("是否購買?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.No) { return null; } AddSold(sender); Change_Color(); UpdateSeat(); MessageBox.Show("購買成功!"); Ticket ticket = Chose_Rdo(); ticket.ScheduleItem = cinema.Schedule.Item[tvMovies.SelectedNode.Text]; ticket.Seat = cinema.Seats[label.Text]; ticket.Price = cinema.Schedule.Item[tvMovies.SelectedNode.Text].Movie.Price; /* * 在電腦硬碟記憶體儲已買座位的小票(方便列印) * */ string time = (ticket.ScheduleItem.Time).Replace(':', '-'); string num = ticket.Seat.SeatNum; string PrintStr = ticket.Show(); string nowTime = (DateTime.Now.ToShortDateString().ToString()).Replace(':', '-').Replace('/', '-'); string path = "Ticket\\" + nowTime + " ^ " + time + " " + num + ".txt"; FileStream fs = new FileStream(path, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.Write(PrintStr); sw.Close(); fs.Close(); return PrintStr; } /* * 遍歷已售票集合與座位集合,,將已售出的相應座位標記到座位集合中 * */ public void Change_Color() { string key = this.tvMovies.SelectedNode.Text; foreach (Ticket item in dictionary[tvMovies.SelectedNode.Text].SoldTickets) { foreach (Seat items in cinema.Seats.Values) { if (item.ScheduleItem.Time == key && item.Seat.SeatNum == items.SeatNum) { /* * 這裡將Color屬性賦值為Red作為已售出的座位 * */ //MessageBox.Show("Test"); items.Color = Color.Red; } } } } //遍歷座位集合。。為控制項改變狀態,表示此座位已售出,(方便用戶) public void UpdateSeat() { foreach (string key in cinema.Seats.Keys) { labels[key].BackColor = cinema.Seats[key].Color; } } /* * 買票時要作出的相應的非空判斷 * */ public bool Not_Null() { if (tvMovies.SelectedNode == null) { MessageBox.Show("請選擇影片"); return false; } if (tvMovies.SelectedNode.Tag == null) { MessageBox.Show("請選擇影片放映時間"); return false