Objective-c "Complete OC Project-ticketing system-system analysis-code implementation"

Source: Internet
Author: User

Cinema Ticket System/Concert ticket system

Requirements Analysis:

First we enter the system, then we will choose to buy movie tickets or buy concert tickets, so this involves the synthesis of two systems. But we know all is the ticket system, so we can first create a class, belong to buy movie tickets and concert tickets in common, we can create a ticket, let movie tickets Movieticket and concert tickets concertticket to inherit ticket, Then write their own unique methods in Movieticket and concert. So there are three of classes involved. Think again, we call in the main function buy movie ticket Buymovieticket method and buy concert ticket Buyconcertticket method, strictly speaking, we should define them as functions, because we do not know here what class/ Instance object to call these two tickets, so we might as well define them as a parameterless method, and then let the user choose whether to call Buymovieticket or call the Buyconcertticket method.

So the next step, we should get into the two ticket-buying function.

(1) Thinking analysis

First we go into the Buymovieticket function. In this function we are going to call a lot of ways, including buying tickets, showing the movies that are being shown, choosing a movie, choosing the number of rows, choosing the seat number, paying, and ticketing. So in order to invoke these methods, we have to create an instance object for invocation, so which class should we use to create the object? First cannot use ticket, because this ticket is an illusion class, not actually exists, then uses the Movieticket class? Obviously instantiating an object with the Movieticket class we should call it "a movie ticket", and then there are many instance variable information on the instance object of this movie ticket. Obviously movie tickets are not able to call the method of buying tickets (because we have to call the method of buying tickets before we get the ticket). So we obviously have to create another class, since we usually go to the cinema to go to the movies, then why don't we create a cinema class that means that the movie theater sees the information provided by the cinema and then buys the tickets. This makes a lot of sense. So what are the instance variables that the cinema class should have and how? Obviously a movie theater should have a name that we can set as _cinemaname, and in addition to the movie theater we can see the film showing on the big screen scrolling, for example, to put 5 movies today, we can create an array, and then to store the five movies, and then for the user to choose. So what are the attributes (instance variables) of the five films? Obviously, the movie information is similar to the movie ticket information, we can instantiate a Moiveticket instance array ticket[5] to store the five movie information, and then each of the array members is a movie message that might be viewed by the user. The array member can inherit some properties from the Movieticket because it is of type movieticket, so we can override the construction method when we initialize the cinema instance object, and create an array of information for five released movies while creating the instance object. Then call the first object method in the Buymovieticket function Buyticket, of course buyticket This behavior is produced in the cinema, so Buyticket method belongs to the cinema class.

We accomplished a very important step, that is, to rewrite the construction method, then go to the Buyticket object method, and then call, and then we need to complete: show the movie is showing (-(void) list;), select the Movie (-(void) Selectmovie;), Select the number of rows (-(void) selectrow;), select the seat number (-(void) selectcol;), start paying (-(void) pay;), the Ticket (-(void) printTicket;), and a series of methods to operate. ★ The second method is called inside the method at the end of each method. Until the end of the purchase of a movie ticket for the user to print the ticket information. ★

The specific steps are as follows: First, we should know how to define these attributes (instance variables) in the right place, and secondly, we should be aware of how these methods should be implemented.

Well, now, buymovieticket this function needs to be done and even the following series of operations we have taken into account, in the thought analysis of this one link we do so much is enough, the next is to write the Code section.

(2) Code section

Time reason we only wrote the process of buying movie tickets, we did not write a concert ticket purchase process.

The entire code is divided into two folders and a main function: (Each of the various classes of. h and. m files is isolated from each folder)

①cinema folder

—————————————————— Cinema.h ——————————————————


#import <Foundation/Foundation.h>
#import "MovieTicket.h"

@interface Cinema:nsobject
{
The name of the cinema
NSString * _CINEMANAME;

The movie theater is showing five films, are all movieticket type, then we will have these movie objects in an array of movieticket type
Movieticket *tickets[5];
}

Of course, I think it's necessary for us to define a new construction method
-(Instancetype) Initwithcinemaname: (NSString *) cinemaname;

Start buying Tickets
-(void) buytickets;

Show the movie that is showing
-(void) listmovies;

Choose a movie
-(void) selectmovies;

Select number of rows
-(void) SelectRow;

Select seat number
-(void) Selectcol;

Start payment
-(void) paytickets;

Ticketing (print out the ticket information)
-(void) printtickets;


@end


—————————————————— Cinema.h ——————————————————

