abstract class Approver
{
protected String name;
protected Approver successor;
public Approver(String name)
{
this.name=name;
}
public void SetSuccessor(Approver successor)
{
this.successor=successor;
}
public abstract void ProcessRequest(PurchaseRequest request);
}
class Director extends Approver
{
public Director(String name)
{
super(name);
}
public void ProcessRequest(PurchaseRequest request)
{
if(request.getAmount()<10000.0)
{
System.out.println("Director " + name + " handles the Request:" + request.getNumber() + ",amount is " + request.getAmount());
}
else
{
if(successor!=null)
successor.ProcessRequest(request);
}
}
}
class ViceCEO extends Approver
{
public ViceCEO(String name)
{
super(name);
}
public void ProcessRequest(PurchaseRequest request)
{
if(request.getAmount()<50000.0)
{
System.out.println("ViceCEO " + name + " handles the Request:" + request.getNumber() + ",amount is " + request.getAmount());
}
else
{
if(successor!=null)
successor.ProcessRequest(request);
}
}
}
class CEO extends Approver
{
public CEO(String name)
{
super(name);
}
public void ProcessRequest(PurchaseRequest request)
{
if(request.getAmount()<100000.0)
{
System.out.println("CEO " + name + " handles the Request:" + request.getNumber() + ",amount is " + request.getAmount());
}
else
{
System.out.println("Need a meeting for Request:" + request.getNumber() + ",amount is " + request.getAmount());
}
}
}
class PurchaseRequest
{
private int number;
private double amount;
private String purpose;
public PurchaseRequest(int number,double amount,String purpose)
{
this.number=number;
this.amount=amount;
this.purpose=purpose;
}
public double getAmount()
{
return amount;
}
public void setAmount(double amount)
{
this.amount=amount;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number=number;
}
public String getPurpose()
{
return purpose;
}
public void setPurpose(String purpose)
{
this.purpose=purpose;
}
}
class Client
{
public static void main(String[] a)
{
Director zhangsan=new Director("zhangsan");
ViceCEO lisi=new ViceCEO("lisi");
CEO wangwu=new CEO("wangwu");
zhangsan.SetSuccessor(lisi);
lisi.SetSuccessor(wangwu);
PurchaseRequest pr1=new PurchaseRequest(1001,6000,"Project1");
zhangsan.ProcessRequest(pr1);
PurchaseRequest pr2=new PurchaseRequest(1002,16000,"Project2");
zhangsan.ProcessRequest(pr2);
PurchaseRequest pr3=new PurchaseRequest(1003,86000,"Project3");
zhangsan.ProcessRequest(pr3);
PurchaseRequest pr4=new PurchaseRequest(1004,186000,"Project4");
zhangsan.ProcessRequest(pr4);
}
}
评论0