-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDoctorThread.java
165 lines (140 loc) · 4.31 KB
/
DoctorThread.java
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package finalServer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.*;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Set;
public class DoctorThread extends Thread {
String myAccount;
Socket client;
BufferedReader br;
PrintWriter pw;
static HashMap<String,Server.MedicineInfo> medicineData=new HashMap<String,Server.MedicineInfo>();
DoctorThread(String account,Socket s){
this.client=s;
this.myAccount=account;
sendInitMedicineInfo();
try {
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
String info;
try {
while(true){
info=br.readLine();
System.out.println("xinxi"+info);
if(info==null){
doctorExit();
System.out.println("Mac");
Server.sendMessageToShow(); //发信息给滚动屏
Server.sendMessageToPresident(); //发送更新信息给院长
break;
}
analysis(info); //分析数据
}
} catch (IOException e) {
doctorExit();
Server.sendMessageToShow(); //发信息给滚动屏
Server.sendMessageToPresident(); //发送更新信息给院长
e.printStackTrace();
}
doctorExit();
Server.sendMessageToShow(); //发信息给滚动屏
Server.sendMessageToPresident(); //发送更新信息给院长
}
//(doctorName)
//info=name(patient),sex,age,id#medicine,count-medicine,count....
private void analysis(String info) {
int price=0;
String string[];
String patientInfo[] = null;
String medicineInfo[] = null;
try{
string=info.split("#");
patientInfo=string[0].split(",");
medicineInfo=string[1].split("-");
}catch (Exception e) {
doctorExit();
Server.sendMessageToShow(); //发信息给滚动屏
Server.sendMessageToPresident(); //发送更新信息给院长
return;
}
// SQLOperate.doctorReadMedicineData();
for(String s:medicineInfo){
String str[]=s.split(",");
price+=medicineData.get(str[0]).price*Integer.parseInt(str[1]);
}
//message=boolean,price,name(patient),sex,age,id#medicine,count-medicine,count....
String i=price+"";
String message="false,"+i+","+info;
try {
if(Server.charge!=null){
PrintWriter pw= new PrintWriter(Server.charge.getOutputStream());
//bw = new BufferedWriter(new OutputStreamWriter(Server.charge.getOutputStream()));
pw.println(message);
pw.flush();
System.out.println("发送信息给收费:"+message);
}
} catch (IOException e) {
//与收费员连接失败
e.printStackTrace();
}
try {
if(Server.store!=null){
PrintWriter pw= new PrintWriter(Server.store.getOutputStream());
pw.println(message);
pw.flush();
System.out.println("发送信息给药师:"+message);
}
} catch (IOException e) {
//与药师连接失败
e.printStackTrace();
}
for(Server.DoctorInfo di:Server.onlineDoctor){
if(di.account.equals(myAccount)){
di.waitCount--;
di.patientQueue.remove(patientInfo[3]);
System.out.println("减了这个账号:"+myAccount+" 等待人数:"+di.waitCount);
}
}
Server.sendMessageToShow(); //发信息给滚动屏
Server.sendMessageToPresident(); //发送更新信息给院长
//把病人从医生队列里移除
}
//message=medicine,name-price#name-price....
public void sendInitMedicineInfo(){
SQLOperate.doctorReadMedicineData();
String message="medicine,";
try {
pw=new PrintWriter(client.getOutputStream());
Set<String> set = medicineData.keySet();
for (String s:set) {
message+=(s+"-"+medicineData.get(s).price+"#");
}
System.out.println("发送给医生的药品信息"+message);
pw.println(message);
pw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void doctorExit() {
for(Server.DoctorInfo d:Server.onlineDoctor){
if(d.account.equals(myAccount)){
Server.onlineDoctor.remove(d);
break;
}
}
}
}