------- android培训、java培训、期待与您交流! ----------
有三种对应类型的客户:VIP客户,普通客户,快速客户 ,异步随机生成各种类型的客户,各类型客户在其对应窗口按顺序依次办理业务 。
每一个客户其实就是由银行的一个取号机器产生号码的方式来表示的。所以,我想到要有一个号码管理器对象,让这个对象不断地产生号码,就等于随机生成了客户。
由于有三类客户,每类客户的号码编排都是完全独立的,本系统一共要产生三个号码管理器对象,各自管理一类用户的排队号码。
这三个号码管理器对象统一由一个号码机器进行管理,这个号码机器在整个系统中始终只能有一个,所以,它要被设计成单例。
各类型客户在其对应窗口按顺序依次办理业务 ,准确地说,应该是窗口依次叫号。
各个窗口怎么知道该叫哪一个号了呢?它一定是问的相应的号码管理器,即服务窗口每次找号码管理器获取当前要被服务的号码。
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;
}
}
}
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;
}
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放在case下面
// 找NumberMachine要号码管理器
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();
}
}
}
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;
}
}
public class Constants {
public static int MAX_SERVICE_TIME = 10000; //10秒!
public static int MIN_SERVICE_TIME = 1000; //1秒!
public static int COMMON_CUSTOMER_INTERVAL_TIME = 1;//
}
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++){
ServiceWindow window = new ServiceWindow();
window.setNumber(i);
window.start();
}
//产生1个快速窗口
ServiceWindow expressWindow = new ServiceWindow();
expressWindow.setType(CustomerType.EXPRESS);
expressWindow.start();
//产生1个VIP窗口
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();
//logger.info("第" + serviceNumber + "号普通客户正在等待服务!");
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);
//VIP客户拿号
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);
}
}
这个需求比较复杂,需要好好分析才能下手,基本上所有JAVA基础内容在该项目中都有体现。多看几遍会对面对对象有更好的理解。