Java代碼理論12306售票算法(二)。本站提示廣大學習愛好者:(Java代碼理論12306售票算法(二))文章只能為提供參考,不一定能成為您想要的結果。以下是Java代碼理論12306售票算法(二)正文
周五閒來無事,基於上一篇關於淺析12306售票算法(java版)實際,停止了java編碼理論供列位讀者參考(以下為相干代碼的簡略描寫)
1.訂票對象類
1.1初始化一列車箱的單子信息
/** * 生成Ticket信息 * * @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 生成站點購票(好比說第停止移位便可如第1站1,第二站‘10' 這裡前往的十進制的)
/** * 創立 * @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 訂票主法式,這裡一次只定一張票(A=A|B)
/** * 依據挑選前提獲得對應的車次 * @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()); // 假如可以訂票,那末久停止扣除庫存&& // (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()); tempOrder.setFromDate(ticket.getFromDate()); tempOrder.setSaleChannel(ticket.getSaleChannel()); tempOrder.setTicketGuid(ticket.getGuid()); ticket.setTicketFlag(fromTicket.or(toTicket).toString()); break; } } return tempOrder; }
1.4 斷定能否郵票,A=~(~A|B)
/** * 訂票斷定能否可以訂票 * * @param fromTicket * @param toTicket * @return */ private static boolean canTicket(BigInteger fromTicket, BigInteger toTicket) { return fromTicket.equals(fromTicket.not().or(toTicket).not()); }
2.定單實體(保存需要的定單信息)
package com.train.ticket; /** * 定單實體 * @author guo_zhifeng * */ public class Order { private String orderId; private String ticketGuid;//單子id private String ticketFlag;//訂票標志 private String seatType;//坐位類型 private String fromDate;//發車日期 private String trainNO;//列車編號 private int saleChannel;//發賣渠道 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.票務虛體(保存需要的票務信息)
package com.train.ticket; /** * 票務虛體 * @author guo_zhifeng * */ public class Ticket { private String ticketFlag; private String ticketType; private int saleChannel; private String trainNo; private String guid; private String fromDate;//發車日期 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.列車初始化信息(只停止重要信息)
package com.train.ticket; import java.util.Map; /** * 某一趟的列車的信息 * @author guo_zhifeng * */ public class Train { private String trainNo;// 火車編號 private int stationNum;// 車站數目 private Map<String, Integer> seatTotalNum;// 各類坐位的數目 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.主法式
5.1初始化一列車箱
5.2依照逐站購票的情勢,最年夜化 即 AB BC CD DE EF等等
5.3輸入耗不時間
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("商務座", ); seatMap.put("一等座", ); seatMap.put("二等座", ); train.setSeatTotalNum(seatMap); // 生成單子 System.out.println("初始化列車中的票"); 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("開端訂票"); 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("訂票完成"); //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("生成定單" + orderResult.size() + "||耗不時間:" + (endTime - beginTime) + "毫秒"); System.out.println("每秒鐘生成單據數(逐張訂票)"+ new BigDecimal(orderResult.size()).multiply(new BigDecimal()).divide(new BigDecimal(endTime - beginTime),,BigDecimal.ROUND_HALF_DOWN)); System.out.println("履行終了"); } }
6.運轉成果
以上所述是小編給年夜家引見的Java代碼理論12306售票算法(二),願望對年夜家有所贊助!