I had nothing to do on Friday. Based on the previous article on the analysis of the 12306 ticketing algorithm (java version) theory, I conducted Java coding practice for readers to refer to (the following is a simple description of the relevant code)
1. Ticket booking tools
1.1 Initialize the ticket information of a train car
/*** Generate Ticket information** @param train* @return*/public static List<Ticket> initTicketList(Train train) {List<Ticket> result = new ArrayList<Ticket>();Map<String, Integer> seatMap = train.getSeatTotalNum();for (Entry<String, Integer> entry : seatMap.entrySet()) {int ticketSize = entry.getValue();String ticketType = entry.getKey();for (int i = 0; i < ticketSize; i++) {int saleChannel = (int) (Math.random() * 10) % 8;Ticket ticket = new Ticket();ticket.setSaleChannel(saleChannel);ticket.setTicketType(ticketType);ticket.setGuid(UUID.randomUUID().toString());ticket.setFromDate(train.getFromDate());ticket.setTicketFlag(CommonUtil.initTicketFlag(train));ticket.setTrainNo(train.getTrainNo());result.add(ticket);}}return result;}1.2 Generate site ticket purchase (for example, the first shift can be done, such as the first station 1, the second station '10', and the second station is returned in the decimal system)
/*** Create * @param i* @param stationNum* @return*/public static String buidTicket(int i, int stationNum) {BigInteger temp = new BigInteger("0");for (int j = i; j < stationNum; j++) {temp = temp.or(new BigInteger(buidTicket(j)));}return temp.shiftRight(1).toString();}1.3 The main procedure for booking tickets, only one ticket is booked at a time (A=A|B)
/*** Obtain the corresponding train number according to the filter conditions* @param ticketStr* @param ticketList* @param condition* @return*/public static Order createOrderByCondition(String ticketStr,List<Ticket> ticketList,Map condition){Order tempOrder = null;for (Ticket ticket : ticketList) {BigInteger toTicket = new BigInteger(ticketStr);BigInteger fromTicket = new BigInteger(ticket.getTicketFlag());// If you can book tickets, then you will deduct inventory for a long time&&// (ticket.getSaleChannel()==(ticket.getSaleChannel()|1))if (canTicket(fromTicket, toTicket)&&ticket.getTicketType().equals(condition.get("ticketType").toString())//&&(ticket.getSaleChannel()==(ticket.getSaleChannel()|2))) {tempOrder = new Order();tempOrder.setOrderId(UUID.randomUUID().toString());tempOrder.setSeatType(ticket.getTicketType());tempOrder.setTicketFlag(toTicket.toString());tempOrder.setTrainNO(ticket.getTrainNo());tempOrd er.setFromDate(ticket.getFromDate());tempOrder.setSaleChannel(ticket.getSaleChannel());tempOrder.setTicketGuid(ticket.getGuid());ticket.setTicketFlag(fromTicket.or(toTicket).toString());break;}} return tempOrder;}1.4 Determine whether to stamp, A=~(~A|B)
/*** Determine whether you can book tickets** @param fromTicket* @param toTicket* @return*/private static boolean canTicket(BigInteger fromTicket, BigInteger toTicket) {return fromTicket.equals(fromTicket.not().or(toTicket).not());}2. Order entity (retain the necessary order information)
package com.train.ticket;/*** Order entity* @author guo_zhifeng**/public class Order {private String orderId;private String ticketGuid;//Bill idprivate String ticketFlag;//Purchase mark private String seatType;//Seat type private String fromDate;//Departure date private String trainNO;//Train number private int saleChannel;//Sales channel public String getOrderId() {return orderId;}public void setOrderId(String orderId) {this.orderId = orderId;}public String getTicketGuid() {return ticketGuid;}public void setTicketGuid(String ticketGuid) {this.ticketGuid = ticketGuid;}public String getTicketFlag() {return ticketFlag;}public void setTicketFlag(String ticketFlag) {this.ticketFlag = ticketFlag;}public String getSeatType() {return seatType;}public void setSeatType(String seatType) {this.seatType = seatType;}public String getFromDate() {return fromDate;}public void setFromDate(String fromDate) {this.fromDate = fromDate;}public String getTrainNO() {return trainNO;}public void setTrainNO(String trainNO) {this.trainNO = trainNO;}public int getSaleChannel() {return saleChannel;}public void setSaleChannel(int saleChannel) {this.saleChannel = saleChannel;}}3. Ticketing entity (retain necessary ticketing information)
package com.train.ticket;/*** Ticket entity* @author guo_zhifeng**/public class Ticket {private String ticketFlag;private String ticketType;private int saleChannel;private String trainNo;private String guid;private String fromDate;//Departure date public String getGuid() {return guid;}public void setGuid(String guid) {this.guid = guid;}public String getTrainNo() {return trainNo;}public void setTrainNo(String trainNo) {this.trainNo = trainNo;}public String getTicketFlag() {return ticketFlag;}public void setTicketFlag(String ticketFlag) {this.ticketFlag = ticketFlag;}public String getTicketType() {return ticketType;}public void setTicketType(String ticketType) {this.ticketType = ticketType;}public int getSaleChannel() {return saleChannel;}public void setSaleChannel(int saleChannel) {this.saleChannel = saleChannel;}public String getFromDate() {return fromDate;}public void setFromDate(String fromDate) {this.fromDate = fromDate;}}4. Train initialization information (only main information)
package com.train.ticket;import java.util.Map;/*** Information about a train* @author guo_zhifeng**/public class Train {private String trainNo;// Train number private int stationNum;// Number of stations private Map<String, Integer> seatTotalNum;// Number of various seats private String fromDate;public String getFromDate() {return fromDate;}public void setFromDate(String fromDate) {this.fromDate = fromDate;}public String getTrainNo() {return trainNo;}public void setTrainNo(String trainNo) {this.trainNo = trainNo;}public int getStationNum() {return stationNum;}public void setStationNum(int stationNum) {this.stationNum = stationNum;}public Map<String, Integer> getSeatTotalNum() {return seatTotalNum;}public void setSeatTotalNum(Map<String, Integer> seatTotalNum) {this.seatTotalNum = seatTotalNum;}} 5. Main program
5.1 Initialize a train car
5.2 According to the form of ticket purchases on site, maximization is AB BC CD DE EF, etc.
5.3 Time-consuming output
package com.train.main;import java.io.File;import java.math.BigDecimal;import java.math.BigInteger;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.train.ticket.Order;import com.train.ticket.Ticket;import com.train.ticket.Train;import com.train.util.CommonUtil;public class MainTest {public static void main(String[] args) {Train train = new Train();train.setTrainNo("SA");train.setFromDate("//");train.setStationNum();Map<String, Integer> seatMap = new HashMap<String, Integer>();seatMap.put("Business Seat", );seatMap.put("First Class", );seatMap.put("Second Class", );train.setSeatTotalNum(seatMap);// Generate ticket System.out.println("Initialize tickets in the train");List<Ticket> ticketList = CommonUtil.initTicketList(train);String fileName = "D://RESULT.txt";File f = new File(fileName);if(f.exists()) f.delete(); long startTime = System.currentTimeMillis();//int i = ;//for (Ticket ticket : ticketList) {// CommonUtil.appendMethodA(fileName,// i + "||" + CommonUtil.toJSON(ticket) + "/n", true);// i++;//}System.out.println("Start booking"); long beginTime = System.currentTimeMillis();List<Order> orderResult = new ArrayList<Order>(); for (int j = ; j < train.getStationNum() - ; j++) {String ticketStr = CommonUtil.buidTicket(j);//String ticketStr = CommonUtil.buidTicket(,train.getStationNum());;//System.exit();List<Order> tempListOrder = CommonUtil.createOrderList(ticketStr,ticketList, train);orderResult.addAll(tempListOrder);}long endTime = System.currentTimeMillis();System.out.println("Reservation completed");//int m = ;// for (Ticket ticket : ticketList) {// String temp = m + "||" + CommonUtil.toJSON(ticket) + ",";// // System.out.println(temp);// CommonUtil.appendMethodA(fileName, temp, true);// m++;// }// int k = ;// for (Order order : orderResult) {// String temp = order.getOrderId() // + "||" + order.getSaleChannel() // + "||" + order.getFromDate() // + "||" + order.getSeatType() // + "||" + order.getTicketGuid()// + "||" + order.getTrainNO() // + "||" + order.getTicketFlag()// + "||" + new BigInteger(order.getTicketFlag()).toString()// + "||" +k;// CommonUtil.appendMethodA(fileName,temp, true);// k++;// }long eedTime = System.currentTimeMillis();System.out.println("Generate order" + orderResult.size() + "||Time-consuming time: "+ (endTime - beginTime) + "milliseconds");System.out.println("Generate number of documents per second (order by ticket)" + new BigDecimal(orderResult.size()).multiply(new BigDecimal()).divide(new BigDecimal(endTime - beginTime),,BigDecimal.ROUND_HALF_DOWN));System.out.println("Execution Completed");}}6. Operation results
The above is the Java code practice 12306 ticketing algorithm introduced to you by the editor (II). I hope it will be helpful to everyone!