黑马程序员技术交流社区

标题: 模拟银行调度系统 [打印本页]

作者: Tking    时间: 2014-4-6 02:36
标题: 模拟银行调度系统
模拟银行调度系统
需求:具有六个窗口,四个普通窗口,一个快速窗口,一个vip窗口;
普通窗口:只为普通用户服务。
快速窗口:优先为快速窗口服务,再为普通窗口服务。
VIP窗口,优先为VIP窗口服务,再为普通窗口服务。
服务时间:最大值与最小值,最小值都是快速客户。
客户出现比例。普通:快速:VIP = 6:3:1
模拟用户:自身携带了需要完成的业务,与自身的等级
  1. public class client implements Comparable<client>
  2. {
  3.         private int clientrank;//客户的等级
  4.         private int clientneed;//客户需求
  5.         client()
  6.         {
  7.                 set();
  8.         }
  9.         void set()//初始化客户的需求,与等级
  10.         {
  11.                 this.clientrank=(int)(Math.random()*10+1);
  12.                 if(this.clientrank<4)
  13.                         this.clientneed=1;
  14.                 else this.clientneed=(int)(Math.random()*4+2);
  15.                 System.out.println("产生一个等级为"+getRankString()+"业务为"+clientneed);
  16.         }
  17.         public int getClientrank() {
  18.                 return clientrank;
  19.         }
  20.         public int getClientneed() {
  21.                 return clientneed;
  22.         }
  23.         @Override
  24.         public int compareTo(client o) {
  25.                 return 1;
  26.         }
  27.         public String toString()
  28.         {
  29.                 return "等级为"+this.getRankString()+"-----需求为"+this.clientneed;
  30.         }
  31.         public String getRankString()
  32.         {
  33.                 return this.clientrank<4?"<___快速用户___>":this.clientrank<10?"{___普通用户___}":"【___Vip用户___】";
  34.         }
  35. }
复制代码
模拟服务窗口:窗口具备向某个银行取出用户的功能,每个窗口都向银行的等候厅取出用户,这里做成线程。
  1. public class Fram implements Runnable
  2. {
  3.         private int rank;
  4.         private Bank bank;
  5.         Fram(int rank)//在构造时,就指明窗口的权限
  6.         {
  7.                 this.rank=rank;
  8.                 bank=Bank.getBank();//指定为某个银行服务
  9.         }
  10.         public void run()
  11.         {
  12.                 while(true)
  13.                 {
  14.                         if(this.rank==1)//窗口等级化
  15.                                 mommon();
  16.                         else if(this.rank==2)
  17.                                 quick();
  18.                         else
  19.                                 VIP();
  20.                 }
  21.         }
  22.         private void mommon()//普通窗口服务
  23.         {
  24.                 client temp=bank.delClient();
  25.                 if(temp!=null)
  26.                 {
  27.                         System.out.println(Thread.currentThread().getName()+"接收到客户--"+temp);
  28.                         clientEvent(temp.getClientneed());
  29.                 }else
  30.                 {
  31.                         try{Thread.sleep(1000);//没有普通用户则休息一秒
  32.                         }catch
  33.                         (Exception e)
  34.                         {}
  35.                 }
  36.         }
  37.         private void quick()//快速窗口服务
  38.         {
  39.                 clientEventPD(bank.quickdelClient());
  40.         }
  41.        
  42.         private void  VIP()//vip窗口服务
  43.         {
  44.                 clientEventPD(bank.VIPdelClient());
  45.         }
  46.         private void clientEventPD(client temp) //特殊客户判断
  47.         {
  48.                 if(temp!=null)
  49.                 {
  50.                         System.out.println(Thread.currentThread().getName()+"接收到-----"+temp);
  51.                         clientEvent(temp.getClientneed());
  52.                 }
  53.                 else mommon();
  54.         }
  55.        
  56.         private void clientEvent(int time)//处理客户事件
  57.         {
  58.                 try
  59.                 {
  60.                         System.out.println(Thread.currentThread().getName()+"处理客户等待"+time+"秒");
  61.                         Thread.sleep(time*1000);
  62.                 }
  63.                 catch(InterruptedException e){}
  64.         }
  65. }
复制代码
模拟银行:银行具备存储用户的功能,对相应等级的用户分开存储,并拥有六个窗口
import java.util.LinkedList;
public class Bank
{
        private LinkedList<client> wait;
        private LinkedList<client> VIPwait;
        private LinkedList<client> quickwait;
        private static final Bank  bank=new Bank();
        private Bank()
        {
                this.inio();
        }
        private void inio()
        {
                this.wait=new LinkedList<>();//存储普通用户
                this.VIPwait=new LinkedList<>();//存储快速用户
                this.quickwait=new LinkedList<>();//存储VIP用户
        }
        public static Bank getBank()
        {
                return bank;
        }
        //三个窗口取用户
        synchronized client delClient()
        {
                        return wait.size()!=0?wait.remove():null;
        }
        synchronized client quickdelClient()
        {
                        return quickwait.size()!=0?quickwait.remove():delClient();
        }
        synchronized client VIPdelClient()
        {
                        return VIPwait.size()!=0?VIPwait.remove():delClient();
        }
       
        synchronized void addClient(client c)//进入客户
        {
                if(c.getClientrank()<4)
                                quickwait.add(c);
                else if(c.getClientrank()<10)
                        wait.add(c);
                else VIPwait.add(c);
                System.out.println("---------普通用户"+wait.size()+"人"+"---------快速用户"+quickwait.size()+"人"+"----------VIP用户"+VIPwait.size()+"人");
        }
        void openFram()//打开六个窗口的服务
        {
                new Thread(new Fram(1),"1号普通窗口").start();
                new Thread(new Fram(1)," 2号普通窗口").start();
                new Thread(new Fram(1),"  3号普通窗口").start();
                new Thread(new Fram(1),"   4号普通窗口").start();
                new Thread(new Fram(2),"     快速窗口").start();;
                new Thread(new Fram(3),"      VIP窗口").start();
        }
       
}


作者: Tking    时间: 2014-4-6 02:37
主程序:模拟随机产生数名用户,间隔(1~10)秒一名用户进入银行,
  1. public class Main {
  2.         public static void main(String[] args)
  3.         {
  4.                 final Bank bank=Bank.getBank();
  5.                 int person =(int)(Math.random()*100+1);//随机产生人数
  6.                 bank.openFram();
  7.                 System.out.println("\t\t\t\t\t\t\t将会产生" + person + "客户");
  8.                 for(int i=1;i<=person;i++)
  9.                 {
  10.                         int time=(int)(Math.random()*10+1);
  11.                         try{Thread.sleep(time*1000);}catch(Exception e){}

  12.                         bank.addClient(new client());
  13.                 }
  14.         }
  15. }
复制代码

作者: Tking    时间: 2014-4-6 02:39
附上草图一份:victory:
作者: Tking    时间: 2014-4-7 00:49
顶起:@顶起:@顶起:@
作者: syusikoku    时间: 2014-4-7 08:26
不使用号码管理器和号码机器。应该会有点麻烦啊。你这里是不是把这个设备给取消掉了。
作者: Tking    时间: 2014-4-7 15:56
syusikoku 发表于 2014-4-7 08:26
不使用号码管理器和号码机器。应该会有点麻烦啊。你这里是不是把这个设备给取消掉了。 ...

这设备本就应该存在银行内部




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2