先說一下在醫院掛號的情況:患者到醫院后要先交錢掛號,然后在等候大廳等待叫號,叫到你,你才能去看病。怎么掛號呢?醫院有好多坐診的醫生或者專家,掛號的時候患者可以選擇讓哪個醫生或者專家為自己看病,選好以后,等著你選的醫生或者專家叫你就好了。這個掛號系統怎么做的呢,我們就寫一個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; } }
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; } }
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; } }
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(); } } }
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(); } } }
PatientUI
病人客戶端的運行效果:
DoctorUI
醫生客戶端的運行效果: