当前位置:  首页  >  PHP资讯  >  业界资讯

Java代码实践12306售票算法(二)

这篇文章主要介绍了Java代码实践12306售票算法(二)的相关资料,需要的朋友可以参考下

周五闲来无事,基于上一篇关于浅析12306售票算法(java版)理论,进行了java编码实践供各位读者参考(以下为相关代码的简单描述)

1.订票工具类

1.1初始化一列车厢的票据信息

 /** * 生成Ticket信息 * * @param train * @return */ public static List initTicketList(Train train) { List result = new ArrayList(); Map seatMap = train.getSeatTotalNum(); for (Entry entry : seatMap.entrySet()) { int ticketSize = entry.getValue(); String ticketType = entry.getKey(); for (int i = 0; i  

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  

1.3 订票主程序,这里一次只定一张票(A=A|B)

 /** * 根据筛选条件取得对应的车次 * @param ticketStr * @param ticketList * @param condition * @return */ public static Order createOrderByCondition(String ticketStr,List 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 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 getSeatTotalNum() { return seatTotalNum; } public void setSeatTotalNum(Map 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 seatMap = new HashMap(); seatMap.put("商务座", ); seatMap.put("一等座", ); seatMap.put("二等座", ); train.setSeatTotalNum(seatMap); // 生成票据 System.out.println("初始化列车中的票"); List 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 orderResult = new ArrayList(); for (int j = ; j  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售票算法(二),希望对大家有所帮助!

内容推荐:免费高清PNG素材下载
吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有 京ICP备19059560号-4