C#编写影院售票系统(A project with a higher amount of gold )(2:相关代码)
此篇文章为项目代码,,,需要项目需求 ,思路分析与窗体效果请访问: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); , Seat.SeatNum.Length - ); 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() { ; } //打印售票信息 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); , Seat.SeatNum.Length - ); 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() { ; } //打印售票信息 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); , Seat.SeatNum.Length - ); 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; ; ; ; i < seatRow; i++) { ; j < seatLine; j++) { label = new Label(); label.BackColor = Color.Yellow; label.Font = )); label.AutoSize = false; label.Size = , ); label.Text = (j + ).ToString() + ).ToString(); label.TextAlign = ContentAlignment.MiddleCenter; label.Location = + (i * ), + (j * )); 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; } if (rdoFree.Checked && string.IsNullOrEmpty(txtCustomer.Text)) { MessageBox.Show("请输入赠片人姓名"); txtCustomer.Focus(); return false; } else { return true; } } //普通票 private void rdoNormal_Click(object sender, EventArgs e) { try { Price(); txtCustomer.Text = null; cmbDisCount.Text = null; cmbDisCount.Enabled = false; txtCustomer.Enabled = false; } catch (Exception) { //MessageBox.Show("请先选择场次再选择票类型"); } } //赠票(免费) private void rdoFree_Click(object sender, EventArgs e) { try { Price(); cmbDisCount.Text = null; cmbDisCount.Enabled = false; txtCustomer.Enabled = true; } catch (Exception) { //MessageBox.Show("请先选择场次再选择票类型"); } } //学生票(折扣) private void rdoStudent_Click(object sender, EventArgs e) { try { Price(); txtCustomer.Text = null; cmbDisCount.SelectedIndex = ; cmbDisCount.Enabled = true; txtCustomer.Enabled = false; } catch (Exception) { // MessageBox.Show("请先选择场次再选择票类型"); } } //获取新放映列表 private void tsmiNew_Click(object sender, EventArgs e) { cinema = new Cinema(); Information_Emptying(); AddTree(); Butten(); dictionary.Clear(); foreach (TreeNode item in tvMovies.Nodes) { foreach (TreeNode items in item.Nodes) { Cinema c = new Cinema(); dictionary.Add(items.Text, c); } } } //树形菜单,,点击节点作出的动作 private void tvMovies_AfterSelect(object sender, TreeViewEventArgs e) { rdoNormal.Checked = true; if (tvMovies.SelectedNode.Tag == null) { return; } Information_Filling(); Butten(); Change_Color(); UpdateSeat(); } //接收计算的最终票价 public void Price() { Ticket ticket = Chose_Rdo(); ticket.Price = Convert.ToInt32(lblPrice.Text); this.lblCalcPrice.Text = ticket.CalcPrice().ToString(); } //判断选择的票类型,,实例化相应 的类并做出返回值 public Ticket Chose_Rdo() { Ticket ticket = null; if (rdoStudent.Checked) { ticket = TicketTtil.CreateTicket(rdoStudent.Text); (ticket as StudentTicket).Discount = Convert.ToInt32(this.cmbDisCount.Text); } else if (rdoFree.Checked) { ticket = TicketTtil.CreateTicket(rdoFree.Text); } else { ticket = TicketTtil.CreateTicket(rdoNormal.Text); } return ticket; } //下拉框选择相应折扣计算票价 private void cmbDisCount_SelectedIndexChanged(object sender, EventArgs e) { if (cmbDisCount.Enabled == true) { return; } Price(); } //保存(持久化)(关闭窗体后下次打开,点击继续销售可继续上次销售的票) private void tsmiSave_Click(object sender, EventArgs e) { cinema.Save(dictionary); } //窗体关闭时的事件(持久化)(关闭窗体后下次打开,点击继续销售可继续上次销售的票) private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { DialogResult result = MessageBox.Show("是否保存当前销售状态!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (result == DialogResult.OK) { cinema.Save(dictionary); } } //继续销售 可继续上次销售的票 private void tsmiMovies_Click(object sender, EventArgs e) { cinema = new Cinema(); Information_Emptying(); AddTree(); Butten(); dictionary.Clear(); foreach (TreeNode item in tvMovies.Nodes) { foreach (TreeNode items in item.Nodes) { Cinema c = new Cinema(); dictionary.Add(items.Text, c); } } dictionary = cinema.Load(dictionary); } } }
C#编写影院售票系统(A project with a higher amount of gold )(2:相关代码)的更多相关文章
- C#编写影院售票系统(A project with a higher amount of gold )
项目需求: 影院售票系统 1.基础设施 放映厅 座位集合 2.一个海报------------>放映计划 3.售票设置----------->观影 领域模型:程序中提炼出的实体 4.从电影 ...
- C#总结项目《影院售票系统》编写总结一
C#学习经历从基本语法结构到窗体再到面向对象终于走完了.NET初级程序员的道路,做为品德优良好学生更不能落下课程的总结项目-某某鸟<影院售票系统>.用了大概一天半的时间做完这个练手项目,先 ...
- 「影院售票系统」 · Java Swing + MySQL JDBC开发
目录 文档说明: 一.语言和环境 二.实现功能 三.数据库设计 四.具体要求及推荐实现步骤 五.注意事项 六.评分标准 实现代码: 一.数据库: 二.Java Swing: com.ynavc.Bea ...
- C#总结项目《影院售票系统》编写总结完结篇
回顾:昨天总结了影院售票系统核心部分-售票,整个项目也就完成了2/3了,需求中也要求了对销售信息的保存,今天就继续总结销售信息的保存以及加载销售信息. 分析:退出程序时将已售出票集合中的对象循环写入到 ...
- C#总结项目《影院售票系统》编写总结三
昨天总结了动态绘制控件.票类型的切换以及数据在窗体中的展现.今天继续总结,自己喜欢的就去做吧,让别人说去吧,省的自己再留下什么后悔遗憾,噢耶,加油! 今天总结项目中最核心的部分--购票.座位颜色状态的 ...
- C#总结项目《影院售票系统》编写总结二
昨天发布了总结项目的第一篇,需求分析以及类的搭建,今天继续更新,动态绘制控件.票类型的切换以及数据在窗体中的展现. 先从简单的开始,票类型的切换. 分析: 1.当点击普通票时 学生折扣和赠送者是禁用的 ...
- 努力做一个优秀的programmer [ C# 影院售票系统]
Cinema.cs类 [Serializable] // 电影院类 public class Cinema { public Cinema() { //二进制 SoldTickets = new Li ...
- GitHub看板系统(Project)
/********************************************************************** * GitHub看板系统(Project) * 说明: ...
- 干货 | C#开发的电影售票系统
01 介绍 一个有会员制的电影院购票系统.具有会员注册功能,可区分会员和散客两种身份,实现会员及折扣管理.购票具有挑选电影场次,选择座位和查看电影信息等功能. 查看电影详情.获取排片信息. 选择场次座 ...
随机推荐
- Git操作流程,基本命令演示
任务列表: 有一个中央库Center,和三个工作站A,B,C. 初始化时,代码存放在中央库中,A,B,C三个工作站开始工作之前都要首先从中央库克隆一份代码到本地. 第一个任务:A和B合作修复一个缺陷, ...
- c# 调用python语言
config 文件配置 <configuration>节中 第一个的位置插入如下节点,版本根据实际用到的来写 <configSections> <section ...
- 使用XML序列化实现系统配置 - 开源研究系列文章
在实际的C#软件系统开发过程中,会遇到系统配置的保存问题,以及系统存储问题.在以前的系统开发过程中,笔者使用的是INI文件配置管理的方式.到了现在,INI文件配置保存仍然是一个平常使用的方式.在博客园 ...
- 韩顺平教学资源java、oracle、linux
http://blog.itpub.net/28688617/viewspace-766392/
- LeetCode题目总结(一)
我的代码在github上,https://github.com/WINTERFELLS/LeetCode-Answers 这里只提供个人的解题思路,不一定是最好的. Problems1-20 寻找两个 ...
- MNIST-NameError: name ‘input_data’ is not defined解决办法
在学习TensorFlow文档教程的时候, 在MNIST入门一节,发现直接运行下载MNIST数据集的代码会报错.原代码如下: import tensorflow.examples.tutorials. ...
- 初识Hibernate的主配置和映射配置
Hibernate.cfg.xml 主配置 Hibernate.cfg.xml 主配置文件夹中主要配置:数据库链接配置,其他参数配置,映射信息等. 常用配置查看源码: hibernate-distri ...
- 【数论】洛谷P1414又是毕业季II
题目背景 "叮铃铃铃",随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业 ...
- hdf5 AttributeError: 'UnImplemented' object has no attribute 'read'
问题现象:最近在用pandas分析数据时,用hdf5存储结果,当我监听不同文件时,多个进程同时写入hdf5(save到不同group)时,报hdf5 AttributeError: 'UnImplem ...
- js变量提升小记
作为世界上最优美的语言javascript的使用者,呵呵,js的魅力是无穷的,今天来聊聊他的魅(dan)力(teng)之一,变量提升. 每种语言所定义的变量基本都会有一定得作用域,而js的作用域则存在 ...