职责链模式的入口
public class OnLineChain
{
OnLineIsCardExit onLineIsCardExit = new OnLineIsCardExit();
OnLineLimitCash onLineLimitCash = new OnLineLimitCash();
OnLineIsOnLine onLineIsOnLine = new OnLineIsOnLine();
OnLineCardOnLine onLineCardOnLine = new OnLineCardOnLine();
public Hashtable doHandler(string cardID)
{
onLineIsCardExit.SetSuccessor(onLineLimitCash);
onLineLimitCash.SetSuccessor(onLineIsOnLine);
onLineIsOnLine.SetSuccessor(onLineCardOnLine);
return onLineIsCardExit.HandleRequest(cardID);
}
}
用于上机的抽象类
public abstract class OnLineHandler
{
protected OnLineHandler successor;
protected CustomerFactory customerFactory = new CustomerFactory();
protected BasicdataFactory basicdataFactory = new BasicdataFactory();
protected CustomerEntity customerEntity = new CustomerEntity();
protected LineFactory lineFactory = new LineFactory();
protected LineEntity lineEntity = new LineEntity();
public void SetSuccessor(OnLineHandler successor)
{
this.successor = successor;
}
public abstract Hashtable HandleRequest(string cardID);
}
用于正常上机的情况
public class OnLineCardOnLine:OnLineHandler
{
public override Hashtable HandleRequest(string cardID)
{
Hashtable hashtable = new Hashtable();
CustomerIDAL customerIdal = customerFactory.Customer();
List<CustomerEntity> listCustomer = customerIdal.getCustomerByCardID(cardID);
string cType = listCustomer[0].cType;
decimal cCash = Convert.ToDecimal(listCustomer[0].cCash);
string cName = listCustomer[0].cName;
RateManager rm = new RateManager();
decimal rate = rm.getRate(cType);
string datetime = customerIdal.getTime();
lineEntity.cardID = cardID;
lineEntity.cName = cName;
lineEntity.lineState = Constant.ONLINE;
lineEntity.onDateTime = Convert.ToDateTime(datetime);
lineEntity.type = cType;
lineEntity.computer = Environment.MachineName;
LineIDAL lineIDAL = lineFactory.line();
lineIDAL.insertOnLine(lineEntity);
hashtable.Add("cType", cType);
hashtable.Add("cCash", cCash);
hashtable.Add("cName", cName);
hashtable.Add("rate", rate);
hashtable.Add("datetime", datetime);
return hashtable;
}
}
用于卡号不存在的情况
public class OnLineIsCardExit:OnLineHandler
{
public override Hashtable HandleRequest(string cardID)
{
CustomerIDAL customerIDAL = customerFactory.Customer();
List<CustomerEntity> listCustomer = customerIDAL.getCustomerByCardID(cardID);
if (listCustomer.Count == 0)
{
throw new Exception("该卡号不存在");
}
else
{
return successor.HandleRequest(cardID);
}
}
}
处理卡号已经上机的情况
public class OnLineIsOnLine:OnLineHandler
{
public override Hashtable HandleRequest(string cardID)
{
LineIDAL lineIDAL = lineFactory.line();
List<LineEntity> listLine = lineIDAL.getLastLineByCardID(cardID);
if (listLine.Count==0)
{
return successor.HandleRequest(cardID);
}
if (listLine[0].lineState.Trim() == Constant.ONLINE)
{
throw new Exception("暂时还不可以上机,因为该卡正在上机");
}
else
{
return successor.HandleRequest(cardID);
}
}
}
余额不足的情况
public class OnLineLimitCash : OnLineHandler
{
public override Hashtable HandleRequest(string cardID)
{
CustomerIDAL customerIdal = customerFactory.Customer();
List<CustomerEntity> listCustomer = customerIdal.getCustomerByCardID(cardID);
BasicdataIDAL basicdataIDAL = basicdataFactory.BasicData();
List<BasicdataEntity> listBasicdata = basicdataIDAL.getBasicdataInfo();
if (listCustomer[0].cCash < listBasicdata[0].limitCash)
{
throw new Exception("余额不足,无法上机,请去前台充值");
}
else
{
return successor.HandleRequest(cardID);
}
}
}