C#總結項目《影院售票系統》編寫總結一


  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 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 }
Seat
 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 }
Movie
 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 Ticket
15     {
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 Seat
27         {
28             get { return _seat; }
29             set { _seat = value; }
30         }
31         
32         private int _price;
33         /// <summary>
34         /// 票價
35         /// </summary>
36         public int Price
37         {
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 }
Ticket
 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 : Ticket
14     {
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 Discount
26         {
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 }
StudentTicket
 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:Ticket
15     {
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 CustomerName
28         {
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 }
FreeTicket
 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 }
ScheduleItem
 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     }
64 }
Schedule
 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 Cinema
13     {
14         private Dictionary<string, Seat> _seats = new Dictionary<string, Seat>();
15         /// <summary>
16         /// 座位集合
17         /// </summary>
18         public Dictionary<string, Seat> Seats
19         {
20             get { return _seats; }
21             set { _seats = value; }
22         }
23         private Schedule _schedule = new Schedule();
24         /// <summary>
25         /// 放映計划
26         /// </summary>
27         public Schedule Schedule
28         {
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> SoldTickets
37         {
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 }
Cinema
 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     }
36 }
TicketUtil

明天將繼續更新-電影院座位的動態繪制、電影信息綁定到窗體中展現出來,也望各路大神出手斧正不合理的代碼(不要涉及分層開發,我們在學,以后會用分層開發實現其他的項目)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM