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

网友投稿 343 2023-07-21


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 < 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 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 vhttp://oid 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 < train.getStationNum() - ; j++) {

String ticketStr = CommonUtil.buidTicket(j);

//String ticketStr = CommonUtil.buidTicket(,train.getStationNum());;

//System.exit();

List 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()

// http://+ "||" + 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售票算法(二),希望对大家有所帮助!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:EasyUI闪屏EasyUI页面加载提示(原理+代码+效果图)
下一篇:gameboy网页闯关游戏(riddle webgame)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~