纵观整个题的代码,感觉并不难,张老师代码里总共写了6个类,1个主类(main),5个辅类
但我感觉可以把NumberMachine和NumberManager合写成一个enum就行,再加上ServiceWindows和CustomerType和main就行
下面是我自己写的某个合并类,还有那个constants中的常量直接用数字表示也应该行吧
public enum NumberMachine{
NumberManager_common("普通"),NumberManager_express("快速"),NumberManager_vip("VIP");
Private String name;
Private int lastnumber=0;
private List queueNumbers = new ArrayList();
public NumberMachine(String name){this.name=name;}
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;
}
}
}
我在想如果把ServiceWindows写成一个enum也应该行吧 那样可以简化一些
public enum ServiceWindow{
ServiceWindow_common("普通"),ServiceWindow_express("快速"),ServiceWindow_vip("VIP");
private ServiceWindow(String name){this.name=name;}
private String name;
private int number = 1;
public void setNumber(int number){
this.number = number;
}
public void service(){
while(true){
String windowName = "第" + number + "号" +name + "窗口";
System.out.println(windowName + "开始获取"+name+"任务!");
Integer serviceNumber=NumberMachine(name).fetchNumber();
if(serviceNumber != null ){
System.out.println(windowName + "开始为第" + serviceNumber + "号"+name+"客户服务");
int serviceTime1 = new Random().nextInt(10000)+1+ 1000;
int serviceTime2 = 1000;
int serverTime=(name="普通"|"VIP"?serviceTime1:serviceTime2)
try {
Thread.sleep(serviceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(windowName + "完成为第" + serviceNumber + "号+name+"客户服务,总共耗时" + serviceTime/1000 + "秒");
}else{
if(name="普通"){
System.out.println(windowName + "没有取到任务,正在空闲一秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(name="快速"|"VIP"){System.out.println(windowName + "没有取到VIP任务!");name="普 通";service();}
}
}
} |
|