黑马程序员技术交流社区
标题: 模拟银行调度系统 [打印本页]
作者: Tking 时间: 2014-4-6 02:36
标题: 模拟银行调度系统
模拟银行调度系统
需求:具有六个窗口,四个普通窗口,一个快速窗口,一个vip窗口;
普通窗口:只为普通用户服务。
快速窗口:优先为快速窗口服务,再为普通窗口服务。
VIP窗口,优先为VIP窗口服务,再为普通窗口服务。
服务时间:最大值与最小值,最小值都是快速客户。
客户出现比例。普通:快速:VIP = 6:3:1
模拟用户:自身携带了需要完成的业务,与自身的等级
- public class client implements Comparable<client>
- {
- private int clientrank;//客户的等级
- private int clientneed;//客户需求
- client()
- {
- set();
- }
- void set()//初始化客户的需求,与等级
- {
- this.clientrank=(int)(Math.random()*10+1);
- if(this.clientrank<4)
- this.clientneed=1;
- else this.clientneed=(int)(Math.random()*4+2);
- System.out.println("产生一个等级为"+getRankString()+"业务为"+clientneed);
- }
- public int getClientrank() {
- return clientrank;
- }
- public int getClientneed() {
- return clientneed;
- }
- @Override
- public int compareTo(client o) {
- return 1;
- }
- public String toString()
- {
- return "等级为"+this.getRankString()+"-----需求为"+this.clientneed;
- }
- public String getRankString()
- {
- return this.clientrank<4?"<___快速用户___>":this.clientrank<10?"{___普通用户___}":"【___Vip用户___】";
- }
- }
复制代码模拟服务窗口:窗口具备向某个银行取出用户的功能,每个窗口都向银行的等候厅取出用户,这里做成线程。
- public class Fram implements Runnable
- {
- private int rank;
- private Bank bank;
- Fram(int rank)//在构造时,就指明窗口的权限
- {
- this.rank=rank;
- bank=Bank.getBank();//指定为某个银行服务
- }
- public void run()
- {
- while(true)
- {
- if(this.rank==1)//窗口等级化
- mommon();
- else if(this.rank==2)
- quick();
- else
- VIP();
- }
- }
- private void mommon()//普通窗口服务
- {
- client temp=bank.delClient();
- if(temp!=null)
- {
- System.out.println(Thread.currentThread().getName()+"接收到客户--"+temp);
- clientEvent(temp.getClientneed());
- }else
- {
- try{Thread.sleep(1000);//没有普通用户则休息一秒
- }catch
- (Exception e)
- {}
- }
- }
- private void quick()//快速窗口服务
- {
- clientEventPD(bank.quickdelClient());
- }
-
- private void VIP()//vip窗口服务
- {
- clientEventPD(bank.VIPdelClient());
- }
- private void clientEventPD(client temp) //特殊客户判断
- {
- if(temp!=null)
- {
- System.out.println(Thread.currentThread().getName()+"接收到-----"+temp);
- clientEvent(temp.getClientneed());
- }
- else mommon();
- }
-
- private void clientEvent(int time)//处理客户事件
- {
- try
- {
- System.out.println(Thread.currentThread().getName()+"处理客户等待"+time+"秒");
- Thread.sleep(time*1000);
- }
- catch(InterruptedException e){}
- }
- }
复制代码模拟银行:银行具备存储用户的功能,对相应等级的用户分开存储,并拥有六个窗口
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)秒一名用户进入银行,
- public class Main {
- public static void main(String[] args)
- {
- final Bank bank=Bank.getBank();
- int person =(int)(Math.random()*100+1);//随机产生人数
- bank.openFram();
- System.out.println("\t\t\t\t\t\t\t将会产生" + person + "客户");
- for(int i=1;i<=person;i++)
- {
- int time=(int)(Math.random()*10+1);
- try{Thread.sleep(time*1000);}catch(Exception e){}
- bank.addClient(new client());
- }
- }
- }
复制代码
作者: 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
这设备本就应该存在银行内部
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |