黑马程序员---银行调度系统

---------------------- android培训java培训、期待与您交流! ----------------------


这个写的和张孝祥老师的出入蛮大
没有张老师那么全面
仅仅只是达到了要求

大概是2个主要的类
Customer和Window
每隔一段时间创建一个Customer
然后去找Window
Customer有个time属性表示处理业务所需时间
Window有个计时器
处理Customer时用其time开启Window的计时器
到期后就从Window的队列取出一个Customer继续


import java.io.*;
import java.util.*;

class Customer{
	private static final long MINTIME=5000L;
	private static final long MAXTIME=10000L;
	private long time;									//time表示该处理该客户所用时间
	private CustomerType type;
	public Customer(){									//构造函数里初始化类型和time,并调用findWindow方法去找Window
		int i=(int)(Math.random()*10)+1;
		if(i==1)
			type=CustomerType.VIP;
		else if(i<=4)
			type=CustomerType.QUICK;
		else
			type=CustomerType.NORMAL;
		if(type==CustomerType.QUICK)
			time=MINTIME;
		else
			time=MINTIME+(long)(Math.random()*(MAXTIME-MINTIME));
		findWindow();
	}
	public void findWindow(){							
		Window w;
		ArrayList<Window> al=Bank.alw;
		if(type==CustomerType.NORMAL){					//优先去人少的窗口
			int[] priority=new int[al.size()];
			int minPriority=10001;
			int k=-1;
			for(int i=0;i<al.size();i++){
				w=al.get(i);
				priority[i]=w.getPriority();			//获得窗口的优先级
				if(priority[i]<minPriority){
					minPriority=priority[i];
					k=i;
				}
			}
			al.get(k).addCustomer(this);
		}else if(type==CustomerType.QUICK){				//快速客户和VIP客户只能去相应的窗口
			al.get(4).addCustomer(this);
		}else{
			al.get(5).addCustomer(this);
		}
	}
	public long getTime(){
		return time;
	}
	public CustomerType getType(){
		return type;
	}
}
enum CustomerType{									//枚举3种客户
	NORMAL,QUICK,VIP;
	public String toString(){
		if(this==NORMAL)
			return "普通客户";
		else if(this==QUICK)
			return "快速客户";
		else
			return "VIP客户";
	}
}
class Window{
	private ArrayList<Customer> al=null;				//该窗口的客户队列
	private Customer currentCustomer=null;				//正在办理业务的客户
	private Timer timer=null;							//用来模拟办理业务的时间的计时器
	private String name=null;							//窗口名字,方便查看
	private CustomerType type;							//直接用客户类型来定义窗口类型了
	public Window(String name,CustomerType type){
		al=new ArrayList<Customer>();
		this.name=name;
		this.type=type;
		timer=new Timer();
	}
	public int getPriority(){							//获得优先级方法只对普通客户有效,于是可以只针对普通客户写
		if(currentCustomer==null)
			return 0;
		else
		{
			if(type==CustomerType.NORMAL)				//不是普通窗口,且有人办理,那普通客户就不能用这个窗口
				return al.size()+1;
			else
				return 10000;
		}
	}
	public void addCustomer(Customer c){				//窗口来了一个客户
		if(currentCustomer==null)
			recepteCustomer(c);
		else{
			if(c.getType()==CustomerType.NORMAL)
				al.add(c);
			else{
				int i=0;
				for(;i<al.size();i++){
					if(al.get(i).getType()!=CustomerType.NORMAL)
						break;
				}
				al.add(i,c);
			}
		}
		String str="一位 "+c.getType()+" 到来,前往 "+name;
		System.out.println(str);
		Bank.pw.println(str);
	}
	private void nextCustomer(){						//处理完毕后下一位客户
		if(!al.isEmpty()){
			Customer c=al.get(0);
			al.remove(c);
			recepteCustomer(c);
		}
	}
	private void recepteCustomer(Customer c){			//接受客户,开启计时器模拟处理业务中
		currentCustomer=c;
		timer.schedule(new TimerExpired(this),c.getTime());
	}
	class TimerExpired extends TimerTask{
		private Window w;
		public TimerExpired(Window w){
			this.w=w;
		}
		@Override
		public void run(){
			String str=name+" 处理一位 "+currentCustomer.getType()+" ,用时"+currentCustomer.getTime()+"毫秒";
			System.out.println(str);
			Bank.pw.println(str);
			currentCustomer=null;
			nextCustomer();								//处理完毕后要调用nextCustomer方法
		}
	}
	
}
public class Bank {
	private static final long CREATECUSTOMERINTERVAL=2000; 
	public static ArrayList<Window> alw = null;
	public static PrintWriter pw=null;
	public static void main(String[] args){
		try{
			pw=new PrintWriter(new FileOutputStream("Bank.log"),true);
		}catch(IOException e){
			e.getStackTrace();
			throw new RuntimeException("日志文件创建失败");
		}
		alw=new ArrayList<Window>();
		alw.add(new Window("普通窗口1号",CustomerType.NORMAL));
		alw.add(new Window("普通窗口2号",CustomerType.NORMAL));
		alw.add(new Window("普通窗口3号",CustomerType.NORMAL));
		alw.add(new Window("普通窗口4号",CustomerType.NORMAL));
		alw.add(new Window("快速窗口",CustomerType.QUICK));
		alw.add(new Window("VIP窗口",CustomerType.VIP));
		new Timer().schedule(new TimerTask(){					//创建新客户的计时器
			@Override
			public void run() {
				new Customer();
			}
		},0,CREATECUSTOMERINTERVAL);
	}
}


---------------------- android培训java培训、期待与您交流! ----------------------
详细请查看: http://edu.csdn.net/heima
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值