C#编写影院售票系统(A project with a higher amount of gold )(2:相关代码)
程序员文章站
2022-04-02 19:08:02
此篇文章为项目代码,,,需要项目需求 ,思路分析与窗体效果请访问: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("请选择影片放映时间