using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MyCinema.Models;
namespace MyCinema
{
public partial class MainForm : Form
{
Cinema cinema;
Dictionary<string, Label> labels = new Dictionary<string, Label>();
int ticket = 0;
string key = null;
public MainForm()
{
InitializeComponent();
}using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace MyCinema.Models
{
[Serializable]
public class FreeTicket:Ticket
{
public FreeTicket() { }
public FreeTicket(ScheduleItem scheduleItem, Seat seat, string customerName)
: base(scheduleItem, seat)
{
this.CustomerName = customerName;
}
private string customerName;
public string CustomerName
{
get { return customerName; }
set { customerName = value; }
}
public override void CalcPrice()
{
this.Price = 0;
}
public override void Print()
{
string fileName = this.ScheduleItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("***************************");
sw.WriteLine(" 青鸟影院 (赠票)");
sw.WriteLine("---------------------------");
sw.WriteLine(" 电影名:\t{0}", this.ScheduleItem.Movie.MovieName);
sw.WriteLine(" 时间: \t{0}", this.ScheduleItem.Time);
sw.WriteLine(" 座位号:\t{0}", this.Seat.SeatNum);
sw.WriteLine(" 姓名: \t{0}", this.CustomerName);
sw.WriteLine("***************************");
sw.Close();
fs.Close();
}
/// <summary>
///显示售出票信息
/// </summary>
public override void Show()
{
MessageBox.Show("已售出!\n\n 赠送者:"+this.CustomerName);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
namespace MyCinema.Models
{
public enum MovieType
{
//喜剧
Comedy,
//战争
War,
//爱情
Romance,
//动作
Action,
//卡通
Cartoon,
//恐怖
Thriller,
//冒险
Adventure
}
[Serializable]
public class Movie
{
public Movie() { }
public Movie(string movieName, string poster, string director,string actor,MovieType movieType,int price)
{
this.MovieName = movieName;
this.Poster = poster;
this.Director = director;
this.Actor = actor;
this.MovieType = movieType;
this.Price = price;
}
/// <summary>
/// 电影名称
/// </summary>
private string movieName;
public string MovieName
{
get { return movieName; }
set { movieName = value; }
}
/// <summary>
/// 海报图片名
/// </summary>
private string poster;
public string Poster
{
get { return poster; }
set { poster = value; }
}
/// <summary>
/// 导演名
/// </summary>
private string director;
public string Director
{
get { return director; }
set { director = value; }
}
/// <summary>
/// 主演
/// </summary>
private string actor;
public string Actor
{
get { return actor; }
set { actor = value; }
}
/// <summary>
/// 电影类型
/// </summary>
private MovieType movieType;
public MovieType MovieType
{
get { return movieType; }
set { movieType = value; }
}
/// <summary>
/// 电影定价
/// </summary>
private int price;
public int Price
{
get { return price; }
set { price = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;
namespace MyCinema.Models
{
/// <summary>
/// 描述当天的放映计划
/// </summary>
[Serializable]
public class Schedule
{
public Schedule()
{
items = new Dictionary<string, ScheduleItem>();
}
/// <summary>
/// 放映计划中的放映列表
/// </summary>
private Dictionary<string, ScheduleItem> items;
/// <summary>
/// 放映计划中的放映列表
/// </summary>
public Dictionary<string, ScheduleItem> Items
{
get { return items; }
set { items = value; }
}
/// <summary>
/// 从XML文件读取放映列表数据
/// </summary>
public void LoadItems()
{
if (items == null)
items = new Dictionary<string, ScheduleItem>();
items.Clear();
XmlDocument myXml = new XmlDocument();
myXml.Load("ShowList.xml");
XmlNode feednode = myXml.DocumentElement;
//中间变量
string movieName = null;
string playBill = null;
string director = null;
string actor = null;
string movieType = null;
string price = null;
foreach (XmlNode node in feednode.ChildNodes)
{
if (node.Name == "Movie")
{
foreach (XmlNode subNode in node.ChildNodes)
{
switch (subNode.Name)
{
case "Name":
movieName = subNode.InnerText;
break;
case "Poster":
playBill = subNode.InnerText;
break;
case "Director":
director = subNode.InnerText;
break;
case "Actor":
actor = subNode.InnerText;
break;
case "Type":
movieType = subNode.InnerText;
break;
case "Price":
price = subNode.InnerText;
break;
case "Schedule":
foreach (XmlNode scheduleNode in subNode.ChildNodes)
{
ScheduleItem item = new ScheduleItem();
item.Time = scheduleNode.InnerText;
item.Movie.MovieName = movieName;
item.Movie.Poster = playBill;
item.Movie.Director = director;
item.Movie.Actor = actor;
item.Movie.MovieType = (MovieType)Enum.Parse(typeof(MovieType), movieType);
item.Movie.Price = int.Parse(price);
items.Add(item.Time,item);
}
break;
}
}
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
namespace MyCinema.Models
{
/// <summary>
/// 播放场次
/// </summary>
[Serializable]
public class ScheduleItem
{
public ScheduleItem()
{
movie = new Movie();
}
/// <summary>
/// 放映时间
/// </summary>
private string time;
public string Time
{
get { return time; }
set { time = value; }
}
/// <summary>
/// 要放映的电影对象
/// </summary>
private Movie movie;
public Movie Movie
{
get { return movie; }
set { movie = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
namespace MyCinema.Models
{
/// <summary>
/// 座位类
/// </summary>
[Serializable]
public class Seat
{
public Seat(string seatNum, Color color)
{
this.SeatNum = seatNum;
this.Color = color;
}
/// <summary>
/// 座位号
/// </summary>
private string seatNum;
public string SeatNum
{
get { return seatNum; }
set { seatNum = value; }
}
/// <summary>
/// 显示售出与否的颜色属性
/// </summary>
private Color color;
public Color Color
{
get { return color; }
set { color = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace MyCinema.Models
{
/// <summary>
/// 学生票子类
/// </summary>
[Serializable]
public class StudentTicket:Ticket
{
public StudentTicket() { }
public StudentTicket(ScheduleItem scheduleItem, Seat seat, int discount)
: base(scheduleItem, seat)
{
this.Discount = discount;
}
//折扣数
private int discount;
public int Discount
{
get { return discount; }
set { discount = value; }
}
/// <summary>
/// 重写价格
/// </summary>
public override void CalcPrice()
{
this.Price = this.ScheduleItem.Movie.Price * Discount / 10;
}
/// <summary>
/// 打印数据
/// </summary>
public override void Print()
{
string fileName = this.ScheduleItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("***************************");
sw.WriteLine(" 青鸟影院 (学生)");
sw.WriteLine("---------------------------");
sw.WriteLine(" 电影名:\t{0}", this.ScheduleItem.Movie.MovieName);
sw.WriteLine(" 时间: \t{0}", this.ScheduleItem.Time);
sw.WriteLine(" 座位号:\t{0}", this.Seat.SeatNum);
sw.WriteLine(" 价格: \t{0}", this.Price.ToString());
sw.WriteLine("***************************");
sw.Close();
fs.Close();
}
/// <summary>
/// 显示售出票信息
/// </summary>
public override void Show()
{
MessageBox.Show("已售出!\n\n " + this.discount + "折学生票!");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace MyCinema.Models
{
/// <summary>
/// 售票系统电影票的基类 可以实例化普通票
/// </summary>
[Serializable]
public class Ticket
{
public Ticket() { }
public Ticket(ScheduleItem scheduleItem, Seat seat)
{
this.ScheduleItem = scheduleItem;
this.Seat = seat;
}
/// <summary>
/// 座位对象
/// </summary>
private Seat seat;
public Seat Seat
{
get { return seat; }
set { seat = value; }
}
/// <summary>
/// 票价
/// </summary>
private int price;
public int Price
{
get { return price; }
set { price = value; }
}
/// <summary>
/// 所属的放映场次
/// </summary>
private ScheduleItem scheduleItem;
public ScheduleItem ScheduleItem
{
get { return scheduleItem; }
set { scheduleItem = value; }
}
/// <summary>
/// 计算票价的方法
/// 可重写
/// </summary>
public virtual void CalcPrice()
{
this.Price = this.ScheduleItem.Movie.Price;
}
/// <summary>
/// 打印票实现
/// </summary>
public virtual void Print()
{
string fileName = this.ScheduleItem.Time.Replace(":","-") + " " + this.Seat.SeatNum + ".txt";
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("***************************");
sw.WriteLine(" 青鸟影院");
sw.WriteLine("---------------------------");
sw.WriteLine(" 电影名:\t{0}", this.ScheduleItem.Movie.MovieName);
sw.WriteLine(" 时间: \t{0}", this.ScheduleItem.Time);
sw.WriteLine(" 座位号:\t{0}", this.Seat.SeatNum);
sw.WriteLine(" 价格: \t{0}", this.Price.ToString());
sw.WriteLine("***************************");
sw.Close();
fs.Close();
}
/// <summary>
/// 显示售出票信息
/// </summary>
public virtual void Show()
{
MessageBox.Show("已售出");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace MyCinema.Models
{
/// <summary>
/// 创建票的工具类
/// </summary>
public class TicketUtil
{
public static Ticket CreateTicket(ScheduleItem scheduleItem, Seat seat,
int discount,string customerName, string type)
{
Ticket newTicket = null;
switch (type)
{
case "student":
newTicket = new StudentTicket(scheduleItem, seat, discount);
break;
case "free":
newTicket = new FreeTicket(scheduleItem, seat, customerName);
break;
case "":
newTicket = new Ticket(scheduleItem, seat);
break;
}
return newTicket;
}
}
}
private void MainForm_Load(object sender, EventArgs e) { this.lblActor.Text = ""; this.lblDirector.Text = ""; this.lblMovieName.Text = ""; this.lblPrice.Text = ""; this.lblTime.Text = ""; this.lblType.Text = ""; this.lblCalcPrice.Text = ""; this.txtCustomer.Enabled
= false; this.cmbDisCount.Enabled = false; this.rdoNormal.Checked = true; cinema = new Cinema(); //初始化放映厅座位 InitSeats(7, 5, tpCinema); cinema.Load(); } /// <summary> /// 初始化放映厅座位 /// </summary> /// <param name="seatRow">行数</param> /// <param name="seatLine">列数</param>
/// <param name="tb"></param> private void InitSeats(int seatRow,int seatLine,TabPage tb) { Label label; Seat seat; for (int i = 0; i < seatRow; i++) { for (int j = 0; j < seatLine; j++) { label = new Label(); //设置背景颜色 label.BackColor = Color.Yellow; //设置字体
label.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,((byte)(134))); //设置尺寸 label.AutoSize = false; label.Size = new System.Drawing.Size(50, 25); //设置座位号 label.Text = (j + 1).ToString() + "-"
+ (i + 1).ToString(); label.TextAlign = ContentAlignment.MiddleCenter; //设置位置 label.Location = new Point(60 + (i * 90), 60 + (j * 60)); //所有的标签都绑定到同一事件 label.Click += new System.EventHandler(lblSeat_Click); tb.Controls.Add(label); labels.Add(label.Text, label);
//实例化一个座位 seat = new Seat((j + 1).ToString() + "-" + (i + 1).ToString(), Color.Yellow); //保存的座位集合 cinema.Seats.Add(seat.SeatNum, seat); } } } //选择“继续销售” private void tsmiMovies_Click(object sender, EventArgs e) { //判断放映列表是否为空 if (cinema.Schedule.Items.Count
== 0) { cinema.Schedule.LoadItems(); } InitTreeView(); } //选择“获取最新播放列表” private void tsmiNew_Click(object sender, EventArgs e) { cinema.Schedule.LoadItems(); cinema.SoldTickets.Clear(); InitTreeView(); } /// <summary> /// 初始化TreeView控件 /// </summary> private
void InitTreeView() { tvMovies.BeginUpdate(); tvMovies.Nodes.Clear(); string movieName = null; TreeNode movieNode = null; foreach (ScheduleItem item in cinema.Schedule.Items.Values) { if (movieName != item.Movie.MovieName) { movieNode = new TreeNode(item.Movie.MovieName);
tvMovies.Nodes.Add(movieNode); } TreeNode timeNode = new TreeNode(item.Time); movieNode.Nodes.Add(timeNode); movieName = item.Movie.MovieName; } tvMovies.EndUpdate(); } /// <summary> /// 选择一场电影事件 /// </summary> /// <param name="sender"></param> /// <param
name="e"></param> private void tvMovies_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode node = tvMovies.SelectedNode; if (node == null) return; if (node.Level != 1) return; key = node.Text; //将详细信息显示 this.lblMovieName.Text = cinema.Schedule.Items[key].Movie.MovieName;
this.lblDirector.Text = cinema.Schedule.Items[key].Movie.Director; this.lblActor.Text = cinema.Schedule.Items[key].Movie.Actor; this.lblPrice.Text = cinema.Schedule.Items[key].Movie.Price.ToString(); this.lblTime.Text = cinema.Schedule.Items[key].Time; this.lblType.Text
= cinema.Schedule.Items[key].Movie.MovieType.ToString(); this.picMovie.Image = Image.FromFile(cinema.Schedule.Items[key].Movie.Poster); this.lblCalcPrice.Text = ""; //清空座位 ClearSeat(); //遍历该场电影的座位销售情况 foreach (Ticket ticket in cinema.SoldTickets) { foreach
(Seat seat in cinema.Seats.Values) { if ((ticket.ScheduleItem.Time == key) &&(ticket.Seat.SeatNum == seat.SeatNum)) { seat.Color = Color.Red; } } } UpdateSeat(); } /// <summary> /// 清空座位 /// </summary> private void ClearSeat() { foreach (Seat seat in cinema.Seats.Values)
{ seat.Color = Color.Yellow; } } /// <summary> /// 更新座位状态 /// </summary> private void UpdateSeat() { foreach (string key in cinema.Seats.Keys) { labels[key].BackColor = cinema.Seats[key].Color; } } /// <summary> /// 点击一个座位 /// 买票事件 /// </summary> /// <param
name="sender"></param> /// <param name="e"></param> private void lblSeat_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.lblMovieName.Text)) { MessageBox.Show("您还没选择电影!","提示"); return; } ticket++; try { string seatNum = ((Label)sender).Text.ToString();
string customerName = this.txtCustomer.Text.ToString(); int discount = 0; string type = ""; if (this.rdoStudent.Checked) { type = "student"; if (this.cmbDisCount.Text == null) { MessageBox.Show("请输入折扣数!","提示"); return; } else { discount = int.Parse(this.cmbDisCount.Text);
} } else if (this.rdoFree.Checked) { if (String.IsNullOrEmpty(this.txtCustomer.Text)) { MessageBox.Show("请输入赠票者姓名!","提示"); return; } else { type = "free"; } } //调用工具类创建票 Ticket newTicket = TicketUtil.CreateTicket(cinema.Schedule.Items[key], cinema.Seats[seatNum],
discount, customerName, type); if (cinema.Seats[seatNum].Color == Color.Yellow) { //打印 DialogResult result; result = MessageBox.Show("是否购买?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { cinema.Seats[seatNum].Color
= Color.Red; UpdateSeat(); newTicket.CalcPrice(); cinema.SoldTickets.Add(newTicket); lblCalcPrice.Text = newTicket.Price.ToString(); newTicket.Print(); } else if (result == DialogResult.No) { return; } } else { //显示当前售出票的信息 foreach (Ticket ticket0 in cinema.SoldTickets)
{ //判断是否为同场次、同电影、同座位号 if (ticket0.Seat.SeatNum == seatNum && ticket0.ScheduleItem.Time==tvMovies.SelectedNode.Text && ticket0.ScheduleItem.Movie.MovieName==tvMovies.SelectedNode.Parent.Text) { ticket0.Show(); } } } } catch (Exception ex) { MessageBox.Show(ex.ToString());
} } //选择“赠票”时 private void rdoFree_CheckedChanged(object sender, EventArgs e) { this.txtCustomer.Enabled = true; this.cmbDisCount.Enabled = false; this.cmbDisCount.Text = ""; //设置“优惠价” this.lblCalcPrice.Text = "0"; } //选择“学生票”时 private void rdoStudent_CheckedChanged(object
sender, EventArgs e) { this.txtCustomer.Enabled = false; this.txtCustomer.Text = ""; this.cmbDisCount.Enabled = true; this.cmbDisCount.Text = "7"; //根据当前选中的电影,设置“优惠价” if(this.lblPrice.Text!="") { int price = int.Parse(this.lblPrice.Text); int discount = int.Parse(this.cmbDisCount.Text);
this.lblCalcPrice.Text = (price * discount / 10).ToString(); } } //选择“普通票”时 private void rdoNormal_CheckedChanged(object sender, EventArgs e) { this.cmbDisCount.Enabled = false; this.txtCustomer.Text = ""; this.txtCustomer.Enabled = false; this.cmbDisCount.Text
= ""; this.lblCalcPrice.Text = ""; } private void tsmiExit_Click(object sender, EventArgs e) { cinema.Save(); this.Dispose(); } private void tsmiSave_Click(object sender, EventArgs e) { cinema.Save(); } private void MainForm_FormClosing(object sender, FormClosingEventArgs
e) { DialogResult close; close = MessageBox.Show("是否保存当前销售状态?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (close == DialogResult.Yes) { //退出时保存Cinema对象 cinema.Save(); } } //选择“不同折扣”下拉列表 private void cmbDisCount_SelectedIndexChanged(object
sender, EventArgs e) { //根据当前选中的电影,设置“优惠价” if (this.lblPrice.Text != "") { int price = int.Parse(this.lblPrice.Text); int discount = int.Parse(this.cmbDisCount.Text); this.lblCalcPrice.Text = (price * discount / 10).ToString(); } } }}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
namespace MyCinema.Models
{
public class Cinema
{
public Cinema()
{
seats = new Dictionary<string, Seat>();
soldTickets = new List<Ticket>();
schedule = new Schedule();
}
/// <summary>
/// 放映厅座位集合
/// </summary>
private Dictionary<string, Seat> seats;
public Dictionary<string, Seat> Seats
{
get { return seats; }
set { seats = value; }
}
private Schedule schedule;
/// <summary>
/// 当天的放映计划
/// </summary>
public Schedule Schedule
{
get { return schedule; }
set { schedule = value; }
}
private List<Ticket> soldTickets;
public List<Ticket> SoldTickets
{
get { return soldTickets; }
set { soldTickets = value; }
}
//保存售票信息到文件中
public void Save()
{
FileStream fs = new FileStream("soldTickets.txt", FileMode.OpenOrCreate);
StreamWriter writer = new StreamWriter(fs, Encoding.Default);
for (int index = 0; index < SoldTickets.Count; index++)
{
Ticket ticket = SoldTickets[index];
string info = "|" + ticket.ScheduleItem.Movie.MovieName + "|" + ticket.ScheduleItem.Movie.Poster + "|" +
ticket.ScheduleItem.Movie.Director + "|" + ticket.ScheduleItem.Movie.Actor + "|" + ticket.ScheduleItem.Movie.MovieType.ToString() + "|" +
ticket.ScheduleItem.Movie.Price + "|" + ticket.ScheduleItem.Time + "|" + ticket.Seat.SeatNum + "|" + ticket.Seat.Color + "|" + ticket.Price + "|";
if (ticket is FreeTicket)
{
string customerName=((FreeTicket)ticket).CustomerName;
writer.WriteLine("free" + info + customerName);
}
else if (ticket is StudentTicket)
{
writer.WriteLine("student" + info + "");
}
else
{
writer.WriteLine("" + info + "");
}
}
writer.WriteLine("The End");
writer.Close();
fs.Close();
}
//从文件中读取售票信息
public void Load()
{
try
{
StreamReader reader = new StreamReader("soldTickets.txt", Encoding.GetEncoding("GB2312"));
string line = reader.ReadLine();
string[] propertyValues;
Ticket ticket = null;
while (line.Trim() != "The End")
{
propertyValues = line.Split('|');
string type = propertyValues[0];
Movie movie = new Movie(propertyValues[1], propertyValues[2], propertyValues[3], propertyValues[4], (MovieType)Enum.Parse(typeof(MovieType), propertyValues[5]), int.Parse(propertyValues[6]));
ScheduleItem scheduleItem = new ScheduleItem();
scheduleItem.Time = propertyValues[7];
scheduleItem.Movie = movie;
string color = propertyValues[9];
string endColor = color.Substring(color.IndexOf("[") + 1, color.Length - 1 - color.IndexOf("[") - 1);
Seat seat = new Seat(propertyValues[8], Color.FromName(endColor));
int discount = 10;
switch (type)
{
case "student":
discount = 7;
ticket = TicketUtil.CreateTicket(scheduleItem, seat, discount, "", type);
break;
case "free":
discount = 0;
ticket = TicketUtil.CreateTicket(scheduleItem, seat, discount, propertyValues[11], type);
break;
default:
discount = 10;
ticket = TicketUtil.CreateTicket(scheduleItem, seat, discount, "", "");
break;
}
this.SoldTickets.Add(ticket);
line = reader.ReadLine();
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine("出错了:"+ex.Message);
soldTickets = new List<Ticket>();
}
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-35214.html