黑马程序员--银行业务调度系统学习

1、NumberManager类,使用队列集合存储序号,提供新来客户索取序号的方法和服务窗口索取要服务的序号的方法。

import java.util.*;

public class NumberManager
{
private int number=0;
private List<Integer> queue=new ArrayList<Integer>();

public synchronized Integer generateNumber()
{
queue.add(++number);;
return number;
}

public synchronized Integer fetchServiceNumber()
{
Integer number=null;
if(queue.size()>0)
{
number= queue.remove(0);
}
return number;
}
}

2、NumberMachine类,定义普通、快速和VIP客户的号码管理器,分别用来操作三种业务的序号。NumberMachine类设计成单例。

public class NumberMachine
{
private static NumberMachine instance=new NumberMachine();

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;
}

private NumberMachine(){}

public static NumberMachine getInstance()
{
return instance;
}
}

3、定义三种客户的枚举类型,实现toString()方法。

public enum CustomerType
{
COMMON,EXPRESS,VIP;

public String toString()
{
switch(this)
{
case COMMON:
return "普通";
case EXPRESS:
return "快速";
case VIP:
return "VIP";
default:
return null;
}
}
}

4、常量类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;
public static int EXPRESS_CUSTOMER_INTERVAL_TIME=2;
public static int VIP_CUSTOMER_INTERVAL_TIME=6;
}

5、ServiceWindow类,服务窗口通过一个线程对三种客户进行服务。

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

public class ServiceWindow
{
private CustomerType customerType;
private int windowId;

public void setCustomerType(CustomerType customerType)
{
this.customerType=customerType;
}

public void setWindowId(int windowsId)
{
this.windowId=windowsId;
}

public void start()
{
Executors.newSingleThreadExecutor().execute(new Runnable()
{
public void run()
{
while(true)
{
switch(customerType)
{
case COMMON:
CommonCustomerService();
break;
case EXPRESS:
ExpressCustomerService();
break;
case VIP:
VipCustomerService();
break;
}
}
}
});
}

private void CommonCustomerService()
{
String windowName="第"+windowId+"号"+customerType+"窗口";
System.out.println(windowName+"正在获取任务...");
Integer number=NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
if(number!=null)
{
long startTime=System.currentTimeMillis();
int maxRand=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serviceTime=new Random().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;
try
{
Thread.sleep(serviceTime);
}
catch(Exception e){}
long time=System.currentTimeMillis()-startTime;
System.out.println(windowName+"为第"+number+"号普通客户服务,花费"+time/1000+"秒");
}
else
{
System.out.println(windowName+"没有获取到任务,休息1秒!");
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}
}

private void ExpressCustomerService()
{
String windowName="第"+windowId+"号"+customerType+"窗口";
System.out.println(windowName+"正在获取任务...");
Integer number=NumberMachine.getInstance().getExpressManager().fetchServiceNumber();
if(number!=null)
{
long startTime=System.currentTimeMillis();
long serviceTime=Constants.MIN_SERVICE_TIME;
try
{
Thread.sleep(serviceTime);
}
catch(Exception e){}
long time=System.currentTimeMillis()-startTime;
System.out.println(windowName+"为第"+number+"号"+customerType+"客户服务,花费"+time/1000+"秒");
}
else
{
System.out.println(windowName+"没有获取到任务");
CommonCustomerService();
}
}

private void VipCustomerService()
{
String windowName="第"+windowId+"号"+customerType+"窗口";
System.out.println(windowName+"正在获取任务...");
Integer number=NumberMachine.getInstance().getVipManager().fetchServiceNumber();
if(number!=null)
{
long startTime=System.currentTimeMillis();
int maxRand=Constants.MAX_SERVICE_TIME-Constants.MIN_SERVICE_TIME;
long serviceTime=new Random().nextInt(maxRand)+1+Constants.MIN_SERVICE_TIME;
try
{
Thread.sleep(serviceTime);
}
catch(Exception e){}
long time=System.currentTimeMillis()-startTime;
System.out.println(windowName+"为第"+number+"号"+customerType+"客户服务,花费"+time/1000+"秒");
}
else
{
System.out.println(windowName+"没有获取到任务");
CommonCustomerService();
}
}
}

5、Program类,根据客户间隔时间,创建新的客户号码;创建服务窗口为客户服务。

public class Program
{
public static void main(String[] args)
{
for(int i=0;i<4;i++)
{
ServiceWindow commonWindow=new ServiceWindow();
commonWindow.setCustomerType(CustomerType.COMMON);
commonWindow.setWindowId(i+1);
commonWindow.start();
}

ServiceWindow expressWindow=new ServiceWindow();
expressWindow.setCustomerType(CustomerType.EXPRESS);
expressWindow.setWindowId(5);
expressWindow.start();

ServiceWindow vipWindow=new ServiceWindow();
vipWindow.setCustomerType(CustomerType.VIP);
vipWindow.setWindowId(6);
vipWindow.start();

Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable()
{
public void run()
{
Integer number=NumberMachine.getInstance().getCommonManager().generateNumber();
System.out.println("第"+number+"号普通客户等待服务");
}
},
0,
Constants.COMMON_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);

Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable()
{
public void run()
{
Integer number=NumberMachine.getInstance().getExpressManager().generateNumber();
System.out.println("第"+number+"号快速客户等待服务");
}
},
0,
Constants.EXPRESS_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);

Executors.newScheduledThreadPool(1).scheduleAtFixedRate(
new Runnable()
{
public void run()
{
Integer number=NumberMachine.getInstance().getVipManager().generateNumber();
System.out.println("第"+number+"号VIP客户等待服务");
}
},
0,
Constants.VIP_CUSTOMER_INTERVAL_TIME,
TimeUnit.SECONDS);
}
}

posted on 2012-03-30 20:36  黑马程序员  阅读(233)  评论(0编辑  收藏  举报

导航