zoukankan      html  css  js  c++  java
  • 也做《7K面试题银行业务调度系统实现》

    在网上不小心看到《7K面试题银行业务调度系统实现》,想想反正闲的蛋疼,就练练手吧。

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

    • 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
    • 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
    • 异步随机生成各种类型的客户,生成各类型用户的概率比例为:

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

    • 客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
    • 各类型客户在其对应窗口按顺序依次办理业务。
    • 当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
    • 随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
    • 不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。

    做之前我没有看人家的实现,做完之后看了,感触还是挺多哈。人家的实现:http://blog.csdn.net/zhangxiaoxiang/article/details/6294132

    我先说下我的思路:

    窗口类是独立的(Windows),客户是独立的(Customer),然后有一个维护客户队列的类(CustomerQueue),测试类(Main),就这些了。代码如下:

    package com.kawin.bank;
    
    import java.util.Random;
    
    /**
     * 办理业务窗口类
     * @author Kawin
     *
     */
    public class Windows implements Runnable{
    
        public final static int fastestTracationTime = 3000;//最快办理时间毫秒
        public final static int longTracationTime = 20000;//最久办理时间毫秒
        private final static int noCustomerWaitTime = 5000;//没有人办理业务时,线程休眠时间
    
        public final static String vipWindow = "VIP";
        public final static String commonWindow = "普通";
        public final static String speedWindow = "快速";
        private String type = "普通";//窗口类型 1普通窗口,2快速窗口,3vip窗口
        private String name ;
        
        public Windows(String type, String name){
            this.type = type;
            this.name = name;
        }
        
        @Override
        public void run() {
            try {
                while(true){
                    startTransaction();
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        /**
         * 办理普通业务
         * @throws InterruptedException 
         */
        private Customer transactionCommonBiz() throws InterruptedException{
            if(CustomerQueue.commonQueue.size() < 1 )
                return null;
            
            Customer cs = CustomerQueue.commonQueue.take();
            long rm = randomTime(longTracationTime);
            System.out.println(name+"开始办理->"+cs.getNumber()+"的"+cs.getType()+" 业务...需:"+rm/1000+" 秒");
            Thread.sleep(rm);
            return cs;
        }
        
        /**
         * 办理快速业务
         * @throws InterruptedException 
         */
        private Customer transactionSpeedBiz() throws InterruptedException{
            //如果没有快速客户,则办理普通客户
            if(CustomerQueue.speedQueue.size() < 1){
                System.out.println(name+"无业务,转办理普通业务-->");
                return transactionCommonBiz();
            }else{
                Customer cs =  CustomerQueue.speedQueue.take();
                System.out.println(name+"开始办理->"+cs.getNumber()+"的"+cs.getType()+" 业务...需:"+fastestTracationTime/1000+" 秒");
                Thread.sleep(fastestTracationTime);
                return cs;
            }
        }
        
        /**
         * 办理Vip业务
         * @throws InterruptedException 
         */
        private Customer transactionVipBiz() throws InterruptedException{
            //如果没有vip客户,则办理普通客户
            if(CustomerQueue.vipQueue.size() < 1){
                System.out.println(name+"无业务,转办理普通业务-->");
                return transactionCommonBiz();
            }else{
                Customer cs = CustomerQueue.vipQueue.take();
                long rm = randomTime(longTracationTime);
                System.out.println(name+"开始办理->"+cs.getNumber()+"的"+cs.getType()+" 业务...需:"+rm/1000+" 秒");
                Thread.sleep(rm);
                return cs;
            }
        }
        
        /**
         * 开始办理业务
         * @param args
         * @throws InterruptedException
         */
        private void startTransaction() throws InterruptedException{
            Customer cs = null;
            if(commonWindow.equals(type)){//普通窗口
                cs = transactionCommonBiz();
            }else if(speedWindow.equals(type)){//快速窗口
                cs = transactionSpeedBiz();
            }else{//vip窗口
                cs = transactionVipBiz();
            }
            if(null == cs) {
                System.out.println(name+"无业务,"+noCustomerWaitTime/1000+" 秒后再呼叫!");
                Thread.sleep(noCustomerWaitTime);
                return;
            }
            System.out.println(cs.getNumber()+"--"+cs.getType()+" 业务办理完成!");
        }
         
        /**
         * 随机产生办理时间
         * @return
         */
        private long randomTime(int rm){
            return new Random().nextInt(rm);
        }
    }
    package com.kawin.bank;
    
    import java.util.Random;
    
    /**
     * 客户的父类
     * @author Kawin
     *
     */
    public class Customer{
        
        //客户类型
        public final static String vip = "<VIP>";
        public final static String common = "【普通】";
        public final static String speed = "[快速]";
        
        private String number;//序列号
        private String type ;
        
        /**
         * 按比例生成客户方法
         * VIP客户 :普通客户 :快速客户  =  1 :6 :3。
         */
        public Customer creatCustomer(){
            String customerName = null;
            
            Random rm = new Random();
            int cs = rm.nextInt(10);
            if(cs < 1){//vip客户
                customerName = vip;
            }else if(cs < 4){
                customerName = speed;
            }else{
                customerName = common;
            }
            this.setType(customerName);
            return this;
        }
    
        
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        
        public static void main(String[] args) {
            Customer c = new Customer();
            for (int i = 0; i < 1000000; i++) {
                c.creatCustomer();
            }
            System.out.println(vip);
            System.out.println(common);
            System.out.println(speed);
        }
        
    }
    package com.kawin.bank;
    
    import java.util.Random;
    import java.util.concurrent.LinkedBlockingQueue;
    
    /**
     * 客户队列
     * 负责创建客户及维护客户
     * @author Kawin
     *
     */
    public class CustomerQueue implements Runnable{
    
        public final static int lastComeTime = 5000;//最大多少毫秒将出现客户
        public static LinkedBlockingQueue<Customer> vipQueue = new LinkedBlockingQueue<Customer>();//vip队列
        public static LinkedBlockingQueue<Customer> commonQueue = new LinkedBlockingQueue<Customer>();//普通队列
        public static LinkedBlockingQueue<Customer> speedQueue = new LinkedBlockingQueue<Customer>();//快速队列
        private static int commonNumber = 1;
        private static int speedNumber = 1;
        private static int vipNumber = 1;
     
        @Override
        public void run() {
            while(true){
                try {
                    long willcome = new Random().nextInt(lastComeTime);
                    //System.out.println(willcome/1000 +" 秒后开始按比例创建客户...");
                    Thread.sleep(willcome);
                    putQueue(new Customer().creatCustomer());//将创建的客户放入队列
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } 
        }
        
        /**
         * 将创建的客户存入不同的队列
         * @param cs
         * @throws InterruptedException
         */
        private void putQueue(Customer cs) throws InterruptedException{
            int curcount = 0;
            if(Customer.common.equals(cs.getType())){//普通客户
                curcount = commonQueue.size() + vipQueue.size() + speedQueue.size();
                cs.setNumber("C"+commonNumber++);
                commonQueue.put(cs);
            }
            else if(Customer.speed.equals(cs.getType())){//快速客户
                curcount = speedQueue.size();
                cs.setNumber("S"+speedNumber++);
                speedQueue.put(cs);
            }
            else{//vip客户
                curcount = vipQueue.size();
                cs.setNumber("V"+vipNumber++);
                vipQueue.put(cs);
            }
            System.out.println(cs.getNumber() +"--"+ cs.getType() +"客户, 您好!"+ "   您前面有【" + curcount + "】客户等待!");
        }
        
    }
    package com.kawin.bank;
    
    /**
     * 
     * @author Kawin
    模拟实现银行业务调度系统逻辑,具体需求如下:
    *银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
    *有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
    *异步随机生成各种类型的客户,生成各类型用户的概率比例为:
    VIP客户 :普通客户 :快速客户  =  1 :6 :3。
    *客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,
    快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
    *各类型客户在其对应窗口按顺序依次办理业务。
    当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,
    而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
    随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
    不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。 
     */
    public class Main {
        public static void main(String[] args) {
            //模拟普通窗口
            Thread  commonWindow1 = new Thread(new Windows(Windows.commonWindow,"【普通窗口1】"));
            commonWindow1.start();
            Thread  commonWindow2 = new Thread(new Windows(Windows.commonWindow,"【普通窗口2】"));
            commonWindow2.start();
    //        Thread  commonWindow3 = new Thread(new Windows(Windows.commonWindow,"【普通窗口3】"));
    //        commonWindow3.start();
    //        Thread  commonWindow4 = new Thread(new Windows(Windows.commonWindow,"【普通窗口4】"));
    //        commonWindow4.start();
            //模拟快速窗口
            Thread  speedWindow1 = new Thread(new Windows(Windows.speedWindow,"【快速窗口1】"));
            speedWindow1.start();
            //模拟VIP窗口
            Thread  vipWindow1 = new Thread(new Windows(Windows.vipWindow,"【VIP窗口1】"));
            vipWindow1.start();
            
            //模拟客户,根据需要启用线程
            Thread creatCustomerThread1 = new Thread(new CustomerQueue());
            creatCustomerThread1.start();
            Thread creatCustomerThread2 = new Thread(new CustomerQueue());
            creatCustomerThread2.start();
        }
    }

    运行结果:

    【普通窗口1】无业务,5 秒后再呼叫!
    【普通窗口2】无业务,5 秒后再呼叫!
    【VIP窗口1】无业务,转办理普通业务-->
    【VIP窗口1】无业务,5 秒后再呼叫!
    【快速窗口1】无业务,转办理普通业务-->
    【快速窗口1】无业务,5 秒后再呼叫!
    C1--【普通】客户, 您好!   您前面有【0】客户等待!
    C2--【普通】客户, 您好!   您前面有【1】客户等待!
    C3--【普通】客户, 您好!   您前面有【2】客户等待!
    C4--【普通】客户, 您好!   您前面有【3】客户等待!
    【普通窗口1】开始办理->C1的【普通】 业务...需:13 秒
    【普通窗口2】开始办理->C2的【普通】 业务...需:5 秒
    【快速窗口1】无业务,转办理普通业务-->
    【快速窗口1】开始办理->C3的【普通】 业务...需:9 秒
    【VIP窗口1】无业务,转办理普通业务-->
    【VIP窗口1】开始办理->C4的【普通】 业务...需:12 秒
    S1--[快速]客户, 您好!   您前面有【0】客户等待!
    V1--<VIP>客户, 您好!   您前面有【0】客户等待!
    C5--【普通】客户, 您好!   您前面有【2】客户等待!
    S2--[快速]客户, 您好!   您前面有【1】客户等待!
    C2--【普通】 业务办理完成!
    【普通窗口2】开始办理->C5的【普通】 业务...需:2 秒
    C6--【普通】客户, 您好!   您前面有【3】客户等待!
    C5--【普通】 业务办理完成!
    【普通窗口2】开始办理->C6的【普通】 业务...需:18 秒
    C3--【普通】 业务办理完成!
    【快速窗口1】开始办理->S1的[快速] 业务...需:3 秒
    C7--【普通】客户, 您好!   您前面有【2】客户等待!
    C8--【普通】客户, 您好!   您前面有【3】客户等待!
    S1--[快速] 业务办理完成!
    【快速窗口1】开始办理->S2的[快速] 业务...需:3 秒
    C4--【普通】 业务办理完成!
    【VIP窗口1】开始办理->V1的<VIP> 业务...需:7 秒
    S3--[快速]客户, 您好!   您前面有【0】客户等待!
    C1--【普通】 业务办理完成!
    【普通窗口1】开始办理->C7的【普通】 业务...需:1 秒
    C9--【普通】客户, 您好!   您前面有【2】客户等待!
    C10--【普通】客户, 您好!   您前面有【3】客户等待!
    S2--[快速] 业务办理完成!
    【快速窗口1】开始办理->S3的[快速] 业务...需:3 秒
    C7--【普通】 业务办理完成!
    【普通窗口1】开始办理->C8的【普通】 业务...需:19 秒
    V2--<VIP>客户, 您好!   您前面有【0】客户等待!
    C11--【普通】客户, 您好!   您前面有【3】客户等待!
    S4--[快速]客户, 您好!   您前面有【0】客户等待!
    S3--[快速] 业务办理完成!
    【快速窗口1】开始办理->S4的[快速] 业务...需:3 秒
    C12--【普通】客户, 您好!   您前面有【4】客户等待!
    C13--【普通】客户, 您好!   您前面有【5】客户等待!
    S5--[快速]客户, 您好!   您前面有【0】客户等待!
    C14--【普通】客户, 您好!   您前面有【7】客户等待!
    V1--<VIP> 业务办理完成!
    【VIP窗口1】开始办理->V2的<VIP> 业务...需:8 秒
    S6--[快速]客户, 您好!   您前面有【1】客户等待!
    S4--[快速] 业务办理完成!
    【快速窗口1】开始办理->S5的[快速] 业务...需:3 秒
    C15--【普通】客户, 您好!   您前面有【7】客户等待!
    C16--【普通】客户, 您好!   您前面有【8】客户等待!
    S5--[快速] 业务办理完成!
    【快速窗口1】开始办理->S6的[快速] 业务...需:3 秒
    C17--【普通】客户, 您好!   您前面有【8】客户等待!
    C18--【普通】客户, 您好!   您前面有【9】客户等待!
    C6--【普通】 业务办理完成!
    【普通窗口2】开始办理->C9的【普通】 业务...需:10 秒
    S7--[快速]客户, 您好!   您前面有【0】客户等待!
    S6--[快速] 业务办理完成!
    【快速窗口1】开始办理->S7的[快速] 业务...需:3 秒
    C19--【普通】客户, 您好!   您前面有【9】客户等待!
    V2--<VIP> 业务办理完成!
    【VIP窗口1】无业务,转办理普通业务-->
    【VIP窗口1】开始办理->C10的【普通】 业务...需:19 秒
    C20--【普通】客户, 您好!   您前面有【9】客户等待!
    S7--[快速] 业务办理完成!
    【快速窗口1】无业务,转办理普通业务-->
    【快速窗口1】开始办理->C11的【普通】 业务...需:6 秒
    C21--【普通】客户, 您好!   您前面有【9】客户等待!
  • 相关阅读:
    div+css与table布局
    自动刷新网页效果
    Spring框架之Filter应用,filter可以使用spring注入资源
    http://localhost:8080/hohode
    java jacob 操作word 文档,进行写操作,如生成表格,添加 图片(这个不错,可以拿来直接用,非常好)
    java 填充word中的表格
    360抢票
    easyui 时间段校验,开始时间小于结束时间,并且时间间隔不能超过30天
    java操作word示例
    FastStone Capture 注册码 序列号
  • 原文地址:https://www.cnblogs.com/kawinzhao/p/3566398.html
Copyright © 2011-2022 走看看