Home>

C#learning experience has finally gone from basic grammatical structure to form to object-oriented.net junior programmer's path,It took about a day and a half to finish this hand training project"Cinema Ticketing System",First take a screenshot of the effect

Take the time to do this project which is not small for me at present.

The knowledge points used are:Object-oriented thinking, treeview, xml reading, file stream, generic collectionThe most difficult thing for me here is the combination of object-oriented and generic collection.It seems that the difficulty in learning a programming language is still in design thinking.

Let's introduce it againProject requirements:Select a movie from a time period in the movie list.Click on the seat to select a genre of movie tickets,And create movies,Calculate prices and print movie ticket information,The seat is then set to red to indicate that it has been sold.

The theater updates its screening list daily,The system supports real-time viewing,Including movie show time, movie overview.

The cinema offers three types of movie tickets:Ordinary tickets, free tickets and student tickets (free tickets;Student tickets are discounted)

Allow users to view the sales of a seat

Support ticket purchase,And allow users to choose seats

Users can choose tickets, ticket types and free seats to purchase tickets.And print movie tickets

The system can save sales,And allow it to be restored

I. Problem Analysis

1. System development steps

(1) Clear requirements

(2) Design

(3) Create a project

(4) Determine the coding order

Main form

2. View the new show list

3. View movie introduction

4. View movie ticket fare

5. Check the auditorium seats

6. Buy tickets and print movie tickets

7. Continue to buy tickets

(5) Test

Second, the design of the class

Contribute the code of these 9 classes,According to the order in which the classes are written by dependencies,Not exactly in the order above

1.seat:save theater seat information,The main attributes are as follows

Seat number (seatnum):string type

Seat selling status color (system):system.drawing.color type

using system;
 using system.collections.generic;
 using system.linq;
 using system.text;
 using system.threading.tasks;
 using system.drawing;
 namespace cinema ticketing system
 {
 ///<summary>
 ///Save theater seat information
 ///</summary>
 public class seat
 {
 public seat () {}
 public seat (string seatnum, color color)
 {
 this.seatnum=seatnum;
 this.color=color;
 }
 private string _seatnum;
 ///<summary>
 ///seat number
 ///</summary>
 public string seatnum
 {
 get {return _seatnum;}
 set {_seatnum=value;}
 }
 private color _color;
 ///<summary>
 ///seat sale status color
 ///</summary>
 public color color
 {
 get {return _color;}
 set {_color=value;}
 }
 }
 }

2.movie:movie

Moviename:string type

Poster image path (poster):string type

Director name:string type

Actor:string type

Movietype:movietype custom enumeration type

Price (int)

using system;
 using system.collections.generic;
 using system.linq;
 using system.text;
 using system.threading.tasks;
 namespace cinema ticketing system
 {
 ///<summary>
 ///movie
 ///</summary>
 public class movie
 {
 private string _moviename;
 ///<summary>
 ///movie name
 ///</summary>
 public string moviename
 {
 get {return _moviename;}
 set {_moviename=value;}
 }
 private string _poster;
 ///<summary>
 ///poster picture name
 ///</summary>
 public string poster
 {
 get {return _poster;}
 set {_poster=value;}
 }
 private string _director;
 ///<summary>
 ///Director's name
 ///</summary>
 public string director
 {
 get {return _director;}
 set {_director=value;}
 }
 private string _actor;
 ///<summary>
 ///starring
 ///</summary>
 public string actor
 {
 get {return _actor;}
 set {_actor=value;}
 }
 private int _price;
 ///<summary>
 ///pricing
 ///</summary>
 public int price
 {
 get {return _price;}
 set {_price=value;}
 }
 ///<summary>
 ///movie type
 ///</summary>
 public movietype movietype {get;set;}
 }
 ///<summary>
 ///movie type,1 comedy 2 war 3 love
 ///</summary>
 public enum movietype
 {
 ///<summary>
 ///Action movie
 ///</summary>
 action=0, ///<summary>
 ///War Movies
 ///</summary>
 war=1, ///<summary>
 ///romance
 ///</summary>
 comedy=2
 }
 }