—————————————————— CINEMA.M ——————————————————


#import "Cinema.h"

Define a global variable to record the movie number
int Selectno;

Macro defines the number of rows and columns in the cinema hall
#define ROW 8
#define COL 15

@implementation Cinema

Overriding construction methods
-(Instancetype) Initwithcinemaname: (NSString *) cinemaname
{
if (Self=[super init]) {
Make _cinemaname equal to the cinemaname we passed in.
_cinemaname=cinemaname;

Next we will initialize the second instance variable in cinema, which is an array of type Movieticket
Naturally we are going to use a for loop to initialize (this place note, although it is the initialization of all elements in the array, but we can only guarantee that the individual information inside the difference, that is, the value of the loop can be a different place, and some of the text part, temporarily unable to initialize the value of the same)
for (int i=0; i<5; i++) {
Create a new Movieticket instance object each time for the For loop (create a total of 5) and assign a value to its instance variable
Movieticket *mvticket=[movieticket New];
Movie Name
Mvticket.name=[nsstring stringwithformat:@ "Mission Spy%02d", i+1];
Director
[Email protected] "Wang Zhengyao";
Starring
[Email protected] "Ma Shunze, Zhangtongsheng, Zheng";
Auditorium Information
Mvticket.place=[nsstring stringwithformat:@ "%02d Hall", I+1];
Premiere time
Mvticket.showtime=[nsstring stringwithformat:@ "200%d-12-31", I];
Start time
Mvticket.starttime=[nsstring stringwithformat:@ "1%d:15", I];
Movie number
mvticket.no=i+1;
Duration
mvticket.lengthtime=120;
Fare
mvticket.movieticketprice=35;
Finally, we assign our initialized instance object to an array of type Movieticket.
Tickets[i]=mvticket;
}

}
return self;
}

Start buying Tickets
-(void) buytickets
{
NSLog (@ "Start buying tickets! ");
There is no unnecessary implementation process, the purchase of tickets directly into the next method: Show the movie is showing
[Self listmovies];
}

