黑马程序员-自己写的银行调度系统

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

自己写的银行调度系统


张老师讲银行调度系统题目及结果演示时,就迫不及待的先开始了自己的实现。

设计要求:
1.模拟客户到来,有三种客户:普通,快速和VIP,比例是6:3:1
2.银行有6个业务窗口:1-4普通,5快速,6 VIP
3.每个客户所需求的时间在最大值和最小值之间,快速用于为最小值
4.客户按顺序办理业务(排队)
5.VIP和快速窗口空闲时(没人排队)可以为普通用户服务


一、准备工作
1.需要7个线程,1个用于模拟客户的到来(GenerateClientThread);其他6个模拟6个服务窗口(WindowThread)
2.需要3个队列,分别用于普通用户,快速用户,VIP用户排队。GenerateClientThread负责随机生成用户并放入队列。WindowThread负责从相应的队列中取用户,并为他服务。
3.每个客户Client有这些属性:业务类型type(普通,快速还是VIP),在该业务类型中的排队编号number,,需要时间(在最大值最小值内随机生成,快速用户为最小值)serverTime。以及相应的get,set方法。
4.怎么实现队列?暂时还没搞懂Java中队列怎么实现,这里就先用ArrayList替代。队列中存放Client对象,所以定义为ArrayList<Client>类型。自己编写进队(inQ)和出队(outQ)方法。
共有三个队列,每个队列都是单例模式。
5.怎么模拟产生客户。建立一个客户产生器(ClientGenerator)类,因为只能有一个产生器,所以定义为单例模式。提供generateClient方法按照比例产生Client对象并初始化随机生成的服务时间。GenerateClientThread线程每隔一段时间生成一个客户,并加入到相应的队列中去。WindowThread线程每隔一段时间从相应的队列中取出用户,特别注意,取用户操作一定要加同步锁,防止多个窗口同时取一个队列导致的错误。


二、主函数
用于程序的入口,里面实现了7个线程的定义和启动。即:1个模拟客户生成的线程(GenerateClientThread)和6个服务窗口线程(WindowThread)。因为他们是独立工作的,所以使用线程独立运行。
public static void main(String[] args)
{
GenerateClientThread thread0 = new GenerateClientThread();
WindowThread1 wt1 = new WindowThread1();
WindowThread2 wt2 = new WindowThread2();
WindowThread3 wt3 = new WindowThread3();
WindowThread4 wt4 = new WindowThread4();
WindowThread5 wt5 = new WindowThread5();
WindowThread6 wt6 = new WindowThread6();
thread0.start();
wt1.start();
wt2.start();
wt3.start();
wt4.start();
wt5.start();
wt6.start();
}
(省略线程的定义)


三、服务窗口的定义


//服务窗口的抽象类,维护窗口类型和窗口编号两个属性,提供服务方法
public abstract class ServerWindow
{
private int WindowNumber;
private int WindowType;
public int getWindowNumber()
{
return WindowNumber;
}
public int getWindowType()
{
return WindowType;
}

public String getWindowTypeName()
{
String typeName = null;
switch(this.getWindowType())
{
case Client.COMMON: 
typeName = "普通 ";
break;
case Client.FAST:
typeName = "快速 ";
break;
case Client.VIP:
typeName = "VIP ";
break;
default: break;
}
return typeName;
}
public ServerWindow(int number, int type)
{
this.WindowNumber = number;
this.WindowType = type;
}
//每种窗口获取相应的客户的方法是不同的(队列不同),所以定义为抽象方法,等继承的实现类来完成。
public abstract Client fech();
//获取普通队列中的客户,这个方法是公用且已知的。
protected synchronized Client fechCom()
{
BankQueue queue = CommonQueue.getInstance();
Client client = queue.outQ();
return client;
}
//启动服务的入口
public Client serve()
{
//从自己的队列中获取用户
Client client = fech();
if(client != null)
{
serveFor(client);//为这个客户服务
}
//如果自己的队列中没有用户排队,则为普通用户服务(非普通窗口干的事)
else if(this.getWindowType() != Client.COMMON)
{
client = fechCom();
if(client != null)
serveFor(client);
}
return client;
}
private void serveFor(Client client)
{
System.out.println("服务: "+this.WindowNumber+"号 ["+this.getWindowTypeName()+"] 窗口开始为"+"第"+client.getNumber()+"号"+client.getTypeName()+"服务");
}

}


class CommonServerWindow extends ServerWindow
{
public CommonServerWindow(int number, int type)
{
super(number, type);
// TODO Auto-generated constructor stub
}
@Override
public synchronized Client fech()
{//fech这个方法一定要加上synchronized修饰符,防止多线程同时访问。
return fechCom();
}
}


class FastServerWindow extends ServerWindow
{


public FastServerWindow(int number, int type)
{
super(number, type);
// TODO Auto-generated constructor stub
}


@Override
public synchronized Client fech()
{
BankQueue queue = FastQueue.getInstance();
Client client = queue.outQ();
return client;
}

}


class VIPServerWindow extends ServerWindow
{


public VIPServerWindow(int number, int type)
{
super(number, type);
// TODO Auto-generated constructor stub
}


@Override
public synchronized Client fech()
{
BankQueue queue = VIPQueue.getInstance();
Client client = queue.outQ();
return client;
}

}


四、客户产生器
public class ClientGenerator
{//客户产生器,专门用于模拟客户的到来,模拟客户(普通,快速,VIP,6:3:1)
private static ClientGenerator generator = new ClientGenerator();
private static long commonNumberBase = 1;
private static long fastNumberBase = 1;
private static long VIPNumberBase = 1;
private ClientGenerator()
{
}
public static ClientGenerator getInstance()
{
return generator;
}
public Client generateClient()
{
Client client = new Client();
double num = Math.random();
double time = Math.random();
int number = (int)(num * 100);
if(number < 10)
{
client.setType(Client.VIP);
client.setNumber(VIPNumberBase);
time = time * 7000 + 500;
client.setServerTime((long)time);
VIPNumberBase++;
}
else if(number < 40)
{
client.setType(Client.FAST);
client.setNumber(fastNumberBase);
client.setServerTime(500);
fastNumberBase++;
}
else 
{
client.setType(Client.COMMON);
client.setNumber(commonNumberBase);
time = time * 7000 + 500;
client.setServerTime((long)time);
commonNumberBase++;
}

return client;
}
}


五、客户类的定义
package com.snow;
class Client
{
public static final int COMMON = 0;
public static final int FAST = 1;
public static final int VIP = 2;
private int type;
private long number;
private long serverTime;
public int getType()
{
return type;
}

public String getTypeName()
{
String typeName = null;
switch(this.getType())
{
case COMMON: 
typeName = "普通用户";
break;
case FAST:
typeName = "快速用户";
break;
case VIP:
typeName = "VIP用户";
break;
default: break;
}
return typeName;
}


public void setType(int type)
{
this.type = type;
}


public long getNumber()
{
return number;
}
public void setNumber(long number)
{
this.number = number;
}
public void setServerTime(long serverTime)
{
this.serverTime = serverTime;
}
public long getServerTime()
{
return serverTime;
}
@Override
public String toString()
{
return "Client [number=" + number + ", type=" + this.getTypeName() + "]";
}
}

六、队列的定义
abstract class BankQueue
{


public void inQ(Client client)
{
this.getList().add(client);
}
public synchronized Client  outQ()
{
Client client = null;
List<Client> list = this.getList();
if(!list.isEmpty())
client = list.remove(0);
return client;
}
public abstract int getLength();
public abstract List<Client> getList();
}


class CommonQueue extends BankQueue
{
private static List<Client> list = new ArrayList<Client>();
private static CommonQueue queue = new CommonQueue();
private CommonQueue()
{
}
public static BankQueue getInstance()
{
return queue;
}

@Override
public int getLength()
{
// TODO Auto-generated method stub
return list.size();
}
@Override
public List<Client> getList()
{
// TODO Auto-generated method stub
return list;
}
}


class FastQueue extends BankQueue
{
private static List<Client> list = new ArrayList<Client>();
private static FastQueue queue = new FastQueue();
private FastQueue()
{
}
public static BankQueue getInstance()
{
return queue;
}

@Override
public int getLength()
{
// TODO Auto-generated method stub
return list.size();
}
@Override
public List<Client> getList()
{
// TODO Auto-generated method stub
return list;
}
}
class VIPQueue extends BankQueue
{
private static List<Client> list = new ArrayList<Client>();
private static VIPQueue queue = new VIPQueue();
private VIPQueue()
{
}
public static BankQueue getInstance()
{
return queue;
}

@Override
public int getLength()
{
// TODO Auto-generated method stub
return list.size();
}
@Override
public List<Client> getList()
{
// TODO Auto-generated method stub
return list;
}


}

不多说,继续看视频,看看老师是怎么实现的。

android培训java培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net/heima
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值