C#学习经历从基本语法结构到窗体再到面向对象终于走完了.NET初级程序员的道路,做为品德优良好学生更不能落下课程的总结项目-某某鸟《影院售票系统》。用了大概一天半的时间做完这个练手项目,先上效果截图一张

C#总结项目《影院售票系统》编写总结一-LMLPHP

抽出时间做些这个对目前的我来说算不小的项目。

用到的知识点有:面向对象思想、TreeView、XML读取、File文件流、泛型集合,这里面对我来说难度最大的是面向对象与泛型集合的结合,看来学习一门编程语言的难点还是在设计思想上。

  再来介绍一下项目需求:在影片列表中选择某个时段的一场电影,单击座位选择一个种类的电影票,并创建电影,计算价格并打印影票信息,然后该座位被置为红色表示已经售出。

影院每天更新放映列表,系统支持实时查看,包括电影放映场次时间、电影概况。

影院提供三类影票:普通票、赠票和学生票(赠票免费;学生票有折扣)

允许用户查看某场次座位的售出情况

支持购票,并允许用户选座

用户可以选择场次、影票类型及空闲座位进行购票,并打印电影票

系统可以保存销售情况,并允许对其进行恢复

二.问题分析

1.系统开发步骤

(1)明确需求

(2)设计类

(3)创建项目

(4)确定编码顺序

1.主窗体

2.查看新放映列表

3.查看电影介绍

4.查看影票票价

5.查看放映厅座位

6.购票和打印电影票

7.继续购票

(5)测试

三、类的设计

1.Seat:保存影院的座位信息,主要属性如下

座位号(SeatNum):string类型

座位卖出状态颜色(Color):System.Drawing.Color类型

2.Movie:电影类

电影名(MovieName):string类型

海报图片路径(Poster):string类型

导演名(Director):string类型

主演(Actor):string类型

电影类型(MovieType):MovieType自定义枚举类型

定价(Price):int类型

3.Ticket:电影票父类,保存电影票信息

放映场次(ScheduleItem):ScheduleItem自定义类

所属座位对象(Seat):Seat自定义类型

票价(Price):int类型

计算票价的虚方法CalcPrice()

打印售票信息的虚方法Print()

显示当前售出票信息的虚方法Show()

4.StudentTicket:学生票子类,继承父类Ticket

学生票的折扣(Discount):int类型

重写父类计算票价CalcPrice

重写父类打印售票信息的Print()

重写父类显示当前出票信息的Show()方法

5.FreeTicket:赠票子类,继承父类Ticket

获得赠票者的名字属性(CustomerName):string类型

重写父类计算票价CalcPrice()

重写父类打印售票信息Print()

重写父类显示当前出票信息Show()

6.ScheduleItem:影院每天计划放映计划的场次,保存每场电影的信息

放映时间属性(Time):string类型

本场所放映电影属性(Movie):Movie自定义类型

7.Schedule:放映计划类

放映场次属性(Items):自定义泛型集合Dictionary<string,ScheduleItem>

读取XML文件获取放映计划集合的LoadItems()方法

8.Cinema:影院类,保存放映计划和座位类

座位集合属性(Seat):自定义泛型集合Dictionary<string,Seat>

放映计划Schedule:Schedule自定义类型

已售出电影票的集合(SoldTicket):自定义泛型集合List<Ticket>

保存和读取售票情况的Save()和Load()方法

献上这9个类的代码,根据依赖编写类的顺序,不能完全按照上面顺序

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing; namespace 影院售票系统
{
/// <summary>
/// 保存影院的座位信息
/// </summary>
public class Seat
{
public Seat() { }
public Seat(string seatNum,Color color)
{
this.SeatNum = seatNum;
this.Color = color;
}
private string _seatNum;
/// <summary>
/// 座位号
/// </summary>
public string SeatNum
{
get { return _seatNum; }
set { _seatNum = value; }
}
private Color _color;
/// <summary>
/// 座位卖出状态颜色
/// </summary>
public Color Color
{
get { return _color; }
set { _color = value; }
}
}
}

Seat

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 影院售票系统
{
/// <summary>
/// 电影类
/// </summary>
public class Movie
{
private string _movieName;
/// <summary>
/// 电影名
/// </summary>
public string MovieName
{
get { return _movieName; }
set { _movieName = value; }
}
private string _poster;
/// <summary>
/// 海报图片名
/// </summary>
public string Poster
{
get { return _poster; }
set { _poster = value; }
}
private string _director;
/// <summary>
/// 导演名
/// </summary>
public string Director
{
get { return _director; }
set { _director = value; }
}
private string _actor;
/// <summary>
/// 主演
/// </summary>
public string Actor
{
get { return _actor; }
set { _actor = value; }
} private int _price;
/// <summary>
/// 定价
/// </summary>
public int Price
{
get { return _price; }
set { _price = value; }
}
/// <summary>
/// 电影类型
/// </summary>
public MovieType MovieType { get; set; }
}
/// <summary>
/// 电影类型,1喜剧2战争3爱情
/// </summary>
public enum MovieType
{
/// <summary>
/// 动作片
/// </summary>
Action = ,
/// <summary>
/// 战争片
/// </summary>
War = ,
/// <summary>
/// 爱情片
/// </summary>
Comedy =
}
}

