我们做一个医院挂号排队小系统:患者根据需要选择相应的专家挂号排队等候,专家看完病人后通过点击按钮选择下一个患者。
其中包括患者,专家,患者客户端服务器,专家客户端服务器四个类。
患者类:
package com.dr.hospital;
public class Patient {
private String name;
private int num;
private int frontNum;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getFrontNum() {
return frontNum;
}
public void setFrontNum(int frontNum) {
this.frontNum = frontNum;
}
}
患者客户端服务器:
package com.dr.hospital;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
public class QueueServer1 {
Map<Integer,Queue<Patient>> patientMap=new HashMap<Integer,Queue<Patient>>();
Queue<Patient> patientList1=new LinkedList<Patient>();
Queue<Patient> patientList2=new LinkedList<Patient>();
Queue<Patient> patientList3=new LinkedList<Patient>();
public Queue<Patient> init(Integer j) {
if(j==1){
for(int i=0;i<10;i++){
Patient p=new Patient();
p.setNum(i);
p.setFrontNum(i-1);
patientList1.offer(p);
patientMap.put(j, patientList1);
}
return patientList1;
}
else if(j==2){
for(int i=0;i<5;i++){
Patient p=new Patient();
p.setNum(i);
p.setFrontNum(i-1);
patientList2.offer(p);
patientMap.put(j, patientList2);
}
return patientList2;
}
else {
for(int i=0;i<3;i++){
Patient p=new Patient();
p.setNum(i);
p.setFrontNum(i-1);
patientList3.offer(p);
patientMap.put(j, patientList3);
}
return patientList3;
}
}
}
患者客户端:
package com.dr.hospital;
import java.util.Queue;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class PatientUI {
public static void main(String[] args) {
final Display display=Display.getDefault();
final Shell shell=new Shell();
shell.setMaximized(true);
shell.setText("医院挂号之患者使用的客户端");
QueueServer1 qs=new QueueServer1();//实例化类
final Queue<Patient> patientList1=qs.init(1);//初始化队列服务器
final Queue<Patient> patientList2=qs.init(2);//初始化队列服务器
final Queue<Patient> patientList3=qs.init(3);//初始化队列服务器
final Text txt1=new Text(shell,SWT.NULL);
txt1.setBounds(100,50,200,200);
final Button button1=new Button(shell,SWT.Activate);
button1.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Patient patient1=new Patient(); //实例化患者
patient1.setNum(patientList1.size()+1);
patient1.setFrontNum(patientList1.size());
patientList1.offer(patient1);
if(patientList1.size()<=20){
txt1.setText("您是第"+patient1.getNum()+"号,您前面还有"+patient1.getFrontNum()+"名患者,请耐心等候。");
}
else{
txt1.setText("您前面有20名患者,请考虑");
}
}
});
button1.setBounds(130, 300, 150, 50); // 设置按钮位置
button1.setFont(new Font(display,"宋体",12,SWT.BOLD));
button1.setText("1号专家");// 设置按钮上的文字
final Text txt2=new Text(shell,SWT.NULL);
txt2.setBounds(400,50,200,200);
final Button button2=new Button(shell,SWT.Activate);
button2.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Patient patient2=new Patient();//实例化患者
patient2.setNum(patientList2.size()+1);
patient2.setFrontNum(patientList2.size());
patientList2.offer(patient2);
if(patientList2.size()<=20){
txt2.setText("您是第"+patient2.getNum()+"号,\n您前面还有"+patient2.getFrontNum()+"名患者,请耐心等候。");
}
else{
txt2.setText("您前面有20名患者,请考虑");
}
}
});
button2.setBounds(430, 300, 150, 50); // 设置按钮位置
button2.setFont(new Font(display,"宋体",12,SWT.BOLD));
button2.setText("2号专家");// 设置按钮上的文字
final Text txt3=new Text(shell,SWT.NULL);
txt3.setBounds(700,50,200,200);
final Button button3=new Button(shell,SWT.Activate);
button3.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Patient patient3=new Patient();//实例化患者
patient3.setNum(patientList3.size()+1);
patient3.setFrontNum(patientList3.size());
patientList3.offer(patient3);
if(patientList3.size()<=20){
txt3.setText("您是第"+patient3.getNum()+"号,您前面还有"+patient3.getFrontNum()+"名患者,请耐心等候。");
}
else{
txt3.setText("您前面有20名患者,请考虑");
}
}
});
button3.setBounds(730, 300, 150, 50); // 设置按钮位置
button3.setFont(new Font(display,"宋体",12,SWT.BOLD));
button3.setText("3号专家");// 设置按钮上的文字
shell.layout();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
}
专家类:
package com.dr.hospital;
public class Doctor {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
专家客户端服务器:
package com.dr.hospital;
import java.util.LinkedList;
import java.util.Queue;
public class QueueServer {
Queue<Patient> patientList1=new LinkedList<Patient>();
Queue<Patient> patientList2=new LinkedList<Patient>();
Queue<Patient> patientList3=new LinkedList<Patient>();
public Queue<Patient> init(Integer j){
if(j==1){
for(int i=1;i<10;i++){
Patient p=new Patient();
p.setNum(i);
p.setFrontNum(i-1);
patientList1.add(p);
}
return patientList1;
}
else if(j==2){
for(int i=1;i<15;i++){
Patient p=new Patient();
p.setNum(i);
p.setFrontNum(i-1);
patientList2.add(p);
}
return patientList2;
}
else{
for(int i=1;i<10;i++){
Patient p=new Patient();
p.setNum(i);
p.setFrontNum(i-1);
patientList3.add(p);
}
return patientList3;
}
}
}
专家客户端:
package com.dr.hospital;
import java.util.Queue;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class DoctorUI {
public static void main(String[] args) {
final Display display=Display.getDefault();
final Shell shell=new Shell();
shell.setMaximized(true);
shell.setText("医院挂号之专家使用的客户端");
QueueServer qs=new QueueServer();//实例化类
final Queue<Patient> patientList1=qs.init(1);//初始化队列服务器
final Queue<Patient> patientList2=qs.init(2);//初始化队列服务器
final Queue<Patient> patientList3=qs.init(3);//初始化队列服务器
final Text txt1=new Text(shell,SWT.NULL);
txt1.setBounds(100,50,200,200);
final Button button1=new Button(shell,SWT.Activate);
button1.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Patient patient=patientList1.poll();
if(patient!=null){
txt1.setText(patient.getNum()+"号您好!请到1号专家室就诊");
}else{
txt1.setText("现在没有病人,您可以休息会了");
}
}
});
button1.setBounds(130, 300, 150, 50); // 设置按钮位置
button1.setFont(new Font(display,"宋体",10,SWT.BOLD));
button1.setText("1号专家,下一个");// 设置按钮上的文字
final Text txt2=new Text(shell,SWT.NULL);
txt2.setBounds(400,50,200,200);
final Button button2=new Button(shell,SWT.Activate);
button2.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Patient patient=patientList2.poll();
if(patient!=null){
txt2.setText(patient.getNum()+"号您好,请到2号专家室就诊");
}else{
txt2.setText("现在没有病人,您可以休息会了");
}
}
});
button2.setBounds(430, 300, 150, 50); // 设置按钮位置
button2.setFont(new Font(display,"宋体",10,SWT.BOLD));
button2.setText("2号专家,下一个");// 设置按钮上的文字
final Text txt3=new Text(shell,SWT.NULL);
txt3.setBounds(700,50,200,200);
final Button button3=new Button(shell,SWT.Activate);
button3.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Patient patient=patientList3.poll();
if(patient!=null){
txt3.setText(patient.getNum()+"号您好,请到3号专家室就诊");
}else{
txt3.setText("现在没有病人,您可以休息会了");
}
}
});
button3.setBounds(730, 300, 150, 50); // 设置按钮位置
button3.setFont(new Font(display,"宋体",10,SWT.BOLD));
button3.setText("3号专家,下一个");// 设置按钮上的文字
shell.layout();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
}
患者客户端运行结果:
专家客户端运行结果: