先说一下在医院挂号的情况:患者到医院后要先交钱挂号,然后在等候大厅等待叫号,叫到你,你才能去看病。怎么挂号呢?医院有好多坐诊的医生或者专家,挂号的时候患者可以选择让哪个医生或者专家为自己看病,选好以后,等着你选的医生或者专家叫你就好了。这个挂号系统怎么做的呢,我们就写一个JAVA程序模拟一下。
 首先,这个系统会有患者类Patient,医生类Doctor,还有服务器类Server,当然还有用户界面UI。
 Patient
 病人类,要有名字、号码,还要有挂号的医生的标记号,这些都要作为病人的属性,然后实现set、get方法。
 
 Patient
package hospital;
public class Patient {
	private String name;
	private int flag;
	private int num;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getNum() {
		return num;
	}
	
}
Patient
package hospital;
public class Patient {
	private String name;
	private int flag;
	private int num;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getNum() {
		return num;
	}
	
}
Doctor
医生类,要有姓名和编号,编号用来供给病人来挂自己的号,姓名就不用再说了。然后给这些属性实现set、get方法。

 Doctor
package 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;
	}
	
}
Doctor
package 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;
	}
	
}
 
QueueServer
挂号排队系统的服务器,由这个服务器来实现对病人、医生的关联,实现病人挂号和医生叫号的功能。
具体方法为:为每个医生创建一个患者队列,装载对应的挂号患者。这些队列list,要先进先出即firstin,firstout,所以要用Queue。然后把这些队列初始化,由于是模拟的程序,所以初始化时先分别加入几个病人。

 QueueServer
package 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> init1(){
		for(int i=1;i<=10;i++){
			Patient p = new Patient();
		    p.setNum(i);
			patientList1.offer(p);
		}
		return patientList1;
	}
	public Queue<Patient> init2(){
		for(int i=1;i<=15;i++){
			Patient p = new Patient();
		    p.setNum(i);
			patientList2.offer(p);
		}
		return patientList2;
	}
	public Queue<Patient> init3(){
		for(int i=1;i<=20;i++){
			Patient p = new Patient();
		    p.setNum(i);
			patientList3.offer(p);
		}
		return patientList3;
	}
}
QueueServer
package 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> init1(){
		for(int i=1;i<=10;i++){
			Patient p = new Patient();
		    p.setNum(i);
			patientList1.offer(p);
		}
		return patientList1;
	}
	public Queue<Patient> init2(){
		for(int i=1;i<=15;i++){
			Patient p = new Patient();
		    p.setNum(i);
			patientList2.offer(p);
		}
		return patientList2;
	}
	public Queue<Patient> init3(){
		for(int i=1;i<=20;i++){
			Patient p = new Patient();
		    p.setNum(i);
			patientList3.offer(p);
		}
		return patientList3;
	}
}
PatientUI
这是病人挂号系统的病人客户端。要有一个Text,几个Button(对应相应的医生),当然要有一个容器装载这些东西。
当患者点击医生按钮挂号后,患者会加入到医生对应的患者队列,Text内会显示所选择的医生或者专家、自己的号码和所排在位置(如果前面排队的人太多还会有小小的温馨提示呵)。

 PatientUI
package 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.setBounds(100, 100, 620, 450);
//		shell.setMaximized(true);
		shell.setText("医院专家挂号系统");
//		界面核心代码
		QueueServer qs1 = new QueueServer();
		final Queue<Patient> patientList1 = qs1.init1();
		final Text txt = new Text(shell,SWT.MULTI);
		txt.setFont(new Font(display,"宋体",20,SWT.BOLD));
		txt.setBounds(100,50,400,200);
