首页 > 编程 > C# > 正文

C#影院售票系统毕业设计(1)

2019-10-29 21:35:59
字体:
来源:转载
供稿:网友
这篇文章主要介绍了C#影院售票系统毕业设计,献上了9个类的设计,需要的朋友可以参考下
 

 C#学习经历从基本语法结构到窗体再到面向对象终于走完了.NET初级程序员的道路,用了大概一天半的时间做完这个练手项目《影院售票系统》,先上效果截图一张

C#影院售票系统毕业设计(1)

抽出时间做些这个对目前的我来说算不小的项目。

用到的知识点有:面向对象思想、TreeView、XML读取、File文件流、泛型集合,这里面对我来说难度最大的是面向对象与泛型集合的结合,看来学习一门编程语言的难点还是在设计思想上。

再来介绍一下项目需求:在影片列表中选择某个时段的一场电影,单击座位选择一个种类的电影票,并创建电影,计算价格并打印影票信息,然后该座位被置为红色表示已经售出。

影院每天更新放映列表,系统支持实时查看,包括电影放映场次时间、电影概况。

影院提供三类影票:普通票、赠票和学生票(赠票免费;学生票有折扣)

允许用户查看某场次座位的售出情况

支持购票,并允许用户选座

用户可以选择场次、影票类型及空闲座位进行购票,并打印电影票

系统可以保存销售情况,并允许对其进行恢复

一、问题分析

1.系统开发步骤

(1)明确需求

(2)设计类

(3)创建项目

(4)确定编码顺序

      1.主窗体

      2.查看新放映列表

      3.查看电影介绍

      4.查看影票票价

     5.查看放映厅座位

     6.购票和打印电影票

     7.继续购票

(5)测试

二、类的设计

 献上这9个类的代码,根据依赖编写类的顺序,不能完全按照上面顺序

1.Seat:保存影院的座位信息,主要属性如下

座位号(SeatNum):string类型

座位卖出状态颜色(Color):System.Drawing.Color类型
 

  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 Seat 
  14.  { 
  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 SeatNum 
  26.  { 
  27.  get { return _seatNum; } 
  28.  set { _seatNum = value; } 
  29.  } 
  30.  private Color _color; 
  31.  /// <summary> 
  32.  /// 座位卖出状态颜色 
  33.  /// </summary> 
  34.  public Color Color 
  35.  { 
  36.  get { return _color; } 
  37.  set { _color = value; } 
  38.  } 
  39.  } 
  40.  } 
?

2.Movie:电影类

电影名(MovieName):string类型

海报图片路径(Poster):string类型

导演名(Director):string类型

主演(Actor):string类型

电影类型(MovieType):MovieType自定义枚举类型