Movie

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; namespace 影院售票系统
{
/// <summary>
/// 电影票父类
/// </summary>
public class Ticket
{
public Ticket() { }
public Ticket(ScheduleItem sch,Seat seat)
{
this.ScheduItem = sch;
this.Seat = seat;
}
private Seat _seat = new Seat();
/// <summary>
/// 所属座位
/// </summary>
public Seat Seat
{
get { return _seat; }
set { _seat = value; }
} private int _price;
/// <summary>
/// 票价
/// </summary>
public int Price
{
get { return _price; }
set { _price = value; }
}
/// <summary>
/// 放映场次
/// </summary>
public ScheduleItem ScheduItem { get; set; }
/// <summary>
/// 计算票价
/// </summary>
public virtual void CalcPrice()
{
this.Price = ScheduItem.Movie.Price;
}
/// <summary>
/// 打印售票信息
/// </summary>
public virtual void Print()
{
string info = string.Format("************************************************\n\t青鸟影院\n------------------------------------------------\n电影名:\t{0}\n时间:\t{1}\n座位号:\t{2}\n价格:\t{3}\n************************************************", this.ScheduItem.Movie.MovieName, this.ScheduItem.Time, this.Seat.SeatNum, this.Price);
MessageBox.Show(info);
//存到文件中
string fileName = this.ScheduItem.Time.Replace(":", "-")+" "+this.Seat.SeatNum+".txt";
FileStream fs = new FileStream(fileName,FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(info);
sw.Close();
fs.Close();
}
/// <summary>
/// 显示当前售票信息
/// </summary>
public virtual void Show()
{
string info = string.Format("已售出!\n普通票!");
MessageBox.Show(info);
}
}
}

Ticket

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace 影院售票系统
{
/// <summary>
/// 学生票
/// </summary>
public class StudentTicket : Ticket
{
public StudentTicket() { }
public StudentTicket(ScheduleItem sch, Seat seat, int discount)
: base(sch, seat)
{
this.Discount = discount;
}
private int _discount;
/// <summary>
/// 学生票的折扣
/// </summary>
public int Discount
{
get { return _discount; }
set { _discount = value; }
}
/// <summary>
/// 计算学生票价
/// </summary>
public override void CalcPrice()
{
this.Price =this.ScheduItem.Movie.Price* Discount / ;
}
/// <summary>
/// 打印学生票的售票信息
/// </summary>
public override void Print()
{
string info = string.Format("************************************************\n\t青鸟影院(学生)\n------------------------------------------------\n电影名:\t{0}\n时间:\t{1}\n座位号:\t{2}\n价格:\t{3}\n************************************************", this.ScheduItem.Movie.MovieName, this.ScheduItem.Time, this.Seat.SeatNum, this.Price);
MessageBox.Show(info);
//存到文件中
string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(info);
sw.Close();
fs.Close();
}
/// <summary>
/// 显示当前售出票信息
/// </summary>
public override void Show()
{
string info = string.Format("已售出!\n{0}折学生票!",this.Discount);
MessageBox.Show(info);
}
}
}

StudentTicket

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; namespace 影院售票系统
{
/// <summary>
/// 赠票
/// </summary>
public class FreeTicket:Ticket
{
public FreeTicket() { }
public FreeTicket(ScheduleItem sch,Seat seat,string name)
{
this.Seat = seat;
this.CustomerName = name;
this.ScheduItem = sch;
}
private string _customerName;
/// <summary>
/// 获得赠票者的名字
/// </summary>
public string CustomerName
{
get { return _customerName; }
set { _customerName = value; }
}
/// <summary>
/// 计算票价
/// </summary>
public override void CalcPrice()
{
this.Price = ;
}
/// <summary>
/// 打印售票信息
/// </summary>
public override void Print()
{
string info = string.Format("************************************************\n\t青鸟影院(赠票)\n------------------------------------------------\n电影名:\t{0}\n时间:\t{1}\n座位号:\t{2}\n姓名:\t{3}\n************************************************", this.ScheduItem.Movie.MovieName, this.ScheduItem.Time, this.Seat.SeatNum, this.CustomerName);
MessageBox.Show(info);
//存到文件中
string fileName = this.ScheduItem.Time.Replace(":", "-") + " " + this.Seat.SeatNum + ".txt";
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(info);
sw.Close();
fs.Close();
}
/// <summary>
/// 显示当前售出票信息
/// </summary>
public override void Show()
{
MessageBox.Show("已售出!\n赠票!");
}
}
}

FreeTicket

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 影院售票系统
{
/// <summary>
/// 影院每天计划放映的场次,保存每场电影的信息
/// </summary>
public class ScheduleItem
{
private string _time;
/// <summary>
/// 放映时间
/// </summary>
public string Time
{
get { return _time; }
set { _time = value; }
}
private Movie _movie = new Movie(); /// <summary>
/// 本场放映的电影
/// </summary>
public Movie Movie
{
get { return _movie; }
set { _movie = value; }
}
private List<Ticket> _soldTickets=new List<Ticket>(); private Dictionary<string, Seat> _seats=new Dictionary<string,Seat>();
/// <summary>
/// 本场次的座位状态
/// </summary>
public Dictionary<string, Seat> Seats
{
get { return _seats; }
set { _seats = value; }
}
}
}

ScheduleItem

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace 影院售票系统
{
/// <summary>
/// 放映计划类,保存影院当天的放映计划集合
/// </summary>
public class Schedule
{
/// <summary>
/// 放映场次
/// </summary>
public Dictionary<string, ScheduleItem> Items = new Dictionary<string, ScheduleItem>();
/// <summary>
/// 读取XML文件获取放映计划集合
/// </summary>
public void LoadItems()
{
Items.Clear();
XmlDocument xml = new XmlDocument();
xml.Load("ShowList.xml");
XmlElement root = xml.DocumentElement;
foreach (XmlNode item in root.ChildNodes)
{
Movie movie = new Movie();
movie.MovieName = item["Name"].InnerText;
movie.Poster = item["Poster"].InnerText;
movie.Director = item["Director"].InnerText;
movie.Actor = item["Actor"].InnerText;
switch (item["Type"].InnerText)
{
case "Action":
movie.MovieType = MovieType.Action;
break;
case "War":
movie.MovieType = MovieType.War;
break;
case "Comedy":
movie.MovieType = MovieType.Comedy;
break;
}
movie.Price = Convert.ToInt32(item["Price"].InnerText);
if (item["Schedule"].HasChildNodes)
{
foreach (XmlNode item2 in item["Schedule"].ChildNodes)
{
ScheduleItem schItem = new ScheduleItem();
schItem.Time = item2.InnerText;
schItem.Movie = movie;
Items.Add(schItem.Time, schItem);
}
} } }
}
}

Schedule

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 影院售票系统
{
/// <summary>
/// 影院类
/// </summary>
public class Cinema
{
private Dictionary<string, Seat> _seats = new Dictionary<string, Seat>();
/// <summary>
/// 座位集合
/// </summary>
public Dictionary<string, Seat> Seats
{
get { return _seats; }
set { _seats = value; }
}
private Schedule _schedule = new Schedule();
/// <summary>
/// 放映计划
/// </summary>
public Schedule Schedule
{
get { return _schedule; }
set { _schedule = value; }
}
private List<Ticket> _soldTickets=new List<Ticket>();
/// <summary>
/// 已经售出的票
/// </summary>
public List<Ticket> SoldTickets
{
get { return _soldTickets; }
set { _soldTickets = value; }
}
/// <summary>
/// 保存售票信息到文件中
/// </summary>
public void Save()
{
//Save和Load的代码在窗体的代码实现了
}
/// <summary>
/// 从文件中读取售票信息
/// </summary>
public void Load() { }
}
}

Cinema

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 影院售票系统
{
/// <summary>
/// 工具类
/// </summary>
public class TicketUtil
{
/// <summary>
/// 创建电影票
/// </summary>
/// <returns></returns>
public static Ticket CreateTicket(ScheduleItem sch,Seat seat,int discount,string customerName,string type)
{
Ticket ticket=null;
switch (type)
{
case "StudentTicket":
ticket = new StudentTicket(sch,seat,discount);
break;
case "FreeTicket":
ticket = new FreeTicket(sch,seat,customerName);
break;
default:
ticket = new Ticket(sch,seat);
break;
}
return ticket;
}
}
}

TicketUtil

明天将继续更新-电影院座位的动态绘制、电影信息绑定到窗体中展现出来,也望各路大神出手斧正不合理的代码(不要涉及分层开发,我们在学,以后会用分层开发实现其他的项目)

05-11 11:31