欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#影院售票系统毕业设计(1)

程序员文章站 2022-05-14 22:37:47
 c#学习经历从基本语法结构到窗体再到面向对象终于走完了.net初级程序员的道路,用了大概一天半的时间做完这个练手项目《影院售票系统》,先上效果截图一张 抽出时间做些...

 c#学习经历从基本语法结构到窗体再到面向对象终于走完了.net初级程序员的道路,用了大概一天半的时间做完这个练手项目《影院售票系统》,先上效果截图一张

C#影院售票系统毕业设计(1)

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

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

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

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

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

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

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

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

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

一、问题分析

1.系统开发步骤

(1)明确需求

(2)设计类

(3)创建项目

(4)确定编码顺序

      1.主窗体

      2.查看新放映列表

      3.查看电影介绍

      4.查看影票票价

     5.查看放映厅座位

     6.购票和打印电影票

     7.继续购票

(5)测试

二、类的设计

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

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

座位号(seatnum):string类型

座位卖出状态颜色(color):system.drawing.color类型

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; }
 }
 }
 }

2.movie:电影类

电影名(moviename):string类型

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

导演名(director):string类型

主演(actor):string类型

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

定价(price):int类型

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 = 0,
 /// <summary>
 /// 战争片
 /// </summary>
 war = 1,
 /// <summary>
 /// 爱情片
 /// </summary>
 comedy = 2
 }
 }

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

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

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

票价(price):int类型

计算票价的虚方法calcprice()

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

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

 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);
 }
 }
 }

4.studentticket:学生票子类,继承父类ticket

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

重写父类计算票价calcprice

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

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

 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 / 10;
 }
 /// <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);
 }
 }
 }

5.freeticket:赠票子类,继承父类ticket

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

重写父类计算票价calcprice()

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

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

 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 = 0;
 }
 /// <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赠票!");
 }
 }
 }

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

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

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

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; }
 }
 }
 }

7.schedule:放映计划类

放映场次属性(items):自定义泛型集合dictionary<string,scheduleitem>

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

 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);
  }
  }
 
 }

 }
 }
 }

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

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

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

已售出电影票的集合(soldticket):自定义泛型集合list<ticket>

保存和读取售票情况的save()和load()方法

 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() { }
 }
 }

9.工具类

 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;
 }
 }
}

下篇文章将继续更新,内容有电影院座位的动态绘制、电影信息绑定到窗体中展现出来,希望大家不要走开,有什么地方需要改进的欢迎大家指出,共同探讨进步。