黑马程序员技术交流社区
标题:
游乐园售票过程模拟(毕老师售票例子终极升级版)
[打印本页]
作者:
as604049322
时间:
2014-12-24 12:56
标题:
游乐园售票过程模拟(毕老师售票例子终极升级版)
本帖最后由 as604049322 于 2014-12-24 16:10 编辑
需求分析:
有一个游乐场,产票中心共有N张票,每张票有一个独一的编号(每张票用集合的一个元素表示)
每个售票窗口最大能装200张票,会在启动前从产票中心补满
售票窗口有上班和下班的操作,
下班有2种情况,票卖完或者公司统一下班
每次上班前都会检查当前的窗口的票是否为空,不为空直接上班,为空则从产票中心补满。
每个售票窗口工作人员为一人,一旦上班便开始卖票,每卖一张票打印买票人信息
买票人信息为电脑生成的随机人物
-------------------------------------------------------------
代码经过三次重写,现在测试,暂时没有发现问题。望马友友多多指证改进
import java.util.LinkedList;
class SaleTicket
{
public static void main(String[] args)
{
Ticket t=Ticket.getInstanceof();
SaleTicketWindow s1=new SaleTicketWindow("售票窗口1");
SaleTicketWindow s2=new SaleTicketWindow("售票窗口2");
SaleTicketWindow s3=new SaleTicketWindow("售票窗口3");
SaleTicketWindow s4=new SaleTicketWindow("售票窗口4");
int num=0;
while(num<20){
s1.goWork();
s2.goWork();
s3.goWork();
s4.goWork();
try{Thread.sleep(5000);}catch(Exception e){}//每隔5秒钟就集体下班
s1.afterWork();
s2.afterWork();
s3.afterWork();
s4.afterWork();
try{Thread.sleep(5000);}catch(Exception e){}//5秒钟后再集体下班
num++;
}
}
}
class Ticket//产票中心
{
private LinkedList<Integer> tickList=new LinkedList<Integer>();
private final int START_NUM=700001;//票的起始编号为3001
private final int TOTAL=5000;//产票中心总共生产5000张票
//单例设计模式
private static final Ticket t=new Ticket();
private Ticket(){//初始化产票中心
for(int i=START_NUM;i<START_NUM+TOTAL;i++)
tickList.add(i);
}
public static Ticket getInstanceof(){
return t;
}
public synchronized void printListre(){
if(isTickNoNull())
System.out.print(tickList);//打印产票中心剩下的票
}
public synchronized boolean isTickNoNull(){
if(tickList.size()>0)
return true;
else
return false;
}
public synchronized LinkedList<Integer> getTicket(int num){//num代表获取的票的数量
LinkedList<Integer> result=new LinkedList<Integer>();
for(int i=0;i<num;i++)
if(isTickNoNull())
result.add(tickList.removeFirst());
return result;
}
}
class SaleTicketWindow//售票窗口
{
private static final Ticket t=Ticket.getInstanceof();
private static final int MAX_TICKET=50;//每个售票窗口最多放200张票
private LinkedList<Integer> tickWin=new LinkedList<Integer>();//当前窗口的票
boolean flag=false;//售票状态
String name;//窗口名字
SaleTicketWindow(String name){
this.name=name;
}
class Run implements Runnable{
public void run(){
while(flag)
sale();
}
}
public synchronized void sale(){
if(tickWin.size()>0){
try{Thread.sleep(100);}catch(Exception e){}
int number=tickWin.removeLast();
System.out.println(Thread.currentThread().getName()+":编号为"+number+"的票被卖出!信息:");
System.out.println("姓名:"+creatRandomName());
String sex=creatRandomSex();
System.out.println("性别:"+sex);
System.out.println("身份证号:"+creatRandomIDCard(sex));
System.out.println("编号:"+getNumber(number));
}else{
afterWork();
}
}
public void goWork(){//上班
if(flag==false){
if(tickWin.size()==0)
tickWin=t.getTicket(MAX_TICKET);
flag=true;
new Thread(new Run(),name).start();
}
}
public void afterWork(){//下班
flag=false;
}
public static String creatRandomSex(){
if((int)(Math.random()*2)==1)
return "男";
else
return "女";
}
public static String creatRandomName(){
String[] firstName={"王","李","张","刘", "陈","杨","黄","赵", "周","吴","徐","孙", "马","胡","朱","郭", "何","罗","高","林",
"郑","梁","谢","唐", "许","冯","宋","韩", "邓","彭","曹","曾", "田","于","肖","潘", "袁","董","叶","杜",
"丁","蒋","程","余", "吕","魏","蔡","苏", "任","卢","沈","姜", "姚","钟","崔","陆", "谭","汪","石","付",
"贾","范","金","方", "韦","夏","廖","侯", "白","孟","邹","秦", "尹","江","熊","薛", "邱","闫","段","雷",
"季","史","陶","毛", "贺","龙","万","顾", "关","郝","孔","向", "龚","邵","钱","武", "扬","黎","汤","戴",
"严","文","常","牛", "莫","洪","米","康", "温","代","赖","施"};
if((int)(Math.random()*2)==0)
return firstName[(int)(Math.random()*112)]+(char)(Math.random()*10000+21834);
else
return firstName[(int)(Math.random()*112)]+(char)(Math.random()*10000+21834)+(char)(Math.random()*10000+21834);
}
/*
(1)前1、2位数字表示:所在省份的代码;
身份证号与省份的关系 11 北京市;12 天津市;13 河北省;14 山西省;15 内蒙古自治区;21 辽宁省;22 吉林省;23 黑龙江省;31 上海市;32 江苏省;33 浙江省;
34 安徽省;35 福建省;36 江西省;37 山东省;41 河南省;42 湖北省;43 湖南省;44 广东省;45 广西壮族自治区;46 海南省;50 重庆市;51 四川省;52 贵州省;
53 云南省;54 西藏自治区;61 陕西省;62 甘肃省;63 青海省;64 宁夏回族自治区;65 新疆维吾尔自治区;71 台湾省;81 香港特别行政区;82 澳门特别行政区
(2)第3、4位数字表示:所在城市的代码;
(3)第5、6位数字表示:所在区县的代码;
(4)第7~14位数字表示:出生年、月、日;
(5)第15、16位数字表示:所在地的派出所的代码;
(6)第17位数字表示性别:奇数表示男性,偶数表示女性;
第十八位数字的计算方法为:
1.将前面的身份证号码17位数分别乘以不同的系数。
从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
2.将这17位数字和系数相乘的结果相加。
3.用加出来和除以11,看余数是多少?
4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。
其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2。
*/
public static String creatRandomIDCard(String man){
String IDCard="";
int[] province={11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82};
int temp=province[(int)(Math.random()*province.length)];//省代码
IDCard+=temp;
temp=getRandomNum(1,22);//市代码,没搞不清楚,随机生成1~22.
IDCard+=addNum(temp);
temp=getRandomNum(1,30);//市代码,没搞不清楚,随机生成1~30.
IDCard+=addNum(temp);
temp=getRandomNum(1900,2005);//出生年份为1900~2005年
IDCard+=temp;
temp=getRandomNum(1,12);//月份1~12
IDCard+=addNum(temp);
temp=getRandomNum(1,30);//日1~30.
IDCard+=addNum(temp);
temp=getRandomNum(1,92);//所在地的派出所的代码,没搞不清楚,随机生成1~92.
IDCard+=addNum(temp);
if(man=="男")
temp=(((int)(Math.random()*5))<<1)+1;
else
temp=((int)(Math.random()*5))<<1;
IDCard+=temp;
int[] expon=new int[]{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
String[] checkSheet=new String[]{"1","0","X","9","8","7","6","5","4","3","2"};
int sum=0;
for(int i=0;i<expon.length;i++)
sum+=new Integer(IDCard.charAt(i))*expon[i];
IDCard+=checkSheet[sum%11];
return IDCard;
}
private static String addNum(int num){
if(num<10)
return "0"+num;
else
return num+"";
}
private static int getRandomNum(int start,int end){
return (int)(Math.random()*(end-start+1))+start;
}
private String getNumber(int num){
String result=Integer.toHexString(num).toUpperCase();
while(result.length()<8)
result="0"+result;
return result;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2