黑马程序员—银行管理系统

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ---------

模拟实现银行业务调度系统逻辑,具体需求如下:

 

Ø         银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。

 

Ø         有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。

 

Ø         异步随机生成各种类型的客户,生成各类型用户的概率比例为:

 

        VIP客户 :普通客户 :快速客户  =  1 :6 :3。

 

Ø         客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。

 

Ø         各类型客户在其对应窗口按顺序依次办理业务。

 

Ø         当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。

 

Ø         随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。

 

Ø         不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。

 启动类

[java]  view plain copy
  1. package heima.com.bankcallsystem;  
  2.   
  3.   
  4. import heima.com.bankcallsystem.business.Test;  
  5.   
  6. public class App {  
  7.     public static void main(String[] args) {  
  8.         Test.start();  
  9.           
  10.     }  
  11. }  


模拟6个窗口和生成用户

[java]  view plain copy
  1. package heima.com.bankcallsystem.business;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.Executors;  
  5. import java.util.concurrent.ScheduledExecutorService;  
  6. import java.util.concurrent.TimeUnit;  
  7.   
  8. import heima.com.bankcallsystem.manage.Constant;  
  9. import heima.com.bankcallsystem.manage.CustomerType;  
  10. import heima.com.bankcallsystem.manage.NumberGeneratorMachine;  
  11.   
  12. public class Test {  
  13.     static ScheduledExecutorService pool=Executors.newScheduledThreadPool(7);  
  14.     public static void start(){  
  15.         generateUser();  
  16.         generateWindow();  
  17.     }  
  18.     private static void generateWindow() {  
  19.         int index=1;  
  20.         for(;index<5;index++){  
  21.             final Window commonWindow=new Window();  
  22.             commonWindow.setCustomerType(CustomerType.common);  
  23.             commonWindow.setWindowType(CustomerType.common);  
  24.             commonWindow.setWindowId(index);  
  25.             pool.scheduleAtFixedRate(  
  26.                     new Runnable(){  
  27.                         @Override  
  28.                         public void run() {  
  29.                             commonWindow.calling();  
  30.                             if(commonWindow.isWork()){  
  31.                                 commonWindow.work();  
  32.                             }  
  33.                         }  
  34.                     },   
  35.                     0,  
  36.                     1,   
  37.                     TimeUnit.SECONDS);  
  38.         }  
  39.           
  40.         final Window fastWindow=new Window();  
  41.         fastWindow.setCustomerType(CustomerType.fast);  
  42.         fastWindow.setWindowType(CustomerType.fast);  
  43.         fastWindow.setWindowId(index++);  
  44.         pool.scheduleAtFixedRate(  
  45.                 new Runnable(){  
  46.                     @Override  
  47.                     public void run() {  
  48.                         fastWindow.calling();  
  49.                         if(fastWindow.isWork()){  
  50.                             fastWindow.work();  
  51.                         }  
  52.                     }  
  53.                 },   
  54.                 0,  
  55.                 1,   
  56.                 TimeUnit.SECONDS);  
  57.           
  58.           
  59.         final Window vipWindow=new Window();  
  60.         vipWindow.setCustomerType(CustomerType.vip);  
  61.         vipWindow.setWindowType(CustomerType.vip);  
  62.         vipWindow.setWindowId(index);  
  63.         pool.scheduleAtFixedRate(  
  64.                 new Runnable(){  
  65.                     @Override  
  66.                     public void run() {  
  67.                         vipWindow.calling();  
  68.                         if(vipWindow.isWork()){  
  69.                             vipWindow.work();  
  70.                         }  
  71.                     }  
  72.                 },   
  73.                 0,  
  74.                 1,   
  75.                 TimeUnit.SECONDS);  
  76.     }  
  77.     public static void generateUser(){  
  78.         System.out.println("客户升请服务机启动。。。。");  
  79.         pool.scheduleAtFixedRate(  
  80.                 new Runnable(){  
  81.                     @Override  
  82.                     public void run() {  
  83.                           
  84.                         Random rd=new Random();  
  85.                         int type=rd.nextInt(10)+1;  
  86.                         if(type==1){  
  87.                             CustomerType customerType=CustomerType.vip;  
  88.                             generateUser(customerType,Constant.VIP_RATIO);  
  89.                         }else if(type>1 && type<5){  
  90.                             CustomerType customerType=CustomerType.fast;  
  91.                             generateUser(customerType,Constant.FAST_RATIO);  
  92.                         }else{  
  93.                             CustomerType customerType=CustomerType.common;  
  94.                             generateUser(customerType,Constant.COMMON_RATIO);  
  95.                         }  
  96.                     }  
  97.   
  98.                     private void generateUser(CustomerType customerType,long typeRatio) {  
  99.                         try {  
  100.                             Thread.currentThread().sleep(Constant.GENERATE_USER_INTERVAL_TIME*typeRatio);  
  101.                         } catch (InterruptedException e) {  
  102.                             // TODO Auto-generated catch block  
  103.                             e.printStackTrace();  
  104.                         }  
  105.                         NumberGeneratorMachine numberMachine=NumberGeneratorMachine.getInstance();  
  106.                         numberMachine.setCustomerType(customerType);  
  107.                         System.out.println(numberMachine.generateNumberManage().generateServiceNumber()+"号"+customerType+"客户升请服务");  
  108.                     }  
  109.                 },  
  110.                 0,  
  111.                 1,  
  112.                 TimeUnit.MILLISECONDS);  
  113.     }  
  114. }  