定价(Price):int类型
 

  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 Movie 
  13.  { 
  14.  private string _movieName; 
  15.  /// <summary> 
  16.  /// 电影名 
  17.  /// </summary> 
  18.  public string MovieName 
  19.  { 
  20.  get { return _movieName; } 
  21.  set { _movieName = value; } 
  22.  } 
  23.  private string _poster; 
  24.  /// <summary> 
  25.  /// 海报图片名 
  26.  /// </summary> 
  27.  public string Poster 
  28.  { 
  29.  get { return _poster; } 
  30.  set { _poster = value; } 
  31.  } 
  32.  private string _director; 
  33.  /// <summary> 
  34.  /// 导演名 
  35.  /// </summary> 
  36.  public string Director 
  37.  { 
  38.  get { return _director; } 
  39.  set { _director = value; } 
  40.  } 
  41.  private string _actor; 
  42.  /// <summary> 
  43.  /// 主演 
  44.  /// </summary> 
  45.  public string Actor 
  46.  { 
  47.  get { return _actor; } 
  48.  set { _actor = value; } 
  49.  } 
  50.   
  51.  private int _price; 
  52.  /// <summary> 
  53.  /// 定价 
  54.  /// </summary> 
  55.  public int Price 
  56.  { 
  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 MovieType 
  69.  { 
  70.  /// <summary> 
  71.  /// 动作片 
  72.  /// </summary> 
  73.  Action = 0, 
  74.  /// <summary> 
  75.  /// 战争片 
  76.  /// </summary> 
  77.  War = 1, 
  78.  /// <summary> 
  79.  /// 爱情片 
  80.  /// </summary> 
  81.  Comedy = 2 
  82.  } 
  83.  } 
?

3.Ticket:电影票父类,保存电影票信息

放映场次(ScheduleItem):ScheduleItem自定义类

所属座位对象(Seat):Seat自定义类型

票价(Price):int类型

计算票价的虚方法CalcPrice()

打印售票信息的虚方法Print()

显示当前售出票信息的虚方法Show() 
 

  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. /// <summary> 
  11. /// 电影票父类 
  12. /// </summary> 
  13. public class Ticket 
  14. public Ticket() { } 
  15. public Ticket(ScheduleItem sch,Seat seat)  
  16. this.ScheduItem = sch; 
  17. this.Seat = seat; 
  18. private Seat _seat = new Seat(); 
  19. /// <summary> 
  20. /// 所属座位 
  21. /// </summary> 
  22. public Seat Seat 
  23. get { return _seat; } 
  24. set { _seat = value; } 
  25.  
  26. private int _price; 
  27. /// <summary> 
  28. /// 票价 
  29. /// </summary> 
  30. public int Price 
  31. get { return _price; } 
  32. set { _price = value; } 
  33. /// <summary> 
  34. /// 放映场次 
  35. /// </summary> 
  36. public ScheduleItem ScheduItem { get; set; } 
  37. /// <summary> 
  38. /// 计算票价 
  39. /// </summary> 
  40. public virtual void CalcPrice() 
  41. this.Price = ScheduItem.Movie.Price; 
  42. /// <summary> 
  43. /// 打印售票信息 
  44. /// </summary> 
  45. public virtual void Print() 
  46. 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); 
  47. MessageBox.Show(info); 
  48. //存到文件中 
  49. string fileName = this.ScheduItem.Time.Replace(":""-")+" "+this.Seat.SeatNum+".txt"
  50. FileStream fs = new FileStream(fileName,FileMode.Create); 
  51. StreamWriter sw = new StreamWriter(fs); 
  52. sw.Write(info); 
  53. sw.Close(); 
  54. fs.Close(); 
  55. /// <summary> 
  56. /// 显示当前售票信息 
  57. /// </summary> 
  58. public virtual void Show() 
  59. string info = string.Format("已售出!/n普通票!"); 
  60. MessageBox.Show(info); 
?

4.StudentTicket:学生票子类,继承父类Ticket

学生票的折扣(Discount):int类型

重写父类计算票价CalcPrice

重写父类打印售票信息的Print()

重写父类显示当前出票信息的Show()方法
 

  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. /// <summary> 
  10. /// 学生票 
  11. /// </summary> 
  12. public class StudentTicket : Ticket 
  13. public StudentTicket() { } 
  14. public StudentTicket(ScheduleItem sch, Seat seat, int discount) 
  15. : base(sch, seat) 
  16. this.Discount = discount; 
  17. private int _discount; 
  18. /// <summary> 
  19. /// 学生票的折扣 
  20. /// </summary> 
  21. public int Discount 
  22. get { return _discount; } 
  23. set { _discount = value; } 
  24. /// <summary> 
  25. /// 计算学生票价 
  26. /// </summary> 
  27. public override void CalcPrice() 
  28. this.Price =this.ScheduItem.Movie.Price* Discount / 10; 
  29. /// <summary> 
  30. /// 打印学生票的售票信息 
  31. /// </summary> 
  32. public override void Print() 
  33. 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); 
  34. MessageBox.Show(info); 
  35. //存到文件中 
  36. string fileName = this.ScheduItem.Time.Replace(":""-") + " " + this.Seat.SeatNum + ".txt"
  37. FileStream fs = new FileStream(fileName, FileMode.Create); 
  38. StreamWriter sw = new StreamWriter(fs); 
  39. sw.Write(info); 
  40. sw.Close(); 
  41. fs.Close(); 
  42. /// <summary> 
  43. /// 显示当前售出票信息 
  44. /// </summary> 
  45. public override void Show() 
  46. string info = string.Format("已售出!/n{0}折学生票!",this.Discount); 
  47. MessageBox.Show(info); 
?

5.FreeTicket:赠票子类,继承父类Ticket

获得赠票者的名字属性(CustomerName):string类型

重写父类计算票价CalcPrice()

重写父类打印售票信息Print()

