周五闲来无事,基于上一篇关于初探12306售票算法(一)-理论,进行了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.订单实体(保留必要的订单信息)
1 package com.train.ticket; 2 /** 3 * 订单实体 4 * @author guo_zhifeng 5 * 6 */ 7 public class Order { 8 9 private String orderId; 10 private String ticketGuid;//票据id 11 private String ticketFlag;//订票标记 12 private String seatType;//座位类型 13 private String fromDate;//发车日期 14 private String trainNO;//列车编号 15 private int saleChannel;//销售渠道 16 public String getOrderId() { 17 return orderId; 18 } 19 public void setOrderId(String orderId) { 20 this.orderId = orderId; 21 } 22 public String getTicketGuid() { 23 return ticketGuid; 24 } 25 public void setTicketGuid(String ticketGuid) { 26 this.ticketGuid = ticketGuid; 27 } 28 public String getTicketFlag() { 29 return ticketFlag; 30 } 31 public void setTicketFlag(String ticketFlag) { 32 this.ticketFlag = ticketFlag; 33 } 34 public String getSeatType() { 35 return seatType; 36 } 37 public void setSeatType(String seatType) { 38 this.seatType = seatType; 39 } 40 public String getFromDate() { 41 return fromDate; 42 } 43 public void setFromDate(String fromDate) { 44 this.fromDate = fromDate; 45 } 46 public String getTrainNO() { 47 return trainNO; 48 } 49 public void setTrainNO(String trainNO) { 50 this.trainNO = trainNO; 51 } 52 public int getSaleChannel() { 53 return saleChannel; 54 } 55 public void setSaleChannel(int saleChannel) { 56 this.saleChannel = saleChannel; 57 } 58 59 60 }
3.票务实体(保留必要的票务信息)
1 package com.train.ticket; 2 /** 3 * 票务实体 4 * @author guo_zhifeng 5 * 6 */ 7 public class Ticket { 8 private String ticketFlag; 9 private String ticketType; 10 private int saleChannel; 11 private String trainNo; 12 private String guid; 13 private String fromDate;//发车日期 14 15 public String getGuid() { 16 return guid; 17 } 18 19 public void setGuid(String guid) { 20 this.guid = guid; 21 } 22 23 public String getTrainNo() { 24 return trainNo; 25 } 26 27 public void setTrainNo(String trainNo) { 28 this.trainNo = trainNo; 29 } 30 31 32 public String getTicketFlag() { 33 return ticketFlag; 34 } 35 36 public void setTicketFlag(String ticketFlag) { 37 this.ticketFlag = ticketFlag; 38 } 39 40 public String getTicketType() { 41 return ticketType; 42 } 43 44 public void setTicketType(String ticketType) { 45 this.ticketType = ticketType; 46 } 47 48 public int getSaleChannel() { 49 return saleChannel; 50 } 51 52 public void setSaleChannel(int saleChannel) { 53 this.saleChannel = saleChannel; 54 } 55 56 public String getFromDate() { 57 return fromDate; 58 } 59 60 public void setFromDate(String fromDate) { 61 this.fromDate = fromDate; 62 } 63 64 }
4.列车初始化信息(只进行主要信息)
1 package com.train.ticket; 2 3 import java.util.Map; 4 /** 5 * 某一趟的列车的信息 6 * @author guo_zhifeng 7 * 8 */ 9 public class Train { 10 private String trainNo;// 火车编号 11 private int stationNum;// 车站数量 12 private Map<String, Integer> seatTotalNum;// 各种座位的数量 13 private String fromDate; 14 15 16 public String getFromDate() { 17 return fromDate; 18 } 19 20 public void setFromDate(String fromDate) { 21 this.fromDate = fromDate; 22 } 23 24 public String getTrainNo() { 25 return trainNo; 26 } 27 28 public void setTrainNo(String trainNo) { 29 this.trainNo = trainNo; 30 } 31 32 public int getStationNum() { 33 return stationNum; 34 } 35 36 public void setStationNum(int stationNum) { 37 this.stationNum = stationNum; 38 } 39 40 public Map<String, Integer> getSeatTotalNum() { 41 return seatTotalNum; 42 } 43 44 public void setSeatTotalNum(Map<String, Integer> seatTotalNum) { 45 this.seatTotalNum = seatTotalNum; 46 } 47 48 }
5.主程序
5.1初始化一列车厢
5.2按照逐站购票的形式,最大化 即 AB BC CD DE EF等等
5.3输出耗时时间
1 package com.train.main; 2 3 import java.io.File; 4 import java.math.BigDecimal; 5 import java.math.BigInteger; 6 import java.util.ArrayList; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 11 import com.train.ticket.Order; 12 import com.train.ticket.Ticket; 13 import com.train.ticket.Train; 14 import com.train.util.CommonUtil; 15 16 public class MainTest { 17 18 public static void main(String[] args) { 19 Train train = new Train(); 20 train.setTrainNo("0909123SA873878"); 21 train.setFromDate("2016/06/11"); 22 train.setStationNum(18); 23 Map<String, Integer> seatMap = new HashMap<String, Integer>(); 24 seatMap.put("商务座", 500); 25 seatMap.put("一等座", 1000); 26 seatMap.put("二等座", 2000); 27 train.setSeatTotalNum(seatMap); 28 // 生成票据 29 System.out.println("初始化列车中的票"); 30 List<Ticket> ticketList = CommonUtil.initTicketList(train); 31 String fileName = "D:\RESULT.txt"; 32 File f = new File(fileName); 33 if(f.exists()) f.delete(); 34 long startTime = System.currentTimeMillis(); 35 //int i = 0; 36 //for (Ticket ticket : ticketList) { 37 // CommonUtil.appendMethodA(fileName, 38 // i + "||" + CommonUtil.toJSON(ticket) + " ", true); 39 // i++; 40 //} 41 System.out.println("开始订票"); 42 long beginTime = System.currentTimeMillis(); 43 List<Order> orderResult = new ArrayList<Order>(); 44 45 for (int j = 0; j < train.getStationNum() - 1; j++) { 46 String ticketStr = CommonUtil.buidTicket(j); 47 //String ticketStr = CommonUtil.buidTicket(1,train.getStationNum());; 48 //System.exit(0); 49 50 List<Order> tempListOrder = CommonUtil.createOrderList(ticketStr, 51 ticketList, train); 52 orderResult.addAll(tempListOrder); 53 54 } 55 long endTime = System.currentTimeMillis(); 56 System.out.println("订票完成"); 57 //int m = 0; 58 59 // for (Ticket ticket : ticketList) { 60 // String temp = m + "||" + CommonUtil.toJSON(ticket) + ","; 61 // // System.out.println(temp); 62 // CommonUtil.appendMethodA(fileName, temp, true); 63 // m++; 64 // } 65 // int k = 1; 66 // for (Order order : orderResult) { 67 // String temp = order.getOrderId() 68 // + "||" + order.getSaleChannel() 69 // + "||" + order.getFromDate() 70 // + "||" + order.getSeatType() 71 // + "||" + order.getTicketGuid() 72 // + "||" + order.getTrainNO() 73 // + "||" + order.getTicketFlag() 74 // + "||" + new BigInteger(order.getTicketFlag()).toString(2) 75 // + "||" +k; 76 // CommonUtil.appendMethodA(fileName,temp, true); 77 // k++; 78 // } 79 80 long eedTime = System.currentTimeMillis(); 81 System.out.println("生成订单" + orderResult.size() + "||耗时时间:" 82 + (endTime - beginTime) + "毫秒"); 83 System.out.println("每秒钟生成单据数(逐张订票)"+ new BigDecimal(orderResult.size()).multiply(new BigDecimal(1000)).divide(new BigDecimal(endTime - beginTime),0,BigDecimal.ROUND_HALF_DOWN)); 84 85 System.out.println("执行完毕"); 86 87 } 88 89 }
6.运行结果
完整代码: 源码
本文原创:转载请注明出处 http://www.cnblogs.com/feichengwurao/p/5202100.html