1. Taking the G71 train as an example, firstly encode the train station (from 1 to the last stop)
The above placeholder is briefly described below: G71 has 18 sites in total. Then the seat identification of our single seat can be represented by 18-bit-length binary strings of 100000000000000000000000. Each digit represents a site. It is initialized to the ticket list below before issuing the ticket every day. The data is as follows. The remaining tickets determine the maximum number of remaining tickets based on the number of 0 in the seat identification.
The origin restricted sites and end restricted sites in the ticket booking form can be flexibly matched (this can achieve restricted sites on sale)
2. Query the remaining tickets
If we want to query the G71 second-class seat F seat ticket from the originating station Baoding East Station (3) to Shaoguan Station (15) only needs to execute the following sql (this SQL can realize the functions of seat selection and car selection)
select GUID, train number, train type, seat type, carriage number, seat code, seat position from Ticket list
where to_number(substring(seat mark, 3, 15))=0
and departure date='2016-06-11'
and train number code='G71'
and substring(start restricted station, 3, 4)=1
and substring(finally arrive at the restricted station, 15, 16)=1
and ticket status = 'For Sale'
and train type = 'Second Class'
and seat position ='F'
3. Book tickets
3.1 Obtain a record according to the query conditions in the second step and change the ticket status to lock
3.2 Payment is made after locking is successful
3.2 After the payment is successful, the ticket from Baoding to Shaoguan (0001111111111111000 is marked as 0) with the original ticket, and the ticket status is changed to for sale
10000000000000000000 | 000111111111111000 = 100111111111111000 The remaining ticket logo at this time is the dynamic remaining ticket
3.3 If the specified time is not paid, the ticket status of this record can be restored to for sale.
10011111111111111000^0001111111111111000 = 100000000000000000000000000000000000000000000000000000000 The remaining ticket and automatically restored it to the time.
4. Refund ticket
Obtain the ticket from Baoding to Shaoguan (000111111111111000) and the corresponding tickets for non-operation, and then you can return to the ticket pool.
The following is the relevant java code
import java.math.BigDecimal;public class MainTest {public static void main(String[] args) {String ticketFlag = "";int beginStation = ;int endStation = ;long beginTime = System.currentTimeMillis();String result = orderTicket(ticketFlag, beginStation, endStation);if (result.equals(ticketFlag)) {System.out.println("Subscription failed");} else {System.out.println("Result after booking: " + result);// If you want to cancel, do this, String b = buildTicket(ticketFlag.length(), beginStation,endStation);System.out.println("Result after release: " + releaseTicket(ticketFlag, b));} long endTime = System.currentTimeMillis();System.out.println("Time-consuming: " + (endTime - beginTime));}/*** Ticket booking* * @param ticketFlag* @param beginStation* @param endStation* @return*/private static String orderTicket(String ticketFlag, int beginStation,int endStation) {String result = "";if (checkCanTicket(ticketFlag, beginStation, endStation)) {String b = buildTicket(ticketFlag.length(), beginStation, endStation);String currentTicked = toTicket(ticketFlag, b);System.out.println("Result before ticketing: " + ticketFlag);result = currentTicked;} else {result = ticketFlag;};return result;}/*** Cancel the ticket* * @param ticketFlag* @param b* @return*/private static String releaseTicket(String ticketFlag, String b) {StringBuilder tempSt = new StringBuilder("");int length = ticketFlag.length();for (int i = ; i < length; i++) {char tempA = ticketFlag.charAt(i);char tempB = b.charAt(i);if (tempA == '' && tempB == '') {tempSt.append("");} else {tempSt.append(tempA);}}return tempSt.toString();}/*** Create interval placeholder* * @param length* @param beginStation* @param endStation* @return*/private static String buildTicket(int length, int beginStation,int endStation) {StringBuilder st = new StringBuilder("");for (int i = ; i < length; i++) {if (i >= beginStation && i < endStation) {st.append("");} else {st.append("");}}System.out.println("Create interval ticket: " + st.toString());return st.toString();}/*** Result after booking* * @param ticketFlag* @param b* @return*/private static String toTicket(String ticketFlag, String b) {StringBuilder tempSt = new StringBuilder("");int length = ticketFlag.length();for (int i = ; i < length; i++) {char tempA = ticketFlag.charAt(i);char tempB = b.charAt(i);if (tempA == '' || tempB == '') {tempSt.append("");} else {tempSt.append(tempA);}}return tempSt.toString();}/*** Is it possible to book tickets* * @param ticketFlag* @param beginStation* @param endStation* @return*/private static boolean checkCanTicket(String ticketFlag, int beginStation,int endStation) {boolean result = false;String tempTicket = ticketFlag.substring(beginStation, endStation);BigDecimal b = new BigDecimal(tempTicket);if (b.equals(new BigDecimal(""))) {result = true;}return result;}}