-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketingDS.java
More file actions
122 lines (107 loc) · 3.17 KB
/
TicketingDS.java
File metadata and controls
122 lines (107 loc) · 3.17 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package ticketingsystem;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
/**
* @author sunyangwei
*
*/
public class TicketingDS implements TicketingSystem {
/**
*
*/
static final int thread = 16;//每个线程是一个票务代理
private ExecutorService executorService = Executors.newFixedThreadPool(thread);
static BlockingQueue<Ticket> list = new LinkedBlockingQueue<>();
private int routenum = 5;
private int coachnum = 8;
private int seatnum = 100;
private int stationnum = 10;
private Route[] routes = new Route[routenum];
public TicketingDS(int routenum, int coachnum, int seatnum, int stationnum){
this.routenum = routenum;
this.coachnum = coachnum;
this.seatnum = seatnum;
this.stationnum = stationnum;
for(int i = 1; i <= routenum; i++){
routes[i - 1] = new Route(executorService, i, coachnum, seatnum, stationnum);
}
}
@Override
public Ticket buyTicket(String passenger, int route, int departure, int arrival) {
if(route <= 0 || route > routenum ||
departure <= 0 || departure >= stationnum ||
arrival <= 1 || arrival > stationnum ||
arrival <= departure){
return null;
}
Ticket ret = null;
try {
ret = routes[route - 1].buyTicket(passenger, route, departure, arrival);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(ret);
return ret;
}
@Override
public int inquiry(int route, int departure, int arrival) {
if(route <= 0 || route > routenum ||
departure <= 0 || departure >= stationnum ||
arrival <= 1 || arrival > stationnum ||
arrival <= departure){
return 0;
}
int ret = 0;
try {
ret = routes[route - 1].inquiry(route, departure, arrival);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
@Override
public boolean refundTicket(Ticket ticket) {
int route = ticket.getRoute();
if(route <= 0 || route > routenum){
return false;
}
//是否有效票标志位
boolean flag = false;
for(Ticket t:list){
Long a = ticket.getTid();
Long b = t.getTid();
if(a.equals(b)&&ticket.getPassenger().equals(t.getPassenger())&&ticket.getRoute()==t.getRoute()&&
ticket.getDeparture()==t.getDeparture()&&ticket.getArrival()==t.getArrival()&&
ticket.getCoach()==t.getCoach()&&ticket.getSeat()==t.getSeat())
{
flag= true;
list.remove(t);
}
}
boolean ret = false;
if(flag){
try {
ret = routes[route - 1].refundTicket(ticket);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return ret;
}
public void shutDown(){
executorService.shutdown();;
}
}