Objective-C [complete OC project-ticketing system-System Analysis-code implementation], objective-coc
Cinema Ticketing System/concert Ticketing System
Requirement Analysis:
First, we enter the system, and then we will choose to buy a movie ticket or a concert ticket, so this involves the synthesis of the two systems. But we know that they are all buying tickets, so we can create a category first, which is the same as buying movie tickets and tickets for concerts. We can create a Ticket, let movie tickets MovieTicket and Concert tickets ConcertTicket inherit Ticket, and then write their own unique methods in MovieTicket and Concert. So this involves three classes. Let's try again. In the main function, we call the buyMovieTicket Method for buying movie tickets and the buyConcertTicket Method for buying concert tickets. Strictly speaking, we should define them as functions, because we do not know what classes/instance objects are used to call the two ticket buying methods, we should define them as the no-argument method, then, let the user choose whether to call buyMovieTicket or the buyConcertTicket method.
Next, we should enter the two ticket buying functions.
(1) Train of Thought Analysis
First, go to the buyMovieTicket function. In this function, we need to call a lot of methods, including buying tickets, displaying movies in progress, selecting movies, selecting the number of rows, selecting the seat number, paying, and issuing tickets. To call these methods, we must create an instance object for calling. Which class should we use to create an object? Ticket cannot be used first, Because Ticket is an hypothetical class and does not actually exist. What about the MovieTicket class? Obviously, the MovieTicket class is used to instantiate an object. We should call it "a movie ticket", and there is a lot of instance variable information on this movie ticket instance object. Obviously, the method of buying tickets cannot be called for movie tickets (because we have to call the method of buying tickets before getting tickets ). Then we obviously want to create another category. Since we usually go to the Cinema to watch movies, why don't we create a Cinema class, which means that the Cinema can see the information provided by the Cinema and then buy a ticket. This makes a lot of sense. So which instance variables and methods should the Cinema class have? Obviously, a cinema should have a name. We can set it to _ cinemaName. In addition, when we enter the cinema, we can see the information for showing the scrolling movies on the large screen. For example, we want to release five movies today, we can create an array and store these five movies for users to choose. What are the attributes of these five films (instance variables? 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, then, every array Member in this array is a piece of information that may be watched by users. Because the array member is of the MovieTicket type, it can inherit some attributes of MovieTicket, in this way, we can override the constructor method when initializing the Cinema instance object, and create an array of information for the five released films while creating the instance object, then, call the first object method buyTicket in the buyMovieTicket function. Of course, the buyTicket behavior is generated in the Cinema, so the buyTicket method belongs to the Cinema class.
We have completed a very important step, that is, to rewrite the constructor method, then enter the buyTicket object method, and then call it. Then we need to complete: display the ongoing movie (-(void) list;), select movie (-(void) selectMovie;), select the number of rows (-(void) selectRow;), select the seat number (-(void) selectCol ;), start to pay (-(void) pay;), issue tickets (-(void) printTicket;) and other methods.★At the end of each method, the second method is called within the method. The ticket information is printed for the user after the final purchase.★
The specific steps are as follows: first, we should know how to define these attributes (instance variables) at the right position; second, we should know how to implement these methods.
Now, we have considered the operations required for the buyMovieTicket function and a series of subsequent operations. We have done so much in the analysis of ideas. The next step is to write the code.
(2) Code Section
For the reason of time, we only wrote about the process of purchasing movie tickets. We did not write about the purchase process of concert tickets.
The entire code is divided into two folders and one main function: (each folder has its own. h and. m files for each class)
① Cinema folder
------------------ Cinema. h ------------------
# Import <Foundation/Foundation. h>
# Import "MovieTicket. h"
@ Interface Cinema: NSObject
{
// Cinema name
NSString * _ cinemaName;
// The five films being released in the cinema are of the MovieTicket type, so we may wish to store these movie objects in an array of the MovieTicket type.
MovieTicket * tickets [5];
}
// Of course, I think it is necessary to define a new constructor.
-(Instancetype) initWithCinemaName :( NSString *) cinemaName;
// Start ticket purchase
-(Void) buyTickets;
// Display the ongoing movie
-(Void) listMovies;
// Select a movie
-(Void) selectMovies;
// Select the number of rows
-(Void) selectRow;
// Select the seat number
-(Void) selectCol;
// Start payment
-(Void) payTickets;
// Issue the ticket (print 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 and movie hall
# Define ROW 8
# Define COL 15
@ Implementation Cinema
// Rewrite the constructor
-(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, that is, an array of the MovieTicket type.
// Naturally, we need to use the for loop for initialization (note that, although all elements in the initialization array, we can only ensure that the individual information in it is different, that is, the places where values can be recycled can be different, while some text parts cannot be initialized for the time being)
For (int I = 0; I <5; I ++ ){
// Create a new MovieTicket instance object every time in the for loop (create five together) and assign values to its instance variables
MovieTicket * mvTicket = [MovieTicket new];
// Movie name
MvTicket. name = [NSString stringWithFormat: @ "Mission Impossible % 02d", I + 1];
// Director
MvTicket. director = @ "Wang zhongyao ";
// Starring
MvTicket. actor = @ "Ma xingze, Zhang Tong, Zheng Eran ";
// Photo studio 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 Id
MvTicket. no = I + 1;
// Duration
MvTicket. lengthTime = 120;
// Fare
MvTicket. movieTicketPrice = 35;
// Finally, assign the initialized instance object to an array of the MovieTicket type.
Tickets [I] = mvTicket;
// The following figure shows an incorrect initialization object array method.
// If the above image is used, we can see that the output result is neither 0 nor null, so it is incorrect. Why? That's because the system doesn't know what type of data you initialize into the array. You only know that the data type you initialize is suitable, but the system doesn't know it. He is skeptical, the system will think that you will save some wrong data types. This hidden error will result in an error.
}
}
Return self;
}
// Start ticket purchase
-(Void) buyTickets
{
NSLog (@ "Start ticket purchase! ");
// There Is No redundant implementation process here. If you purchase a ticket, you can directly go to the next method: display the currently released movie.
[Self listMovies];
}
// Display the ongoing movie
/*
Ideas:
1) print the array information using the existing tickets Array
2) traverse the Array
Format:
ID
Director starring
Start Time
Movie hall information at the premiere
Executed fare
*/
// Display the ongoing movie
-(Void) listMovies
{
For (int I = 0; I <5; I ++ ){
NSLog (@ "\ n no.: % d name: % @ \ n Director: % @ Starring: % @ \ n start time: % @ duration: % d \ n premiere time: % @ movie hall information: % @ \ n run fare: %. 2f \ 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];
}
// Select a movie
-(Void) selectMovies
{
NSLog (@ "select the movie number you want to purchase and enter \ n ");
// The user enters the number and determines whether the number is correct. If yes, assign the value to the created global variable selectNo.
Int num;
Scanf ("% d", & num );
If (num> 0 | num <= 5 ){
SelectNo = num;
}
Else
// If the input is incorrect, jump out of the program.
Return;
// If the input is successful, a prompt is displayed for the selected movie.
NSLog (@ "\ n you selected the % d movie, name: % @ \ n", selectNo, tickets [selectNo-1]. name );
// Select the number of rows for the next Method
[Self selectRow];
}
// Select the number of rows
/*
Ideas:
1) Let's take a look at all the seats in the cinema.
2) Tips: How many rows are there in the movie room and how many seats are there in each row?
3) prompt to allow the user to select the number of rows
4) Save the number of rows
*/
// Select the number of rows
-(Void) selectRow
{
NSLog (@ "\ n select the number of rows ");
NSLog (@ "\ n: \ n ");
// Print the cinema location information for the user to check.
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 ");
}
// Prompt the number of rows in the hall, with several seats in each row
Printf ("\ n this Hall has % d rows, each ROW has % d seats \ n", ROW, COL );
Printf ("select the number of rows: \ n ");
// Let the customer select the number of rows. when entering the number of rows, it is best to judge the value entered by the customer
Int num;
Scanf ("% d", & num );
// Condition: the number of rows must be 1 ~ Number between 8
If (num> 0 | num <= 8 ){
// Assign a reasonable number of rows to rowNum
// Here to explain the selectNo-1, the first step has been confirmed the number of the film, then the actual number of the film is 0 ~ 4. Therefore, you must subtract one from the input number to use
Tickets [selectNo-1]. rowNum = num;
}
// Select a seat and enter the next Method
[Self selectCol];
}
// Select the seat number
-(Void) selectCol
{
NSLog (@ "\ n Select seat number ");
Int num;
Scanf ("% d", & num );
If (num> 0 | num <= 15 ){
// Assign a reasonable number of rows to rowNum
Tickets [selectNo-1]. colNum = num;
}
// Print the user-selected ticket information for the user to view and confirm
// Format:
// Select xxx video
// Start time: Movie duration:
// Column xx
// Price:
NSLog (@ "\ n you selected the video: % @ \ n start time: % @ movie duration: % d \ n % d seat \ n the fare you paid is: %. 2f \ n ", tickets [selectNo-1]. name, tickets [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. Confirm 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 to pay ...........");
NSLog (@ "\ n please wait! ");
NSLog (@ "\ n payment successful !!! ");
[Self printTickets];
}
// Issue the ticket (print the ticket information)
-(Void) printTickets
{
NSLog (@ "\ n ticketing ");
NSLog (@ "\ n you selected the video: % @ \ n start time: % @ movie duration: % d \ n % d seat \ n the fare you paid is: %. 2f \ n movie hall: % @ \ n ", tickets [selectNo-1]. name, tickets [selectNo-1]. startTime, tickets [selectNo-1]. lengthTime, tickets [selectNo-1]. rowNum, tickets [selectNo-1]. colNum, tickets [selectNo-1]. movieTicketPrice, tickets [selectNo-1]. place );
// Print the seating 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
{
// The code in else can be directly copied above
If (I =-1)
// When I is 0, it indicates 1st rows. In this case, the position of the row-1 must be written to the column
{
Printf ("% 02d", j + 1 );
}
Else if (j =-1)
// When the value of j is 0, it indicates 1st columns. In this case, the position of column-1 must be written as the uplink standard.
{
Printf ("% 02d", I + 1 );
}
Else
Printf ("+ ");
}
}
Printf ("\ n ");
}
}
@ End
------------------ Cinema. m ------------------
② AllTicket folder
------------------ Ticket. h ------------------
# Import <Foundation/Foundation. h>
@ Interface Ticket: NSObject
// Location
@ Property NSString * place;
// Premiere time
@ Property NSString * showTime;
// Start time of the screening
@ 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;
// No.
@ 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;
// Movie ticket No. (sequential number)
@ Property int movieTicketNum;
@ End
------------------ MovieTicket. h ------------------
------------------ MovieTicket. m ------------------
# Import "MovieTicket. h"
@ Implementation MovieTicket
@ End
------------------ MovieTicket. m ------------------
★The whole project is so simple. In general, what we train is not basic knowledge, but how to build the mind of such a framework, and all aspects should be reached. We should exercise this ability to make ourselves project-minded.
It's all!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.