3.ticket:movie ticket parent class,Save movie ticket information

Showtime (scheduleitem):scheduleitem custom class

The seat object (seat):seat custom type

Price:int type

Virtual method for calculating fare calcprice ()

Virtual method print () for printing ticket information

Virtual method show () to display the current ticket information

using system;
 using system.collections.generic;
 using system.linq;
 using system.text;
 using system.threading.tasks;
 using system.windows.forms;
 using system.io;
 namespace cinema ticketing system
 {
 ///<summary>
 ///movie ticket parent
 ///</summary>
 public class ticket
 {
 public ticket () {}
 public ticket (scheduleitem sch, seat seat)
 {
 this.scheduitem=sch;
 this.seat=seat;
 }
 private seat _seat=new seat ();
 ///<summary>
 ///The seat
 ///</summary>
 public seat seat
 {
 get {return _seat;}
 set {_seat=value;}
 }
 private int _price;
 ///<summary>
 ///fare
 ///</summary>
 public int price
 {
 get {return _price;}
 set {_price=value;}
 }
 ///<summary>
 ///Showtimes
 ///</summary>
 public scheduleitem scheduitem {get;set;}
 ///<summary>
 ///calculate fare
 ///</summary>
 public virtual void calcprice ()
 {
 this.price=scheduitem.movie.price;
 }
 ///<summary>
 ///Print ticket information
 ///</summary>
 public virtual void print ()
 {
 string info=string.format ("****************************************** ****** \ n \ tBluebird Cinema \ n ------------------------------------ ------------ \ nMovie name:\ t {0} \ nTime:\ t {1} \ nSeat number:\ t {2} \ nPrice:\ t {3} \ n ******************************************** ", this.scheduitem.movie.moviename, this.scheduitem.time, this.seat.seatnum, this.price);
 messagebox.show (info);
 //Save to file
 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>
 ///show current ticket information
 ///</summary>
 public virtual void show ()
 {
 string info=string.format ("Sold! \ nOrdinary tickets!");
 messagebox.show (info);
 }
 }
 }

4.studentticket:student ticket subclass,Inherit the parent ticket

Discount for student tickets (int)

Override parent class to calculate fare calcprice

Override print () of the parent class to print ticket information

Override the show () method of the parent class to display the current ticketing information

using system;
 using system.collections.generic;
 using system.linq;
 using system.text;
 using system.threading.tasks;
 using system.windows.forms;
 using system.io;
 namespace cinema ticketing system
 {
 ///<summary>
 ///student ticket
 ///</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>
 ///student ticket discount
 ///</summary>
 public int discount
 {
 get {return _discount;}
 set {_discount=value;}
 }
 ///<summary>
 ///calculate student fare
 ///</summary>
 public override void calcprice ()
 {
 this.price=this.scheduitem.movie.price * discount/10;
 }
 ///<summary>
 ///Print student ticket sales information
 ///</summary>
 public override void print ()
 {
 string info=string.format ("****************************************** ****** \ n \ tBluebird Cinema (Student) \ n --------------------------------- --------------- \ nMovie name:\ t {0} \ nTime:\ t {1} \ nSeat number:\ t {2} \ nPrice:\ t {3} \ n ********************************************* *** ", this.scheduitem.movie.moviename, this.scheduitem.time, this.seat.seatnum, this.price);
 messagebox.show (info);
 //Save to file
 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>
 ///Show current ticket information
 ///</summary>
 public override void show ()
 {
 string info=string.format ("Sold! \ n {0} Discount Student Tickets!", this.discount);
 messagebox.show (info);
 }
 }
 }

5.freeticket:free ticket subclass,Inherit the parent ticket

Get the name of the ticket giver (customername):string type

Override parent class to calculate fare calcprice ()

Override parent class print ticketing information print ()

Override the parent class to show the current ticketing information 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 cinema ticketing system
 {
 ///<summary>
 ///free ticket
 ///</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>
 ///get the name of the ticket giver
 ///</summary>
 public string customername
 {
 get {return _customername;}
 set {_customername=value;}
 }
 ///<summary>
 ///calculate fare
 ///</summary>
 public override void calcprice ()
 {
 this.price=0;
 }
 ///<summary>
 ///Print ticket information
 ///</summary>
 public override void print ()
 {
 string info=string.format ("****************************************** ****** \ n \ tBluebird Cinema (free ticket) \ n -------------------------------- ---------------- \ nMovie name:\ t {0} \ nTime:\ t {1} \ nSeat number:\ t {2} \ nName:\ t {3} \ n ******************************************** **** ", this.scheduitem.movie.moviename, this.scheduitem.time, this.seat.seatnum, this.customername);
 messagebox.show (info);
 //Save to file
 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>
 ///Show current ticket information
 ///</summary>
 public override void show ()
 {
 messagebox.show ("Sold! \ nFree tickets!");
 }
 }
 }

6.scheduleitem:The planned schedule of the theater every day,Save information for each movie

Show time attribute (time):string type

Movie properties at this venue:movie custom type

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
 namespace cinema ticketing system
 {
 ///<summary>
 ///The theater plans to show every day,Save information for each movie
 ///</summary>
 public class scheduleitem
 {
 private string _time;
 ///<summary>
 ///showtime
 ///</summary>
 public string time
 {
 get {return _time;}
 set {_time=value;}
 }
 private movie _movie=new movie ();
 ///<summary>
 ///movies shown in this field
 ///</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>
 ///Seating status of this session
 ///</summary>
 public dictionary<string, seat>seats
 {
 get {return _seats;}
 set {_seats=value;}
 }
 }
 }

7.schedule:Show schedule class

Showtime properties (items):custom generic collection dictionary<string, scheduleitem>

Load xml file to get loaditems () method of show plan collection

using system;
 using system.collections.generic;
 using system.linq;
 using system.text;
 using system.threading.tasks;
 using system.xml;
namespace cinema ticketing system
 {
 ///<summary>
 ///show plan class,Save the day's show schedule collection in the theater
 ///</summary>
 public class schedule
 {
 ///<summary>
 ///Showtimes
 ///</summary>
 public dictionary<string, scheduleitem>items=new dictionary<string, scheduleitem>();
 ///<summary>
 ///read the xml file to get the show plan collection
 ///</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:theater class, save show plan and seat class

Seat collection attributes (seat):custom generic collection dictionary<string, seat>

Show schedule:Schedule custom type

Collection of sold movie tickets (soldticket):custom generic collection list<ticket>

Save () and load () methods for saving and reading ticket sales

using system;
 using system.collections.generic;
 using system.linq;
 using system.text;
 using system.threading.tasks;
 namespace cinema ticketing system
 {
 ///<summary>
 ///theater
 ///</summary>
 public class cinema
 {
 private dictionary<string, seat>_seats=new dictionary<string, seat>
 ///<summary>
 ///Seat collection
 ///</summary>
 public dictionary<string, seat>seats
 {
 get {return _seats;}
 set {_seats=value;}
 }
 private schedule _schedule=new schedule ();
 ///<summary>
 ///show plan
 ///</summary>
 public schedule schedule
 {
 get {return _schedule;}
 set {_schedule=value;}
 }
 private list<ticket>_soldtickets=new list<ticket>();
 ///<summary>
 ///already sold tickets
 ///</summary>
 public list<ticket>soldtickets
 {
 get {return _soldtickets;}
 set {_soldtickets=value;}
 }
 ///<summary>
 ///Save the ticket information to a file
 ///</summary>
 public void save ()
 {
 //save and load code is implemented in the form code
 }
 ///<summary>
 ///read ticket information from the file
 ///</summary>
 public void load () {}
 }
 }

9. Tools

using system;
 using system.collections.generic;
 using system.linq;
 using system.text;
 using system.threading.tasks;
 namespace cinema ticketing system
 {
 ///<summary>
 ///Tools
 ///</summary>
 public class ticketutil
 {
 ///<summary>
 ///Create movie tickets
 ///</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;
 }
 }
}
c
  • Previous MySQL method to verify user permissions
  • Next How to write a for loop in Swift programming