黑马程序员技术交流社区
标题:
优化银行业务调动管理系统
[打印本页]
作者:
何苦似梦离
时间:
2014-2-23 14:10
标题:
优化银行业务调动管理系统
其他部分代码都是一样的,唯独这个窗口服务这个类的代码改了一下,我把三个服务函数,集中成了一个,提高了可复用性,下面看代码
package cn.apple.bank;
import java.util.Random;
import java.util.concurrent.Executors;
public class WindowsServices
{
//窗口类型
private WindowType windowtype = WindowType.COMMON;
//服务类型
private ServiceType servicetype = ServiceType.COMMON;
private int windowID;
WindowsServices(int windowID,WindowType windowtype,ServiceType servicetype)
{
this.windowID = windowID;
this.windowtype = windowtype;
this.servicetype = servicetype;
}
WindowsServices(int id)
{
this.windowID = id;
}
public void start()
{
Executors.newSingleThreadExecutor().execute(
new Runnable()
{
@Override
public void run()
{
while(true)
{
Service();
}
}
});
}
private void Service()
{
System.out.println(windowID + "号" + windowtype + "窗口正在获取" + servicetype + "任务....");
Integer number = null;
//如果服务类型是什么,就获取什么样的号码
switch (servicetype)
{
case COMMON:
number = NumberMachine.getInstance().getCommonManager().fetchNumber();
break;
case EXPRESS:
number = NumberMachine.getInstance().getExpressManager().fetchNumber();
break;
case VIP:
number = NumberMachine.getInstance().getVipManager().fetchNumber();
break;
}
//如果获取成功
if(number != null)
{
System.out.println(windowID + "号" + windowtype + "窗口正在给"+number+"号"+servicetype+"客服服务");
long beginTime = System.currentTimeMillis();
int maxRandom = 0;
switch (windowtype)
{
case COMMON:
maxRandom = ConstantsTime.MAX_SERVER_TIME - ConstantsTime.MIN_SERVER_TIME;
break;
case EXPRESS:
maxRandom = ConstantsTime.MAX_SERVER_TIME ;
break;
case VIP:
maxRandom = ConstantsTime.MAX_SERVER_TIME - ConstantsTime.MIN_SERVER_TIME;
break;
}
int serviceTime = new Random().nextInt(maxRandom)+1 + ConstantsTime.MIN_SERVER_TIME;
try
{
Thread.sleep(serviceTime);
long endTime = System.currentTimeMillis();
System.out.println(windowID + "号" + windowtype + "窗口正在给"+number+"号"+servicetype+
"客服服务用了:"+(endTime - beginTime) / 1000 +"秒");
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
//获取任务失败,我们马上判断窗口,和服务类型,如果是VIP窗口服务的是VIP,那么马上开始获取普通任务
//否则就休息一秒
if(servicetype == ServiceType.EXPRESS || servicetype == servicetype.VIP)
{
System.out.println(windowID + "号" + windowtype + "窗口没有获取到"+servicetype+"任务");
}
else
{
System.out.println(windowID + "号" + windowtype + "窗口没有获取到"+servicetype+"任务休息1秒");
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//如果服务窗口是VIP或者快速,服务类型是普通,那么改回所属服务,不然就让VIP,或者快速窗口去服务普通窗口
if(windowtype == WindowType.VIP && servicetype == ServiceType.COMMON)
{
servicetype = ServiceType.VIP;
}
else if(windowtype == WindowType.EXPRESS && servicetype == ServiceType.COMMON)
{
servicetype = ServiceType.EXPRESS;
}
else
{
switch (windowtype)
{
case EXPRESS:
servicetype = servicetype.COMMON;
Service();
break;
case VIP:
servicetype = servicetype.COMMON;
Service();
break;
}
}
}
}
}
复制代码
欢迎高手指出不足
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2