}
}
}
public boolean change(float result){
//判断money是否是5的倍数,不是就舍去个位数转成5的倍数
float temp=result/5;
result=5*temp;
//操作的map只是暂时的,如果返回的是false那么要恢复到原来的
Map
tempMap.putAll(this.balance);
for(int i=0;i
if(result==type[i]){
result-=type[i];
result+=this.delete(this.strType[i],1,type[i],tempMap);
}else{
float temp1,temp2,temp3,temp4;
if((temp1=result/50)>0){
result-=50*(int)temp1;//应该减去这么多的
result+=this.delete(this.strType[3],(int)temp1,50*(int)temp1,tempMap);//加上没有减掉的
}
if((temp2=result/20)>0){
result-=20*(int)temp2;
result+=this.delete(this.strType[2],(int)temp2,20*(int)temp2,tempMap);
}
if((temp3=result/10)>0){
result-=10*(int)temp3;
result+=this.delete(this.strType[1],(int)temp3,10*(int)temp3,tempMap);
}
if((temp4=result/5)>0){
result-=5*(int)temp4;
result+=this.delete(this.strType[0],(int)temp4,10*(int)temp4,tempMap);
}
}
if(result==0.0){
this.balance=null;
this.balance=tempMap;
return true;
}else{
}
}
}
return false;
}
//售票
public synchronized void saleTicket(int count,float money){
float sum=count*this.TICKET_PRICE;//购买票需要总金额
float result=0.0F;//应找零
if(money
return;
}else if(count>this.getTicket()){
System.out.println(Thread.currentThread().getName()+"退还钱,对不起,今天的票只剩下"+this.getTicket()+"张了");
return;
}else{
this.separate(money);
result=money-sum;
}
if(result==0.0){
System.out.println(Thread.currentThread().getName()+"恭喜你买票成功,谢谢惠顾");
return;
}else{
if(this.change(result)){
System.out.println(Thread.currentThread().getName()+"应找你"+result+"元,请收好找零,欢迎下次惠顾");
}else{
Thread changeTask=new ChangeTask(this,result);//后台找零程序
changeTask.setDaemon(true);//设置为后台线程,需要在start之前
changeTask.start();
try {
System.out.println(Thread.currentThread().getName()+"请等待。。。");
this.wait();//暂停等待
System.out.println(Thread.currentThread().getName()+"对不起让你久等了,现在有零钱了,应找你"+result+"元,欢迎下次惠顾");
} catch (InterruptedException e) {
e.printStackTrace();