`
fcmfcm01
  • 浏览: 65743 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

银行排队的简单实现

阅读更多

这段时间在看Java并发编程方面的东西,注意到“生产者-消费者”模式,去某公司笔试的时候也遇到了这样的题,今天顺便把他用程序的方式写了下来。

 

UML就免了,不想画!顺便吐槽一下,小组开发,一定得用UML吗?随便画点图不行么?)

 

 

先上ServiceManager,它相当于大厅里的排号机,客户自己去排号,然后柜台的服务人员会去自动的取号:

 

/**
 * 
 */
package com.fcm.thread.banksample;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @author fenggcai
 * 
 */
public class ServiceManager {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//only 50 customers could be served a time 
		final BlockingQueue<FIFOEntry<Customer>> customerQueue = new PriorityBlockingQueue<FIFOEntry<Customer>>(
				50);

		for (int i = 0; i < 52; i++) {
			int randomLevel = new Double(Math.random() * 5).intValue();
			Customer c = new Customer("Customer" + i,
					CustomerLevel.values()[randomLevel]);

			new Thread(new CustomerService(customerQueue, c)).start();

		}

		for (int i = 0; i < 5; i++) {
			BankService bs = new BankService(customerQueue);
			new Thread(bs).start();
			try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
}

 

接下来是客户自助排号CustomerService,这里做了一个简单的优先级CustomerLevel:

package com.fcm.thread.banksample;

import java.util.concurrent.BlockingQueue;

public class CustomerService implements Runnable {

	private final BlockingQueue<FIFOEntry<Customer>> customerQueue;
	private final Customer rootCustomer;

	public CustomerService(BlockingQueue<FIFOEntry<Customer>> customerQueue,
			Customer rootCustomer) {
		super();
		this.customerQueue = customerQueue;
		this.rootCustomer = rootCustomer;
	}

	@Override
	public void run() {	
			try {
				addCustomer(rootCustomer);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		
	}

	private void addCustomer(Customer customer) throws InterruptedException {
		if (!customerQueue.contains(customer)) {
			try {
				customerQueue.add(new FIFOEntry<Customer>(customer));
			} catch (IllegalStateException e) {
				System.out.println("The queue is full!!");
				throw new InterruptedException("Reach capacity limit.");
			}
		}
	}
}


package com.fcm.thread.banksample;

public enum CustomerLevel {
	BELOW_NORMAL,NORMAL,VIP,GOLD_VIP,URGUNT;
}



 

对于customer本身,因为前文提到了优先级的问题,所以为了排序的需要,这里我加入了对Comparable接口的支持:

package com.fcm.thread.banksample;

public class Customer implements Comparable<Customer> {

	private String customerName;
	private CustomerLevel level;

	/**
	 * @return the customerName
	 */
	public String getCustomerName() {
		return customerName;
	}

	public Customer(String customerName, CustomerLevel level) {
		super();
		this.customerName = customerName;
		this.level = level;
	
	}

	/**
	 * @param customerName
	 *            the customerName to set
	 */
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	/**
	 * @return the level
	 */
	public CustomerLevel getLevel() {
		return level;
	}

	/**
	 * @param level
	 *            the level to set
	 */
	public void setLevel(CustomerLevel level) {
		this.level = level;
	}

	@Override
	public int compareTo(Customer o) {
		return o.getLevel().ordinal()-this.level.ordinal();
	}
	
	public String toString(){
		return (this.getCustomerName()+":"+this.getLevel().toString());
	}
}

 

我们可以想象,在同一时间到银行办业务的人,优先级(业务类型)一样的肯定很多,则必须得有个先来后到不是,所以我引入了FIFOEntry,来保证相应的顺序:

package com.fcm.thread.banksample;

import java.util.concurrent.atomic.AtomicInteger;

public class FIFOEntry<E extends Comparable<? super E>> implements
		Comparable<FIFOEntry<E>> {

	final static AtomicInteger seq = new AtomicInteger();
	final long seqNum;
	final E entry;

	public FIFOEntry(E entry) {
		seqNum = seq.getAndIncrement();
		this.entry = entry;
	}

	public E getEntry() {
		return entry;
	}

	@Override
	public int compareTo(FIFOEntry<E> other) {
		int result = entry.compareTo(other.entry);
		if (result == 0 && other.entry != this.entry) {
			result = (seqNum < other.seqNum ? -1 : 1);
		}
		return result;
	}

}

 好,到现在可以由银行的职员根据相应的优先级取回对应的工作了:

package com.fcm.thread.banksample;

import java.util.concurrent.BlockingQueue;

public class BankService implements Runnable {
	//working queue,it would be given by service manager
	private final BlockingQueue<FIFOEntry<Customer>> customerQueue;
	
	
	public BankService(BlockingQueue<FIFOEntry<Customer>> customerQueue) {
		super();
		this.customerQueue = customerQueue;
	}
	@Override
	public void run() {
		try {
			while(!customerQueue.isEmpty()){
				service(customerQueue.take().getEntry());
			}
		} catch (Exception e) {
			Thread.currentThread().interrupt();
		}
		
	}

	public void service(Customer c){
		System.out.println(c.getCustomerName()+":"+c.getLevel().toString());
	}
}

 

 

分享到:
评论

相关推荐

    银行排队叫号系统实现

    银行排队叫号系统源码,若发现bug,欢迎指正,谢谢。操作系统是windows,开发工具vs2008

    C语言编写的银行排队系统

    内含客户存取款、逗留时间、营业时间、银行接待客户人数等多种功能

    java实现简单的银行随机排队

    银行随机排队程序,使用的事随机洗牌的方法。老师给的,写的挺好的

    基于红帽Linux的银行排队叫号系统.pdf

    基于红帽Linux的银行排队叫号系统.pdf

    银行客户排队系统模拟

    客户到银行办理业务,需要取号排队等候。客户分为VIP客户、理财客户、一般客户三种类型。不同类型客户,取得不同的排队序号凭证,进入不同序列排队等候。当服务窗口出现空闲时,按既定策略从三种类型客户中选取客户...

    实现简单银行叫号模拟系统附代码(C++版)

    实现一个简单银行叫号模拟系统。银行有三个窗口可以同时办理业务,当有用户到达银行时,首先选择则既要办理的业务,可以选择一种或多种。系统计算办理此业务所需的时间并显示给用户,然后系统查看有无空闲的窗口,...

    银行排队代码

    //2)能比较简单,迅速地实现插入和删除,以实现开户和销户的需要。 #include #include #include using namespace std; int total; //初始时银行现存资金总额 int closeTime; //营业结束时间 int ...

    叫号系统排队系统挂号系统实现

    叫号系统排队系统实现 简单明了,注释清晰!

    银行排号叫号系统的实现

    银行排号叫号系统,该程序应该具有下列功能: get(取号)、call(叫号)、delete(删除号码)、count(获取当前排队总人数)、countN(获取号码N以前的排队人数)、reset(重置排号机)、quit(退出排号机)、login...

    利用Java实现的银行业务调度系统

    利用Java实现简单的银行业务窗口排队调度,包括多个窗口,利用线程池解决多线程问题。

    数据结构课程设计-银行业务模拟

    因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进和银行的客户。如果某个窗口的业务员正空闲,则可上前办理业务。反之,若个窗口均有客户所占,他便会排在为数最少的队伍后面。编制一个程序模拟银行的这种...

    C++简易银行叫号系统

    一个用C++简易的银行叫号系统,主要实现了排队功能,巧妙模拟了时间函数,实现的银行叫号过程的基本模拟。

    基于SSM的银行排队叫号系统.zip

    同时,框架的模块化结构也使得项目的开发、测试和维护更加简单和高效。 总之,这些项目利用Java语言和SSM框架的强大功能和优势,为各个领域的管理和服务提供了可靠的解决方案。无论是开展在线考试、管理医院分诊,...

    排队叫号系统软件说明

    本软件已广泛应用于银行、工商、税务、电信、医院、邮政、财政 局、劳动局、人事局、社保局、政府一站式服务厅、保险、车辆管理所、交通管理局、领事馆、出入境、海关、疾病预防控制中心、技术监督局、产品客户服务...

    C++课程设计:基于QT的银行排队模拟系统.zip

    而Qt又是基于C++一种语言的扩展,大家都知道C++ 有快速、简易、面向对象等很多优点,所以Qt自然也继承者C++这些的优点。 Qt良好的封装机制使得Qt的模块化程度非常高,可重用性较好,对用户开发来货是非常方便的。Qt...

    模拟银行叫号系统

    银行叫号系统:由于比较繁杂,做一个简化版本,实现普通矿口、VIP窗口、对公窗口的排队(队列)、叫号(出队)、窗口服务等功能,关于时间序列的部分,可以简单的用计数累加来实现。

    循环队列c++实现c++实现

    typedef struct { QElemType* base; int front; int rear; }SeqQueue;

    基于Java的银行排号系统的设计与实现.zip

    银行排号系统是为解决一些服务业营业大厅排队问题而设计的,它能够有效地提高工作人员的工作效率,也能够使顾客合理的安排等待时间,让顾客感到服务的公平公正。论文首先讨论了排号系统的背景、意义、应用现状以及...

    基于Java实现银行排号系统【源码+LW文档+PPT+数据库+讲解视频】

    银行排号系统是为解决一些服务业营业大厅排队问题而设计的,它能够有效地提高工作人员的工作效率,也能够使顾客合理的安排等待时间,让顾客感到服务的公平公正。论文首先讨论了排号系统的背景、意义、应用现状以及...

    c++银行系统

    c++使用典例,c++用于银行叫号排队系统,简单实现,更易懂的清晰,但仍然有部分bug,使用时最好按照自己的要求进行相应更改!

Global site tag (gtag.js) - Google Analytics