1.首先我们需要了解这个软件的功能:
1).影院每天更新放映列表,系统支持实时查看,包括电影放映场次的时间;
2).影院提供三类影票:普通票,学生票和赠票;
3).允许用户查看某场次座位的售出情况;
4).允许购票,并允许用户选座;
5).用户可以选择场次、影票类型及空闲座位进行购票,并打印电影票;
6).系统可以保存销售情况,并允许对其进行恢复;
[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;
}
}
影院座位信息
电影类
[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;
}
}
}
}
}
放映场次集合
//电影票父类
[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普通票");
}
}
电影票父类
//增票子类
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);
}
}
增票子类
学生子类
//影院类
[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();
} }
影院类
private void FrmMian_Laod(Object senber, EventArgs e)
{
InitSeatNumber(); //初始化座位号
rdoTickets.Checked = true; //选中普通票
}
窗体LOAD事件
private void InitSeatNumber()
{
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
lbl = new Label();
lbl.BackColor = Color.Yellow;
lbl.Location = new Point( + j * , + i * );
lbl.Font = new Font("宋体",);
lbl.Name = (i + ) + "-" + (j + );
lbl.Size = new Size(,);
lbl.TabIndex = ;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Text = (i + ) + "-" + (j + );
//创建一个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);
}
}
}
创建座位号方法
private void lblSeat_Click(object sender, EventArgs e)
{
if (tvMovies.Nodes.Count == )
{
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;
}
}
}
座位号单击事件
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);
}
}
获取新列表
private void tvMovies_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvMovies.SelectedNode.Level == )
{
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;
}
}
} }
}
TreeView
private void rdoTickets_CheckedChanged(object sender, EventArgs e)
{
cmbDisCount.Enabled = false;
txtName.Enabled = false;
cmbDisCount.Text = "";
txtName.Text = "";
lblCalcPrice.Text = "";
}
普通票
private void rdoFree_CheckedChanged(object sender, EventArgs e)
{
txtName.Enabled = true;
cmbDisCount.Enabled = false;
cmbDisCount.Text = "";
lblCalcPrice.Text = "";
}
增票
private void rdoStudent_checkedChanged(object sender, EventArgs e)
{
cmbDisCount.Enabled = true;
if (cmbDisCount.Text == "")
{
cmbDisCount.Text = "";
}
txtName.Enabled = false;
txtName.Text = "";
}
private void cmbDisCount_SelectedIndexChanged(object sender, EventArgs e)
{
lblCalcPrice.Text = (Convert.ToInt32(lblPrice.Text) * Convert.ToInt32(cmbDisCount.Text) / ).ToString();
}
学生票
放映计划场次