1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace 影院售票 12 { 13 public partial class FrmMain : Form 14 { 15 public FrmMain() 16 { 17 InitializeComponent(); 18 } 19 20 //创建一个LAble集合 21 Dictionary<string, Label> labels = new Dictionary<string, Label>(); 22 23 24 #region 动态加载所有的座位 25 public void ShowSeat() 26 { 27 for (int i = 0; i < 5; i++) 28 { 29 for (int k = 0; k < 6; k++) 30 { 31 Label lbl = new Label(); 32 lbl.BackColor = Color.Yellow; 33 lbl.Font = new Font("宋体", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); 34 lbl.AutoSize = false; 35 lbl.Size = new Size(50, 25); 36 lbl.Text = (k + 1).ToString() + "-" + (i + 1).ToString(); 37 lbl.TextAlign = ContentAlignment.MiddleCenter; 38 lbl.Location = new Point(90 + (i * 104), 30 + (k * 75)); 39 lbl.Click += new EventHandler(lbl_Click); 40 tpseat.Controls.Add(lbl); 41 labels.Add(lbl.Text, lbl); 42 Seat seat = new Seat((k + 1).ToString() + "-" + (i + 1).ToString(), Color.Yellow); 43 Cinema.seats.Add(seat.Seatnum, seat); 44 } 45 } 46 } 47 #endregion 48 49 //加载trreeview信息 50 #region 绑定TREEVIEW 51 public void ShowtvMovies() 52 { 53 Schedule schedule = new Schedule(); 54 schedule.Items = new Dictionary<string, ScheduleItem>(); 55 schedule.LoadItems(); 56 string movie = null; 57 TreeNode treeNode = null; 58 int num = 0; 59 foreach (ScheduleItem item in schedule.Items.Values) 60 { 61 if (movie != item.Movie.Moviesname) 62 { 63 num++; 64 treeNode = new TreeNode(); 65 movie = item.Movie.Moviesname; 66 treeNode.Text = movie; 67 tvMovies.Nodes.Add(treeNode); 68 TreeNode tree = new TreeNode(); 69 tree.Text = item.Time; 70 tree.Tag = item; 71 tvMovies.Nodes[num - 1].Nodes.Add(tree); 72 } 73 else 74 { 75 TreeNode tree = new TreeNode(); 76 tree.Text = item.Time; 77 tree.Tag = item; 78 tvMovies.Nodes[num - 1].Nodes.Add(tree); 79 } 80 } 81 } 82 #endregion 83 Cinema cinema = new Cinema(); 84 85 private void FrmMain_Load(object sender, EventArgs e) 86 { 87 88 DialogResult result= MessageBox.Show("是否继续进行","提示",MessageBoxButtons.YesNo); 89 90 //动态加载座位 91 ShowSeat(); 92 //绑定TRee方法 93 ShowtvMovies(); 94 95 } 96 97 98 //更新座位填充颜色 99 #region 更新座位填充颜色 100 public void Updateseat() 101 { 102 foreach (string item in Cinema.seats.Keys) 103 { 104 ; 105 labels[item].BackColor = Cinema.seats[item].Color; 106 //MessageBox.Show(Cinema.seats[item].Color.ToString()); 107 } 108 } 109 #endregion 110 111 //加载对应座位 112 #region 选择更改座位颜色 113 public void SeatInfo() 114 { 115 foreach (string item in Cinema.seats.Keys) 116 { 117 Cinema.seats[item].Color = Color.Yellow; 118 } 119 if (tvMovies.SelectedNode.Level==1) 120 { 121 foreach (Ticket item in Cinema.Soldticket) 122 { 123 //&& tvMovies.SelectedNode.Parent.Text == item.ScheduleItem.Movie.Moviesname; 124 if (tvMovies.SelectedNode.Text == item.ScheduleItem.Time) 125 { 126 foreach (string item1 in Cinema.seats.Keys) 127 { 128 if (item.Seat.Seatnum == Cinema.seats[item1].Seatnum) 129 { 130 Cinema.seats[item1].Color = Color.Red; 131 } 132 } 133 } 134 } 135 } 136 //重新绑定 137 Updateseat(); 138 } 139 #endregion 140 141 //购票 142 143 private void lbl_Click(object sender, EventArgs e) 144 { 145 #region 买票弹框 146 Ticket ticket = null; 147 if (rdoCommon.Checked == true) 148 { 149 ticket = new Ticket(); 150 } 151 else if (rdoFree.Checked == true) 152 { 153 ticket = new FreeTicket(); 154 } 155 else if (rdoStudent.Checked == true) 156 { 157 ticket = new StudentTicket(); 158 } 159 else 160 { 161 MessageBox.Show("请选择您要购买的票类型!"); 162 return; 163 } 164 165 string number = ((Label)sender).Text.ToString(); 166 if (Cinema.seats[number].Color == Color.Yellow) 167 { 168 if (MessageBox.Show("是否购买?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) 169 { 170 ScheduleItem scheduleItem = new ScheduleItem(); 171 scheduleItem.Time = lblTime.Text; 172 Movies movies = new Movies(); 173 scheduleItem.Movie = movies; 174 scheduleItem.Movie.Moviesname = lblName.Text.Trim(); 175 scheduleItem.Movie.Movietype = (MovieType)Enum.Parse(typeof(MovieType), lblType.Text); 176 scheduleItem.Movie.Poster = ((ScheduleItem)(tvMovies.SelectedNode.Tag)).Movie.Poster; 177 scheduleItem.Movie.Price = Convert.ToDouble(lblPrivilege.Text); 178 scheduleItem.Movie.Director = lblDirect.Text; 179 scheduleItem.Movie.Actor = lblProtagonist.Text; 180 Cinema.seats[number].Color = Color.Red; 181 Updateseat(); 182 ticket.Price1 = Convert.ToDouble(lblPrivilege.Text); 183 ticket.ScheduleItem = scheduleItem; 184 ticket.Seat = Cinema.seats[number]; 185 Cinema.Soldticket.Add(ticket); 186 ticket.Print(); 187 } 188 } 189 else 190 { 191 foreach (Ticket item in Cinema.Soldticket) 192 { 193 if (item.Seat.Seatnum == number && item.ScheduleItem.Time == lblTime.Text && item.ScheduleItem.Movie.Moviesname == lblName.Text) 194 { 195 item.Show(); 196 } 197 } 198 } 199 200 201 #endregion 202 203 204 } 205 //显示电影详细信息 206 private void tvMovies_AfterSelect(object sender, TreeViewEventArgs e) 207 { 208 if (tvMovies.SelectedNode.Level==1) 209 { 210 //更新座位颜色 211 SeatInfo(); 212 ScheduleItem sh = (ScheduleItem)tvMovies.SelectedNode.Tag; 213 MessageBox.Show(tvMovies.SelectedNode.Tag.ToString()); 214 lblName.Text = sh.Movie.Moviesname; 215 lblDirect.Text = sh.Movie.Director; 216 lblProtagonist.Text = sh.Movie.Actor; 217 lblType.Text = sh.Movie.Movietype.ToString(); 218 lblTime.Text = sh.Time; 219 lblPrice.Text = sh.Movie.Price.ToString(); 220 picMovies.Image = Image.FromFile(sh.Movie.Poster); 221 lblPrivilege.Enabled = true; 222 txtFreeName.Enabled = true; 223 cboStudentDiscount.Enabled = true; 224 225 } 226 } 227 //普通票显示价格 228 private void rdoCommon_CheckedChanged(object sender, EventArgs e) 229 { 230 if (tvMovies.SelectedNode==null) 231 { 232 return; 233 } 234 if (lblPrivilege.Enabled == true) 235 { 236 Ticket ticket = new Ticket(); 237 ticket.Price1 = Convert.ToDouble(lblPrice.Text); 238 lblPrivilege.Text=ticket.CalcPrice().ToString(); 239 txtFreeName.Enabled = false; 240 cboStudentDiscount.Enabled = false; 241 cboStudentDiscount.Text = null; 242 } 243 } 244 //赠票显示价格 245 private void rdoFree_CheckedChanged(object sender, EventArgs e) 246 { 247 if (tvMovies.SelectedNode==null) 248 { 249 return; 250 } 251 if (lblPrivilege.Enabled == true) 252 { 253 FreeTicket ticket = new FreeTicket(); 254 lblPrivilege.Text = ticket.CalcPrice().ToString(); 255 txtFreeName.Enabled = true; ; 256 cboStudentDiscount.Enabled = false; 257 cboStudentDiscount.Text = null; 258 } 259 } 260 //学生票显示价格 261 private void rdoStudent_CheckedChanged(object sender, EventArgs e) 262 { 263 if (tvMovies.SelectedNode==null) 264 { 265 return; 266 } 267 if (lblPrivilege.Enabled == true) 268 { 269 txtFreeName.Enabled = false; 270 cboStudentDiscount.Enabled = true; 271 cboStudentDiscount.Text = "7"; 272 StudentTicket ticket = new StudentTicket(); 273 ticket.Price1 = Convert.ToDouble(lblPrice.Text); 274 lblPrivilege.Text = ticket.CalcPrice().ToString(); 275 276 } 277 } 278 279 280 private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) 281 { 282 283 } 284 285 private void tpseat_Click(object sender, EventArgs e) 286 { 287 288 } 289 290 private void msFunction_ItemClicked(object sender, ToolStripItemClickedEventArgs e) 291 { 292 293 } 294 295 private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) 296 { 297 Cinema c=new Cinema(); 298 c.Save(); 299 } 300 301 private void 新增ToolStripMenuItem_Click(object sender, EventArgs e) 302 { 303 Cinema.Soldticket.Clear(); 304 Updateseat(); 305 } 306 307 private void 购票ToolStripMenuItem_Click(object sender, EventArgs e) 308 { 309 310 } 311 312 private void 继续销售ToolStripMenuItem_Click(object sender, EventArgs e) 313 { 314 cinema.jaa(); 315 MessageBox.Show(Cinema.Soldticket.Count.ToString()); Updateseat(); 316 } 317 } 318 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.IO; 7 using System.Drawing; 8 using System.Windows.Forms; 9 using System.Xml; 10 using System.Runtime.Serialization.Formatters.Binary; 11 namespace 影院售票 12 { 13 [Serializable] 14 public class Cinema 15 { 16 //座位集合 17 public static Dictionary<string, Seat> seats = new Dictionary<string, Seat>(); 18 19 //放映计划 20 public static Schedule Schedule = new Schedule(); 21 22 //已售出电影票集合 23 public static List<Ticket> Soldticket = new List<Ticket>(); 24 25 //保存售票情况 26 public void Save() 27 { 28 //序列化:有三种;二进制序列化 XML序列化 SOAP序列化器 29 BinaryFormatter bf = new BinaryFormatter(); 30 //两个参数:存放路径,文件操作方式 31 Stream stream = new FileStream("kkk.bin", FileMode.Create); 32 //两个参数:文件流和存放内容 33 bf.Serialize(stream, Soldticket); 34 stream.Close(); 35 36 } 37 //加载售票情况 38 public void jaa() 39 { //创建反序列化对象 40 BinaryFormatter bf = new BinaryFormatter(); 41 42 // 两个参数:读取路径,文件操作方式枚举类型 43 Stream stream1 = new FileStream("kkk.bin", FileMode.Open); 44 //把二进制文件强制转换为相对应的类型 45 List<Ticket> list = (List<Ticket>)bf.Deserialize(stream1); 46 Soldticket = list; 47 stream1.Close(); 48 MessageBox.Show(Soldticket.Count.ToString()); 49 50 } 51 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 [Serializable] 10 public class FreeTicket:Ticket 11 { 12 public FreeTicket() 13 { 14 } 15 public FreeTicket(double price, ScheduleItem scheduleItem, Seat seat) 16 : base(price, scheduleItem, seat) 17 { 18 } 19 20 //计算票价信息 21 public override double CalcPrice() 22 { 23 return 0; 24 } 25 26 //打印票价信息 27 public override void Print() 28 { 29 StringBuilder stringBuilder = new StringBuilder("************************\n"); 30 stringBuilder.AppendLine("青鸟影视(赠票)"); 31 stringBuilder.AppendLine("-----------------------------------------------"); 32 stringBuilder.AppendFormat("电影名:{0}\n", this.ScheduleItem.Movie.Moviesname); 33 stringBuilder.AppendFormat("时间:{0}\n", this.ScheduleItem.Time); 34 stringBuilder.AppendFormat("座位号:{0}\n", this.Seat.Seatnum); 35 stringBuilder.AppendFormat("价格:{0}\n", this.Price1); 36 stringBuilder.AppendFormat("************************\n"); 37 System.Windows.Forms.MessageBox.Show(stringBuilder.ToString()); 38 } 39 40 //显示票价信息 41 public override void Show() 42 { 43 StringBuilder stringBuilder = new StringBuilder("***********对不起,已售出*************\n"); 44 stringBuilder.AppendLine("青鸟影视(赠票)"); 45 stringBuilder.AppendLine("------------------------------------------------"); 46 stringBuilder.AppendFormat("电影名:{0}\n", this.ScheduleItem.Movie.Moviesname); 47 stringBuilder.AppendFormat("时间:{0}\n", this.ScheduleItem.Time); 48 stringBuilder.AppendFormat("座位号:{0}\n", this.Seat.Seatnum); 49 stringBuilder.AppendFormat("价格:{0}\n", this.Price1); 50 stringBuilder.AppendFormat("************************\n"); 51 System.Windows.Forms.MessageBox.Show(stringBuilder.ToString()); 52 } 53 } 54 }
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 [Serializable] 10 public class Movies 11 { 12 //电影名 13 private string moviesname; 14 public string Moviesname 15 { 16 get { return moviesname; } 17 set { moviesname = value; } 18 } 19 20 //海报名 21 private string poster; 22 public string Poster 23 { 24 get { return poster; } 25 set { poster = value; } 26 } 27 28 //导演名 29 private string director; 30 public string Director 31 { 32 get { return director; } 33 set { director = value; } 34 } 35 36 //主演 37 private string actor; 38 public string Actor 39 { 40 get { return actor; } 41 set { actor = value; } 42 } 43 44 //电影类型 45 private MovieType movietype; 46 public MovieType Movietype 47 { 48 get { return movietype; } 49 set { movietype = value; } 50 } 51 52 //定价 53 private double price; 54 public double Price 55 { 56 get { return price; } 57 set { price = value; } 58 } 59 60 public Movies() 61 { 62 } 63 public Movies(string moviename,string poster,string director,string actor,MovieType movietype,double price) 64 { 65 this.Actor = actor; 66 this.Director = director; 67 this.Moviesname = moviename; 68 this.Movietype = movietype; 69 this.Poster = poster; 70 this.Price = price; 71 } 72 } 73 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 影院售票 7 { 8 [Serializable] 9 public enum MovieType 10 { 11 Comedy,//喜剧 12 War,//战争 13 Romance,//角色 14 Action,//动作 15 Cartoon,//动画 16 Thriller,//惊悚 17 Adventure,//冒险 18 Love//爱情 19 } 20 }
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 [Serializable] 11 public class Schedule 12 { 13 //放映场次集合 14 public Dictionary<string,ScheduleItem> Items { get; set; } 15 16 public Schedule() { } 17 public Schedule(Dictionary<string, ScheduleItem> items) 18 { 19 this.Items = items; 20 } 21 22 //解析XML 23 public void LoadItems() 24 { 25 XmlDocument xml = new XmlDocument(); 26 xml.Load("影院售票/ShowList.xml"); 27 XmlElement xmlelement = xml.DocumentElement; 28 foreach (XmlNode item in xmlelement.ChildNodes) 29 { 30 foreach (XmlNode item1 in item.ChildNodes) 31 { 32 if (item1.Name == "Schedule") 33 { 34 foreach (XmlNode item2 in item1.ChildNodes) 35 { 36 Movies movie =new Movies(item["Name"].InnerText, item["Poster"].InnerText, item["Director"].InnerText, 37 item["Actor"].InnerText, (MovieType)Enum.Parse(typeof(MovieType), item["Type"].InnerText), 38 Convert.ToDouble(item["Price"].InnerText)); 39 ScheduleItem scheduleItem = new ScheduleItem(item2.InnerText, movie); 40 Items.Add(scheduleItem.Time, scheduleItem); 41 } 42 } 43 } 44 } 45 } 46 47 } 48 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 影院售票 7 { 8 [Serializable] 9 public class ScheduleItem 10 { 11 //放映时间 12 private string time; 13 public string Time 14 { 15 get { return time; } 16 set { time = value; } 17 } 18 19 //电影属性 20 private Movies movie; 21 public Movies Movie 22 { 23 get { return movie; } 24 set { movie = value; } 25 } 26 27 public ScheduleItem() { } 28 public ScheduleItem(string time,Movies movie) 29 { 30 this.Movie = movie; 31 this.Time = time; 32 } 33 } 34 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 7 namespace 影院售票 8 { 9 [Serializable] 10 public class Seat 11 { 12 //座位号 13 private string seatnum; 14 public string Seatnum 15 { 16 get { return seatnum; } 17 set { seatnum = value; } 18 } 19 20 //座位颜色 21 private Color color; 22 public Color Color 23 { 24 get { return color; } 25 set { color = value; } 26 } 27 28 public Seat() { } 29 public Seat(string seatnum,Color color) 30 { 31 this.Color = color; 32 this.Seatnum = seatnum; 33 } 34 } 35 }
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 [Serializable] 10 public class StudentTicket:Ticket 11 { 12 public StudentTicket() 13 { 14 } 15 public StudentTicket(double price, ScheduleItem scheduleItem, Seat seat):base(price,scheduleItem,seat) 16 { 17 18 } 19 20 //计算票价信息 21 public override double CalcPrice() 22 { 23 return Price1 * 0.7; 24 } 25 26 //打印票价信息 27 public override void Print() 28 { 29 StringBuilder stringBuilder = new StringBuilder("************************\n"); 30 stringBuilder.AppendLine(" 青鸟影视(学生)"); 31 stringBuilder.AppendLine("------------------------"); 32 stringBuilder.AppendFormat("电影名:{0}\n", this.ScheduleItem.Movie.Moviesname); 33 stringBuilder.AppendFormat("时间:{0}\n", this.ScheduleItem.Time); 34 stringBuilder.AppendFormat(" 座位号:{0}\n", this.Seat.Seatnum); 35 stringBuilder.AppendFormat("价格:{0}\n", this.Price1); 36 stringBuilder.AppendFormat("************************\n"); 37 System.Windows.Forms.MessageBox.Show(stringBuilder.ToString()); 38 } 39 40 //显示票价信息 41 public override void Show() 42 { 43 StringBuilder stringBuilder = new StringBuilder("************抱歉,已售出************\n"); 44 stringBuilder.AppendLine("青鸟影视(学生)"); 45 stringBuilder.AppendLine(" ------------------------"); 46 stringBuilder.AppendFormat("电影名:{0}\n", this.ScheduleItem.Movie.Moviesname); 47 stringBuilder.AppendFormat("时间:{0}\n", this.ScheduleItem.Time); 48 stringBuilder.AppendFormat("座位号:{0}\n", this.Seat.Seatnum); 49 stringBuilder.AppendFormat("价格:{0}\n", this.Price1); 50 stringBuilder.AppendFormat("************************\n"); 51 System.Windows.Forms.MessageBox.Show(stringBuilder.ToString()); 52 } 53 } 54 }
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 [Serializable] 10 public class Ticket 11 { 12 //价格 13 private double price; 14 public double Price1 15 { 16 get { return price; } 17 set { price = value; } 18 } 19 20 //放映场次 21 private ScheduleItem scheduleItem; 22 public ScheduleItem ScheduleItem 23 { 24 get { return scheduleItem; } 25 set { scheduleItem = value; } 26 } 27 28 //座位 29 private Seat seat; 30 public Seat Seat 31 { 32 get { return seat; } 33 set { seat = value; } 34 } 35 36 public Ticket() 37 { 38 } 39 public Ticket(double price, ScheduleItem scheduleItem, Seat seat) 40 { 41 this.Price1 = price; 42 this.ScheduleItem = scheduleItem; 43 this.Seat = seat; 44 } 45 //计算票价信息 46 public virtual double CalcPrice() 47 { 48 return price; 49 } 50 51 //打印票价信息 52 public virtual void Print() 53 { 54 StringBuilder stringBuilder = new StringBuilder("************************\n"); 55 stringBuilder.AppendLine("青鸟影视"); 56 stringBuilder.AppendLine("------------------------"); 57 stringBuilder.AppendFormat("电影名:{0}\n",scheduleItem.Movie.Moviesname); 58 stringBuilder.AppendFormat("时间:{0}\n",scheduleItem.Time); 59 stringBuilder.AppendFormat("座位号:{0}\n", seat.Seatnum); 60 stringBuilder.AppendFormat("价格:{0}\n",price); 61 stringBuilder.AppendFormat("************************\n"); 62 System.Windows.Forms.MessageBox.Show(stringBuilder.ToString()); 63 } 64 65 //显示票价信息 66 public virtual void Show() 67 { 68 StringBuilder stringBuilder = new StringBuilder("***********抱歉,已售出*************\n"); 69 stringBuilder.AppendLine(" 青鸟影视"); 70 stringBuilder.AppendLine("-----------------------"); 71 stringBuilder.AppendFormat("电影名:{0}\n", scheduleItem.Movie.Moviesname); 72 stringBuilder.AppendFormat("时间:{0}\n", scheduleItem.Time); 73 stringBuilder.AppendFormat("座位号:{0}\n", seat.Seatnum); 74 stringBuilder.AppendFormat("价格:{0}\n", price); 75 stringBuilder.AppendFormat("************************\n"); 76 System.Windows.Forms.MessageBox.Show(stringBuilder.ToString()); 77 } 78 } 79 }
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 [Serializable] 10 public class TicketUtil 11 { 12 //创建电影票 13 public void GreateTicket(ScheduleItem scheduleItem,Seat seat,double money,string type ) 14 { 15 16 } 17 } 18 }