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. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6 using System.Drawing;
  7. 7
  8. 8 namespace 影院售票系统
  9. 9 {
  10. 10 /// <summary>
  11. 11 /// 保存影院的座位信息
  12. 12 /// </summary>
  13. 13 public class Seat
  14. 14 {
  15. 15 public Seat() { }
  16. 16 public Seat(string seatNum,Color color)
  17. 17 {
  18. 18 this.SeatNum = seatNum;
  19. 19 this.Color = color;
  20. 20 }
  21. 21 private string _seatNum;
  22. 22 /// <summary>
  23. 23 /// 座位号
  24. 24 /// </summary>
  25. 25 public string SeatNum
  26. 26 {
  27. 27 get { return _seatNum; }
  28. 28 set { _seatNum = value; }
  29. 29 }
  30. 30 private Color _color;
  31. 31 /// <summary>
  32. 32 /// 座位卖出状态颜色
  33. 33 /// </summary>
  34. 34 public Color Color
  35. 35 {
  36. 36 get { return _color; }
  37. 37 set { _color = value; }
  38. 38 }
  39. 39 }
  40. 40 }

Seat

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6
  7. 7 namespace 影院售票系统
  8. 8 {
  9. 9 /// <summary>
  10. 10 /// 电影类
  11. 11 /// </summary>
  12. 12 public class Movie
  13. 13 {
  14. 14 private string _movieName;
  15. 15 /// <summary>
  16. 16 /// 电影名
  17. 17 /// </summary>
  18. 18 public string MovieName
  19. 19 {
  20. 20 get { return _movieName; }
  21. 21 set { _movieName = value; }
  22. 22 }
  23. 23 private string _poster;
  24. 24 /// <summary>
  25. 25 /// 海报图片名
  26. 26 /// </summary>
  27. 27 public string Poster
  28. 28 {
  29. 29 get { return _poster; }
  30. 30 set { _poster = value; }
  31. 31 }
  32. 32 private string _director;
  33. 33 /// <summary>
  34. 34 /// 导演名
  35. 35 /// </summary>
  36. 36 public string Director
  37. 37 {
  38. 38 get { return _director; }
  39. 39 set { _director = value; }
  40. 40 }
  41. 41 private string _actor;
  42. 42 /// <summary>
  43. 43 /// 主演
  44. 44 /// </summary>
  45. 45 public string Actor
  46. 46 {
  47. 47 get { return _actor; }
  48. 48 set { _actor = value; }
  49. 49 }
  50. 50
  51. 51 private int _price;
  52. 52 /// <summary>
  53. 53 /// 定价
  54. 54 /// </summary>
  55. 55 public int Price
  56. 56 {
  57. 57 get { return _price; }
  58. 58 set { _price = value; }
  59. 59 }
  60. 60 /// <summary>
  61. 61 /// 电影类型
  62. 62 /// </summary>
  63. 63 public MovieType MovieType { get; set; }
  64. 64 }
  65. 65 /// <summary>
  66. 66 /// 电影类型,1喜剧2战争3爱情
  67. 67 /// </summary>
  68. 68 public enum MovieType
  69. 69 {
  70. 70 /// <summary>
  71. 71 /// 动作片
  72. 72 /// </summary>
  73. 73 Action = 0,
  74. 74 /// <summary>
  75. 75 /// 战争片
  76. 76 /// </summary>
  77. 77 War = 1,
  78. 78 /// <summary>
  79. 79 /// 爱情片
  80. 80 /// </summary>
  81. 81 Comedy = 2
  82. 82 }
  83. 83 }

Movie

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6 using System.Windows.Forms;
  7. 7 using System.IO;
  8. 8
  9. 9 namespace 影院售票系统
  10. 10 {
  11. 11 /// <summary>
  12. 12 /// 电影票父类
  13. 13 /// </summary>
  14. 14 public class Ticket
  15. 15 {
  16. 16 public Ticket() { }
  17. 17 public Ticket(ScheduleItem sch,Seat seat)
  18. 18 {
  19. 19 this.ScheduItem = sch;
  20. 20 this.Seat = seat;
  21. 21 }
  22. 22 private Seat _seat = new Seat();
  23. 23 /// <summary>
  24. 24 /// 所属座位
  25. 25 /// </summary>
  26. 26 public Seat Seat
  27. 27 {
  28. 28 get { return _seat; }
  29. 29 set { _seat = value; }
  30. 30 }
  31. 31
  32. 32 private int _price;
  33. 33 /// <summary>
  34. 34 /// 票价
  35. 35 /// </summary>
  36. 36 public int Price
  37. 37 {
  38. 38 get { return _price; }
  39. 39 set { _price = value; }
  40. 40 }
  41. 41 /// <summary>
  42. 42 /// 放映场次
  43. 43 /// </summary>
  44. 44 public ScheduleItem ScheduItem { get; set; }
  45. 45 /// <summary>
  46. 46 /// 计算票价
  47. 47 /// </summary>
  48. 48 public virtual void CalcPrice()
  49. 49 {
  50. 50 this.Price = ScheduItem.Movie.Price;
  51. 51 }
  52. 52 /// <summary>
  53. 53 /// 打印售票信息
  54. 54 /// </summary>
  55. 55 public virtual void Print()
  56. 56 {
  57. 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. 58 MessageBox.Show(info);
  59. 59 //存到文件中
  60. 60 string fileName = this.ScheduItem.Time.Replace(":", "-")+" "+this.Seat.SeatNum+".txt";
  61. 61 FileStream fs = new FileStream(fileName,FileMode.Create);
  62. 62 StreamWriter sw = new StreamWriter(fs);
  63. 63 sw.Write(info);
  64. 64 sw.Close();
  65. 65 fs.Close();
  66. 66 }
  67. 67 /// <summary>
  68. 68 /// 显示当前售票信息
  69. 69 /// </summary>
  70. 70 public virtual void Show()
  71. 71 {
  72. 72 string info = string.Format("已售出!\n普通票!");
  73. 73 MessageBox.Show(info);
  74. 74 }
  75. 75 }
  76. 76 }

Ticket

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6 using System.Windows.Forms;
  7. 7 using System.IO;
  8. 8 namespace 影院售票系统
  9. 9 {
  10. 10 /// <summary>
  11. 11 /// 学生票
  12. 12 /// </summary>
  13. 13 public class StudentTicket : Ticket
  14. 14 {
  15. 15 public StudentTicket() { }
  16. 16 public StudentTicket(ScheduleItem sch, Seat seat, int discount)
  17. 17 : base(sch, seat)
  18. 18 {
  19. 19 this.Discount = discount;
  20. 20 }
  21. 21 private int _discount;
  22. 22 /// <summary>
  23. 23 /// 学生票的折扣
  24. 24 /// </summary>
  25. 25 public int Discount
  26. 26 {
  27. 27 get { return _discount; }
  28. 28 set { _discount = value; }
  29. 29 }
  30. 30 /// <summary>
  31. 31 /// 计算学生票价
  32. 32 /// </summary>
  33. 33 public override void CalcPrice()
  34. 34 {
  35. 35 this.Price =this.ScheduItem.Movie.Price* Discount / 10;
  36. 36 }
  37. 37 /// <summary>
  38. 38 /// 打印学生票的售票信息
  39. 39 /// </summary>
  40. 40 public override void Print()
  41. 41 {
  42. 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. 43 MessageBox.Show(info);
  44. 44 //存到文件中
  45. 45 string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";
  46. 46 FileStream fs = new FileStream(fileName, FileMode.Create);
  47. 47 StreamWriter sw = new StreamWriter(fs);
  48. 48 sw.Write(info);
  49. 49 sw.Close();
  50. 50 fs.Close();
  51. 51 }
  52. 52 /// <summary>
  53. 53 /// 显示当前售出票信息
  54. 54 /// </summary>
  55. 55 public override void Show()
  56. 56 {
  57. 57 string info = string.Format("已售出!\n{0}折学生票!",this.Discount);
  58. 58 MessageBox.Show(info);
  59. 59 }
  60. 60 }
  61. 61 }

StudentTicket

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6 using System.Windows.Forms;
  7. 7 using System.IO;
  8. 8
  9. 9 namespace 影院售票系统
  10. 10 {
  11. 11 /// <summary>
  12. 12 /// 赠票
  13. 13 /// </summary>
  14. 14 public class FreeTicket:Ticket
  15. 15 {
  16. 16 public FreeTicket() { }
  17. 17 public FreeTicket(ScheduleItem sch,Seat seat,string name)
  18. 18 {
  19. 19 this.Seat = seat;
  20. 20 this.CustomerName = name;
  21. 21 this.ScheduItem = sch;
  22. 22 }
  23. 23 private string _customerName;
  24. 24 /// <summary>
  25. 25 /// 获得赠票者的名字
  26. 26 /// </summary>
  27. 27 public string CustomerName
  28. 28 {
  29. 29 get { return _customerName; }
  30. 30 set { _customerName = value; }
  31. 31 }
  32. 32 /// <summary>
  33. 33 /// 计算票价
  34. 34 /// </summary>
  35. 35 public override void CalcPrice()
  36. 36 {
  37. 37 this.Price = 0;
  38. 38 }
  39. 39 /// <summary>
  40. 40 /// 打印售票信息
  41. 41 /// </summary>
  42. 42 public override void Print()
  43. 43 {
  44. 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. 45 MessageBox.Show(info);
  46. 46 //存到文件中
  47. 47 string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";
  48. 48 FileStream fs = new FileStream(fileName, FileMode.Create);
  49. 49 StreamWriter sw = new StreamWriter(fs);
  50. 50 sw.Write(info);
  51. 51 sw.Close();
  52. 52 fs.Close();
  53. 53 }
  54. 54 /// <summary>
  55. 55 /// 显示当前售出票信息
  56. 56 /// </summary>
  57. 57 public override void Show()
  58. 58 {
  59. 59 MessageBox.Show("已售出!\n赠票!");
  60. 60 }
  61. 61 }
  62. 62 }

FreeTicket

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6
  7. 7 namespace 影院售票系统
  8. 8 {
  9. 9 /// <summary>
  10. 10 /// 影院每天计划放映的场次,保存每场电影的信息
  11. 11 /// </summary>
  12. 12 public class ScheduleItem
  13. 13 {
  14. 14 private string _time;
  15. 15 /// <summary>
  16. 16 /// 放映时间
  17. 17 /// </summary>
  18. 18 public string Time
  19. 19 {
  20. 20 get { return _time; }
  21. 21 set { _time = value; }
  22. 22 }
  23. 23 private Movie _movie = new Movie();
  24. 24
  25. 25 /// <summary>
  26. 26 /// 本场放映的电影
  27. 27 /// </summary>
  28. 28 public Movie Movie
  29. 29 {
  30. 30 get { return _movie; }
  31. 31 set { _movie = value; }
  32. 32 }
  33. 33 private List<Ticket> _soldTickets=new List<Ticket>();
  34. 34
  35. 35 private Dictionary<string, Seat> _seats=new Dictionary<string,Seat>();
  36. 36 /// <summary>
  37. 37 /// 本场次的座位状态
  38. 38 /// </summary>
  39. 39 public Dictionary<string, Seat> Seats
  40. 40 {
  41. 41 get { return _seats; }
  42. 42 set { _seats = value; }
  43. 43 }
  44. 44 }
  45. 45 }

ScheduleItem

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6 using System.Xml;
  7. 7
  8. 8 namespace 影院售票系统
  9. 9 {
  10. 10 /// <summary>
  11. 11 /// 放映计划类,保存影院当天的放映计划集合
  12. 12 /// </summary>
  13. 13 public class Schedule
  14. 14 {
  15. 15 /// <summary>
  16. 16 /// 放映场次
  17. 17 /// </summary>
  18. 18 public Dictionary<string, ScheduleItem> Items = new Dictionary<string, ScheduleItem>();
  19. 19 /// <summary>
  20. 20 /// 读取XML文件获取放映计划集合
  21. 21 /// </summary>
  22. 22 public void LoadItems()
  23. 23 {
  24. 24 Items.Clear();
  25. 25 XmlDocument xml = new XmlDocument();
  26. 26 xml.Load("ShowList.xml");
  27. 27 XmlElement root = xml.DocumentElement;
  28. 28 foreach (XmlNode item in root.ChildNodes)
  29. 29 {
  30. 30 Movie movie = new Movie();
  31. 31 movie.MovieName = item["Name"].InnerText;
  32. 32 movie.Poster = item["Poster"].InnerText;
  33. 33 movie.Director = item["Director"].InnerText;
  34. 34 movie.Actor = item["Actor"].InnerText;
  35. 35 switch (item["Type"].InnerText)
  36. 36 {
  37. 37 case "Action":
  38. 38 movie.MovieType = MovieType.Action;
  39. 39 break;
  40. 40 case "War":
  41. 41 movie.MovieType = MovieType.War;
  42. 42 break;
  43. 43 case "Comedy":
  44. 44 movie.MovieType = MovieType.Comedy;
  45. 45 break;
  46. 46 }
  47. 47 movie.Price = Convert.ToInt32(item["Price"].InnerText);
  48. 48 if (item["Schedule"].HasChildNodes)
  49. 49 {
  50. 50 foreach (XmlNode item2 in item["Schedule"].ChildNodes)
  51. 51 {
  52. 52 ScheduleItem schItem = new ScheduleItem();
  53. 53 schItem.Time = item2.InnerText;
  54. 54 schItem.Movie = movie;
  55. 55 Items.Add(schItem.Time, schItem);
  56. 56 }
  57. 57 }
  58. 58
  59. 59 }
  60. 60
  61. 61
  62. 62 }
  63. 63 }
  64. 64 }

