JAVA多线程作业-多人买票

JAVA多线程作业

作业要求:

1、 假设现在有一个售票员进行售票,票价为5元,初始状态:票数不限,票售员手中有1张10元钱;

2、 每来一个顾客买票,相当于是创建一个线程,注意,此时顾客共享的资源是售票员及其手中的钱;

3、 当一个顾客到达后相当于创建一个线程,创建该线程时有两个参数,一是线程名,也就是顾客的名字,二是顾客带的钱(规定顾客带的钱只能为5元,10元,20元和50元)。

4、 某一个顾客买票时,如果售票员无法找零,则让该顾客等待,如果某一个顾客买票成功,则唤醒所有等待的顾客。

5、 主线程中用一个死循环来实现持续售票,可提供选择,是继续售票还是终止。

售票员售票,票价5元一张,假设只有5元,10元,20元,50元和100元五种币种,售票员手上有若干钱(自己初始化),顾客会拿这一张(5元,10元,20元,50元中的一张)钱来购票,设计一个算法,根据售票员手上的钱和顾客拿的钱判断是否可以售票?

代码:

package cxx_Maipiao;

public class Conductor implements Runnable{

	int  x[]=new int [5];
    private int bk=0;
	Conductor()
	{
		
	}
	//判断是否能找钱成功
	public boolean check(int money)
	{
		int a[] =new int [5];
		System.arraycopy(x, 0, a, 0, 5);
		int needchange=money-5;
		if(needchange==0) return true;
		else
		{
			int tp=0;
			for(int i=4;i>=0;i--) 
			{
				if(a[i]>0) 
				{
					if(i==4) tp=100;
					else if(i==3) tp=50;
					else if(i==2) tp=20;
					else if(i==1) tp=10;
					else if(i==0) tp=5;
					
					while(needchange-tp>=0)
					{
						needchange-=tp;
						a[i]--;
						if(a[i]<=0) break;
					}
				
				 
				if(needchange==0) {
				System.arraycopy(a, 0, x, 0, 5);
				return true;
				}
			}
			}
			return false;
		}
	}
	
	@Override
	public void run() {
		// TODO 自动生成的方法存根
		String sst=Thread.currentThread().getName();
		String sstr[]=sst.split("-");
		int money=Integer.valueOf(sstr[1]);
		if(bk==0) x[1]=1;
		synchronized(this)
		{
			boolean fg=false;
			while(fg==false)
			{
				String st=Thread.currentThread().getName();
				String str[]=st.split("-");
				 money=Integer.valueOf(str[1]);
				//System.out.println(money+"qqqqqq");
				if(check(money)==true)
				{
					System.out.println(Thread.currentThread().getName()+"买到票");
					if(money==5) x[0]++;
					else if(money==10) x[1]++;
					else if(money==20) x[2]++;
					else if(money==50) x[3]++;
					else x[4]++;
					
					for(int j=0;j<5;j++)
						 System.out.print("x["+j+"] " +x[j]+"  ");
						 System.out.println("");
				   super.notifyAll();
				   fg=true;
				   bk=1;
				}
				else
				{
					try
					{
						System.out.println(Thread.currentThread().getName()+"没买到票");
						for(int j=0;j<5;j++)
							 System.out.print("x["+j+"] " +x[j]+"  ");
							 System.out.println("");
						super.wait();
					}catch(InterruptedException e)
					{
						e.printStackTrace();
					}
				}
			}
		}
	}
}

package cxx_Maipiao;

import java.util.Scanner;

public class Test {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Conductor st=new Conductor();
		String name;
		Scanner input=new Scanner(System.in);
		int imoney,flag;
		flag=1;
		while(flag==1)
		{
			System.out.println("input name:");
			name=input.next();
			System.out.println("input money");
			imoney=input.nextInt();
			Thread th=new Thread (st,name+"-"+imoney);
			th.start();
			System.out.println("1 continue; 0 end");
			flag=input.nextInt();
		}
	}

}

写个博客防忘记~感谢xu zong 非常友好的代码非常“有耐心的”找bug​~~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值