黑马程序员技术交流社区
标题:
模拟火车票售票系统(多线程运用)
[打印本页]
作者:
as604049322
时间:
2014-12-23 20:52
标题:
模拟火车票售票系统(多线程运用)
弄了一整天,没弄好,老是在开头出现空指针异常坏了2个售票窗口,望高人指定。我表示多线程好难玩,
import java.util.LinkedList;
class ThreadTest
{
public static void main(String[] args)
{
new ProduceTicketCenter(10000,"产票中心1").start();
new ProduceTicketCenter(5000,"产票中心2").start();
new ProduceTicketCenter(7000,"产票中心3").start();
try{Thread.sleep(100);}catch(Exception e){}
new SaleTicketWindow("售票窗口1").start();
new SaleTicketWindow("售票窗口2").start();
new SaleTicketWindow("售票窗口3").start();
new SaleTicketWindow("售票窗口4").start();
}
}
class SaleTicketWindow extends Thread//售票窗口
{
private static Ticket t=Ticket.getInstanceof();
private static final int MAX_TICKET=200;//每个售票窗口最多放200张票
private LinkedList<Integer> tickWin=new LinkedList<Integer>();//当前窗口的票
boolean flag=true;
SaleTicketWindow(String name){
super(name);
}
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("编号:"+Integer.toHexString(number).toUpperCase());
}else{
getTickettoWindow();
}
}
public synchronized void getTickettoWindow(){
while(t.isTickNoNull() && tickWin.size()<MAX_TICKET ){
tickWin.add(t.getTicket());
}
}
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,2060);//出生年份为1900~2060年
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;
}
public void changeFlag(){
flag=!flag;
}
}
class ProduceTicketCenter extends Thread//产票中心
{
private static Ticket t=Ticket.getInstanceof();
boolean flag=true;
private int max;//代表当前产票中心的产票数量
private int num=0;//当前产票中心产出的票数
ProduceTicketCenter(int max,String name){
super(name);
this.max=max;
}
public void run(){
while(flag){
flag=t.ProduceTicket();
System.out.println(Thread.currentThread().getName()+":产生第"+(++num)+"张票,所有中心总共产生:"+t.getTotal());
if(num>=max)
flag=false;
}
}
public void changeFlag(){
flag=!flag;
}
}
class Ticket
{
private LinkedList<Integer> tickList=new LinkedList<Integer>();
private int number=700000;//票的起始编号为70万
private final int MAX=900000;//最大票号为90万
private static Ticket t=new Ticket();
private Ticket(){}//单例设计模式
public static Ticket getInstanceof(){
return t;
}
public synchronized void printList(){
for(int num:tickList)
System.out.print(num+",");
}
public synchronized boolean ProduceTicket(){
try{Thread.sleep(20);}catch(Exception e){}
if(number<=MAX){
tickList.add(number++);
return true;
}
return false;
}
public int getTicket(){
return tickList.removeFirst();
}
public int getTotal(){
return number-700000;
}
public boolean isTickNoNull(){
if(tickList.size()>0)
return true;
else
return false;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2