黑马程序员 (7k面试)银行取票系统学习笔记

时间:2023-02-18 11:39:36

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ---------------------

 

通过对银行取票系统的分析,需要至少需要创建两个类,一个是窗口类,用于取票,服务完了,就将使客服减少一个。一个类就是取票机类,该类的任务是产生票,并具有删除票的功能。

具体事项是这样的,窗体类常见三种不同窗口对象,创建完了之后,就不断的向各自的取票机要票,如果有就服务,并将票从对应取票机中删除,如果没有票就等待1秒钟,然后,再进行要票。普通用户窗体只需要完成上述功能就可以了,而快速窗口和VIP窗口则需要完成上述操作之后,如果对应的客户没有票的话,就需要为普通客户服务,这样不断的循环执行。

窗体类的具体实现代码如下:

importjava.util.Random;

importjava.util.concurrent.Executors;

importjava.util.concurrent.TimeUnit;

 

public classClientWindow {

       NumberManager numType = null;

       int num = 0;

      

       public ClientWindow(NumberManagernumType,int num){//确定窗体类型和编号

              this.numType = numType;

              this.num = num;

       }

      

       public void windowServiceStart(){//根据不同类型的窗口,调用不同的功能体。使用//了定时器,让这一过程能过不断进行

              switch(numType){

              case COMMON:

                     Executors.newScheduledThreadPool(1).scheduleAtFixedRate(

                                   newRunnable(){

                                          publicvoid run(){

                                                 commonService();

                                          }

                                   },

                                   0,

                                   1,

                                   TimeUnit.SECONDS);

                     break;

              case EXPRESS:

                     Executors.newScheduledThreadPool(1).scheduleAtFixedRate(

                                   newRunnable(){

                                          publicvoid run(){

                                                 expressService();

                                          }

                                   },

                                   0,

                                   1,

                                   TimeUnit.SECONDS);

                    

                     break;

              case VIP:

                     Executors.newScheduledThreadPool(1).scheduleAtFixedRate(

                                   newRunnable(){

                                          publicvoid run(){

                                                 VIPService();

                                          }

                                   },

                                   0,

                                   1,

                                   TimeUnit.SECONDS);

                 break;

              }

       }

      

       private void commonService(){//普通用户功能代码体

              String massage = "第"+num+"号普通窗口";

              System.out.println(massage +"正在获取普通客户!...");

              if(numType.number.size()!=0){

                     long bengin =System.currentTimeMillis();

                     try {

                            Thread.sleep(((newRandom().nextInt(7)+1)*1000));

                     } catch(InterruptedException e) {

                            e.printStackTrace();

                     }

                     Integer temp =numType.fatchNumber();

                     System.out.println(massage+ "正在为" +temp+ "普通客户服务!");

                     long count =System.currentTimeMillis() - bengin;

                    

                 System.out.println(massage + "为第" + temp + "号普通客户完成服务,耗时" + count/1000+ "秒");

              }else{

                     System.out.println(massage+ "没有获得普通用户,等待1秒!");

                     try {

                            Thread.sleep(1000);

                     } catch(InterruptedException e) {

                            e.printStackTrace();

                     }

              }

       }

       private void expressService(){//快速用户功能代码体

              String massage = "第"+num+"号快速窗口";

              System.out.println(massage +"正在获取快速客户!...");

              if(numType.number.size()!=0){

                     long bengin =System.currentTimeMillis();

                     try {

                            Thread.sleep(((newRandom().nextInt(2)+1)*1000));

                     }catch (InterruptedException e) {

                            e.printStackTrace();

                     }

                     Integer temp =numType.fatchNumber();

                     System.out.println(massage+ "正在为" +temp+ "快速客户服务!");

                     long count =System.currentTimeMillis() - bengin;

                    

                 System.out.println(massage + "为第" + temp + "号快速客户完成服务,耗时" + count/1000+ "秒");

              }else{

                            System.out.println(massage+ "没有获得快速用户,等待1秒!");

                            try {

                                   Thread.sleep(1000);

                            } catch(InterruptedException e) {

                                   e.printStackTrace();

                            }

              }

       }

       private void VIPService(){//VIP用户功能代码体

              String massage = "第"+num+"号VIP窗口";

              System.out.println(massage +"正在获取VIP客户!...");

              if(numType.number.size()!=0){

                     long bengin =System.currentTimeMillis();

                     try {

                            Thread.sleep(((newRandom().nextInt(2)+1)*1000));

                     } catch (InterruptedExceptione) {

                            e.printStackTrace();

                     }

                     Integer temp =numType.fatchNumber();

                     System.out.println(massage+ "正在为" +temp+ "VIP客户服务!");

                     long count =System.currentTimeMillis() - bengin;

                    

                 System.out.println(massage + "为第" + temp + "号VIP客户完成服务,耗时"+ count/1000+ "秒");

              }else{

                            System.out.println(massage+ "没有获得VIP用户,等待1秒!");

                            try {

                                   Thread.sleep(1000);

                            } catch(InterruptedException e) {

                                   e.printStackTrace();

                            }

              }    

       }

}

 

     取票机类的实现代码如下:

public enum NumberManager {

       COMMON(null),EXPRESS(null,null),VIP(null,null,null);

      

       privateNumberManager(String s) {

              ScheduledExecutorServicepool = Executors.newScheduledThreadPool(1);

              pool.scheduleAtFixedRate(

                            newRunnable(){

                    public void run(){

                                          addNumber();

                                          //System.out.println(fatchNumber()+"common移除了");;

                                   }

                            },

                            0,

                            1,

                            TimeUnit.SECONDS);

       }

       privateNumberManager(String s0,String s1) {

              ScheduledExecutorServicepool = Executors.newScheduledThreadPool(1);

              pool.scheduleAtFixedRate(

                            newRunnable(){

                    public void run(){

                                          addNumber();

                                          //System.out.println(fatchNumber()+"express移除了");;

                                   }

                            },

                            0,

                            2,

                            TimeUnit.SECONDS);

       }

       privateNumberManager(String s0,String s1,String s2) {

              ScheduledExecutorServicepool = Executors.newScheduledThreadPool(1);

              pool.scheduleAtFixedRate(

                            newRunnable(){

                    public void run(){

                                          addNumber();

                                          //System.out.println(fatchNumber()+"vip移除了");;

                                   }

                            },

                            0,

                            6,

                            TimeUnit.SECONDS);

       }

      

       Integeri = 0;

       List<Integer>number = new ArrayList<Integer>();

      

       publicvoid addNumber(){

              number.add(++i);

              System.out.println("第" + i + "号" + this.name() + "客户正在等待接受服务!");

       }

      

       publicInteger fatchNumber(){

              returnnumber.remove(0);

       }

    根据接受来的参数,调用不同的构造方法,来创建不同类型的取票机对象,一旦创建就开始了产生了票。将生成的票储存在集合中,同时存在一个将集合中的数据删除的功能。










----------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------详细请查看:www.itheima.com