package cn.itheima;
import java.util.Random;
import java.util.concurrent.Executors;
//业务窗口
public class ServiceWindow
{
//窗口号码,默认1号,可设置
private int windowId = 1;
//窗口类型,默认普通,可设置
private CustomerType type = CustomerType.COMMON;
//提供设置窗口号和窗口类型的方法
public void setWindowId(int windowId)
{
//System.out.println("设置窗口号::"+windowId);
this.windowId = windowId;
}
public void setType(CustomerType type)
{
this.type = type;
}
//开启服务
public void start()
{
//每个窗口都需要单独运行
Executors.newSingleThreadExecutor().execute(
new Runnable()
{
@Override
public void run()
{
//窗口一旦开启就连续提供服务
while (true)
{
//根据窗口类型提供相应服务
switch (type)
{
default:
case COMMON:
service(CustomerType.COMMON);
break;
case EXPRESS:
service(CustomerType.EXPRESS);
break;
case VIP:
service(CustomerType.VIP);
break;
}
}
}
});
}
//服务过程,需指定服务类型
private void service(CustomerType type)
{
//获取窗口名称:类型和编号
String windowName = "第"+windowId+"号"+type+"窗口";
//从排队机相应的管理器中获取服务对象号码
Integer customerNum = NumberMachine.getInstance().getManagerByType(type).fetchServiceNum();
//给出提示信息
System.out.println(windowName+"正在获取"+type+"任务。。。");
//判断是否拿到客户好吗
if (customerNum!=null)
{
//开始为客户服务,记录开始时间
long beginTime = System.currentTimeMillis();
//给出提示信息
System.out.println(windowName+"正在为第"+customerNum+"位"+type+"客户办理业务………");
//模拟服务过程,线程休眠一段服务时间
try
{
Thread.sleep(getServiceTime(type));
} catch (InterruptedException e)
{
e.printStackTrace();
}
//计算服务耗时
long costTime = System.currentTimeMillis() - beginTime;
//给出服务结束信息
System.out.println(windowName+"为第"+customerNum+"位"+type+"客户服务结束,耗时"+costTime/1000+"秒!");
}
else
{
synchronized (this)
{
//没有获取到对应任务,给出提示
System.out.print(windowName + "没有获取到" + type + "任务,");
//如果是快速或vip窗口没有任务,就转向普通客户服务
if (type == CustomerType.EXPRESS || type == CustomerType.VIP)
{
System.out.println("现在开始提供普通业务办理。");
//将窗口类型设置为type+"/普通",在枚举类中添加这个类型
String str = type + "_COMMON";
this.setType(CustomerType.valueOf(str));
//调用普通窗口服务方法
service(CustomerType.valueOf(str));
//service(CustomerType.COMMON);
//本次服务结束后,还要将窗口改为原来的类型
this.setType(type);
} else
//不然就是普通窗口空闲 就休息吧
{
System.out.println("暂停服务一秒...");
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
//根据服务类别不同,返回相应的服务时间
private long getServiceTime(CustomerType type)
{
//最大最小服务时间
int MAX_SERVICE_TIME = 10000;
int MIN_SERVICE_TIME = 1000;
//判断业务类型
switch (type)
{
case EXPRESS: return MIN_SERVICE_TIME;
default:
case COMMON:
case VIP:
//最大随机时间
int MAX_RAND = MAX_SERVICE_TIME - MIN_SERVICE_TIME + 1;
//计算服务时间
long serviceTime = new Random().nextInt(MAX_RAND) + MIN_SERVICE_TIME;
return serviceTime;
}
}
}
张孝祥银行业务调度系统源代码 第二次写 复习
4星 · 超过85%的资源 需积分: 12 94 浏览量
2013-01-25
14:27:32
上传
评论
收藏 15KB RAR 举报
luck638
- 粉丝: 13
- 资源: 19
最新资源
- Unity XR 手势射击控制脚本(适用于任何可手势识别的设备)
- 机械设计全自动电表(NB和IC卡表)控制和上壳装配线sw16可编辑非常好的设计图纸100%好用.zip
- 基于matlab的EAN-13条形码识别系统GUI界面.zip代码53
- matlab基于bp神经网络交通信号标志识别GUI界面13个标志.zip代码54
- 电子万年历答辩实物展示视频mp4格式
- 基于python实现的程序,包括哈希感知算法cvHash,图像切割cvsplit,固定目标检测cvRec(附文档ppt)等
- 计算0-10000之间所有偶数的和
- multiled.zip
- 基于php实现的哈希算法的人脸检索
- 单片机 电子钟 设计报告/课程设计
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