我们做一个银行排队金卡卡塞小系统,并用swt实现图形界面功能。
银行系统中包括:银行工作人员、办理业务的用户(包括普通用户和金卡用户),还有存放用户对列的服务器。
用户有3个属性:排队号码、排在前面的人数、卡的类型(普通卡和金卡)。
需要注意的是:金卡用户来的时候,会自动排在队列的最前面办理业务。
代码如下:
用户类:
package com.dr.ui;
public class Waiter {
private int num;
private int frontNum;
private int flag; //卡的类型 0=普通卡 1=金卡
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;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
}
服务器:
package com.dr.ui;
import java.util.LinkedList;
public class QueueServer {
LinkedList<Waiter> waiterList=new LinkedList<Waiter>();
public LinkedList<Waiter> init() {
for(int i=1;i<=10;i++){
Waiter w=new Waiter();
w.setNum(i);
w.setFrontNum(i-1);
waiterList.add(w);
}
return waiterList;
}
}
用户客户端:
package com.dr.ui;
import java.util.LinkedList;
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 WaiterQueueUI {
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 LinkedList<Waiter> waiterList=qs.init();
final Text txt=new Text(shell,SWT.NULL);
txt.setBounds(50,50,500,450);
final Button button=new Button(shell,SWT.Activate);
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Waiter waiter=new Waiter();
waiter.setNum(waiterList.size()+1);
waiter.setFrontNum(waiterList.size());
waiterList.offer(waiter);
if(waiterList.size()<=50){
txt.setText("尊敬的用户您好!您现在排在"+waiter.getNum()+"位置上,\n您前面有"+waiter.getFrontNum()+"个人。\n请耐心等候吧");
}
else{
txt.setText("尊敬的用户您好!您前面已经排50个人,请考虑!\n"+"您现在排在"+waiter.getNum()+"位置上,\n您前面有"+waiter.getFrontNum()+"个人\n请您耐心等候吧!哈哈");
}
}
});
button.setBounds(180,530,200,75);
button.setText("个人业务");
final Text goldTxt=new Text(shell,SWT.NULL);
goldTxt.setBounds(650,50,500,450);
final Button goldButton=new Button(shell,SWT.Activate);
goldButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Waiter waiter=new Waiter();
waiter.setFlag(1);
waiterList.addFirst(waiter);
goldTxt.setText("尊敬的金卡用户您好,现在可以去办理业务");
}
});
goldButton.setBounds(780,530,200,75);
goldButton.setText("金卡通道");
shell.layout();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
}
}
银行工作人员客户端:
package com.dr.ui;
import java.util.LinkedList;
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 BankWorkerUI {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
shell.setText("银行排队之工作人员客户端");
QueueServer qs=new QueueServer();
final LinkedList<Waiter> waiterList = qs.init();//初始化队列服务器
final Text txt = new Text(shell,SWT.MULTI);
txt.setBounds(65, 30, 180, 70);
// 事件代码里要访问button
final Button button = new Button(shell, SWT.Activate);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Waiter waiter= waiterList.poll();
if(waiter!= null){
txt.setText(waiter.getNum()+"号顾客请到1号窗口办理业务");
}else{
txt.setText("现在没有人办理业务了,您可以休息会了\n 喝杯咖啡吧,哈哈!");
}
}
});
button.setBounds(90, 141, 100, 25); // 设置按钮位置
button.setText("下一个");// 设置按钮上的文字
// ------------------END---------------------------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
用户客户端运行结果:
银行工作人员客户端运行结果: