黑马程序员----java编程之银行业务调度系统

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


模拟实现银行业务调度系统逻辑,具体需求如下:

  • 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
  • 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
  • 异步随机生成各种类型的客户,生成各类型用户的概率比例为:

        VIP客户:普通客户:快速客户  =  1 :6 :3。

  • 客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
  • 各类型客户在其对应窗口按顺序依次办理业务。
  • 当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
  • 随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
  • 不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。

NumberManager类

public class NumberManager {
	private int lastNumber=0;
	private List queueNumbers=new ArrayList(){};
	
	public Integer generateNewNumber(){
		queueNumbers.add(++lastNumber);
		return lastNumber;
	}
	
	public Integer fetchNumber(){
		if(queueNumbers.size()>0){
			return (Integer)queueNumbers.remove(0);
		}else{
			return null;
		}
	}	
}


NumberMachine


public class NumberMachine {

	private NumberMachine(){}
	private static NumberMachine instance=new 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;
	}
}


Constants


public class Constants {
	public static int MAX_SERVICE_TIME=10000;
	public static int MIN_SERVICE_TIME=1000;
	
	public static int COMMON_CUSTOMER_INTERVAL_TIME=1;
}


CustomerType

public enum CustomerType {
	COMMON,EXPRESS,VIP;
	public String toString(){
		String name=null;
		switch(this){
		case COMMON:
			name="普通";
			break;
		case EXPRESS:
			name="快速";
			break;
		case VIP:
			name=name();
			break;
		}
		return name;
		
	}
}


ServiceWindow


public class ServiceWindow {
	private CustomerType type=CustomerType.COMMON;
	private int number=1;
	public CustomerType getType(){
		return type;
	}
	public void setType(CustomerType type){
		this.type=type;
	}
	public void setNumber(int number){
		this.number=number;
	}
	
	public void start(){
		Executors.newSingleThreadExecutor().execute(
				new Runnable(){
					public void run(){
						while(true){
							switch(type){
								case COMMON:
									commonService();
									break;
								case EXPRESS:
									expressService();
									break;
								case VIP:
									vipService();
									break;
							}
						}
					}
				}
		);
	}
	
	private void commonService(){
		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 + "号普通客户服务,总共耗时" + serviceTime/1000 + "秒");		
		}else{
			System.out.println(windowName + "没有取到普通任务,正在空闲一秒");		
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}				
		}
	}
	
	private void expressService(){
		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 + "号快速客户服务,总共耗时" + serviceTime/1000 + "秒");		
		}else{
			System.out.println(windowName + "没有取到快速任务!");				
			commonService();
		}
	}
	
	private void vipService(){

		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客户服务,总共耗时" + serviceTime/1000 + "秒");		
		}else{
			System.out.println(windowName + "没有取到VIP任务!");				
			commonService();
		}	
	}
}

MainClass

public class MainClass {

	public static void main(String[] args) {
		for(int i=1;i<5;i++){
			ServiceWindow window =  new ServiceWindow();
			window.setNumber(i);
			window.start();
		}
	
		ServiceWindow expressWindow =  new ServiceWindow();
		expressWindow.setType(CustomerType.EXPRESS);
		expressWindow.start();
			
		ServiceWindow vipWindow =  new ServiceWindow();
		vipWindow.setType(CustomerType.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);
		
		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);

	}

}





---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值