Java code Practice 12306 ticketing Algorithm (ii) _java

Source: Internet
Author: User
Tags tojson uuid ticket

Friday free to do nothing, based on an analysis of the 12306 ticketing algorithm (Java version) theory, the Java coding practice for your readers reference (the following is a brief description of the relevant code)

1. Booking tool category

1.1 The ticket information of the initialization of a train compartment

/**
* Generate Ticket information
* *
@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 () *)% 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 Build site Purchase tickets (for example, shift can be like the 1th Station 1, the second station ' 10 ' here Returns the decimal)

/** *
Create
* @param i
* @param stationnum
* @return * * Public
static String buidticket (int i, int st Ationnum) {
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 Booking Main program, only one ticket at a time (a=a| B

/** * According to the selection of the corresponding train * @param ticketstr * @param ticketlist * @param condition * @return/public static order Createorde Rbycondition (String ticketstr,list<ticket> ticketlist,map condition) {order Temporder = null; for (Ticket ticket:t
icketlist) {BigInteger toticket = new BigInteger (TICKETSTR);
BigInteger fromticket = new BigInteger (Ticket.getticketflag ()); If you can book a ticket, take a long time to deduct the inventory &&//(Ticket.getsalechannel () = = (Ticket.getsalechannel () |1)) if (Canticket Fromticket, Toticket) &&ticket.gettickettype (). Equals (Condition.get ("TicketType"). ToString ())//&& ( Ticket.getsalechannel () = = (Ticket.getsalechannel ())) {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 Judge whether the stamp, a=~ (~a| B

/**
* Booking decision can be booked
*
* @param fromticket
* @param toticket
* @return * *
private static Boolean Canticket (BigInteger fromticket, BigInteger toticket) {return
fromticket.equals (Fromticket.not (). or ( Toticket). not ());

2. Order entity (keep the necessary order information)

Package com.train.ticket; /** * @author Guo_zhifeng * * * * * * * * * */public class Order {private string orderId; private string ticketguid;//ticket ID Priva Te string ticketflag;//booking token private string seattype;//seat type private string fromdate;//departure date private string trainno;//train number PR ivate int salechannel;//Sales channel 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 = Ti
Cketguid; Public String Getticketflag () {return ticketflag.} public void Setticketflag (String ticketflag) {This.ticketflag = Ti
Cketflag; 
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;} p Ublic string Gettrainno () {return trainno.} public void Settrainno (string trainNO) {This.trainno = trainno} public int Getsalechannel () {return salechannel;} public void Setsalechannel (int salecha Nnel) {this.salechannel = Salechannel;}}

3. Ticket entities (keep the necessary ticket information)

 package com.train.ticket;/** * Ticket entity * @author Guo_zhifeng * * * * public class Ticket {private St
Ring Ticketflag;
Private String TicketType;
private int salechannel;
Private String Trainno;
Private String GUID; Private string fromdate;//Departure date public string GetGuid () {return GUID:} public void SetGuid (String guid) {this.guid = GUI
D  Public String Gettrainno () {return trainno.} public void Settrainno (String trainno) {This.trainno = Trainno; String Getticketflag () {return ticketflag.} public void Setticketflag (string ticketflag) {This.ticketflag = Ticketflag
; Public String Gettickettype () {return tickettype.} public void Settickettype (String tickettype) {this.tickettype = Ti
Ckettype; public int Getsalechannel () {return salechannel.} public void Setsalechannel (int salechannel) {This.salechannel = Sal
Echannel; 
Public String Getfromdate () {return fromdate.} public void Setfromdate (String fromdate) {this.fromdate = FromDate;} }

4. Train initialization information (main information only)

Package com.train.ticket;
Import Java.util.Map;
/**
* Information on a particular train
@author Guo_zhifeng * * */Public
class Train {
private String trainno;//train number
private int stationnum;//Station number private
map<string, integer> seattotalnum;//the number of various seats
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. Main program

5.1 Initialization of a train compartment

5.2 In accordance with the form of purchase tickets by station, the maximum is AB BC CD DE ef and so on

5.3 Output Time consuming

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");
Setfromdate ("//");
Train.setstationnum ();
map<string, integer> seatmap = new hashmap<string, integer> ();
Seatmap.put ("Business seat");
Seatmap.put ("first-class seat");
Seatmap.put ("Second-class seat",);
Train.setseattotalnum (SEATMAP);
Generate Bill System.out.println ("The ticket in the initialization Train");
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 ("Start booking");
Long begintime = System.currenttimemillis ();
list<order> Orderresult = new arraylist<order> (); for (int j =; J < Train.getstationnum ()-; J +) {String ticketstr = Commonutil.buidticket (j);//string ticketstr = C
Ommonutil.buidticket (, Train.getstationnum ());
System.exit ();
list<order> Templistorder = commonutil.createorderlist (Ticketstr, ticketlist, train);
Orderresult.addall (Templistorder);
Long endtime = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("booking completed");

int m =; for (Ticket ticket:ticketlist) {//String temp = m + "| |" + commonutil.tojson (Ticket) + ",";////System.out.printl
N (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.gettic
Ketflag ()). ToString ()//+ "| |" +K;
Commonutil.appendmethoda (Filename,temp, true);
k++;
Long eedtime = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Generate Order" + orderresult.size () + "| |
Time consuming: "+ (Endtime-begintime) +" millisecond "); System.out.println ("Number of documents generated per second (ticket-by-book)" + New BigDecimal (Orderresult.size ()). Multiply (new BigDecimal ()). Divide (new
BigDecimal (Endtime-begintime), Bigdecimal.round_half_down));
System.out.println ("execution completed"); }
}

6. Operating Results

The above is a small series to introduce Java code practice 12306 ticketing Algorithm (ii), I hope to help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.