码迷,mamicode.com
首页 > 编程语言 > 详细

黑马程序员——Java面试题之银行业务调度系统

时间:2014-05-18 00:09:18      阅读:473      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   class   c   java   

                         android培训java培训、期待与您交流!  
对象分析:
1.客户类按照业务分类,客户可以分成三类。
public enum Custommer {
COMMON ,VIP , EXPRESS;
public String toString(){
String name =null ;    
           switch (this ){
           case COMMON :
                   name ="普通" ;
                    break;
           case VIP :
                   name ="VIP" ;
                    break;
           case EXPRESS :
                   name ="快速" ;
                    break;
                    }
           return name;
}
}
2.号码机类。号码机用于产生各种号码管理类。
public class NumberMachine {
private NumberMachine(){}
private static NumberMachine instancenew NumberMachine() ;
public static NumberMachine getInstance (){
           return instance ;
           }
private NumberManager commonManager = new NumberManager ();
private NumberManager expressManager = new NumberManager ();
private NumberManager vipManager = new NumberManager ();
public NumberManager getCommonManager() {
           return commonManager ;
}
public NumberManager getExpressManager() {
           return expressManager ;
}
public NumberManager getVipManager() {
           return vipManager ;
}
}
 
 
3.号码管理类。号码机管理类主要作用存放号码,给外界提供号码信息。
import java. util.*;
public class NumberManager {
private int lastnumber= 0;
private List queueNumbernew ArrayList ();
public synchronized Integer generateNewNumber (){
           queueNumber .add ( lastnumber++ );
           return lastnumber ;
}
public synchronized Integer fetchNumber (){
           if (queueNumber .size ()0 )
           return ( Integer ) queueNumberremove (0 );
           else return null;
}
}
 
4.服务窗口类。为客户提供服务
public class ServiceWindow {
Custommer type= Custommer. COMMON;
public Custommer getType() {
           return type ;
}
public void setType (Custommer type ) {
           thistype = type;
}
public int getNumber () {
           return number ;
}
public void setNumber (int number ) {
           thisnumber = number;
}
int number =1 ;
public void start (){
Executors .newSingleThreadExecutor ()  .execute ( new Runnable (){
 
           public void run () {
                    // TODO Auto-generated method stub
                    while ( true )
                    switch ( type ){
                    case COMMON :
                             commonService ();
                              break;
                    case VIP :
                             vipService ();
                              break;
                    case EXPRESS :
                             expressService ();
                   
                    }
           }
 
}) ;
}
private void commonService () {
           // TODO Auto-generated method stub
          String windowName = "第" + number + "号" + type + "窗口" ;                  
          System . out. println( windowName "开始获取普通任务!" );
          Integer serviceNumber = NumberMachine.getInstance ()getCommonManager (). fetchNumber() ;               
           if (serviceNumber != null ){
                   System . out. println( windowName "开始为第" + serviceNumber "号普通客户服务" )           
                    int maxRandom = Constants. MAX_SERVICE_TIME - Constants. MIN_SERVICE_TIME;
                    int serviceTime = new Random (). nextInt( maxRandom )+ 1 + Constants. MIN_SERVICE_TIME;
 
                    try {
                             Thread .sleep (serviceTime );
                    } catch (InterruptedException e ) {
                             e .printStackTrace ();
                    }         
                   System . out. println( windowName "完成为第" + serviceNumber "号普通客户服务,总共耗时" +serviceTime1000 "秒") ;            
           } else{
                   System . out. println( windowName "没有取到普通任务,正在空闲一秒" )                   
                    try {
                             Thread .sleep (1000 );
                    } catch (InterruptedException e ) {
                             e .printStackTrace ();
                    }                                      
           }
}         
private void vipService () {
           // TODO Auto-generated method stub
          Integer serviceNumber = NumberMachine.getInstance ()getExpressManager (). fetchNumber() ;
          String windowName = "第" + number + "号" + type + "窗口" ;         
          System . out. println( windowName "开始获取快速任务!" );               
           if (serviceNumber != null){
                   System . out. println( windowName "开始为第" + serviceNumber "号快速客户服务" )                    
                    int serviceTime = Constants. MIN_SERVICE_TIME;
                    try {
                             Thread .sleep (serviceTime );
                    } catch (InterruptedException e ) {
                             e .printStackTrace ();
                    }                   
                   System . out. println( windowName "完成为第" + serviceNumber "号快速客户服务,总共耗时" +serviceTime1000 "秒") ;            
           } else{
                   System . out. println( windowName "没有取到快速任务!" );                                
                   commonService ();
           }         
}
private void expressService () {
           // TODO Auto-generated method stub
          Integer serviceNumber = NumberMachine.getInstance ()getVipManager (). fetchNumber() ;
          String windowName = "第" + number + "号" + type + "窗口" ;         
          System . out. println( windowName "开始获取VIP任务!" );                          
           if (serviceNumber != null){
                   System . out. println( windowName "开始为第" + serviceNumber "号VIP客户服务" )                     
                    int maxRandom = Constants. MAX_SERVICE_TIME - Constants. MIN_SERVICE_TIME;
                    int serviceTime = new Random (). nextInt( maxRandom )+ 1 + Constants. MIN_SERVICE_TIME;
                    try {
                             Thread .sleep (serviceTime );
                    } catch (InterruptedException e ) {
                             e .printStackTrace ();
                    }                   
                   System . out. println( windowName "完成为第" + serviceNumber "号VIP客户服务,总共耗时" +serviceTime1000 "秒") ;             
           } else{
                   System . out. println( windowName "没有取到VIP任务!" );                                 
                   commonService ();
           }         
}         
}
5.测试类类。
class Constants {
public final static int MAX_SERVICE_TIME =10 ;
public final static int MIN_SERVICE_TIME =1 ;
public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;
}
 
public class MainClass {
          
           private static Logger logger = Logger .getLogger ("threadTest" );
          
 
           public static void main ( String[] args ) {
                    //产生4个普通窗口
                    for (int i1i5i++ ){
                             ServiceWindow window =  new ServiceWindow() ;
                             window .setNumber ( i) ;
                             window .start ();
                    }
          
                    //产生1个快速窗口
                   ServiceWindow expressWindow =  new ServiceWindow() ;
                   expressWindow .setType ( Custommer. EXPRESS) ;
                   expressWindow .start ();
                   
                    //产生1个VIP窗口            
                   ServiceWindow vipWindow =  new ServiceWindow() ;
                   vipWindow .setType ( Custommer. VIP) ;
                   vipWindow .start ()           
                   
                    //普通客户拿号
                   Executors .newScheduledThreadPool ( 1) .scheduleAtFixedRate (
                                       new Runnable(){
                                                 public void run (){
                                                          Integer serviceNumber = NumberMachine.getInstance ()getCommonManager (). generateNewNumber() ;
                                      
                                                          System . out. println( "第" + serviceNumber + "号普通客户正在等待服务!" )                                                       
                                                 }
                                       },
                                      0 ,
                                       Constants . COMMON_CUSTOMER_INTERVAL_TIME,
                                      TimeUnit . SECONDS) ;
                   
                    //快速客户拿号
                   Executors .newScheduledThreadPool ( 1) .scheduleAtFixedRate (
                                       new Runnable(){
                                                 public void run (){
                                                          Integer serviceNumber = NumberMachine.getInstance ()getExpressManager (). generateNewNumber() ;
                                                          System . out. println( "第" + serviceNumber + "号快速客户正在等待服务!" );
                                                 }
                                       },
                                      0 ,
                                       Constants . COMMON_CUSTOMER_INTERVAL_TIME * 2 ,
                                      TimeUnit . SECONDS) ;
                   
                    //VIP客户拿号
                   Executors .newScheduledThreadPool ( 1) .scheduleAtFixedRate (
                                       new Runnable(){
                                                 public void run (){
                                                          Integer serviceNumber = NumberMachine.getInstance ()getVipManager (). generateNewNumber() ;
                                                          System . out. println( "第" + serviceNumber + "号VIP客户正在等待服务!" );
                                                 }
                                       },
                                      0 ,
                                       Constants . COMMON_CUSTOMER_INTERVAL_TIME * 6 ,
                                      TimeUnit . SECONDS) ;
           }
                         android培训java培训、期待与您交流!  

黑马程序员——Java面试题之银行业务调度系统,布布扣,bubuko.com

黑马程序员——Java面试题之银行业务调度系统

标签:android   style   blog   class   c   java   

原文地址:http://www.cnblogs.com/shuawang/p/3734404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!