1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Project.Entity 8 { 9 /// <summary> 10 /// 电影票(父类) 11 /// </summary> 12 public abstract class Ticket 13 { 14 /// <summary> 15 /// 放映场次 16 /// </summary> 17 public ScheduleItem ScheduleItem { get; set; } 18 /// <summary> 19 /// 所属座位对象 20 /// </summary> 21 public Seat Seat { get; set; } 22 /// <summary> 23 /// 票价 24 /// </summary> 25 public int Price { get; set; } 26 27 public Ticket() { } 28 29 public Ticket(ScheduleItem scheduleItem, Seat seat, int price) 30 { 31 this.ScheduleItem = scheduleItem; 32 this.Seat = seat; 33 this.Price = price; 34 } 35 36 /// <summary> 37 /// 虚拟方法:计算票价 38 /// </summary> 39 public abstract void CalcPrice(); 40 41 /// <summary> 42 /// 虚拟方法:打印售票信息 43 /// </summary> 44 public abstract void Print(); 45 46 /// <summary> 47 /// 虚拟方法:显示当前售出票信息 48 /// </summary> 49 public abstract void Show(); 50 } 51 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Project.Entity 8 { 9 /// <summary> 10 /// 赠票(Ticket的子类) 11 /// </summary> 12 public class FreeTicket:Ticket 13 { 14 /// <summary> 15 /// 赠票者名字 16 /// </summary> 17 public string CustomerName { get; set; } 18 19 /// <summary> 20 /// 方法重造 21 /// </summary> 22 public FreeTicket() { } 23 24 public FreeTicket(ScheduleItem scheduleItem, Seat seat, int price, string customerName) :base(scheduleItem,seat,price) 25 { 26 this.ScheduleItem = scheduleItem; 27 this.Seat = seat; 28 this.Price = price; 29 this.CustomerName = customerName; 30 } 31 32 /// <summary> 33 /// 重写方法:学生票计算价格 34 /// </summary> 35 public override void CalcPrice() 36 { 37 38 } 39 40 /// <summary> 41 /// 重写方法:打印售票信息 42 /// </summary> 43 public override void Print() 44 { 45 46 } 47 48 /// <summary> 49 /// 重写方法:显示当前售票信息 50 /// </summary> 51 public override void Show() 52 { 53 54 } 55 } 56 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Project.Entity 8 { 9 /// <summary> 10 /// 学生票(Ticket的子类) 11 /// </summary> 12 public class StudentTicket : Ticket 13 { 14 /// <summary> 15 /// 学生票折扣价 16 /// </summary> 17 public int Discount { get; set; } 18 19 public StudentTicket() { } 20 21 public StudentTicket(ScheduleItem scheduleItem, Seat seat, int price, int discount) 22 : base(scheduleItem, seat, price) 23 { 24 this.ScheduleItem = scheduleItem; 25 this.Seat = seat; 26 this.Price = price; 27 this.Discount = discount; 28 } 29 30 /// <summary> 31 /// 重写方法:学生票计算价格 32 /// </summary> 33 public override void CalcPrice() 34 { 35 36 } 37 38 /// <summary> 39 /// 重写方法:打印售票信息 40 /// </summary> 41 public override void Print() 42 { 43 44 } 45 46 /// <summary> 47 /// 重写方法:显示当前售票信息 48 /// </summary> 49 public override void Show() 50 { 51 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 Project.Entity 8 { 9 /// <summary> 10 /// 电影院类 11 /// </summary> 12 public class Cinema 13 { 14 /// <summary> 15 /// 座位集合属性 16 /// </summary> 17 public Dictionary<string, Seat> Seats { get; set; } 18 19 /// <summary> 20 /// 放映计划 21 /// </summary> 22 public Schedule Schedule { get; set; } 23 24 /// <summary> 25 /// 已售出电影票的集合 26 /// </summary> 27 public List<Ticket> SoldTickets { get; set; } 28 29 /// <summary> 30 /// 保存 31 /// </summary> 32 public void Save() { } 33 34 /// <summary> 35 /// 加载 36 /// </summary> 37 public void Load() { } 38 } 39 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Project.Entity 8 { 9 /// <summary> 10 /// 电影类 11 /// </summary> 12 public class Movie 13 { 14 /// <summary> 15 /// 电影名 16 /// </summary> 17 public string MovieName { get; set; } 18 /// <summary> 19 /// 海报名 20 /// </summary> 21 public string Poster { get; set; } 22 /// <summary> 23 /// 导演 24 /// </summary> 25 public string Dirctor { get; set; } 26 /// <summary> 27 /// 主演 28 /// </summary> 29 public string Actor { get; set; } 30 /// <summary> 31 /// 电影类型 32 /// </summary> 33 public MovieType MovieType { get; set; } 34 /// <summary> 35 /// 定价 36 /// </summary> 37 public int Price { get; set; } 38 } 39 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Project.Entity 8 { 9 /// <summary> 10 /// 电影类型,枚举类型 11 /// </summary> 12 public enum MovieType 13 { 14 动作, 15 奇幻, 16 冒险, 17 科幻, 18 爱情, 19 剧情, 20 惊悚, 21 犯罪 22 } 23 }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.IO; namespace Project.Entity { /// <summary> /// 放映计划类,保存影院当天的放映计划集合 /// </summary> public class Schedule { /// <summary> /// 放映场次属性 /// </summary> public Dictionary<string, ScheduleItem> Items { get; set; } /// <summary> /// 加载XML文件,获取电影信息 /// </summary> public void LoadItem() { } } }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Project.Entity 8 { 9 /// <summary> 10 /// 影院每天放映计划的场次,保存每场电影的信息 11 /// </summary> 12 public class ScheduleItem 13 { 14 /// <summary> 15 /// 放映时间 16 /// </summary> 17 public string Time { get; set; } 18 /// <summary> 19 /// 本场放映电影属性 20 /// </summary> 21 public Movie Movie { get; set; } 22 } 23 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Project.Entity 8 { 9 /// <summary> 10 /// 保存影院的座位信息 11 /// </summary> 12 public class Seat 13 { 14 /// <summary> 15 /// 座位号 16 /// </summary> 17 public string SeatNum { get; set; } 18 19 /// <summary> 20 /// 座位卖出状态颜色 21 /// </summary> 22 public System.Drawing.Color Color { get; set; } 23 } 24 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Project.Entity 8 { 9 /// <summary> 10 /// 工具类 11 /// </summary> 12 public class TicketUtil 13 { 14 /// <summary> 15 /// 创建电影票 16 /// </summary> 17 public Ticket CreateTicket(string Item, Seat seats, int discount, string custName, string type) 18 { 19 return null; 20 } 21 } 22 }
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 using System.Xml; 11 using System.IO; 12 using Project.Entity; 13 14 namespace Project 15 { 16 public partial class FrmMain : Form 17 { 18 public FrmMain() 19 { 20 InitializeComponent(); 21 BindPlayList(); 22 BindSeat(); 23 } 24 25 /// <summary> 26 /// 放映场次集合对象,ScheduleItem为播放时间 27 /// </summary> 28 Dictionary<string, ScheduleItem> items = new Dictionary<string, ScheduleItem>(); 29 30 Cinema cinema = new Cinema(); 31 32 /// <summary> 33 /// 获取电影名 34 /// </summary> 35 /// <param name="Xml">XML文件</param> 36 /// <returns>返回电影集合,里面包含了XML里存有的电影名</returns> 37 public List<string> GetMoviesName(string Xml) 38 { 39 //存储电影名字 40 List<string> Name = new List<string>(); 41 XmlDocument doc = new XmlDocument(); 42 doc.Load(Xml); 43 XmlNode nodeList = doc.DocumentElement; 44 //遍历MovieList的子节点Movie 45 foreach (XmlNode node in nodeList.ChildNodes) 46 { 47 //遍历子节点Moive中的所有子节点 48 foreach (XmlNode sonNode in node.ChildNodes) 49 { 50 switch (sonNode.Name) 51 { 52 case "Name": 53 Name.Add(node["Name"].InnerText); 54 break; 55 } 56 } 57 } 58 return Name; 59 } 60 61 /// <summary> 62 /// 绑定电影放映列表(有bug,并不能读取所有时间,能读取有多少时间,但只会按照第一个时间重复) 63 /// </summary> 64 public void BindPlayList() 65 { 66 this.tvList.Nodes.Clear(); 67 //List<string> movieName = GetMoviesName("MovieList.xml"); 68 //foreach (string names in movieName) 69 //{ 70 // this.tvList.Nodes.Add(names); 71 //} 72 ////往下绑定时间点 73 XmlDocument doc = new XmlDocument(); 74 doc.Load("MovieList.xml"); 75 XmlNode nodeList = doc.DocumentElement; 76 foreach (XmlNode node in nodeList.ChildNodes) 77 { 78 //电影名称 79 TreeNode treeNode = new TreeNode(); 80 treeNode.Text = node["Name"].InnerText; 81 82 foreach (XmlNode itemNode in node["Schedule"].ChildNodes) 83 { 84 //获取场次时间段 85 TreeNode ScheduleItem = new TreeNode(); 86 ScheduleItem.Text = itemNode.InnerText; 87 //添加在电影下的时间段 88 treeNode.Nodes.Add(ScheduleItem); 89 } 90 tvList.Nodes.Add(treeNode); 91 } 92 } 93 94 /// <summary> 95 /// 点击电影场次后,获取电影详情信息(unFinish,Bug:无法更改演员等信息) 96 /// </summary> 97 /// <param name="sender"></param> 98 /// <param name="e"></param> 99 private void tvList_AfterSelect(object sender, TreeViewEventArgs e) 100 { 101 //通过选中场次,获取xml中对应的场次,再获取该场次对应的电影名 102 //获取选择的节点 103 TreeNode itemNode = this.tvList.SelectedNode; //选中的场次 104 TreeNode movieNode = itemNode.Parent; //场次的父节点(也就是电影名) 105 //判断选择节点为空,空为无效,不继续执行 106 if (itemNode == null) 107 { 108 return; 109 } 110 //判断选择节点不是时间节点,为无效,不继续执行 111 if (itemNode.Level != 1) 112 { 113 return; 114 } 115 string item = itemNode.Text; //场次 116 string movieName = movieNode.Text;//电影名 117 XmlDocument doc = new XmlDocument(); 118 doc.Load("MovieList.xml"); 119 XmlNode node = doc.DocumentElement; 120 bool isMovie = false; //判断电影名是否正确 121 bool isItem = false; //判断电影场次是否正确 122 foreach (XmlNode movieNameNode in node.ChildNodes) //总节点中找子节点 123 { 124 //判断电影名存在,改为true 125 if (movieNameNode["Name"].InnerText.Equals(movieName)) 126 { 127 isMovie = true; 128 foreach (XmlNode itemNodes in movieNameNode["Schedule"].ChildNodes) //子节点中找Schedule 129 { 130 //判断场次存在,改为true 131 if (itemNodes.InnerText.Equals(item)) 132 { 133 isItem = true; 134 } 135 } 136 } 137 if (isMovie && isItem) 138 { 139 //电影名 140 this.lblName.Text = movieName; 141 //导演 142 this.lblDirector.Text = movieNameNode["Director"].InnerText; 143 //主演 144 this.lblActor.Text = movieNameNode["Actor"].InnerText; 145 //类型 146 this.lblType.Text = movieNameNode["Type"].InnerText; 147 //放映时间 148 this.lblSchedule.Text = item; 149 //原票价 150 this.lblPrice.Text = movieNameNode["Price"].InnerText; 151 //电影图片 152 this.picPoster.Image = Image.FromFile(movieNameNode["Poster"].InnerText); 153 break; 154 } 155 } 156 157 } 158 159 /// <summary> 160 /// 选择赠票时 161 /// </summary> 162 /// <param name="sender"></param> 163 /// <param name="e"></param> 164 private void rbtnFree_CheckedChanged(object sender, EventArgs e) 165 { 166 this.cmbDiscount.Enabled = false; 167 this.txtSender.Enabled = true; 168 this.lblDisPrice.Text = "0"; 169 } 170 171 /// <summary> 172 /// 选择普通票时 173 /// </summary> 174 /// <param name="sender"></param> 175 /// <param name="e"></param> 176 private void rbtnNormal_CheckedChanged(object sender, EventArgs e) 177 { 178 this.cmbDiscount.Enabled = false; 179 this.txtSender.Enabled = false; 180 this.lblDisPrice.Text = ""; 181 } 182 183 /// <summary> 184 /// 选择学生票时 185 /// </summary> 186 /// <param name="sender"></param> 187 /// <param name="e"></param> 188 private void rbtnStudent_CheckedChanged(object sender, EventArgs e) 189 { 190 this.txtSender.Text = ""; 191 this.cmbDiscount.Enabled = true; 192 this.txtSender.Enabled = false; 193 this.cmbDiscount.Text = "7"; 194 Price(); 195 } 196 197 /// <summary> 198 /// 学生折扣下拉更改价格 199 /// </summary> 200 /// <param name="sender"></param> 201 /// <param name="e"></param> 202 private void cmbDiscount_SelectedIndexChanged(object sender, EventArgs e) 203 { 204 Price(); 205 } 206 207 /// <summary> 208 /// 计算折扣价格 209 /// </summary> 210 public void Price() 211 { 212 if (this.lblPrice.Text == "") 213 { 214 return; 215 } 216 int price = int.Parse(this.lblPrice.Text); 217 int discount = int.Parse(this.cmbDiscount.Text); 218 this.lblDisPrice.Text = (price * discount / 10).ToString(); 219 } 220 221 /// <summary> 222 /// 绑定座位 223 /// </summary> 224 public void BindSeat() 225 { 226 int seatRow = 7, seatLine = 5; 227 Label label; 228 for (int i = 0; i < seatRow; i++) 229 { 230 for (int j = 0; j < seatLine; j++) 231 { 232 label = new Label(); 233 //设置标签的背景颜色 234 label.BackColor = Color.Yellow; 235 //设置标签字体 236 label.Font = new System.Drawing.Font("微软黑体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 237 //设置标签尺寸(宽,高) 238 label.AutoSize = false; 239 label.Size = new System.Drawing.Size(50, 25); 240 //设置座位号码 241 label.Text = (j + 1).ToString() + "-" + (i + 1).ToString(); 242 label.TextAlign = ContentAlignment.MiddleCenter; 243 //设置位置 244 label.Location = new Point(60 + (i * 90), 60 + (j * 60)); 245 //将所有标签绑定于点击事件中 246 label.Click += lblSeat_Click; 247 //绑定于tab控件中 248 tbSeat.Controls.Add(label); 249 } 250 } 251 } 252 253 /// <summary> 254 /// 座位标签点击事件 255 /// </summary> 256 /// <param name="sender"></param> 257 /// <param name="e"></param> 258 private void lblSeat_Click(object sender, EventArgs e) 259 { 260 try 261 { 262 //获取点击的坐标文本,这里是座位号 263 string seatNum = ((Label)sender).Text.ToString(); 264 //获取赠送人名字 265 string customerName = this.txtSender.Text.Trim(); 266 int discount = 0; //默认折扣为0 267 string type = ""; //购票类型,默认为空 268 269 if (this.rbtnStudent.Checked) //选中学生票时 270 { 271 type = "student"; 272 discount = int.Parse(this.cmbDiscount.Text); 273 } 274 else if (this.rbtnFree.Checked) //选中赠票时 275 { 276 if (string.IsNullOrEmpty(this.txtSender.Text)) 277 { 278 MessageBox.Show("请输入赠票者姓名。","提示"); 279 return; 280 } 281 else 282 { 283 type = "free"; 284 } 285 } 286 TicketUtil util = new TicketUtil(); 287 Ticket newTicket = util.CreateTicket(lblName.Text.ToString(), cinema.Seats[seatNum], discount, customerName, type); //未完成之处1 288 if (cinema.Seats[seatNum].Color == Color.Yellow) 289 { 290 DialogResult dr; 291 dr = MessageBox.Show("确定购买?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information); 292 if (dr == DialogResult.Yes) 293 { 294 cinema.Seats[seatNum].Color = Color.Red; 295 UpdateSeat(); 296 //重新计算价格 297 newTicket.CalcPrice(); 298 //添加已售出的票 299 cinema.SoldTickets.Add(newTicket); 300 //打印新的电影票 301 newTicket.Print(); // 未完成2 302 } 303 else if (dr == DialogResult.No) 304 { 305 return; 306 } 307 } 308 //显示当前选中票的信息 309 else 310 { 311 foreach (Ticket item in cinema.SoldTickets) 312 { 313 //判断座位号,电影场次,电影名 314 if (item.Seat.SeatNum == seatNum && item.ScheduleItem.Time == tvList.SelectedNode.Text && item.ScheduleItem.Movie.MovieName == tvList.SelectedNode.Parent.Text) 315 { 316 item.Show(); 317 } 318 } 319 } 320 } 321 catch (Exception ex ) 322 { 323 string Message = ex.Message; 324 MessageBox.Show("抱歉有误了," + Message); 325 } 326 } 327 328 /// <summary> 329 /// 更新座位颜色 330 /// </summary> 331 public void UpdateSeat() 332 { 333 foreach (String key in cinema.Seats.Keys) 334 { 335 //cinema.Seats[key].Color; 336 } 337 } 338 339 } 340 }
以为上程序源代码 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
程序运行效果视频:
未完成源代码。
程序界面图: