1.当我们去银行办理什么业务的时候总会先在一个类似电脑的小系统上去一个号,然后等着,当被叫到时就去相应的窗口办理业务,这种排队的小系统主要用到了接口Queue,因为它符合队列的要求先进先出。
2.还有就是有种特殊情况就是,银行的金卡用户,他们的权限较高,总可以在普通用户的前面办理业务
相应代码如下:
package com.dr.queue;
public class Counter {
private int counterId; //���й�̨���ں�
private String counterName; //���й�����Ա������
private Waiter w;
public Counter(Waiter w){
this.w = w;
}
public Waiter getW() {
return w;
}
public void setW(Waiter w) {
this.w = w;
}
public int getCounterId() {
return counterId;
}
public void setCounterId(int counterId) {
this.counterId = counterId;
}
public String getCounterName() {
return counterName;
}
public void setCounterName(String counterName) {
this.counterName = counterName;
}
public void handleBusiness(){
System.out.println("I handling the " + this.w.getNum() +" waiter");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("I have handled the waiter,next ,next ,next");
}
}
package com.dr.queue;
public class Waiter {
private int num;
private int frontNum;
private int flag;//0=普通 1= 金卡
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
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;
}
}
package com.dr.queue;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class QueueServer {
LinkedList<Waiter> waiterList = new LinkedList<Waiter>();
public LinkedList<Waiter> init(){
for(int i=1;i<=30;i++){
Waiter w = new Waiter();
w.setNum(i);
w.setFrontNum(i-1);
waiterList.offer(w);
}
return waiterList;
}
}
package ui;
import java.util.Queue;
import org.eclipse.jface.dialogs.MessageDialog;
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;
import com.dr.queue.QueueServer;
import com.dr.queue.Waiter;
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 Queue<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) {
//System.out.println("############### " + waiterList.size());
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();
}
}
}
package 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.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;
import com.dr.queue.QueueServer;
import com.dr.queue.Waiter;
public class WaiterQueueUI {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setBounds(0,0, 1000, 800);
shell.setText("银行排队之顾客使用的客户端");
// ------------------新插入的界面核心代码------------------------
QueueServer qs = new QueueServer();//实例化类
final LinkedList<Waiter> waiterList = qs.init();//初始化队列服务器
final Text txt = new Text(shell,SWT.MULTI);
txt.setBounds(260, 50, 500, 450);
// 事件代码里要访问button,所以加�?��final
final Button button = new Button(shell, SWT.Activate);
button.addSelectionListener(new SelectionAdapter() { // 加一个�?择监听器
public void widgetSelected(SelectionEvent e) {
//System.out.println("############### " + waiterList.size());
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(100, 530, 200, 75); // 设置按钮位置
button.setFont(new Font(display,"宋体",12,SWT.BOLD));
button.setText("个人业务");// 设置按钮上的文字
//final Text goldTxt = new Text(shell,SWT.MULTI);
// goldTxt.setBounds(600, 50, 500, 450);
// 事件代码里要访问button,所以加�?��final
final Button goldButton = new Button(shell, SWT.Activate);
goldButton.addSelectionListener(new SelectionAdapter() { // 加一个�?择监听器
public void widgetSelected(SelectionEvent e) {
//System.out.println("############### " + waiterList.size());
Waiter w = new Waiter();
w.setFlag(1);
waiterList.addFirst(w);
txt.setText("尊敬的金卡用户,您现在可以到我行的任意窗口随时办理业务");
}
});
goldButton.setBounds(700, 530, 200, 75); // 设置按钮位置
goldButton.setFont(new Font(display,"宋体",12,SWT.BOLD));
goldButton.setText("金卡通道");// 设置按钮上的文字
// ------------------END---------------------------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
运行界面为:
posted on 2010-11-01 13:22
迷人笑笑 阅读(3756)
评论(0) 编辑 收藏