我们今天来做一个医院挂号排队系统:分为病人,医生,服务器三个类。
病人:我只关心我挂哪个专家的号,我排在挂这个专家的队列里的位置和前面的人数。
医生:我有我的属性(id,name,需要看病的病人),而且我有一个处理病情的方法;我只关心挂我的号的人有多少,每次我只需要去从队列里取出病人就可以了,这就够了。
服务器:服务器需要设置一个Map(键值对应关系),将每一个医生对应的id和他的病人队列一一对应起来,需要初始化一些病人存储在每个医生的病人队列里。
再说UI:
病人客户端UI:
在点击事件监听器里:我需要知道排在我前面的人数有多少,我的位置是多少,需要将服务器的队列取过来处理。
医生客户端UI:
再点击事件监听器里:我只需要一个一个的叫下一位病人的名字或者号码就可以了。
下面附代码:
病人:
package com.dr.hospital;
public class Weaker {
private int number;
private int frontnumber;
private String name;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getFrontnumber() {
return frontnumber;
}
public void setFrontnumber(int frontnumber) {
this.frontnumber = frontnumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
医生:
package com.dr.hospital;
public class Doctor {
private int id;
private String name;
private Weaker weaker;
public Doctor(Weaker weaker){
this.setWeaker(weaker);
}
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;
}
public Weaker getWeaker() {
return weaker;
}
public void setWeaker(Weaker weaker) {
this.weaker = weaker;
}
public void handlDisease(){
System.out.println("我正在治疗第"+this.weaker.getNumber()+"号病人的病情");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("我已经处理完第"+this.weaker.getNumber()+"号病人的病例");
System.out.println("请下一位病人到第"+(this.id+1)+"号医务室治疗病情");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
服务器:
package com.dr.hospital;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
public class Server {
public Map<Integer,Queue<Weaker>> doctorlist = new HashMap<Integer,Queue<Weaker>>();
public Queue<Weaker> doctor1weakerlist = new LinkedList<Weaker>();
public Queue<Weaker> doctor2weakerlist = new LinkedList<Weaker>();
public Queue<Weaker> doctor3weakerlist = new LinkedList<Weaker>();
public Queue<Weaker> init(Integer iter){
Weaker w= null;
if(iter==1){
for(int i=1;i<30;i++){
w = new Weaker();
w.setNumber(i);
w.setFrontnumber(i-1);
doctor1weakerlist.offer(w);
doctorlist.put(iter, doctor1weakerlist);
}
return doctor1weakerlist;
}
else if(iter==2){
for(int i=1;i<20;i++){
w = new Weaker();
w.setNumber(i);
w.setFrontnumber(i-1);
doctor2weakerlist.offer(w);
doctorlist.put(iter, doctor2weakerlist);
}
return doctor2weakerlist;
}
else{
for(int i=1;i<10;i++){
w = new Weaker();
w.setNumber(i);
w.setFrontnumber(i-1);
doctor3weakerlist.offer(w);
doctorlist.put(iter, doctor3weakerlist);
}
return doctor3weakerlist;
}
}
}
病人客户端UI:
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.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class WeakerQueueUI {
public static void main(String[] args){
Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setMaximized(true);
shell.setText("医院排队之病人客户端");
final Text text = new Text(shell,SWT.MULTI);
text.setBounds(500, 100, 300, 300);
Server server = new Server();
final Queue<Weaker> zhangweakerlist = server.init(1);
final Queue<Weaker> liweakerlist = server.init(2);
final Queue<Weaker> zhaoweakerlist = server.init(3);
Button button1 = new Button(shell,SWT.Activate);
button1.setBounds(250, 530, 200, 75);
button1.setText("张医生");
button1.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Weaker w = new Weaker();
w.setNumber(zhangweakerlist.size()+1);
w.setFrontnumber(zhangweakerlist.size());
zhangweakerlist.offer(w);
if(zhangweakerlist.size()<=50){
text.setText("您现在排在"+w.getNumber()+"位置上,\n您前面有"+w.getFrontnumber()+"个人\n请您耐心等候吧!哈哈!");
}
else{
text.setText("您前面已经排50个人,请考虑!\n"+"您现在排在"+w.getNumber()+"位置上,\n您前面有"+w.getFrontnumber()+"个人\n请您耐心等候吧!哈哈");
}
}
});
Button button2 = new Button(shell,SWT.Activate);
button2.setBounds(550, 530, 200, 75);
button2.setText("李医生");
button2.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Weaker gw = new Weaker();
gw.setNumber(liweakerlist.size()+1);
gw.setFrontnumber(liweakerlist.size());
liweakerlist.offer(gw);
if(liweakerlist.size()<=50){
text.setText("您现在排在"+gw.getNumber()+"位置上,\n您前面有"+gw.getFrontnumber()+"个人\n请您耐心等候吧!哈哈!");
}
else{
text.setText("您前面已经排超过50人了,请考虑!\n"+"您现在排在"+gw.getNumber()+"位置上,\n您前面有"+gw.getFrontnumber()+"个人\n请您耐心等候吧!哈哈");
}
}
});
Button button3 = new Button(shell,SWT.Activate);
button3.setBounds(850, 530, 200, 75);
button3.setText("赵医生");
button3.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Weaker w = new Weaker();
w.setNumber(zhaoweakerlist.size()+1);
w.setFrontnumber(zhaoweakerlist.size());
zhaoweakerlist.offer(w);
if(zhaoweakerlist.size()<=50){
text.setText("您现在排在"+w.getNumber()+"位置上,\n您前面有"+w.getFrontnumber()+"个人\n请您耐心等候吧!哈哈!");
}
else{
text.setText("您前面已经排50个人,请考虑!\n"+"您现在排在"+w.getNumber()+"位置上,\n您前面有"+w.getFrontnumber()+"个人\n请您耐心等候吧!哈哈");
}
}
});
shell.layout();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
}
医生客户端UI:
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.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) {
Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setMaximized(true);
shell.setText("张医生客户端");
final Text text = new Text(shell,SWT.MULTI);
text.setBounds(500, 100, 300, 300);
final Button button = new Button(shell,SWT.Activate);
button.setBounds(550, 530, 200, 75);
button.setText("下一个病人");
Server server = new Server();
final Queue<Weaker> zhangwaiterlist = server.init(1);
final Queue<Weaker> liwaiterlist = server.init(2);
final Queue<Weaker> zhaowaiterlist = server.init(3);
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Weaker w = zhangwaiterlist.poll();
if(w!=null){
text.setText("请"+w.getNumber()+"号病人到张医生医务室就医");
}
else{
text.setText("现在没有病人看病了,您可以休息会了\n 喝杯咖啡吧,哈哈!");
}
}
});
shell.layout();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
}
最后看看运行结果:
病人客户端:
医生客户端: