建立SaleCD与RentCD的类
该CD租售店,具有可租cd列表,可售cd列表。并具有租、还cd,销售、进货CD方法。假设cd店中可租CD列表10张,可售CD列表10种,每种10张,并实现紧急调货,补货,销售,租赁,归还等线程。
其中包括销售的种类及每种的数目,以及租赁的数目,还包括两个类的构造函数
class SaleCD
{
String Name;
int count;
SaleCD(String Name, int count)
{
this.Name=Name;
this.count=count;}
}
class RentCD
{
String Name;
int ID;
int count;
boolean isRent=false;
RentCD(String Name, int id,int count)
{
this.Name=Name;
this.ID=id;
this.count=count;
}}
构建CDShop类
用数组类的方法,不如直接使用vector类中的add()和get()方法,用for语句初始化每一个类以及用get(i)来引用类以及类中元素,这样代码更加简洁。
public class CDShop {
Vector <RentCD> vRCD=new Vector<RentCD>();
Vector <SaleCD> vSCD=new Vector<SaleCD>();
FileWriter f;
CDShop(FileWriter f)
{
for (int i=1;i<11;i++)
{
vRCD.add(new RentCD("RentCD"+i,i,10));
vSCD.add(new SaleCD("SaleCD"+i,10));
}
this.f=f;
}
synchronized void rentCD()
{
Random r=new Random();
int i=r.nextInt(1);
RentCD rcd=vRCD.get(i);
int num= r.nextInt(8)+2;
System.out.println(new Date()+"尝试租借" +" "+num+"张"+
"");
try
{
f.write(new Date()+"尝试租借" +" "+num+"张"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
{
while (num>rcd.count)
{
System.out.println(new Date()+"租借" +"已借出");
try
{
f.write(new Date()+"租借" +"已借出"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
if (r.nextBoolean()==false)
{
System.out.println(new Date()+"租借" +"不成功于是选择放弃");
try
{
f.write(new Date()+"租借" +"不成功于是选择放弃"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
return;
}
}
rcd.isRent=true;
System.out.println(new Date()+"成功租借" + " "+num+"张");
try
{
f.write(new Date()+"成功租借" +" "+ num+"张"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
rcd.count=rcd.count-num;
try {
wait(200+r.nextInt(100));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rcd.isRent=false;
System.out.println(new Date()+"归还" +" "+(10-rcd.count)+"张");
try
{
f.write(new Date()+"归还" +" "+(10-rcd.count)+"张"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
rcd.count=10;
}
}
synchronized void saleCD()
{
Random rand=new Random();
int i= rand.nextInt(10);
SaleCD scd=vSCD.get(i);
int num= rand.nextInt(8)+2;
System.out.println(new Date()+"尝试购买"+scd.Name+" "+num+"张");
try
{
f.write(new Date()+"尝试购买"+scd.Name+" "+num+"张"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
while(num>scd.count)
{
System.out.println( new Date()+scd.Name+"缺货");
try
{
f.write( new Date()+scd.Name+"缺货"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
if(rand.nextBoolean()==true)
{
if(rand.nextBoolean()==true)
{
System.out.println(new Date()+"由于很想购买"+scd.Name+"于是紧急启动进货行动");
try
{
f.write(new Date()+"由于很想购买"+scd.Name+"于是紧急启动进货行动"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
new Order_requestTrd(this,i,f);
}
else
{
try
{
System.out.println(new Date()+scd.Name+"缺货"+"选择等待");
try
{
f.write(new Date()+scd.Name+"缺货"+"选择等待"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
wait(200);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
if(rand.nextBoolean()==false)
{
System.out.println( new Date()+scd.Name+"缺货"+"选择放弃");
try
{
f.write(new Date()+scd.Name+"缺货"+"选择放弃"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
System.out.println( new Date()+"成功购买"+scd.Name+" "+num+"张");
try
{
f.write( new Date()+"成功购买"+scd.Name+" "+num+"张"+"\r\n");
}
catch (IOException e)
{
e.printStackTrace();
}
scd.count=scd.count-num;
}
public static void main(String[] args) throws InterruptedException {
FileWriter f=null;
try
{
f=new FileWriter("record.txt");
}
catch (IOException e)
{
e.printStackTrace();
}
CDShop c=new CDShop(f);
RControlTrd rt=new RControlTrd(c);
SControlTrd st=new SControlTrd(c);
OrderTrd ot=new OrderTrd(c,f);
Thread.sleep(120*1000);
}}
总结
在选择等待还是补货时我们可以用random类里的nextBoolean()方法用于从该随机数生成器的序列得到下一个伪均匀分布的布尔值,来随机选择等待还是紧急补货。