黑马程序员——根据银行排队取号系统模拟餐馆排队取号

时间:2022-02-03 11:43:09

----------------------------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ------- ---------------------- 

这俩天认真学习了张孝祥老师的7k面试题破解,感觉收获颇多,接下来一定还要再看几遍!!

不知道大家去没去过呷哺呷哺,就是一特火的火锅店,它里边也是排队取号的,不过是人工的,我参照张老师的银行排队例子模拟了一个取号机的,还望大家多多指正哈!!

下面是相关的代码:

package cn.itcast.xiapuxiapu;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class MainClass {
	
	private static Logger logger = Logger.getLogger("cn.itcast.bankqueue");
	

	public static void main(String[] args) {
		//产生4个单人餐桌
		for(int i=1;i<5;i++){
			ServiceTable table =  new ServiceTable();
			table.setNumber(i);
			table.start();
		}
	
		//产生1个双人餐桌
		ServiceTable twoTable =  new ServiceTable();
		twoTable.setType(CustomerType.TWO);
		twoTable.start();
		
		//产生1个多人餐桌	
		ServiceTable fourTable =  new ServiceTable();
		fourTable.setType(CustomerType.FOUR);
		fourTable.start();		
		
		//单人顾客拿号
		Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
				new Runnable(){
					public void run(){
						Integer serviceNumber = NumberMachine.getInstance().getOneManager().generateNewNumber();
						
					
						System.out.println("第" + serviceNumber + "号单人正在等着吃涮锅!");						
					}
				},
				0,
				Constants.ONE_CUSTOMER_INTERVAL_TIME, 
				TimeUnit.SECONDS);
		
		//双人顾客拿号
		Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
				new Runnable(){
					public void run(){
						Integer serviceNumber = NumberMachine.getInstance().getTwoManager().generateNewNumber();
						System.out.println("第" + serviceNumber + "号两人正在等着吃涮锅!");
					}
				},
				0,
				Constants.ONE_CUSTOMER_INTERVAL_TIME * 2, 
				TimeUnit.SECONDS);
		
		//多人顾客拿号
		Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
				new Runnable(){
					public void run(){
						Integer serviceNumber = NumberMachine.getInstance().getFourManager().generateNewNumber();
						System.out.println("第" + serviceNumber + "号多人正在等着吃涮锅!");
					}
				},
				0,
				Constants.ONE_CUSTOMER_INTERVAL_TIME * 6, 
				TimeUnit.SECONDS);
	}

}
package cn.itcast.xiapuxiapu;

public class Constants {
	public static int MAX_SERVICE_TIME = 10000; 
	public static int MIN_SERVICE_TIME = 1000; 
	//时间没敢设置正常的吃饭用时,等不起啊~~~~
	public static int ONE_CUSTOMER_INTERVAL_TIME = 1; 	
}
package cn.itcast.xiapuxiapu;

public enum CustomerType {
	ONE,TWO,FOUR;
	public String toString(){
		String name = null;
		switch(this){
		case ONE:
			name = "单人";
					
			break;
		case TWO:
			name = "双人";
			break;
		case FOUR:
			name = "多人";
			break;
		}
		return name;
	}
}
package cn.itcast.xiapuxiapu;

import java.util.ArrayList;
import java.util.List;

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

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.logging.Logger;

public class ServiceTable {
	private static Logger logger = Logger.getLogger("cn.itcast.bankqueue");
	private CustomerType type = CustomerType.ONE;
	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 ONE:
									oneService();
									break;
								case TWO:
									twoService();
									break;
								case FOUR:
									fourService();
									break;
							}
						}
					}
				}
		);
	}
	
	private void oneService(){
		String tableName = "第" + number + "号" + type + "餐桌";		
		System.out.println(tableName + ":开始获取单人任务!");
		Integer serviceNumber = NumberMachine.getInstance().getOneManager().fetchNumber();		
		if(serviceNumber != null ){
			System.out.println(tableName + ":第" + 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(tableName + ":第" + serviceNumber + "号单人顾客吃饱结账啦!!,总共耗时" + serviceTime/1000 + "秒");		
		}else{
			System.out.println(tableName + ":此餐桌没有人,空闲中....");		
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}				
		}
	}
	
	private void twoService(){
		Integer serviceNumber = NumberMachine.getInstance().getTwoManager().fetchNumber();
		String tableName = "第" + number + "号" + type + "餐桌";	
		System.out.println(tableName + ":开始获取双人任务!");		
		if(serviceNumber !=null){
			System.out.println(tableName + ":第" + serviceNumber + "号双人顾客开始涮锅!");			
			int serviceTime = Constants.MIN_SERVICE_TIME;
			try {
				Thread.sleep(serviceTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}		
			System.out.println(tableName + ":第" + serviceNumber + "号双人顾客吃饱结账啦!!,总共耗时" + serviceTime/1000 + "秒");		
		}else{
			System.out.println(tableName + ":此餐桌没有人,空闲中....");				
			oneService();
		}
	}
	
	private void fourService(){

		Integer serviceNumber = NumberMachine.getInstance().getFourManager().fetchNumber();
		String tableName = "第" + number + "号" + type + "餐桌";	
		System.out.println(tableName + ":开始获取多人任务!");			
		if(serviceNumber !=null){
			System.out.println(tableName + ":第" + 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(tableName + ":第" + serviceNumber + "号多人顾客吃饱结账啦!!,总共耗时" + serviceTime/1000 + "秒");		
		}else{
			System.out.println(tableName + ":此餐桌没有人,空闲中....");				
			oneService();
		}	
	}
}
package cn.itcast.xiapuxiapu;

public class NumberMachine {
	
	private NumberMachine(){}
	private static NumberMachine instance = new NumberMachine();
	public static NumberMachine getInstance(){
		return instance;
	}
	
	private NumberManager oneManager = new NumberManager();
	private NumberManager twoManager = new NumberManager();
	private NumberManager fourManager = new NumberManager();
	public NumberManager getOneManager() {
		return oneManager;
	}
	public NumberManager getTwoManager() {
		return twoManager;
	}
	public NumberManager getFourManager() {
		return fourManager;
	}
	
}