Schedule

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6
  7. 7 namespace 影院售票系统
  8. 8 {
  9. 9 /// <summary>
  10. 10 /// 影院类
  11. 11 /// </summary>
  12. 12 public class Cinema
  13. 13 {
  14. 14 private Dictionary<string, Seat> _seats = new Dictionary<string, Seat>();
  15. 15 /// <summary>
  16. 16 /// 座位集合
  17. 17 /// </summary>
  18. 18 public Dictionary<string, Seat> Seats
  19. 19 {
  20. 20 get { return _seats; }
  21. 21 set { _seats = value; }
  22. 22 }
  23. 23 private Schedule _schedule = new Schedule();
  24. 24 /// <summary>
  25. 25 /// 放映计划
  26. 26 /// </summary>
  27. 27 public Schedule Schedule
  28. 28 {
  29. 29 get { return _schedule; }
  30. 30 set { _schedule = value; }
  31. 31 }
  32. 32 private List<Ticket> _soldTickets=new List<Ticket>();
  33. 33 /// <summary>
  34. 34 /// 已经售出的票
  35. 35 /// </summary>
  36. 36 public List<Ticket> SoldTickets
  37. 37 {
  38. 38 get { return _soldTickets; }
  39. 39 set { _soldTickets = value; }
  40. 40 }
  41. 41 /// <summary>
  42. 42 /// 保存售票信息到文件中
  43. 43 /// </summary>
  44. 44 public void Save()
  45. 45 {
  46. 46 //Save和Load的代码在窗体的代码实现了
  47. 47 }
  48. 48 /// <summary>
  49. 49 /// 从文件中读取售票信息
  50. 50 /// </summary>
  51. 51 public void Load() { }
  52. 52 }
  53. 53 }

Cinema

  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.Linq;
  4. 4 using System.Text;
  5. 5 using System.Threading.Tasks;
  6. 6
  7. 7 namespace 影院售票系统
  8. 8 {
  9. 9 /// <summary>
  10. 10 /// 工具类
  11. 11 /// </summary>
  12. 12 public class TicketUtil
  13. 13 {
  14. 14 /// <summary>
  15. 15 /// 创建电影票
  16. 16 /// </summary>
  17. 17 /// <returns></returns>
  18. 18 public static Ticket CreateTicket(ScheduleItem sch,Seat seat,int discount,string customerName,string type)
  19. 19 {
  20. 20 Ticket ticket=null;
  21. 21 switch (type)
  22. 22 {
  23. 23 case "StudentTicket":
  24. 24 ticket = new StudentTicket(sch,seat,discount);
  25. 25 break;
  26. 26 case "FreeTicket":
  27. 27 ticket = new FreeTicket(sch,seat,customerName);
  28. 28 break;
  29. 29 default:
  30. 30 ticket = new Ticket(sch,seat);
  31. 31 break;
  32. 32 }
  33. 33 return ticket;
  34. 34 }
  35. 35 }
  36. 36 }

TicketUtil

明天将继续更新-电影院座位的动态绘制、电影信息绑定到窗体中展现出来,也望各路大神出手斧正不合理的代码(不要涉及分层开发,我们在学,以后会用分层开发实现其他的项目)

版权声明:本文为AIThink原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/AIThink/p/4931961.html