//		事件代码里要访问button,所以要添加final
		final Button button = new Button(shell,SWT.Activate);
		button.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				Patient patient = new Patient();
				patient.setNum(patientList1.size()+1);
				patientList1.offer(patient);
				if(patientList1.size()<=20){
					txt.setText("\n您选择了  张医生\n\n您现在排在"+patient.getNum()+"号位置上");
				}else{
					txt.setText("\n您选择了  张医生\n\n您现在排在"+patient.getNum()+"号位置上\n\n前面已经排20多人,请考虑!");
				}
			}
		});
		button.setBounds(50, 300, 150, 55); // 设置按钮位置
		button.setFont(new Font(display,"宋体",12,SWT.BOLD));
		button.setText("专家 张医生");// 设置按钮上的文字
		// -----------------2号专家||||--------------------------------------------
		QueueServer qs2 = new QueueServer();
		final Queue<Patient> patientList2 = qs2.init2();
		final Button button2 = new Button(shell,SWT.Activate);
		button2.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				Patient patient = new Patient();
				patient.setNum(patientList2.size()+1);
				patientList2.offer(patient);
				if(patientList2.size()<=20){
					txt.setText("\n您选择了  王医生\n\n您现在排在"+patient.getNum()+"号位置上");
				}else{
					txt.setText("\n您选择了  王医生\n\n您现在排在"+patient.getNum()+"号位置上\n\n前面已经排20多人,请考虑!");
				}
			}
		});
		button2.setBounds(225, 300, 150, 55); // 设置按钮位置
		button2.setFont(new Font(display,"宋体",12,SWT.BOLD));
		button2.setText("专家 王医生");// 设置按钮上的文字
//		--------------------3号专家||||------------------------------------
		QueueServer qs3 = new QueueServer();
		final Queue<Patient> patientList3 = qs3.init3();
		final Button button3 = new Button(shell,SWT.Activate);
		button3.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				Patient patient = new Patient();
				patient.setNum(patientList3.size()+1);
				patientList3.offer(patient);
				if(patientList3.size()<=20){
					txt.setText("\n您选择了  李医生\n您现在排在"+patient.getNum()+"号位置上");
				}else{
					txt.setText("\n您选择了  李医生\n\n您现在排在"+patient.getNum()+"号位置上\n\n前面已经排20多人,请考虑!");
				}
			}
		});
		button3.setBounds(400, 300, 150, 55); // 设置按钮位置
		button3.setFont(new Font(display,"宋体",12,SWT.BOLD));
		button3.setText("专家 李医生");// 设置按钮上的文字
//		-----------------------END---------------------------------------
		shell.layout();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
PatientUI
package 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.setBounds(100, 100, 620, 450);
//		shell.setMaximized(true);
		shell.setText("医院专家挂号系统");
//		界面核心代码
		QueueServer qs1 = new QueueServer();
		final Queue<Patient> patientList1 = qs1.init1();
		final Text txt = new Text(shell,SWT.MULTI);
		txt.setFont(new Font(display,"宋体",20,SWT.BOLD));
		txt.setBounds(100,50,400,200);
//		事件代码里要访问button,所以要添加final
		final Button button = new Button(shell,SWT.Activate);
		button.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				Patient patient = new Patient();
				patient.setNum(patientList1.size()+1);
				patientList1.offer(patient);
				if(patientList1.size()<=20){
					txt.setText("\n您选择了  张医生\n\n您现在排在"+patient.getNum()+"号位置上");
				}else{
					txt.setText("\n您选择了  张医生\n\n您现在排在"+patient.getNum()+"号位置上\n\n前面已经排20多人,请考虑!");
				}
			}
		});
		button.setBounds(50, 300, 150, 55); // 设置按钮位置
		button.setFont(new Font(display,"宋体",12,SWT.BOLD));
		button.setText("专家 张医生");// 设置按钮上的文字
		// -----------------2号专家||||--------------------------------------------
		QueueServer qs2 = new QueueServer();
		final Queue<Patient> patientList2 = qs2.init2();
		final Button button2 = new Button(shell,SWT.Activate);
		button2.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				Patient patient = new Patient();
				patient.setNum(patientList2.size()+1);
				patientList2.offer(patient);
				if(patientList2.size()<=20){
					txt.setText("\n您选择了  王医生\n\n您现在排在"+patient.getNum()+"号位置上");
				}else{
					txt.setText("\n您选择了  王医生\n\n您现在排在"+patient.getNum()+"号位置上\n\n前面已经排20多人,请考虑!");
				}
			}
		});
		button2.setBounds(225, 300, 150, 55); // 设置按钮位置
		button2.setFont(new Font(display,"宋体",12,SWT.BOLD));
		button2.setText("专家 王医生");// 设置按钮上的文字
