您的位置:首页 > 职场人生

黑马程序员---银行业务调度系统

2014-03-05 09:56 363 查看
----------------------
ASP.Net+Unity开发、.Net培训、期待与您交流! --------------------

指出张孝祥老师的一个小错误,窗口办理业务的时间,普通窗口和VIP窗口的时间都是 取最大值与最小值之间的数值,我想应该也包括了最大值或最小值,张老师的是:new Random.nextInt(max-min)+1+min ,我觉得稍微的改下,应该为:new Random.nextInt((max-min)+1)+min

这样的话,最大值和最小值都包括进来了。张老师的只有最大值,最小的随机值也是最小值加1.

 




张孝祥老师视频中说到ServiceWindow可以将普通窗口设为基类,VIP窗口和快速窗口为普通窗口的子类。接下上的代码就是这样的。当然代码也没进行优化简化

普通窗口:

public class CommonWindow {
private WindowType type = WindowType.COMMON;
private int windowId = 1; // 窗口号

//当普通窗口忙不过来时设置为普通窗口
public void setWindowId(int windowId) {
this.windowId = windowId;
}

public void startWork() {
// 创建线程 java1.5技术,推荐
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
commonService(type);
}
}

});
}

protected void commonService(WindowType type) {
String windowName = "第" + windowId + "号" + type + "窗口";
Integer number = NumberMachine.newInstance().getCommonNumManager()
.windGetNumber();// 获取普通号码
System.out.println(windowName + "正在获取业务");
if (number != null) {
System.out.println(windowName + "为第" + number + "个" + "普通"
+ "客户服务");
long beginTime = System.currentTimeMillis();
int maxran = Constants.MAX_SERVICE_TIME
- Constants.MIN_SERVICE_TIME;
long serveTime = new Random().nextInt(maxran+1)
+ Constants.MIN_SERVICE_TIME;// 办理时间																try {
// 线程休眠表示业务办理时间
Thread.sleep(serveTime);
} catch (InterruptedException e) {
e.printStackTrace();
}

long costTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个" + "普通"
+ "客户完成服务,耗时:" + costTime / 1000);
} else {
System.out.println(windowName + "没有业务办理,休息1s ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

 

快速窗口:

public class ExpressWindow extends CommonWindow{
private WindowType type = WindowType.EXPRESS;//窗口类型
private int windowId = 1; // 窗口号
private boolean toCommon;

//当普通窗口忙不过来时设置为普通窗口
public void setToCommon(boolean b){
this.toCommon=b;
}
public void setWindowId(int windowId) {
this.windowId = windowId;
}
public void startWork() {
// 创建线程 java1.5技术,推荐
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
if(!toCommon){
expressService();
}else{
commonService(type);
}
}
}
});
}

private void expressService() {
String windowName = "第" + windowId + "号" + type + "窗口";

Integer number = NumberMachine.newInstance().getExpressNumManager()
.windGetNumber();// 获取快速号码
System.out.println(windowName + "正在获取业务");

// 获得号码
if (number != null) {
System.out.println(windowName + "为第" + number + "个" + type
+ "客户服务");
long beginTime = System.currentTimeMillis();

long serveTime = Constants.MIN_SERVICE_TIME;// 办理时间 最小值

try {
// 线程休眠表示业务办理时间
Thread.sleep(serveTime);
} catch (InterruptedException e) {
e.printStackTrace();
}

long costTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个" + type
+ "客户完成服务,耗时:" + costTime / 1000);
} else {
// 没有快速客户时,为普通客户服务
System.out.println(windowName + "没有业务办理! ");

commonService(type);

}
}
}


VIP窗口:

public class VIPWindow extends CommonWindow {
private WindowType type = WindowType.VIP;
private int windowId = 1; // 窗口号

private boolean toCommon;

// 设置窗口为普通窗口
public void setToCommon() {
this.toCommon = true;
}

public void setWindowId(int windowId) {
this.windowId = windowId;
}

public void startWork() {
// 创建线程 java1.5技术,推荐
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
while (true) {
if (!toCommon) {
VIPService();
} else {
commonService(type);
}

}
}
});
}

private void VIPService() {
String windowName = "第" + windowId + "号" + type + "窗口";
Integer number = NumberMachine.newInstance().getVIPNumManager()
.windGetNumber();// 获取快速号码
System.out.println(windowName + "正在获取业务");
// 获得号码
if (number != null) {
System.out
.println(windowName + "为第" + number + "个" + type + "客户服务");
long beginTime = System.currentTimeMillis();

int maxran = Constants.MAX_SERVICE_TIME
- Constants.MIN_SERVICE_TIME;
long serveTime = new Random().nextInt(maxran + 1)
+ Constants.MIN_SERVICE_TIME;// 办理时间跟普通一样

try {
// 线程休眠表示业务办理时间
Thread.sleep(serveTime);
} catch (InterruptedException e) {
e.printStackTrace();
}

long costTime = System.currentTimeMillis() - beginTime;
System.out.println(windowName + "为第" + number + "个" + type
+ "客户完成服务,耗时:" + costTime / 1000);
} else {
// 没有获得号码
System.out.println(windowName + "没有业务办理! ");
commonService(type);

}
}

}


mainTest:

public class MainTest {

public static void main(String[] args) {

for (int i =1; i < 5; i++) {
CommonWindow common=new CommonWindow();
common.setWindowId(i);
common.startWork();
}

VIPWindow vip=new VIPWindow();
vip.setWindowId(1);
vip.startWork();

ExpressWindow express=new ExpressWindow();
express.setWindowId(1);
express.setToCommon(true);
express.startWork();
//普通客户取号(在本案例中,客户都是由号码机产生)
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Integer num=NumberMachine.newInstance().getCommonNumManager().getNumber();
System.out.println("第"+num+"号普通客户已取号,正待办理业务");
}

}, 0, 1, TimeUnit.SECONDS);
//快速客户取号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Integer num=NumberMachine.newInstance().getExpressNumManager().getNumber();
System.out.println("第"+num+"号快速客户已取号,正待办理业务");
}

}, 0, 2, TimeUnit.SECONDS);
//VIP客户取号
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
Integer num=NumberMachine.newInstance().getVIPNumManager().getNumber();
System.out.println("第"+num+"号VIP客户已取号,正待办理业务");
}

}, 0, 6, TimeUnit.SECONDS);																			}


 

----------------------
ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: