------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
项目需求
1:模拟实现银行业务调度系统逻辑,具体需求如下
1)银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
2)有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
3)异步随机生成各种类型的客户,生成各类型用户的概率比例为:
VIP客户 :普通客户 :快速客户 = 1 :6 :3。
4)客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,
快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)
5)各类型客户在其对应窗口按顺序依次办理业务。
6)VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,
而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
7)随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
8)不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
2:面向对象的程序分析和设计
1)有三种对应类型的客户:VIP客户,普通客户,快速客户,异步随机生成各种类型的客户,各类型客户在其对应窗口按顺序依次办理业务。
(1)要有一个号码管理器对象,让这个对象不断地产生号码,就等于随机生成了客户。
(2)由于有三类客户,每类客户的号码编排都是完全独立的,所以,我想到本系统一共要产生三个号码管理器对象,各自管理一类用户的排队号码。
这三个号码管理器对象统一由一个号码机器进行管理,这个号码机器在整个系统中始终只能有一个,所以,它要被设计成单例。
2)各类型客户在其对应窗口按顺序依次办理业务,准确地说,应该是窗口依次叫号。
(1)各个窗口怎么知道该叫哪一个号了呢?它一定是问的相应的号码管理器,即服务窗口每次找号码管理器获取当前要被服务的号码。
如图所示:
代码实现
1:NumberManager和NumberMachine类
1)NumberManager类
(1)定义一个用于存储上一个客户号码的成员变量和用于存储所有等待服务的客户号码的队列集合。
(2)定义一个产生新号码的方法和获取马上要为之服务的号码的方法,这两个方法被不同的线程操作了相同的数据,所以,要进行同步。
package cn.itcast.study;
import java.util.ArrayList;
import java.util.List;
public class NumberManager {
private int lastNumber=1;//初始化变量,生活中数字从1开始
private List<Integer> queueNumber=new ArrayList<Integer>();
public synchronized Integer generateNewManager(){//返回上一个号码并自增
queueNumber.add(lastNumber);
return lastNumber++;
}
public synchronized Integer fetchServiceNumber(){//取号
Integer number=null;
if(queueNumber.size()>0){
number=queueNumber.remove(0);//若取到null,不能转为int类型,会报异常
}
return number;
}
}
2)ServiceWindow类
(1)定义一个start方法,内部启动一个线程,根据服务窗口的类别分别循环调用三个不同的方法。
(2)定义三个方法分别对三种客户进行服务,为了观察运行效果,应详细打印出其中的细节信息。
package cn.itcast.study;
import java.util.Random;
import java.util.concurrent.Executors;
public class ServiceWindow {
private CustomerType type=CustomerType.COMMON;//指定类型
private int windowId=1;//窗口号
public void setType(CustomerType type) {//设置服务窗口类型
this.type = type;
}
public void setWindowId(int windowId) {//服务窗口编号
this.windowId = windowId;
}
public void start(){
Executors.newSingleThreadExecutor().execute(new Runnable(){//创建线程池运行
public void run(){
while(true){
switch(type){
case COMMON:
commonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
});
}
private void commonService() {
String windowName="第 "+ windowId +"号 "+type+" 窗口";
Integer number=NumberMachine.getInstance().getCommonManager().fetchServiceNumber();//获取号码
System.out.println(windowName+" 正在获取任务");
if(number!=null){
System.out.println(windowName+" 为第"+number+" 个"+ "普通"+" 客户服务 ");
long beginTime=System.currentTimeMillis();//开始时间
int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serviceTime=new Random().nextInt(maxRandom)+1+Constants.MIN_SERVICE_TIME;//1000~10000
try {
Thread.sleep(serviceTime);//模拟服务时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;//花费时间
System.out.println(windowName+" 为第"+number+" 个"+ "普通"+" 客户完成服务,耗时 "+costTime/1000+" 秒");
}
else{
System.out.println("没有取到服务任务,休息一秒 ");
try {
Thread.sleep(1000);//模拟服务时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void expressService() {
String windowName="第 "+ windowId +"号 "+type+" 窗口";
Integer number=NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
System.out.println(windowName+" 正在获取任务");
if(number!=null){
System.out.println(windowName+" 为第"+number+" 个"+ type+" 客户服务 ");
long beginTime=System.currentTimeMillis();//开始时间
int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serviceTime=new Random().nextInt(maxRandom)+1+Constants.MIN_SERVICE_TIME;//1000~10000
try {
Thread.sleep(Constants.MIN_SERVICE_TIME);//模拟服务时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;//花费时间
System.out.println(windowName+" 为第"+number+" 个"+ type+" 客户完成服务,耗时 "+costTime/1000+" 秒");
}
else{
System.out.println(windowName+"没有取到服务任务 ");
commonService();
}
}
private void vipService() {
String windowName="第 "+ windowId +"号 "+type+" 窗口";
Integer number=NumberMachine.getInstance().getVipManager().fetchServiceNumber();
System.out.println(windowName+" 正在获取任务");
if(number!=null){
System.out.println(windowName+" 为第"+number+" 个"+ type+" 客户服务 ");
long beginTime=System.currentTimeMillis();//开始时间
int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serviceTime=new Random().nextInt(maxRandom)+1+Constants.MIN_SERVICE_TIME;//1000~10000
try {
Thread.sleep(serviceTime);//模拟服务时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;//花费时间
System.out.println(windowName+" 为第"+number+" 个"+ type+" 客户完成服务,耗时 "+costTime/1000+" 秒");
}
else{
System.out.println(windowName+"没有取到服务任务 ");
commonService();
}
}
3、MainClass类与Constants类
1)MainClass类
(1)用for循环创建出4个普通窗口,再创建出1个快速窗口和一个VIP窗口。
(2)接着再创建三个定时器,分别定时去创建新的普通客户号码、新的快速客户号码、新的VIP客户号码。
package cn.itcast.study;
import java.util.Random;
import java.util.concurrent.Executors;
public class ServiceWindow {
private CustomerType type=CustomerType.COMMON;//指定类型
private int windowId=1;//窗口号
public void setType(CustomerType type) {//设置服务窗口类型
this.type = type;
}
public void setWindowId(int windowId) {//服务窗口编号
this.windowId = windowId;
}
public void start(){
Executors.newSingleThreadExecutor().execute(new Runnable(){//创建线程池运行
public void run(){
while(true){
switch(type){
case COMMON:
commonService();
break;
case EXPRESS:
expressService();
break;
case VIP:
vipService();
break;
}
}
}
});
}
private void commonService() {
String windowName="第 "+ windowId +"号 "+type+" 窗口";
Integer number=NumberMachine.getInstance().getCommonManager().fetchServiceNumber();//获取号码
System.out.println(windowName+" 正在获取任务");
if(number!=null){
System.out.println(windowName+" 为第"+number+" 个"+ "普通"+" 客户服务 ");
long beginTime=System.currentTimeMillis();//开始时间
int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serviceTime=new Random().nextInt(maxRandom)+1+Constants.MIN_SERVICE_TIME;//1000~10000
try {
Thread.sleep(serviceTime);//模拟服务时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;//花费时间
System.out.println(windowName+" 为第"+number+" 个"+ "普通"+" 客户完成服务,耗时 "+costTime/1000+" 秒");
}
else{
System.out.println("没有取到服务任务,休息一秒 ");
try {
Thread.sleep(1000);//模拟服务时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void expressService() {
String windowName="第 "+ windowId +"号 "+type+" 窗口";
Integer number=NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
System.out.println(windowName+" 正在获取任务");
if(number!=null){
System.out.println(windowName+" 为第"+number+" 个"+ type+" 客户服务 ");
long beginTime=System.currentTimeMillis();//开始时间
int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serviceTime=new Random().nextInt(maxRandom)+1+Constants.MIN_SERVICE_TIME;//1000~10000
try {
Thread.sleep(Constants.MIN_SERVICE_TIME);//模拟服务时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;//花费时间
System.out.println(windowName+" 为第"+number+" 个"+ type+" 客户完成服务,耗时 "+costTime/1000+" 秒");
}
else{
System.out.println(windowName+"没有取到服务任务 ");
commonService();
}
}
private void vipService() {
String windowName="第 "+ windowId +"号 "+type+" 窗口";
Integer number=NumberMachine.getInstance().getVipManager().fetchServiceNumber();
System.out.println(windowName+" 正在获取任务");
if(number!=null){
System.out.println(windowName+" 为第"+number+" 个"+ type+" 客户服务 ");
long beginTime=System.currentTimeMillis();//开始时间
int maxRandom=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serviceTime=new Random().nextInt(maxRandom)+1+Constants.MIN_SERVICE_TIME;//1000~10000
try {
Thread.sleep(serviceTime);//模拟服务时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long costTime=System.currentTimeMillis()-beginTime;//花费时间
System.out.println(windowName+" 为第"+number+" 个"+ type+" 客户完成服务,耗时 "+costTime/1000+" 秒");
}
else{
System.out.println(windowName+"没有取到服务任务 ");
commonService();
}
}
2)Constants类
定义三个常量:MAX_SERVICE_TIME、MIN_SERVICE_TIME、COMMON_CUSTOMER_INTERVAL_TIME
public class Constants {
public static int MAX_SERVICE_TIME=10000;//最大服务时间10秒
public static int MIN_SERVICE_TIME=1000;//最小服务时间1秒
public static int COMMON_CUSTOMER_INTERVAL_TIME=1;
}
输出结果: