服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - Java代码实践12306售票算法(二)

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

2020-04-02 13:22全城热恋 JAVA教程

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

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

1.订票工具类

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 生成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' 这里返回的十进制的)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 创建
* @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)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* 根据筛选条件取得对应的车次
* @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)

?
1
2
3
4
5
6
7
8
9
10
/**
* 订票判断是否可以订票
*
* @param fromTicket
* @param toTicket
* @return
*/
private static boolean canTicket(BigInteger fromTicket, BigInteger toTicket) {
return fromTicket.equals(fromTicket.not().or(toTicket).not());
}

2.订单实体(保留必要的订单信息)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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.票务实体(保留必要的票务信息)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.列车初始化信息(只进行主要信息)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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输出耗时时间

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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售票算法(二)

以上所述是小编给大家介绍的Java代码实践12306售票算法(二),希望对大家有所帮助!

延伸 · 阅读

精彩推荐