现在银行遍地开花,在城市了差不多每隔200米就可以看到一家银行。在银行叫号排队也就成了很平常的事情。但是很让人生气的是在咱们这个世界上最大的社会主义国家里,在银行排队却很不公平,咱们这些没钱的人(在银行里存钱不多,持普通卡)每次排队都要等很长一段时间才能办理业务,而且没有工作人员来招呼,而那些有钱人(在银行里存了很多钱,持“金卡”)却不用等,随时可以办理业务,还美其名曰“金卡”特权。难道有钱就有特权吗?气愤中……...
还是说这个系统吧:
银行大厅里有一个叫号机(顾客客户端),如果持普通卡点一下办理业务按钮就会打印一张小条,显示你的号码和排在你前面的人数;如果持金卡,点一下办理业务就会提示下一位要办理的客户就是你。
每个业务办理窗口都有一个工作人员客户端,点击下一位就会从等待的客户队列中取出第一位到该窗口办理。
客户,也就是等待的人,要有号码、排在前面的人数等属性,然后实现这些属性的set、get方法。
Waiter
Waiter
package queuebanknew;
public class Waiter {
private int num;
private int frontNum;
private int flag;
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;
}
}
工作人员,也就是窗口的处理业务的人员,她们有自己的编号,姓名,顾客,实现这些属性的set、get方法,还加入了一段工作过程的代码。
Counter
Counter
package queuebanknew;
public class Counter {
private int id;
private String name;
private Waiter w;
public Counter(Waiter waiter) {
this.w=waiter;
}
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;
}
public Waiter getW() {
return w;
}
public void setW(Waiter w) {
this.w = w;
}
public void handBussines(){
System.out.println("正在处理:"+this.w.getNum()+"号");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理完毕:"+this.getW().getNum());
}
}
测试一下:
TestBankQueue
package queuebanknew;
import java.util.Queue;
public class TestBankQueue {
/**
* @param args
*/
public static void main(String[] args) {
QueueServer qs = new QueueServer();
Queue<Waiter> waiterList = qs.init();//初始化10个等待者
Waiter waiter= null;
while(true){//模拟工作人员处理业务
waiter = waiterList.poll();
if(waiter != null){
Counter counter = new Counter(waiter);
counter.handBussines();
}else{
break;
}
}
}
}
结果:
服务器端:
QueueServer
package queuebanknew;
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.offer(w);
}
return waiterList;
}
}
BankWorkerUI
工作人员客户端:
BankWorkerUI
package queuebanknew;
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.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 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){
Waiter waiter = waiterList.poll();
if(waiter!=null){
txt.setText(waiter.getNum()+"号顾客到1号窗口办理");
}else{
txt.setText("没人办理业务");
}
}
});
button.setBounds(90, 141, 100, 25);
button.setText("下一个");
shell.layout();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
}
WaiterQueueUI
客户的客户端:
WaiterQueueUI
package queuebanknew;
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;
public class WaiterQueueUI {
public static void main(String[] args){
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setBounds(100, 100, 570, 380);
// shell.setMaximized(true);
shell.setText("银行排队----顾客客户端");
// 界面核心代码
QueueServer qs = new QueueServer();
final LinkedList<Waiter> waiterList = qs.init();
final Text txt = new Text(shell,SWT.MULTI);
txt.setBounds(50,50,200,150);
// 事件代码里要访问button,所以要添加final
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()<=20){
txt.setText("您现在排在"+waiter.getNum()+"位置上\n您前面有"+waiter.getFrontNum()+"个人,请耐心等待!!");
}else{
txt.setText("您前面已经排20多人,请考虑!\n"+"您现在排在"+waiter.getNum()+"位置上,\n您前面有"+waiter.getFrontNum()+"个人,请耐心等待!!");
}
}
});
button.setBounds(75, 250, 150, 50); // 设置按钮位置
button.setFont(new Font(display,"宋体",12,SWT.BOLD));
button.setText("个人业务");// 设置按钮上的文字
// ------------------------------------------------------------------------
final Text goldTxt = new Text(shell,SWT.NULL);
goldTxt.setBounds(300,50,200,150);
// 事件代码里要访问button,所以要添加final
final Button goldButton = new Button(shell,SWT.Activate);
goldButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
Waiter w = new Waiter();
w.setFlag(1);
waiterList.addFirst(w);
goldTxt.setText("金卡用户,下一位就是您!");
}
});
goldButton.setBounds(325, 250, 150, 50); // 设置按钮位置
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 17:16
Mineralwasser 阅读(669)
评论(0) 编辑 收藏