-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClientStoreThread.java
183 lines (158 loc) · 4.79 KB
/
ClientStoreThread.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package client;
import java.io.BufferedReader;
import java.io.*;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.PrivilegedActionException;
import java.util.ArrayList;
import javax.print.Doc;
import javax.swing.JLabel;
public class ClientStoreThread extends Thread {
class StorePatientInfo{
StorePatientInfo(boolean i,int price,String name,String sex,String age,String id){
this.price=price;
this.name=name;
this.sex=sex;
this.age=age;
this.id=id;
this.isPay=i;
}
boolean isPay;
int price;
String id;
String name;
String sex;
String age;
public ArrayList<Medicine> medicineQueue=new ArrayList<Medicine>();
}
class Medicine{
Medicine(String n,int c){
medicineName=n;
count=c;
}
String medicineName;
int count;
}
static public String currentId;
private static String waitString="名字\t性别\t年龄\t病人号码\n";
static public ArrayList<StorePatientInfo> myWaitPatient=new ArrayList<StorePatientInfo>();
private Socket server;
static public PrintWriter pw;
private BufferedReader br;
ClientStoreThread(Socket server){
this.server=server;
try {
br=new BufferedReader(new InputStreamReader(server.getInputStream()));
pw=new PrintWriter(server.getOutputStream());
} catch (IOException e) {
//与服务器连接失败
e.printStackTrace();
}
Store.wait.setText(waitString);
}
@Override
public void run() {
String message;
try {
while(true){
message=br.readLine();
System.out.println("仓库从服务器收到的信息:"+message);
analysis(message); //分析数据
}
} catch (IOException e) {
//连接失败
e.printStackTrace();
}
}
//message=true|false,price,name,sex,age,id#medicine,count-medicine,count-....
private void analysis(String message) {
String string[]=message.split(",");
if(string[0].equals("false")){
analysisFalse(message);
}
else if(string[0].equals("true")){
analysisiTrue(message);
}
}
private void analysisiTrue(String message) {
String s[]=message.split("#");
String property[]=s[0].split(",");
for(StorePatientInfo sp:myWaitPatient){
if(sp.id.equals(property[5])){
sp.isPay=true;
if(myWaitPatient.indexOf(sp)==0){
Store.mark.setText("此病人已付款");
}
}
}
}
//message=true|false,price,name,sex,age,id#medicine,count-medicine,count-....
private void analysisFalse(String message) {
String s[]=message.split("#"); //s[0]=true|false,price,name,sex,age,id s[1]=medicine,count-medicine,count-....
String property[]=s[0].split(",");
StorePatientInfo p=new StorePatientInfo(false, Integer.parseInt(property[1]), property[2], property[3], property[4], property[5]);
String medicineInfo[]=s[1].split("-");
for(String ss:medicineInfo){
String divide[]=ss.split(",");
Medicine m=new Medicine(divide[0], Integer.parseInt(divide[1]));
p.medicineQueue.add(m);
}
myWaitPatient.add(p);
waitString+=(property[2]+"\t"+property[3]+"\t"+property[4]+"\t"+property[5]+"\n");
Store.wait.setText(waitString);
if(myWaitPatient.size()==1){
currentId=property[5];
Store.sname.setText(property[2]);
Store.ssex.setText(property[3]);
Store.sage.setText(property[4]);
Store.ID.setText(property[5]);
showCurrentMedicineInfo();
Store.mark.setText("尚未付款");
}
}
static public void showCurrentMedicineInfo() {
String currentMedicineString="药品名字\t数量\n";
if(myWaitPatient.size()!=0){
for(Medicine m:myWaitPatient.get(0).medicineQueue){
currentMedicineString+=(m.medicineName+"\t"+m.count+"\n");
}
}
Store.content.setText(currentMedicineString);
}
static public void updatePaitenInfo(){
waitString="名字\t性别\t年龄\t病人号码\n";
for(StorePatientInfo mp:myWaitPatient){
waitString+=(mp.name+"\t"+mp.sex+"\t"+mp.age+"\t"+mp.id+"\n");
}
Store.wait.setText(waitString);
//Store.currentString="";
//sendMedicineInfo="";
//Doctor.content.setText("");
}
static public void sendInfo(){
StorePatientInfo p=myWaitPatient.get(0);
String str="true,"+p.price+","+p.name+","+p.sex+","+p.age+","+p.id+"#";
for(Medicine m:p.medicineQueue){
str+=m.medicineName+","+m.count+"-";
}
pw.println(str);
pw.flush();
myWaitPatient.remove(0);
}
static public void nextFunctioin(){
if(myWaitPatient.size()==0){
return;
}
StorePatientInfo p=myWaitPatient.get(0);
myWaitPatient.remove(0);
myWaitPatient.add(p);
updatePaitenInfo();
showCurrentMedicineInfo();
p=myWaitPatient.get(0);
Store.sname.setText(p.name);
Store.ssex.setText(p.sex);
Store.sage.setText(p.age);
Store.ID.setText(p.id);
Store.mark.setText(p.isPay?"此病人已付款":"尚未付款");
}
}