黑马程序员——银行业务调度系统

android培训java培训、期待与您交流! 

上一个交通灯管理系统没有敲代码主要学习的是思想,这次的问题就跟着老师的思路敲代码了,自己敲完之后,看看和老师的有哪些不一样,然后再改正,还是那句话,单独把这些代码拿出来都明白什么意思,但是怎么来的就不一定明白的很彻底,面向对象的设计思想真的很重要,可能对于我这样的初学者来说听困难的,不知道如何开始,之间的逻辑不知道该怎么实现,在老师的眼中一切都是那么简单。这真的就得靠自己的努力了。需求就去网上找张孝祥老师的视频看吧,我在这就不粘贴了,实现代码如下:
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 static Logger logger = Logger.getLogger("cn.itcast.bankqueue");

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();

}

}

}



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

}


}





---------------------- android培训java培训、期待与您交流! ----------------------

详细请查看: http://edu.csdn.net/heima

你可能感兴趣的:(java)