程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 初探12306售票算法(二)-java代碼實踐,12306-java

初探12306售票算法(二)-java代碼實踐,12306-java

編輯:JAVA綜合教程

初探12306售票算法(二)-java代碼實踐,12306-java


周五閒來無事,基於上一篇關於初探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 } View Code

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 } View Code

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 } View Code

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) + "\n", 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 } View Code

 6.運行結果

 完整代碼: 源碼

本文原創:轉載請注明出處 http://www.cnblogs.com/feichengwurao/p/5202100.html 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved