Example of simulating ticket booking and refund using javascript

Source: Internet
Author: User

Someone analyzed the logic of the 12306 background .. Booking and unsubscribing for train tickets are different from ordinary shopping.
One difficulty is that train tickets can be sold in substations. For example, a train ticket from Beijing to Shanghai can have many sites along the way, Beijing-Jinan, Jinan-Nanjing... and so on. How to design a data model to access these tickets is a problem. Instead of a simple number +-1.

One idea is quite good: Use a binary string to represent a train ticket. For example, if there are 10 sites from Beijing to Shanghai, the initial status of a full ticket is '123 ';
If a full-course ticket is sold, the ticket becomes '123 ';
Sell a half-way ticket, for example, Beijing-Jinan three stations (first station-Third Station), then the ticket becomes '123 ';
Sell another half-way ticket, for example, Xuzhou-Nanjing (6th station-9th station), then the previous ticket becomes '123 ';

The logic for ticket return is very simple. I want to return a ticket (XuZhou-Nanjing), then I will find the first ticket from the ticket pool and cannot buy it.
(XuZhou-Nanjing), change it to OK (reverse order of the ticket). For example, if you find the above ticket '123 ',
After the refund, the ticket is changed to (0011111111 );

The basic logic is as follows: 12306 to ensure multi-entry and data consistency, a very efficient logic is required to process ticket checking,
Ticket buying and refund are said to have 0.2 million requests per second during peak hours. Store the ticket data structure in the memory. Instead of databases.
Small and efficient data knot becomes very important.
Copy codeThe Code is as follows:
If (jQuery) {} else {
// Document. write
}

Function Server (){
Var self = this;

Self. ticketsPool = [];

Self. _ init = function (number ){
If (typeof (number )! = 'Number ')
Throw ('Type error ');
For (I = 0; I <number; I ++ ){
Self. ticketsPool. push (new Ticket ());
}
};

// Determine whether a ticket can be bought. It can be achieved through the sum or operation.
// For example, if the order o is Beijing-Jinan (001111111) and the ticket is (0000000011) (Beijing-Nanjing sold), false is returned.
// For example, if the order o is Beijing-Jinan (001111111) and the ticket is (1111100011) (XuZhou-Nanjing sold), true is returned.
Self. canBuy = function (o, t ){
Var _ o =''
For (j = 0; j <o. length; j ++ ){
_ O + = o [j] = '0 '? 1:0;
}
Var r1 = (parseInt (t. tic, 2) | parseInt (o, 2) & parseInt (_ o, 2 );
Var r2 = parseInt (_ o, 2 );
Return r1 = r2;
};

// Sell a ticket
Self. pop1Ticket = function (o ){
For (I = 0; I <self. ticketsPool. length; I ++ ){
If (self. canBuy (o, self. ticketsPool [I]) {
Self. buy (self. ticketsPool [I], o );
Return I;
}
};
Return-1;
};

// The implementation of selling tickets, changing the binary string, such as '20160301'-> '20160301 ';
Self. buy = function (t, o ){
T. tic = (parseInt (t. tic, 2) & parseInt (o, 2). toString (2 );
// Alert (t. tic );

};

// Query the remaining ticket
Self. remainTics = function (o ){
Var count = 0;
For (I = 0; I <self. ticketsPool. length; I ++ ){
Count + = self. canBuy (o, self. ticketsPool [I])? 1:0;
};
Return count;
}

// Refund or calculation
Self. refund = function (o ){
For (I = 0; I <self. ticketsPool. length; I ++ ){
If (! Self. canBuy (o, self. ticketsPool [I]) {
Var _ o =''
For (j = 0; j <o. length; j ++ ){
_ O + = o [j] = '0 '? 1:0;
}
Self. ticketsPool [I]. tic = (parseInt (self. ticketsPool [I]. tic, 2) | parseInt (_ o, 2). toString (2 );
Return I;
}
};

Return-1;
}
}

// Data model: Ticket
Function Ticket (){
Var self = this;
// The initial ticket is the full ticket
Self. tic = '000000 ';
}

// Data model: Order
Function Order (from, ){
Var self = this;
Var s = '';
For (I = 0; I <10; I ++ ){
S + = (I> = from & I <)? ;
}
Return s;
}

// 12306 background
Server = new Server ();
// Initial status, with 400 full tickets in the ticket pool
Server. _ init (400 );

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.