Show the movie that is showing
/*
Ideas:
1) Use an existing tickets array to print the array information
2) iterating through the array
Format:

Number Name
Director starring
Start time length
Premiere Time Hall information
Executive Fares
*/
Show the movie that is showing
-(void) listmovies
{
for (int i=0; i<5; i++) {
NSLog (@ "\ n =" #:%d Name:%@\n Director:%@ Starring:%@\n start:%@ time:%d\n premiere:%@ Hall information:%@\n Executive fare:%.2f\n\n ", Tickets[i].no,tickets[i].name , Tickets[i].director,tickets[i].actor,tickets[i].starttime,tickets[i].lengthtime,tickets[i].showtime,tickets[i ].place,tickets[i].movieticketprice);
}

Select a movie and proceed to the next method
[Self selectmovies];
}

Choose a movie
-(void) selectmovies
{
NSLog (@ "Please select the movie number you want to buy and enter: \ n");

The user enters the number and then determines whether the number is correct, and if so, assigns the value to the created global variable Selectno
int num;
scanf ("%d", &num);
if (num>0| | num<=5) {
Selectno=num;
}
Else
If you enter an error, jump directly out of the program
Return

If the input is successful, prompt for the selected movie
NSLog (@ "\ n you have selected movie%d, name%@\n\n", Selectno,tickets[selectno-1].name);

Select the number of rows to proceed to the next method
[Self selectrow];
}

Select number of rows
/*

Ideas:
1) Take a look at the seating information of the cinema.
2) Tip, how many rows are there in the current hall, and how many seats are in each row?
3) Prompts the user to select the number of rows
4) Save Row count information
*/

Select number of rows
-(void) SelectRow
{
NSLog (@ "\ n Select the number of rows");
NSLog (@ "\ n Below is the seat information for the session: \ n");

Print the cinema location first to let the user see
for (int i=-1; i<row; i++) {
for (int j=-1; j<col; J + +) {
if (i==-1) {
printf ("%02d", j+1);
}
else if (j==-1)
{
printf ("%02d", i+1);
}
Else
printf ("+");
}
printf ("\ n");
}

Tip How many rows there are in the hall, with a few seats in each row
printf ("\ n the hall has%d rows,%d seats in each row \ n", Row,col);
printf ("Please select the number of rows: \ n");
Let the customer choose the number of rows, enter the number of rows, it is best to determine the value of the customer input
int num;
scanf ("%d", &num);
Judging condition: The number of rows must be a number between 1~8
if (num>0| | NUM&LT;=8) {

Assigning a reasonable number of rows to RowNum
Here to explain the selectNo-1, the first step has already determined the number of the film, and then the actual number of the film is 0~4, so you have to enter the number based on the reduction of one to use
Tickets[selectno-1].rownum=num;

}
Select a seat and go to the next method
[Self selectcol];
}

Select seat number
-(void) Selectcol
{
NSLog (@ "\ n Select seat number");
int num;
scanf ("%d", &num);
if (num>0| | NUM&LT;=15) {

Assigning a reasonable number of rows to RowNum
Tickets[selectno-1].colnum=num;
}

Print the user-selected ticket information for the user to view, and then let the user confirm
Format:
You choose XXX Videos
Start time: Movie Duration:
Block xx xx
Price:

NSLog (@ "\ n \ nyou selected a movie:%@ \ n start time:%@ Movie Duration:%d\n%d row%d seat \ n You pay the fare:%.2f\n\n", tickets[selectno-1].name,t ICKETS[SELECTNO-1].STARTTIME,TICKETS[SELECTNO-1].LENGTHTIME,TICKETS[SELECTNO-1].ROWNUM,TICKETS[SELECTNO-1]. Colnum,tickets[selectno-1].movieticketprice);

NSLog (@ "\ n Please confirm your ticket information: (1. Confirmation 0. cancel) \ n");
int flag;
scanf ("%d", &flag);
if (flag) {
Payment, go to the next method
[Self paytickets];
}
Else
NSLog (@ "Exit the system! ");
}

Start payment
-(void) paytickets
{
NSLog (@ "\ n start paying ...");
NSLog (@ "\ n Please wait! ");
NSLog (@ "\ n Pay success!!! ");
[Self printtickets];
}

Ticketing (print out the ticket information)
-(void) printtickets
{
NSLog (@ "\ nthe ticket");

NSLog (@ "\ n you selected a movie:%@ \ n start time:%@ Movie Duration:%d\n%d row%d seat \ n You pay the fare:%.2f\n Hall:%@\n\n", Tickets[selectno-1].name,tick Ets[selectno-1].starttime,tickets[selectno-1].lengthtime,tickets[selectno-1].rownum,tickets[selectno-1].colnum , tickets[selectno-1].movieticketprice,tickets[selectno-1].place);

Print seat Information table!
for (int i=-1;i<row;i++)
{
for (int j=-1; j<col; J + +) {
if ((i==tickets[selectno-1].rownum-1) && (j==tickets[selectno-1].colnum-1))
{
printf ("@");
}
Else
{
Else the code can be copied directly above
if (i==-1)
I is 0 o'clock, which means line 1th, where the position of line 1 is to be marked
{
printf ("%02d", j+1);
}
else if (j==-1)
J is 0 o'clock, which indicates the 1th column, when the position of column 1 is to be written on the upstream label
{
printf ("%02d", i+1);
}
Else
printf ("+");
}
}
printf ("\ n");
}


}

@end


—————————————————— CINEMA.M ——————————————————

②allticket folder

—————————————————— Ticket.h ——————————————————


#import <Foundation/Foundation.h>

@interface Ticket:nsobject

Place
@property NSString * place;

Premiere time
@property NSString * SHOWTIME;

Show Start time
@property NSString *starttime;

Duration
@property int lengthtime;

Name
@property NSString * NAME;

Director
@property NSString * Director;

Starring
@property NSString * actor;

Number of rows
@property int rowNum;

Seat number
@property int colnum;

Number
@property int No;


@end


—————————————————— Ticket.h ——————————————————

—————————————————— TICKET.M ——————————————————


#import "Ticket.h"

@implementation Ticket

@end


—————————————————— TICKET.M ——————————————————

—————————————————— MovieTicket.h ——————————————————


#import "Ticket.h"

@interface Movieticket:ticket

Fare
@property float Movieticketprice;

Number of movie tickets (serial number)
@property int movieticketnum;

@end


—————————————————— MovieTicket.h ——————————————————

—————————————————— MOVIETICKET.M ——————————————————


#import "MovieTicket.h"

@implementation Movieticket

@end


—————————————————— MOVIETICKET.M ——————————————————


★ The whole project is so simple, always exercise our not the basic knowledge, but how to build such a framework of the mind, all aspects are to be. We should exercise this ability and let ourselves have the project mind.

It ' s all!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c "Complete OC Project-ticketing system-system analysis-code implementation"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.