银行的呼叫方法

[java]  view plain copy
  1. package heima.com.bankcallsystem.business;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.Executors;  
  5.   
  6. import heima.com.bankcallsystem.manage.Constant;  
  7. import heima.com.bankcallsystem.manage.CustomerType;  
  8. import heima.com.bankcallsystem.manage.NumberGeneratorMachine;  
  9. import heima.com.bankcallsystem.manage.NumberManage;  
  10.   
  11. public class Window {  
  12.     private CustomerType customerType=CustomerType.common;  
  13.     private CustomerType windowType=CustomerType.common;  
  14.     private int serviceNumber;  
  15.     private int windowId=1;  
  16.     private boolean isWork=true;  
  17.       
  18.     public boolean isWork(){  
  19.         return isWork;  
  20.     }  
  21.       
  22.     public void setWindowId(int windowId) {  
  23.         this.windowId = windowId;  
  24.         System.out.println(this.windowId+"号"+customerType+"窗口打开");  
  25.     }  
  26.     public void setWindowType(CustomerType windowType) {  
  27.         this.windowType = windowType;  
  28.     }  
  29.     public void setCustomerType(CustomerType customerType) {  
  30.         this.customerType = customerType;  
  31.     }  
  32.     public void calling(){  
  33.         NumberGeneratorMachine numberMachine= NumberGeneratorMachine.getInstance();  
  34.         numberMachine.setCustomerType(customerType);  
  35.         NumberManage numberManage=numberMachine.generateNumberManage();  
  36.         numberManage.setWaitFlag(true);  
  37.         if(customerType==CustomerType.common && windowType!=CustomerType.common){  
  38.             numberManage.setWaitFlag(false);  
  39.         }  
  40.         System.out.println(windowId+"号"+windowType+"窗口开始呼叫"+customerType+"用户");  
  41.         serviceNumber=numberManage.fetchServiceNumber();  
  42.         //如果没有vip和快速 为普通用户服务  
  43.         if(serviceNumber==-1){  
  44.             if(customerType==CustomerType.common){  
  45.                 try {   
  46.                     System.out.println(windowId+"号"+windowType+"窗口没叫到普通顾客休息3秒钟 ");  
  47.                     Thread.currentThread().sleep(3*1000);  
  48.                 } catch (InterruptedException e) {  
  49.                     e.printStackTrace();  
  50.                 }  
  51.                 isWork=false;  
  52.                 return;  
  53.             }  
  54.             this.customerType=CustomerType.common;  
  55.             calling();  
  56.             //vip fast窗口为普通用户服务完在服务本类客户  
  57.             customerType=windowType;  
  58.             return;  
  59.         }  
  60.         System.out.println(serviceNumber+"号"+customerType+"客户请到"+windowId+"号"+windowType+"窗口");  
  61.         System.out.println(windowId+"号"+windowType+"窗口呼叫完毕");  
  62.           
  63.          //初始化调用work逻辑  
  64.         isWork=true;  
  65.     }  
  66.     public void work() {  
  67.         System.out.println(windowId+"号"+windowType+"窗口正在为"+serviceNumber+"号"+customerType+"客户服务");  
  68.         long beginTime=System.currentTimeMillis();  
  69.         try {  
  70.             Thread.currentThread().sleep(new Random().nextInt(Constant.WORK_MAX_TIME-Constant.WORK_MIN_TIME+1)+Constant.WORK_MIN_TIME);  
  71.               
  72.         } catch (InterruptedException e) {  
  73.             // TODO Auto-generated catch block  
  74.             e.printStackTrace();  
  75.         }  
  76.         long endTime=System.currentTimeMillis();  
  77.         System.out.println(windowId+"号"+windowType+"窗口为"+serviceNumber+"号"+customerType+"客户服务完成 耗时"+(endTime-beginTime)/1000l+"s");  
  78.           
  79.     }  
  80.       
  81. }  


常量

[java]  view plain copy
  1. package heima.com.bankcallsystem.manage;  
  2.   
  3. public class Constant {  
  4.     //办理业务最短时间1S  
  5.     public final static int WORK_MIN_TIME=1*1000;  
  6.     //办理业务最长时间10S  
  7.     public final static int WORK_MAX_TIME=10*1000;  
  8.     //模拟生成用户的间隔时间  
  9.     public final static int GENERATE_USER_INTERVAL_TIME=1*1000;  
  10.     //不通类型的比例 1:3:6  
  11.     public final static int COMMON_RATIO=1;  
  12.     public final static int FAST_RATIO=3;  
  13.     public final static int VIP_RATIO=6;  
  14. }  


类型

[java]  view plain copy
  1. package heima.com.bankcallsystem.manage;  
  2.   
  3. public enum CustomerType {  
  4.     common("普通"),fast("快速"),vip("vip");  
  5.     private String alias;  
  6.     private CustomerType(String alias){  
  7.         this.alias=alias;  
  8.     }  
  9.     @Override  
  10.     public String toString(){  
  11.         return this.alias;  
  12.     }  
  13. }  


服务号码请求机

[java]  view plain copy
  1. package heima.com.bankcallsystem.manage;  
  2.   
  3. public class NumberGeneratorMachine {  
  4.     private static NumberGeneratorMachine instance=null;  
  5.     private NumberManage commonNumberManage=null;  
  6.     private NumberManage fastNumberManage=null;  
  7.     private NumberManage vipNumberManage=null;  
  8.     private CustomerType customerType=CustomerType.common;  
  9.       
  10.       
  11.     public void setCustomerType(CustomerType customerType) {  
  12.         this.customerType = customerType;  
  13.     }  
  14.   
  15.     public NumberManage generateNumberManage(){  
  16.         NumberManage numberManage=null;  
  17.         switch(customerType){  
  18.             case common:  
  19.                 numberManage=commonNumberManage;  
  20.                 break;  
  21.             case fast:  
  22.                 numberManage=fastNumberManage;  
  23.                 break;  
  24.             case vip:  
  25.                 numberManage=vipNumberManage;  
  26.                 break;  
  27.         }  
  28.         return numberManage;  
  29.     }  
  30.       
  31.     private NumberGeneratorMachine(){  
  32.         commonNumberManage=new NumberManage(CustomerType.common);  
  33.         fastNumberManage=new NumberManage(CustomerType.fast);  
  34.         vipNumberManage=new NumberManage(CustomerType.vip);  
  35.     }  
  36.       
  37.       
  38.     public static NumberGeneratorMachine getInstance(){  
  39.         if(instance==null){  
  40.             synchronized(NumberGeneratorMachine.class){  
  41.                 if(instance==null){  
  42.                     instance=new NumberGeneratorMachine();  
  43.                 }  
  44.             }  
  45.         }  
  46.         return instance;  
  47.     }  
  48. }  


号码管理---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

[java]  view plain copy
  1. package heima.com.bankcallsystem.manage;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class NumberManage {  
  7.     private int serviceNumber;  
  8.     private List<Integer> serviceNumbers;  
  9.     private CustomerType customerType;  
  10.     private boolean isWaitFlag=true;  
  11.     public void setWaitFlag(boolean isWaitFlag) {  
  12.         this.isWaitFlag = isWaitFlag;  
  13.     }  
  14.     NumberManage(CustomerType customerType){  
  15.         serviceNumbers=new ArrayList<Integer>();  
  16.         this.customerType=customerType;  
  17.     }  
  18.     public synchronized int generateServiceNumber(){  
  19.         serviceNumbers.add(++serviceNumber);  
  20.         if(customerType==CustomerType.common){  
  21.             notify();  
  22.         }  
  23.         return serviceNumber;  
  24.     }  
  25.     public synchronized int fetchServiceNumber(){  
  26.         int result=-1;  
  27.         if(serviceNumbers.isEmpty()){  
  28.             if(customerType==CustomerType.common && isWaitFlag){  
  29.                 try {  
  30.                     wait();  
  31.                 } catch (InterruptedException e) {  
  32.                     e.printStackTrace();  
  33.                 }  
  34.             }  
  35.         }  
  36.         if(!serviceNumbers.isEmpty()){  
  37.             result=serviceNumbers.remove(0);  
  38.         }  
  39.           
  40.         return result;  
  41.     }  
  42. }  
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ---------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值