影院售票
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
//影院座位信息
[Serializable] //可序列化
public class Seat
{
public string SeatNum { get; set; } //座位号
public System.Drawing.Color Color { get; set; }
public Seat() { }
public Seat(string seatnum,System.Drawing.Color color)
{
this.SeatNum = seatnum;
this.Color = color;
}
}
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
//放映计划的场次
[Serializable]
public class ScheduleItem
{
public string Time { get; set; } //放映时间
public Movie Movie { get; set; } //本场所放电影
public ScheduleItem()
{
}
public ScheduleItem(string time,Movie movie)
{
this.Time = time;
this.Movie = movie;
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//电影类
[Serializable] //可序列化
public class Movie
{
public string MovieName { get; set; } //电影名
public string Poster { get; set; } //海报图片名
public string Director { get; set; } //导演名
public string Actor { get; set; } //主演
public string MovieType { get; set; } //电影类型
public int Price { get; set; } //电影定价
public Movie() { }
public Movie(string name,string poster,string director,string actor,string movietype,int price)
{
this.MovieName = name;
this.Poster = poster;
this.Director = director;
this.Actor = actor;
this.MovieType = movietype;
this.Price = price;
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[Serializable] //可序列化
public class Schedule
{
//放映场次集合
public Dictionary<string, ScheduleItem> Items = new Dictionary<string, ScheduleItem>();
//解析XML文件
public void LoadItems()
{
XmlDocument myxml = new XmlDocument();
myxml.Load("ShowList.xml");
XmlNode root = myxml.DocumentElement;
foreach (XmlNode item in root.ChildNodes)
{
Movie movie = new Movie();
foreach(XmlNode node in item.ChildNodes)
{
switch (node.Name)
{
case "Name":
movie.MovieName = node.InnerText;
break;
case "Poster":
movie.Poster = node.InnerText;
break;
case "Director":
movie.Director = node.InnerText;
break;
case "Actor":
movie.Actor = node.InnerText;
break;
case "Price":
movie.Price =Convert.ToInt32( node.InnerText);
break;
case "Type":
movie.MovieType = node.InnerText;
break;
case "Schedule":
foreach (XmlNode nodes in node.ChildNodes)
{
ScheduleItem sch = new ScheduleItem(nodes.InnerText,movie);
Items.Add(nodes.InnerText,sch);
}
break;
}
}
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//电影票父类
[Serializable] //可序列化
public class Ticket
{
public ScheduleItem ScheduleItem { get; set; } //放映场次
public Seat Seat { get; set; } //所属座位对象
public int Price { get; set; } //票价
public Ticket()
{
this.ScheduleItem = new ScheduleItem();
this.Seat = new Seat();
}
public Ticket(ScheduleItem item,Seat seat)
{
this.ScheduleItem = item;
this.Seat = seat;
this.Price = CalcPrice();
}
//计算票价
public virtual int CalcPrice()
{
this.Price = ScheduleItem.Movie.Price;
return this.Price;
}
//打印售票信息
public virtual void Print()
{
//取文件名
string fileName = ScheduleItem.Time.Replace(":", "-") + "" + Seat.SeatNum + ".txt";
//生成文件
FileStream fs = new FileStream(fileName,FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("*****************************");
sw.WriteLine("\t青鸟影院");
sw.WriteLine("-----------------------------");
sw.WriteLine("电影名:\t{0}", ScheduleItem.Movie.MovieName);
sw.WriteLine("时 间:\t{0}", ScheduleItem.Time);
sw.WriteLine("座位号:\t{0}", Seat.SeatNum);
sw.WriteLine("价 格:\t{0}", CalcPrice());
sw.WriteLine("*****************************");
sw.WriteLine("\t(a)普通票");
sw.Close();
fs.Close();
}
//显示售票信息
public virtual void Show()
{
MessageBox.Show("已售出!\n普通票");
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//增票子类
public class FreeTicket:Ticket
{
public string CustomerName { get; set; }
public FreeTicket() { }
public FreeTicket(ScheduleItem item,Seat seat,string name):base(item,seat)
{
this.CustomerName = name;
}
//打印售票信息
public override void Print()
{
//取文件名
string fileName = ScheduleItem.Time.Replace(":", "-") + "-" + Seat.SeatNum + ".txt";
//生成文件
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("*****************************");
sw.WriteLine("\t青鸟影院(增票)");
sw.WriteLine("-----------------------------");
sw.WriteLine("电影名:\t{0}", ScheduleItem.Movie.MovieName);
sw.WriteLine("时 间:\t{0}", ScheduleItem.Time);
sw.WriteLine("座位号:\t{0}", Seat.SeatNum);
sw.WriteLine("价 格:\t{0}", this.CustomerName);
sw.WriteLine("*****************************");
sw.WriteLine("\t(c)增票");
sw.Close();
fs.Close();
}
//显示售票信息
public override void Show()
{
MessageBox.Show("已售出!\n{0}增票",this.CustomerName);
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//学生子类
public class StudentTicket:Ticket
{
public int Discount { get; set; } //学生票折扣
public StudentTicket() { }
public StudentTicket(ScheduleItem item,Seat seat,int dis):base(item,seat)
{
this.Discount = dis;
}
//计算票价
public override int CalcPrice()
{
this.Price = ScheduleItem.Movie.Price * Discount / 10;
return Price;
}
//打印售票信息
public override void Print()
{
//取文件名
string fileName = ScheduleItem.Time.Replace(":", "-") + "-" + Seat.SeatNum + ".txt";
//生成文件
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("*****************************");
sw.WriteLine("\t青鸟影院(学生票)");
sw.WriteLine("-----------------------------");
sw.WriteLine("电影名:\t{0}", ScheduleItem.Movie.MovieName);
sw.WriteLine("时 间:\t{0}", ScheduleItem.Time);
sw.WriteLine("座位号:\t{0}", Seat.SeatNum);
sw.WriteLine("价 格:\t{0}", CalcPrice());
sw.WriteLine("*****************************");
sw.WriteLine("\t(c)学生票");
sw.Close();
fs.Close();
}
//显示售票信息
public override void Show()
{
MessageBox.Show("已售出!\n{0}学生票",this.Discount.ToString());
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//影院类
[Serializable] //可序列化
class Cinema
{
public Dictionary<string, Seat> Seats { get; set; } //座位集合
public Schedule Schedule { get; set; } //放映计划
public List<Ticket> SoldTickets { get; set; } //已售出电影
public Cinema()
{
Seats = new Dictionary<string, Seat>();
Schedule = new Schedule();
SoldTickets = new List<Ticket>();
}
//保存
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream("YingPianInfo.bin",FileMode.Create);
bf.Serialize(fs,SoldTickets);
fs.Close();
MessageBox.Show("保存成功");
}
//读取
public void Load()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream("YingPianInfo.bin", FileMode.Open);
SoldTickets = (List<Ticket>)bf.Deserialize(fs);
fs.Close();
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void FrmMian_Laod(Object senber, EventArgs e)
{
InitSeatNumber(); //初始化座位号
rdoTickets.Checked = true; //选中普通票
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void InitSeatNumber()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 7; j++)
{
lbl = new Label();
lbl.BackColor = Color.Yellow;
lbl.Location = new Point(50 + j * 90, 55 + i * 60);
lbl.Font = new Font("宋体",12);
lbl.Name = (i + 1) + "-" + (j + 1);
lbl.Size = new Size(60,25);
lbl.TabIndex = 0;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Text = (i + 1) + "-" + (j + 1);
//创建一个Label事件
lbl.Click += new System.EventHandler(lblSeat_Click);
tbRoom.Controls.Add(lbl);
Seat seat = new Seat(lbl.Text,Color.Yellow);
//保存在座位集合里
cinema.Seats.Add(seat.SeatNum,seat);
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void lblSeat_Click(object sender, EventArgs e)
{
if (tvMovies.Nodes.Count == 0)
{
MessageBox.Show("请选择电影");
return;
}
if (rdoStudent.Checked==true && txtName.Text == "")
{
MessageBox.Show("请输入增票人姓名!");
return;
}
lbl= sender as Label;
if (lbl.BackColor == Color.Red)
{
MessageBox.Show("已售出");
}
else
{
if (DialogResult.OK == MessageBox.Show("确定要购买吗?", "提示",
MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
{
string time = tvMovies.SelectedNode.Text;
ScheduleItem item = cinema.Schedule.Items[time];
string type = rdoTickets.Checked ? "Ticket" : rdoFree.Checked ? "Free" : "Student";
Ticket tic = null;
switch (type)
{
case "Ticket":
tic = new Ticket(item,cinema.Seats[lbl.Text]);
break;
case "Free":
tic = new FreeTicket(item,cinema.Seats[lbl.Text],txtName.Text);
break;
case "Student":
tic = new StudentTicket(item,cinema.Seats[lbl.Text],Convert.ToInt32(cmbDisCount.Text));
break;
}
cinema.SoldTickets.Add(tic);
tic.Print();
lbl.BackColor = Color.Red;
cinema.Seats[lbl.Text].Color = Color.Red;
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void 获取新放映列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
BingTreeView();
}
private void BingTreeView()
{
cinema.Schedule.LoadItems(); //调用解析
TreeNode root = null;
foreach (ScheduleItem item in cinema.Schedule.Items.Values)
{
//
if (root == null || root.Text != item.Movie.MovieName)
{
//根节点
root = new TreeNode(item.Movie.MovieName);
tvMovies.Nodes.Add(root);
}
//子节点
root.Nodes.Add(item.Time);
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void tvMovies_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvMovies.SelectedNode.Level == 1)
{
string time = tvMovies.SelectedNode.Text;
ScheduleItem item = cinema.Schedule.Items[time];
lblMovieName.Text = item.Movie.MovieName;
picMovie.Image = Image.FromFile(item.Movie.Poster);
lblDirector.Text = item.Movie.Director;
lblActor.Text = item.Movie.Actor;
lblType.Text = item.Movie.MovieType;
lblTime.Text = item.Time;
lblPrice.Text = item.Movie.Price.ToString();
lblCalcPrice.Text = "";
//将所有座位设置为黄色
foreach (Seat var in cinema.Seats.Values)
{
var.Color = Color.Yellow;
}
//用已售出票集合循环
foreach (Ticket ticlet in cinema.SoldTickets)
{
//座位集合的Value值
foreach (Seat seat in cinema.Seats.Values)
{
if (ticlet.ScheduleItem.Time == time && ticlet.Seat.SeatNum == seat.SeatNum)
{
seat.Color = Color.Red;
}
}
}
//遍历到Label上
foreach (Seat seat in cinema.Seats.Values)
{
foreach (Label label in tbRoom.Controls)
{
if (label.Text == seat.SeatNum)
{
label.BackColor = seat.Color;
}
}
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void rdoTickets_CheckedChanged(object sender, EventArgs e)
{
cmbDisCount.Enabled = false;
txtName.Enabled = false;
cmbDisCount.Text = "";
txtName.Text = "";
lblCalcPrice.Text = "";
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void rdoFree_CheckedChanged(object sender, EventArgs e)
{
txtName.Enabled = true;
cmbDisCount.Enabled = false;
cmbDisCount.Text = "";
lblCalcPrice.Text = "";
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private void rdoStudent_checkedChanged(object sender, EventArgs e)
{
cmbDisCount.Enabled = true;
if (cmbDisCount.Text == "")
{
cmbDisCount.Text = "7";
}
txtName.Enabled = false;
txtName.Text = "";
}
private void cmbDisCount_SelectedIndexChanged(object sender, EventArgs e)
{
lblCalcPrice.Text = (Convert.ToInt32(lblPrice.Text) * Convert.ToInt32(cmbDisCount.Text) / 10).ToString();
}