C#学习经历从基本语法结构到窗体再到面向对象终于走完了.NET初级程序员的道路,做为品德优良好学生更不能落下课程的总结项目-某某鸟《影院售票系统》。用了大概一天半的时间做完这个练手项目,先上效果截图一张
抽出时间做些这个对目前的我来说算不小的项目。
用到的知识点有:面向对象思想、TreeView、XML读取、File文件流、泛型集合,这里面对我来说难度最大的是面向对象与泛型集合的结合,看来学习一门编程语言的难点还是在设计思想上。
再来介绍一下项目需求:在影片列表中选择某个时段的一场电影,单击座位选择一个种类的电影票,并创建电影,计算价格并打印影票信息,然后该座位被置为红色表示已经售出。
影院每天更新放映列表,系统支持实时查看,包括电影放映场次时间、电影概况。
影院提供三类影票:普通票、赠票和学生票(赠票免费;学生票有折扣)
允许用户查看某场次座位的售出情况
支持购票,并允许用户选座
用户可以选择场次、影票类型及空闲座位进行购票,并打印电影票
系统可以保存销售情况,并允许对其进行恢复
二.问题分析
1.系统开发步骤
(1)明确需求
(2)设计类
(3)创建项目
(4)确定编码顺序
1.主窗体
2.查看新放映列表
3.查看电影介绍
4.查看影票票价
5.查看放映厅座位
6.购票和打印电影票
7.继续购票
(5)测试
三、类的设计
1.Seat:保存影院的座位信息,主要属性如下
座位号(SeatNum):string类型
座位卖出状态颜色(Color):System.Drawing.Color类型
2.Movie:电影类
电影名(MovieName):string类型
海报图片路径(Poster):string类型
导演名(Director):string类型
主演(Actor):string类型
电影类型(MovieType):MovieType自定义枚举类型
定价(Price):int类型
3.Ticket:电影票父类,保存电影票信息
放映场次(ScheduleItem):ScheduleItem自定义类
所属座位对象(Seat):Seat自定义类型
票价(Price):int类型
计算票价的虚方法CalcPrice()
打印售票信息的虚方法Print()
显示当前售出票信息的虚方法Show()
4.StudentTicket:学生票子类,继承父类Ticket
学生票的折扣(Discount):int类型
重写父类计算票价CalcPrice
重写父类打印售票信息的Print()
重写父类显示当前出票信息的Show()方法
5.FreeTicket:赠票子类,继承父类Ticket
获得赠票者的名字属性(CustomerName):string类型
重写父类计算票价CalcPrice()
重写父类打印售票信息Print()
重写父类显示当前出票信息Show()
6.ScheduleItem:影院每天计划放映计划的场次,保存每场电影的信息
放映时间属性(Time):string类型
本场所放映电影属性(Movie):Movie自定义类型
7.Schedule:放映计划类
放映场次属性(Items):自定义泛型集合Dictionary<string,ScheduleItem>
读取XML文件获取放映计划集合的LoadItems()方法
8.Cinema:影院类,保存放映计划和座位类
座位集合属性(Seat):自定义泛型集合Dictionary<string,Seat>
放映计划Schedule:Schedule自定义类型
已售出电影票的集合(SoldTicket):自定义泛型集合List<Ticket>
保存和读取售票情况的Save()和Load()方法
献上这9个类的代码,根据依赖编写类的顺序,不能完全按照上面顺序
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Drawing; 7 8 namespace 影院售票系统 9 {10 /// <summary>11 /// 保存影院的座位信息12 /// </summary>13 public class Seat14 {15 public Seat() { }16 public Seat(string seatNum,Color color) 17 {18 this.SeatNum = seatNum;19 this.Color = color;20 }21 private string _seatNum;22 /// <summary>23 /// 座位号24 /// </summary>25 public string SeatNum26 {27 get { return _seatNum; }28 set { _seatNum = value; }29 }30 private Color _color;31 /// <summary>32 /// 座位卖出状态颜色33 /// </summary>34 public Color Color35 {36 get { return _color; }37 set { _color = value; }38 }39 }40 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 影院售票系统 8 { 9 /// <summary>10 /// 电影类11 /// </summary>12 public class Movie13 {14 private string _movieName;15 /// <summary>16 /// 电影名17 /// </summary>18 public string MovieName19 {20 get { return _movieName; }21 set { _movieName = value; }22 }23 private string _poster;24 /// <summary>25 /// 海报图片名26 /// </summary>27 public string Poster28 {29 get { return _poster; }30 set { _poster = value; }31 }32 private string _director;33 /// <summary>34 /// 导演名35 /// </summary>36 public string Director37 {38 get { return _director; }39 set { _director = value; }40 }41 private string _actor;42 /// <summary>43 /// 主演44 /// </summary>45 public string Actor46 {47 get { return _actor; }48 set { _actor = value; }49 }50 51 private int _price;52 /// <summary>53 /// 定价54 /// </summary>55 public int Price56 {57 get { return _price; }58 set { _price = value; }59 }60 /// <summary>61 /// 电影类型62 /// </summary>63 public MovieType MovieType { get; set; }64 }65 /// <summary>66 /// 电影类型,1喜剧2战争3爱情67 /// </summary>68 public enum MovieType69 {70 /// <summary>71 /// 动作片72 /// </summary>73 Action = 0,74 /// <summary>75 /// 战争片76 /// </summary>77 War = 1,78 /// <summary>79 /// 爱情片80 /// </summary>81 Comedy = 282 }83 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 using System.IO; 8 9 namespace 影院售票系统10 {11 /// <summary>12 /// 电影票父类13 /// </summary>14 public class Ticket15 {16 public Ticket() { }17 public Ticket(ScheduleItem sch,Seat seat) 18 {19 this.ScheduItem = sch;20 this.Seat = seat;21 }22 private Seat _seat = new Seat();23 /// <summary>24 /// 所属座位25 /// </summary>26 public Seat Seat27 {28 get { return _seat; }29 set { _seat = value; }30 }31 32 private int _price;33 /// <summary>34 /// 票价35 /// </summary>36 public int Price37 {38 get { return _price; }39 set { _price = value; }40 }41 /// <summary>42 /// 放映场次43 /// </summary>44 public ScheduleItem ScheduItem { get; set; }45 /// <summary>46 /// 计算票价47 /// </summary>48 public virtual void CalcPrice()49 {50 this.Price = ScheduItem.Movie.Price;51 }52 /// <summary>53 /// 打印售票信息54 /// </summary>55 public virtual void Print()56 {57 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);58 MessageBox.Show(info);59 //存到文件中60 string fileName = this.ScheduItem.Time.Replace(":", "-")+" "+this.Seat.SeatNum+".txt";61 FileStream fs = new FileStream(fileName,FileMode.Create);62 StreamWriter sw = new StreamWriter(fs);63 sw.Write(info);64 sw.Close();65 fs.Close();66 }67 /// <summary>68 /// 显示当前售票信息69 /// </summary>70 public virtual void Show()71 {72 string info = string.Format("已售出!\n普通票!");73 MessageBox.Show(info);74 }75 }76 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 using System.IO; 8 namespace 影院售票系统 9 {10 /// <summary>11 /// 学生票12 /// </summary>13 public class StudentTicket : Ticket14 {15 public StudentTicket() { }16 public StudentTicket(ScheduleItem sch, Seat seat, int discount)17 : base(sch, seat)18 {19 this.Discount = discount;20 }21 private int _discount;22 /// <summary>23 /// 学生票的折扣24 /// </summary>25 public int Discount26 {27 get { return _discount; }28 set { _discount = value; }29 }30 /// <summary>31 /// 计算学生票价32 /// </summary>33 public override void CalcPrice()34 {35 this.Price =this.ScheduItem.Movie.Price* Discount / 10;36 }37 /// <summary>38 /// 打印学生票的售票信息39 /// </summary>40 public override void Print()41 {42 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);43 MessageBox.Show(info);44 //存到文件中45 string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";46 FileStream fs = new FileStream(fileName, FileMode.Create);47 StreamWriter sw = new StreamWriter(fs);48 sw.Write(info);49 sw.Close();50 fs.Close();51 }52 /// <summary>53 /// 显示当前售出票信息54 /// </summary>55 public override void Show()56 {57 string info = string.Format("已售出!\n{0}折学生票!",this.Discount);58 MessageBox.Show(info);59 }60 }61 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 using System.IO; 8 9 namespace 影院售票系统10 {11 /// <summary>12 /// 赠票13 /// </summary>14 public class FreeTicket:Ticket15 {16 public FreeTicket() { }17 public FreeTicket(ScheduleItem sch,Seat seat,string name) 18 {19 this.Seat = seat;20 this.CustomerName = name;21 this.ScheduItem = sch;22 }23 private string _customerName;24 /// <summary>25 /// 获得赠票者的名字26 /// </summary>27 public string CustomerName28 {29 get { return _customerName; }30 set { _customerName = value; }31 }32 /// <summary>33 /// 计算票价34 /// </summary>35 public override void CalcPrice()36 {37 this.Price = 0;38 }39 /// <summary>40 /// 打印售票信息41 /// </summary>42 public override void Print()43 {44 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);45 MessageBox.Show(info);46 //存到文件中47 string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";48 FileStream fs = new FileStream(fileName, FileMode.Create);49 StreamWriter sw = new StreamWriter(fs);50 sw.Write(info);51 sw.Close();52 fs.Close();53 }54 /// <summary>55 /// 显示当前售出票信息56 /// </summary>57 public override void Show()58 {59 MessageBox.Show("已售出!\n赠票!");60 }61 }62 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 影院售票系统 8 { 9 /// <summary>10 /// 影院每天计划放映的场次,保存每场电影的信息11 /// </summary>12 public class ScheduleItem13 {14 private string _time;15 /// <summary>16 /// 放映时间17 /// </summary>18 public string Time19 {20 get { return _time; }21 set { _time = value; }22 }23 private Movie _movie = new Movie();24 25 /// <summary>26 /// 本场放映的电影27 /// </summary>28 public Movie Movie29 {30 get { return _movie; }31 set { _movie = value; }32 }33 private List<Ticket> _soldTickets=new List<Ticket>();34 35 private Dictionary<string, Seat> _seats=new Dictionary<string,Seat>();36 /// <summary>37 /// 本场次的座位状态38 /// </summary>39 public Dictionary<string, Seat> Seats40 {41 get { return _seats; }42 set { _seats = value; }43 }44 }45 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 8 namespace 影院售票系统 9 {10 /// <summary>11 /// 放映计划类,保存影院当天的放映计划集合12 /// </summary>13 public class Schedule14 {15 /// <summary>16 /// 放映场次17 /// </summary>18 public Dictionary<string, ScheduleItem> Items = new Dictionary<string, ScheduleItem>();19 /// <summary>20 /// 读取XML文件获取放映计划集合21 /// </summary>22 public void LoadItems()23 {24 Items.Clear();25 XmlDocument xml = new XmlDocument();26 xml.Load("ShowList.xml");27 XmlElement root = xml.DocumentElement;28 foreach (XmlNode item in root.ChildNodes)29 {30 Movie movie = new Movie();31 movie.MovieName = item["Name"].InnerText;32 movie.Poster = item["Poster"].InnerText;33 movie.Director = item["Director"].InnerText;34 movie.Actor = item["Actor"].InnerText;35 switch (item["Type"].InnerText)36 {37 case "Action":38 movie.MovieType = MovieType.Action;39 break;40 case "War":41 movie.MovieType = MovieType.War;42 break;43 case "Comedy":44 movie.MovieType = MovieType.Comedy;45 break;46 }47 movie.Price = Convert.ToInt32(item["Price"].InnerText);48 if (item["Schedule"].HasChildNodes)49 {50 foreach (XmlNode item2 in item["Schedule"].ChildNodes)51 {52 ScheduleItem schItem = new ScheduleItem();53 schItem.Time = item2.InnerText;54 schItem.Movie = movie;55 Items.Add(schItem.Time, schItem);56 }57 }58 59 }60 61 62 }63 }64 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 影院售票系统 8 { 9 /// <summary>10 /// 影院类11 /// </summary>12 public class Cinema13 {14 private Dictionary<string, Seat> _seats = new Dictionary<string, Seat>();15 /// <summary>16 /// 座位集合17 /// </summary>18 public Dictionary<string, Seat> Seats19 {20 get { return _seats; }21 set { _seats = value; }22 }23 private Schedule _schedule = new Schedule();24 /// <summary>25 /// 放映计划26 /// </summary>27 public Schedule Schedule28 {29 get { return _schedule; }30 set { _schedule = value; }31 }32 private List<Ticket> _soldTickets=new List<Ticket>();33 /// <summary>34 /// 已经售出的票35 /// </summary>36 public List<Ticket> SoldTickets37 {38 get { return _soldTickets; }39 set { _soldTickets = value; }40 }41 /// <summary>42 /// 保存售票信息到文件中43 /// </summary>44 public void Save() 45 {46 //Save和Load的代码在窗体的代码实现了47 }48 /// <summary>49 /// 从文件中读取售票信息50 /// </summary>51 public void Load() { }52 }53 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 影院售票系统 8 { 9 /// <summary>10 /// 工具类11 /// </summary>12 public class TicketUtil13 {14 /// <summary>15 /// 创建电影票16 /// </summary>17 /// <returns></returns>18 public static Ticket CreateTicket(ScheduleItem sch,Seat seat,int discount,string customerName,string type)19 {20 Ticket ticket=null;21 switch (type)22 {23 case "StudentTicket":24 ticket = new StudentTicket(sch,seat,discount);25 break;26 case "FreeTicket":27 ticket = new FreeTicket(sch,seat,customerName);28 break;29 default:30 ticket = new Ticket(sch,seat);31 break;32 }33 return ticket;34 }35 }36 }
明天将继续更新-电影院座位的动态绘制、电影信息绑定到窗体中展现出来,也望各路大神出手斧正不合理的代码(不要涉及分层开发,我们在学,以后会用分层开发实现其他的项目)
- 2楼猫奴
- 好文章,收藏了
- 1楼稳稳的河
- 用的WPF?Print()方法封装下(试试别做成写入文件的),没有数据库的操作?都用枚举代替,你自己也说了没分成,刚学很不错了,尤其动态页面这一块还是不简单的
- Re: 乐智
- @稳稳的河,我们没还学WPF,谢谢鼓励,一定会努力的