//		--------------------3号专家||||------------------------------------
		QueueServer qs3 = new QueueServer();
		final Queue<Patient> patientList3 = qs3.init3();
		final Button button3 = new Button(shell,SWT.Activate);
		button3.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				Patient patient = new Patient();
				patient.setNum(patientList3.size()+1);
				patientList3.offer(patient);
				if(patientList3.size()<=20){
					txt.setText("\n您选择了  李医生\n您现在排在"+patient.getNum()+"号位置上");
				}else{
					txt.setText("\n您选择了  李医生\n\n您现在排在"+patient.getNum()+"号位置上\n\n前面已经排20多人,请考虑!");
				}
			}
		});
		button3.setBounds(400, 300, 150, 55); // 设置按钮位置
		button3.setFont(new Font(display,"宋体",12,SWT.BOLD));
		button3.setText("专家 李医生");// 设置按钮上的文字
//		-----------------------END---------------------------------------
		shell.layout();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
DoctorUI
这是医生的客户端,同样要有一个容器装载一个Text和一个Button。点击Button(下一位..),就会在他的病人队列里取出排在最前面的那个患者,在Text内显示这位前来就诊的患者的信息(如果所有病人都看完了,就会显示没有病人了)。
(这只是一个医生的客户端,其他医生的客户端的编写方法与之相同,这里就不一一写出来了..)

 DoctorUI
package 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) {
		// TODO Auto-generated method stub
		final Display display = Display.getDefault();
		final Shell shell = new Shell();
		shell.setBounds(200, 100, 420, 350);
		shell.setText("医院专家挂号系统--张医生客户端");
//		界面核心代码
		QueueServer qs1 = new QueueServer();
		final Queue<Patient> patientListz = qs1.init1();
        final Text txt = new Text(shell,SWT.MULTI);
		txt.setFont(new Font(display,"微软雅黑",13,SWT.BOLD));
		txt.setBounds(50,50,300,100);
//		事件代码里要访问button,所以要添加final
		final Button button = new Button(shell,SWT.Activate);
		button.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				Patient p = patientListz.poll();
				if(p!=null){
					txt.setText("\n\t\t第   "+p.getNum()+"  号病人" +
							"\n\t 请到张医生医务室就医");
				}
				else{
					txt.setText("\n\t现在没有病人看病了");
				}
			}
		});
		button.setBounds(100, 200, 200, 75); // 设置按钮位置
		button.setFont(new Font(display,"微软雅黑",14,SWT.BOLD));
		button.setText("下一位..");// 设置按钮上的文字
//		---------------------END----------------------------
		shell.layout();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
DoctorUI
package 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) {
		// TODO Auto-generated method stub
		final Display display = Display.getDefault();
		final Shell shell = new Shell();
		shell.setBounds(200, 100, 420, 350);
		shell.setText("医院专家挂号系统--张医生客户端");
//		界面核心代码
		QueueServer qs1 = new QueueServer();
		final Queue<Patient> patientListz = qs1.init1();
        final Text txt = new Text(shell,SWT.MULTI);
		txt.setFont(new Font(display,"微软雅黑",13,SWT.BOLD));
		txt.setBounds(50,50,300,100);
//		事件代码里要访问button,所以要添加final
		final Button button = new Button(shell,SWT.Activate);
		button.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				Patient p = patientListz.poll();
				if(p!=null){
					txt.setText("\n\t\t第   "+p.getNum()+"  号病人" +
							"\n\t 请到张医生医务室就医");
				}
				else{
					txt.setText("\n\t现在没有病人看病了");
				}
			}
		});
		button.setBounds(100, 200, 200, 75); // 设置按钮位置
		button.setFont(new Font(display,"微软雅黑",14,SWT.BOLD));
		button.setText("下一位..");// 设置按钮上的文字
//		---------------------END----------------------------
		shell.layout();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
PatientUI
病人客户端的运行效果:
 
 
DoctorUI
医生客户端的运行效果:

   
	posted on 2010-11-01 16:33 
Mineralwasser 阅读(1517) 
评论(3)  编辑  收藏