把困难踩在脚下

迎难而上

 

用GUI模拟医院挂号系统

要求:

有三个专家,病人可以根据自己的实际情况选择相应的专家,专家通过点击按钮选择下一个病人。

程序代码如下:

//封装病人类

package com.dr.hospitalQueueSystem;

import java.util.LinkedList;
import java.util.Queue;

public class Patient {
    private int num;
    private String name;
    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 Patient(int num,String name)
    {
        this.setName(name);
        this.setNum(num);
    }

}

//封装专家类

package com.dr.hospitalQueueSystem;

import java.util.LinkedList;
import java.util.Queue;

public class Professor {
    private String name;
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.setName(name);
    }
    public Professor(String name,int id)
    {
        this.name=name;
        this.id=id;
    }

}

//挂号服务器

package com.dr.hospitalQueueSystem;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class HospitalQueueServer {
    Map<Integer,Queue<Patient>> map=new HashMap<Integer,Queue<Patient>>();
    Queue<Patient> patientList1=new LinkedList<Patient>();
    Queue<Patient> patientList2=new LinkedList<Patient>();
    Queue<Patient> patientList3=new LinkedList<Patient>();
    public void Init()
    {
        //实例化三个专家
        Professor pfr1=new Professor("张三",1);
        Professor pfr2=new Professor("李四",2);
        Professor pfr3=new Professor("王五",3);
        //实例化15个病人,并将他们每五个添加到三个专家所对应的集合队列中
        patientList1.offer(new Patient(1, "成龙"));
        patientList1.offer(new Patient(2, "李小龙"));
        patientList1.offer(new Patient(3, "大S"));
        patientList1.offer(new Patient(4, "郭富城"));
        patientList1.offer(new Patient(5, "小S"));
        patientList2.offer(new Patient(6, "刘德华"));
        patientList2.offer(new Patient(7, "林心如"));
        patientList2.offer(new Patient(8, "张静初"));
        patientList2.offer(new Patient(9, "王宝强"));
        patientList2.offer(new Patient(10, "李连杰"));
        patientList3.offer(new Patient(11, "大傻"));
        patientList3.offer(new Patient(12, "张国荣"));
        patientList3.offer(new Patient(13, "周星驰"));
        patientList3.offer(new Patient(14, "赵薇"));
        patientList3.offer(new Patient(15, "周迅"));
        //将各个专家与他们对应的病人集合进行键值对
        map.put(pfr1.getId(), patientList1);
        map.put(pfr2.getId(), patientList2);
        map.put(pfr3.getId(), patientList3);
    }

}

//病人使用端

package com.dr.hospitalQueueSystem;

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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class PatientQueueUI {
    public static void main(String args[])
    {
        Display display=new Display();
        Shell shell=new Shell(display);
        shell.setMaximized(true);
        //初始化界面
        shell.setText("医院病人挂号使用系统");
        //服务器初始化
        HospitalQueueServer hq=new HospitalQueueServer();
        hq.Init();
        final Queue<Patient> patientList1=hq.map.get(1);
        final Queue<Patient> patientList2=hq.map.get(2);
        final Queue<Patient> patientList3=hq.map.get(3);
        //添加标签控件
        Label label=new Label(shell,SWT.NONE);
        label.setFont(new Font(display,"宋体",15,SWT.BOLD));
        label.setBounds(450,100,600,100);
        label.setText("想要选择那位专家为你看病,请点击相应专家的姓名按钮!");
        //添加三个文本框
        final Text text1=new Text(shell,SWT.WRAP);
        text1.setBounds(100,250,250,200);
        final Text text2=new Text(shell,SWT.WRAP);
        text2.setBounds(550,250,250,200);
        final Text text3=new Text(shell,SWT.WRAP);
        text3.setBounds(900,250,250,200);
        //添加按钮1
        Button button1=new Button(shell,SWT.Activate);
        button1.setBounds(150,520,120,60);
        button1.setText("1号专家张三");
        //添加按钮2
        Button button2=new Button(shell,SWT.Activate);
        button2.setBounds(600,520,120,60);
        button2.setText("2号专家李四");
        //添加按钮3
        Button button3=new Button(shell,SWT.Activate);
        button3.setBounds(960,520,120,60);
        button3.setText("3号专家王五");
        //为按钮1添加事件处理,事件处理内容为“读取病人的id和姓名,并将此病人添加到相应专家的对立中”
        button1.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e)
            {
                Patient pt=new Patient(patientList1.size()+1,"***");
                patientList1.offer(pt);
                text1.setText(pt.getNum()+"号病人,欢迎选择1号专家为你坐诊,请在等候区等候,会有工作人员通过广播通知你什么时候去看病");
            }
        });
        //为按钮2添加事件处理
        button2.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e)
            {
                Patient pt=new Patient(patientList2.size()+1,"****");
                patientList2.offer(pt);
                text2.setText(pt.getNum()+"号病人,欢迎选择2号专家为你坐诊,请在等候区等候,会有工作人员通过广播通知你什么时候去看病");
            }
        });
        //为按钮3添加事件处理
        button3.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e)
            {
                Patient pt=new Patient(patientList3.size()+1,"****");
                patientList3.offer(pt);
                text3.setText(pt.getNum()+"号病人,欢迎选择3号专家为你坐诊,请在等候区等候,会有工作人员通过广播通知你什么时候去看病");
            }
        });
        shell.layout();
        shell.open();
        while(!shell.isDisposed())
        {
            if(!display.readAndDispatch()){
                display.sleep();
            }
        }
    }

}

**************************************************************************

程序运行结果如下:

未命名

//专家使用端

package com.dr.hospitalQueueSystem;

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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ProfessorUI {
    public static void main(String args[])
    {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setMaximized(true);
        //初始化界面
        shell.setText("专家使用系统端");
        //服务器初始化
        HospitalQueueServer hq=new HospitalQueueServer();
        hq.Init();
        final Queue<Patient> qp1=hq.map.get(1);
        final Queue<Patient> qp2=hq.map.get(2);
        final Queue<Patient> qp3=hq.map.get(3);
        //添加文本控件
        final Text text1=new Text(shell,SWT.WRAP);
        text1.setBounds(100,250,250,200);
        text1.setFont(new Font(display,"宋体",15,SWT.BOLD));
        final Text text2=new Text(shell,SWT.WRAP);
        text2.setFont(new Font(display,"宋体",15,SWT.BOLD));
        text2.setBounds(550,250,250,200);
        final Text text3=new Text(shell,SWT.WRAP);
        text3.setFont(new Font(display,"宋体",15,SWT.BOLD));
        text3.setBounds(900,250,250,200);
        //添加按钮1
        Button button1=new Button(shell,SWT.Activate);
        button1.setBounds(150,520,120,60);
        button1.setText("张三:下一个");
        //添加按钮2
        Button button2=new Button(shell,SWT.Activate);
        button2.setBounds(600,520,120,60);
        button2.setText("李四:下一个");
        Button button3=new Button(shell,SWT.Activate);
        button3.setBounds(960,520,120,60);
        button3.setText("王五:下一个");
        //为按钮1添加事件处理,事件处理内容为“读取病人的id和姓名,并将此病人添加到相应专家的对立中”
        button1.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e)
            {
                Patient pt=qp1.poll();
                if(pt!=null)
                {
                    text1.setText(pt.getName()+"请进来");
                }
                else
                {
                    text1.setText("暂时没有病人了,您先休息会儿");
                }
            }
        });
        //为按钮2添加事件处理
        button2.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e)
            {
                Patient pt=qp2.poll();
                if(pt!=null)
                {
                    text2.setText(pt.getName()+"请进来");
                }
                else
                {
                    text2.setText("暂时没有病人了,您先休息会儿");
                }
            }
        });
        //为按钮3添加事件处理
        button3.addSelectionListener(new SelectionAdapter(){
            public void widgetSelected(SelectionEvent e)
            {
                Patient pt=qp3.poll();
                if(pt!=null)
                {
                    text3.setText(pt.getName()+"请进来");
                }
                else
                {
                    text3.setText("暂时没有病人了,您先休息会儿");
                }
            }
        });
        shell.layout();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

}

********************************************************************************************

程序运行结果如下:

未命名

这个程序还有许多缺点比如说数据冗余,没有实现专家与病人之间的通信!

由于没有实现多线程技术,在专家系统端运行时,只能是调用服务器初始化的病人,不能调用病人系统端添加的病人。

posted on 2010-10-31 19:52 冯魁 阅读(282) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

快乐每一天!

Everything is an object!

常用链接

留言簿(2)

随笔档案

学习网站

搜索

最新评论

阅读排行榜

评论排行榜