重写父类显示当前出票信息Show()
 

  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. /// <summary> 
  11. /// 赠票 
  12. /// </summary> 
  13. public class FreeTicket:Ticket 
  14. public FreeTicket() { } 
  15. public FreeTicket(ScheduleItem sch,Seat seat,string name)  
  16. this.Seat = seat; 
  17. this.CustomerName = name; 
  18. this.ScheduItem = sch; 
  19. private string _customerName; 
  20. /// <summary> 
  21. /// 获得赠票者的名字 
  22. /// </summary> 
  23. public string CustomerName 
  24. get { return _customerName; } 
  25. set { _customerName = value; } 
  26. /// <summary> 
  27. /// 计算票价 
  28. /// </summary> 
  29. public override void CalcPrice() 
  30. this.Price = 0; 
  31. /// <summary> 
  32. /// 打印售票信息 
  33. /// </summary> 
  34. public override void Print() 
  35. 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); 
  36. MessageBox.Show(info); 
  37. //存到文件中 
  38. string fileName = this.ScheduItem.Time.Replace(":""-") + " " + this.Seat.SeatNum + ".txt"
  39. FileStream fs = new FileStream(fileName, FileMode.Create); 
  40. StreamWriter sw = new StreamWriter(fs); 
  41. sw.Write(info); 
  42. sw.Close(); 
  43. fs.Close(); 
  44. /// <summary> 
  45. /// 显示当前售出票信息 
  46. /// </summary> 
  47. public override void Show() 
  48. MessageBox.Show("已售出!/n赠票!"); 
?

6.ScheduleItem:影院每天计划放映计划的场次,保存每场电影的信息

放映时间属性(Time):string类型

本场所放映电影属性(Movie):Movie自定义类型
 

  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 ScheduleItem 
  13.  { 
  14.  private string _time; 
  15.  /// <summary> 
  16.  /// 放映时间 
  17.  /// </summary> 
  18.  public string Time 
  19.  { 
  20.  get { return _time; } 
  21.  set { _time = value; } 
  22.  } 
  23.  private Movie _movie = new Movie(); 
  24.   
  25.  /// <summary> 
  26.  /// 本场放映的电影 
  27.  /// </summary> 
  28.  public Movie Movie 
  29.  { 
  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> Seats 
  40.  { 
  41.  get { return _seats; } 
  42.  set { _seats = value; } 
  43.  } 
  44.  } 
  45.  } 
?

7.Schedule:放映计划类

放映场次属性(Items):自定义泛型集合Dictionary<string,ScheduleItem>

读取XML文件获取放映计划集合的LoadItems()方法

?
  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 Schedule 
  14.  { 
  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.  } 

8.Cinema:影院类,保存放映计划和座位类

座位集合属性(Seat):自定义泛型集合Dictionary<string,Seat>

放映计划Schedule:Schedule自定义类型

已售出电影票的集合(SoldTicket):自定义泛型集合List<Ticket>

保存和读取售票情况的Save()和Load()方法
 

  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. /// <summary> 
  9. /// 影院类 
  10. /// </summary> 
  11. public class Cinema 
  12. private Dictionary<string, Seat> _seats = new Dictionary<string, Seat>(); 
  13. /// <summary> 
  14. /// 座位集合 
  15. /// </summary> 
  16. public Dictionary<string, Seat> Seats 
  17. get { return _seats; } 
  18. set { _seats = value; } 
  19. private Schedule _schedule = new Schedule(); 
  20. /// <summary> 
  21. /// 放映计划 
  22. /// </summary> 
  23. public Schedule Schedule 
  24. get { return _schedule; } 
  25. set { _schedule = value; } 
  26. private List<Ticket> _soldTickets=new List<Ticket>(); 
  27. /// <summary> 
  28. /// 已经售出的票 
  29. /// </summary> 
  30. public List<Ticket> SoldTickets 
  31. get { return _soldTickets; } 
  32. set { _soldTickets = value; } 
  33. /// <summary> 
  34. /// 保存售票信息到文件中 
  35. /// </summary> 
  36. public void Save()  
  37. //Save和Load的代码在窗体的代码实现了 
  38. /// <summary> 
  39. /// 从文件中读取售票信息 
  40. /// </summary> 
  41. public void Load() { } 
?

9.工具类
 

  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 TicketUtil 
  13.  { 
  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.  } 
?

下篇文章将继续更新,内容有电影院座位的动态绘制、电影信息绑定到窗体中展现出来,希望大家不要走开,有什么地方需要改进的欢迎大家指出,共同探讨进步。



注:相关教程知识阅读请移步到c#教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表