---------------------- android培训、java培训、期待与您交流! -------------------------------
附件为源码,因为只能上传图片格式,我将后缀名改了,如需要查看只需将后缀改为zip或者rar即可。
银行业务调度系统
模拟实现银行业务调度系统逻辑,具体需求如下:
Ø 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口。
Ø 有三种对应类型的客户:VIP客户,普通客户,快速客户(办理如交水电费、电话费之类业务的客户)。
Ø 异步随机生成各种类型的客户,生成各类型用户的概率比例为:
VIP客户 :普通客户 :快速客户 = 1 :6 :3。
Ø 客户办理业务所需时间有最大值和最小值,在该范围内随机设定每个VIP客户以及普通客户办理业务所需的时间,快速客户办理业务所需时间为最小值(提示:办理业务的过程可通过线程Sleep的方式模拟)。
Ø 各类型客户在其对应窗口按顺序依次办理业务。
Ø 当VIP(6号)窗口和快速业务(5号)窗口没有客户等待办理业务的时候,这两个窗口可以处理普通客户的业务,而一旦有对应的客户等待办理业务的时候,则优先处理对应客户的业务。
Ø 随机生成客户时间间隔以及业务办理时间最大值和最小值自定,可以设置。
Ø 不要求实现GUI,只考虑系统逻辑实现,可通过Log方式展现程序运行结果。
面向对象的分析:
银行窗口服务是根据叫号机中是否有人需要服务工作的。即如果有人需要服务,则窗口(或者说负责窗口的人)必须要工作;如果没有需要服务的即可以休息。由于一个银行只有一个叫号器,所以将叫号器用单例模式实现。每一个银行只有一个叫号器,每一个窗口都需要和叫号器有联系(窗口完成任务后询问叫号器是否还有任务)。所以可以在窗口类中设置一个叫号器的成员变量,构造窗口时将那个唯一的叫号器传进去即可。又由于客户分为三类,每一个客户来了之后都会到叫号器上叫一个相应的号码(即普通客户叫普通的号,快速的叫快速号),就相当于叫号器添加一个人(Customer)。由于客户是分类的,所以需要几个线程生成不同的Customer。生成的不同的客户即排队等待的客户,先将客户暂时的“安置”一个等待的地方,即有了缓冲池。将等待人放入到不同的等待序列中。Customer类即是存在的缓冲池,将不同的客户分为不同的类别,又由于他们有共同的特性,所以抽象为Customer类。Customer类提供了添加删除等相应的方法。
如下代码是我根据张老师的思想和我自己的思想相融合得出的结果,运行没有错误,和张老师的源码可能有出入,我没有和张老师的代码对比。
package bank;
public enum CustomerType {
COMMON,EXPRESS,VIP;
@Override
public String toString() {
if(this.name().equals(COMMON.name())){
return "普通";
}
else if(this.name().equals(EXPRESS.name())){
return "快速";
}
else
return "VIP";
}
}
package bank;
import java.util.ArrayList;
import java.util.List;
public class Customer {
private CustomerType customerType = null;
private List<String> customers = new ArrayList<String>();
private int lastNum = 1;
public Customer(CustomerType customerType){
this.customerType = customerType;
}
/*
public synchronized String generateNewCustomer(){
if(customers.size() > 0)
customers.add(customers.size() , String.valueOf(customers.size() + 1));
else
customers.add(String.valueOf(1));
return customers.get(customers.size() - 1);
}*/
public synchronized String generateNewCustomer(){
if(customers.size() > 0)
customers.add(customers.size() , String.valueOf(lastNum++));
else
customers.add(String.valueOf(lastNum++));
return customers.get(customers.size() - 1);
}
public synchronized String generateNewCustomer(CustomerType type){
if(customers.size() > 0)
customers.add(customers.size() , String.valueOf(type.toString() + lastNum++));
else
customers.add(String.valueOf(type.toString() + lastNum++));
return customers.get(customers.size() - 1);
}
public synchronized String remove(){
if(customers.size() > 0)
return customers.remove(0);
else
return null;
}
public synchronized int getWaitingNum(){
return customers.size();
}
public CustomerType getCustomerType(){
return this.customerType;
}
}
package bank;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ServiceMachine {
private Customer commonCustomer = new Customer(CustomerType.COMMON);
private Customer expressCustomer = new Customer(CustomerType.EXPRESS);
private Customer vipCustomer = new Customer(CustomerType.VIP);
private static ServiceMachine serviceMachine = new ServiceMachine();
private ServiceMachine() {
}
public static ServiceMachine getServiceMachine() {
return serviceMachine;
}
public Customer getCommonCustomer() {
return commonCustomer;
}
public Customer getExpressCustomer() {
return expressCustomer;
}
public Customer getVipCustomer() {
return vipCustomer;
}
public void start() {
this.serviceCommonCustomer();
this.serviceExpressCommonCustomer();
this.serviceVIPCommonCustomer();
}
private void serviceCommonCustomer() {
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("进来一个普通客户:"+ ServiceMachine.this.commonCustomer.generateNewCustomer(commonCustomer.getCustomerType()) + "正在等待");
// commonCustomer.generateNewCustomer();
}
}, 0, Util.COMMON_CUSTOMER_RATE, TimeUnit.SECONDS);
}
private void serviceExpressCommonCustomer() {
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("进来快速一个客户:"
+ ServiceMachine.this.expressCustomer.generateNewCustomer(expressCustomer.getCustomerType()));
//commonCustomer.generateNewCustomer();
}
}, 0, Util.COMMON_CUSTOMER_RATE * 3, TimeUnit.SECONDS);
}
private void serviceVIPCommonCustomer() {
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("进来vip一个客户:"
+ ServiceMachine.this.vipCustomer.generateNewCustomer(vipCustomer.getCustomerType()));
//commonCustomer.generateNewCustomer();
}
}, 0, Util.COMMON_CUSTOMER_RATE * 6, TimeUnit.SECONDS);
}
}
package bank;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CommonServerWindow {
// private ServiceMachine serviceMachine =
// ServiceMachine.getServiceMachine();
private Customer commonCustomer = null;
private int waitingCumtomerNum = 0;
private int windowId = 1;
public CommonServerWindow(ServiceMachine serviceMachine, int windowId) {
// System.out.println("创建窗口" + windowId);
// serviceMachine.start();
commonCustomer = serviceMachine.getCommonCustomer();
this.waitingCumtomerNum = commonCustomer.getWaitingNum();
this.windowId = windowId;
//this.start();
}
public void start() {
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//CommonServerWindow.this.server();
while(true){
if(CommonServerWindow.this.server(commonCustomer))
continue;
else
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
/*protected void server() {
while (true) {
if (commonCustomer.getWaitingNum() > 0) {
String customerName = CommonServerWindow.this.commonCustomer.remove();
long currentTime = System.currentTimeMillis();
int serverTime = Util.MAX_SERVER_TIME - Util.MIN_SERVER_TIME;
System.out.println("普通窗口" + CommonServerWindow.this.windowId
+ "正在为 :" + customerName + " 服务");
try {
Thread.sleep((new Random().nextInt(serverTime) + 1) * 2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("普通窗口 " + CommonServerWindow.this.windowId
+ "为 :" + customerName + "服务耗时-->"
+ (System.currentTimeMillis() - currentTime) / 1000
+ "秒");
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
protected void server(boolean serverCommon) {
while (serverCommon) {
if (commonCustomer.getWaitingNum() > 0) {
String customerName = CommonServerWindow.this.commonCustomer
.remove();
long currentTime = System.currentTimeMillis();
int serverTime = Util.MAX_SERVER_TIME - Util.MIN_SERVER_TIME;
System.out.println("普通窗口" + CommonServerWindow.this.windowId
+ "正在为 :" + customerName + " 服务");
try {
Thread.sleep((new Random().nextInt(serverTime) + 1) * 2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("普通窗口 " + CommonServerWindow.this.windowId
+ "为 :" + customerName + "服务耗时-->"
+ (System.currentTimeMillis() - currentTime) / 1000
+ "秒");
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}*/
protected boolean server(Customer customer) {
if (customer.getWaitingNum() >= 1) {
String customerName = customer.remove();
//
// if(customerName == null){
// System.out.println(customerName);
// return false;
// }
long currentTime = System.currentTimeMillis();
int serverTime = Util.MAX_SERVER_TIME - Util.MIN_SERVER_TIME;
System.out.println("窗口" + CommonServerWindow.this.windowId
+ "正在为 :" + customer.getCustomerType() + " -- " + customerName + " 服务");
try {
if(customer.getCustomerType() == CustomerType.EXPRESS){
Thread.sleep((new Random().nextInt(Util.MIN_SERVER_TIME) +1) * 1000);
}
else
Thread.sleep((new Random().nextInt(serverTime) + 1) * 3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("窗口 " + CommonServerWindow.this.windowId
+ "为 :" + customer.getCustomerType() + " -- " + customerName + "服务耗时-->"
+ (System.currentTimeMillis() - currentTime) / 1000
+ "秒");
return true;
}
else
return false;
}
}
package bank;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExpressServerWindow extends CommonServerWindow {
private Customer commonCustomer = null;
private Customer expressCustomer = null;
private int windowId = 5;
public ExpressServerWindow(ServiceMachine serviceMachine, int windowId) {
super(serviceMachine, windowId);
// TODO Auto-generated constructor stub
commonCustomer = serviceMachine.getCommonCustomer();
expressCustomer = serviceMachine.getExpressCustomer();
this.windowId = windowId;
}
@Override
public void start() {
// TODO Auto-generated method stub
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ExpressServerWindow.this.server();
}
});
}
protected void server() {
// TODO Auto-generated method stub
boolean serverCommon = false;
while (true) {
if(this.server(expressCustomer))
continue;
else{
if(!this.server(commonCustomer))
continue;
}
/*if (expressCustomer.getWaitingNum() > 0) {
serverCommon = false;
String customerName = "--" + ExpressServerWindow.this.expressCustomer.remove() ;
long currentTime = System.currentTimeMillis();
//int serverTime = Util.MAX_SERVER_TIME - Util.MIN_SERVER_TIME;
System.out.println("快速窗口" + ExpressServerWindow.this.windowId
+ "正在为 :" + customerName + " 服务");
try {
Thread.sleep((new Random().nextInt(Util.MIN_SERVER_TIME) + 1) * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("快速窗口 " + ExpressServerWindow.this.windowId
+ "为 :" + customerName + "服务耗时-->"
+ (System.currentTimeMillis() - currentTime) / 1000
+ "秒");
} else {
serverCommon = true;
}
this.server(serverCommon);
*/
}
}
protected void server(boolean serverCommon) {
if (serverCommon) {
// System.out.println("窗口" + windowId + "开始服务!");
if (commonCustomer.getWaitingNum() > 0) {
//if(expressCustomer.getWaitingNum() > 0) serverCommon = false;
String customerName = ExpressServerWindow.this.commonCustomer.remove();
// if(customerName == null){
// continue;
// }
long currentTime = System.currentTimeMillis();
int serverTime = Util.MAX_SERVER_TIME - Util.MIN_SERVER_TIME;
System.out.println("快速窗口" + ExpressServerWindow.this.windowId
+ "正在为 :" + customerName + " 服务");
try {
Thread.sleep((new Random().nextInt(serverTime) + 1) * 2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("快速窗口 :" + ExpressServerWindow.this.windowId
+ "为 :" + customerName + "服务耗时-->"
+ (System.currentTimeMillis() - currentTime) / 1000
+ "秒");
} else {
}
}
}
}
package bank;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class VIPServerWindow extends CommonServerWindow {
private Customer commonCustomer = null;
private Customer vipCustomer = null;
private int windowId = 5;
public VIPServerWindow(ServiceMachine serviceMachine, int windowId) {
super(serviceMachine, windowId);
// TODO Auto-generated constructor stub
this.commonCustomer = serviceMachine.getCommonCustomer();
this.vipCustomer = serviceMachine.getVipCustomer();
this.windowId = windowId;
}
@Override
public void start() {
// TODO Auto-generated method stub
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
VIPServerWindow.this.server();
}
});
}
protected void server() {
// TODO Auto-generated method stub
while (true) {
if(this.server(vipCustomer))
continue;
else{
// if(!this.server(commonCustomer))//如果没有普通客户要服务,continue
// continue;
this.server(commonCustomer);
}
}
}
}
package bank;
public class Test {
public static void main(String[] args) {
ServiceMachine serviceMachine = ServiceMachine.getServiceMachine();
serviceMachine.start();
for(int i = 1; i <= 4; i++){
//System.out.println(i + "----------" + i);
new CommonServerWindow(serviceMachine,i).start();
}
new ExpressServerWindow(serviceMachine,5).start();
new VIPServerWindow(serviceMachine,6).start();
}
}
------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima