Java代码实践12306售票算法(二)
程序员文章站
2024-03-09 13:13:59
周五闲来无事,基于上一篇关于理论,进行了java编码实践供各位读者参考(以下为相关代码的简单描述)
1.订票工具类
1.1初始化一列车厢的票据信息
/**...
周五闲来无事,基于上一篇关于理论,进行了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.订单实体(保留必要的订单信息)
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<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输出耗时时间
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售票算法(二),希望对大家有所帮助!