职责链模式实现退卡

  public class CancelChain
    {
        CancelIsCardNull isCardNull = new CancelIsCardNull();
        CancelIsCardExit isCardExit = new CancelIsCardExit();
        CancelIsCardOnLine isCardOnLine = new CancelIsCardOnLine();
        CancelCard cancelCard = new CancelCard();
        public Hashtable doHandler(string cardID, string userID)
        {
            isCardNull.SetSuccessor(isCardExit);
            isCardExit.SetSuccessor(isCardOnLine);
            isCardOnLine.SetSuccessor(cancelCard);
            return isCardNull.HandleRequest(cardID, userID);
        }
    }

    abstract class CancelHandler
    {
        protected CancelHandler successor;
        public void SetSuccessor(CancelHandler successor)
        {
            this.successor = successor;
        }
        public abstract Hashtable HandleRequest(string cardID, string userID);

    }

    class CancelIsCardNull : CancelHandler
    {
        public override Hashtable HandleRequest(string cardID, string userID)
        {
            if (cardID == "")
            {
                throw new Exception("请补全信息");
            }
            else
            {
                return successor.HandleRequest(cardID, userID);

            }
        }
    }

    class CancelIsCardExit : CancelHandler
    {
        public override Hashtable HandleRequest(string cardID, string userID)
        {
            CustomerFactory customerFactory = new CustomerFactory();
            CustomerIDAL customerIDAL = customerFactory.Customer();
            List<CustomerEntity> listCustomer = customerIDAL.getCustomerByCardID(cardID);
            if (listCustomer.Count == 0)
            {
                throw new Exception("该卡号不存在");
            }
            else
            {
                return successor.HandleRequest(cardID, userID);

            }
        }
    }
    class CancelIsCardOnLine : CancelHandler
    {
        public override Hashtable HandleRequest(string cardID, string userID)
        {
            LineFactory lineFactory = new LineFactory();
            LineIDAL lineIDAL = lineFactory.line();
            List<LineEntity> listLine = lineIDAL.getLastLineByCardID(cardID);
            //没有上机记录的情况
            if (listLine.Count == 0)
            {
                return successor.HandleRequest(cardID, userID);
            }
            else
            {
                //有上机记录的情况
                if (listLine[0].lineState.Trim() == Constant.ONLINE)
                {
                    //正在上机
                    throw new Exception("该卡正在上机,不能退卡");

                }
                else
                {
                    //没有上机
                    return successor.HandleRequest(cardID, userID);
                }
            }

        }
    }
    //退卡类
    class CancelCard : CancelHandler
    {
        public override Hashtable HandleRequest(string cardID, string userID)
        {
            Hashtable hashtable = new Hashtable();
            CustomerFactory customerFactory = new CustomerFactory();
            CustomerIDAL customerIDAL = customerFactory.Customer();
            List<CustomerEntity> listCustomer = customerIDAL.getCustomerByCardID(cardID);
            //顾客表余额
            decimal cash = listCustomer[0].cCash;
            //退卡日期
            string cancelCardTime = customerIDAL.getTime();
            //删除客户表
            int i = customerIDAL.cancelCardID(cardID);
            //添加退卡表
            CancelCardFactory cancelCardFactory = new CancelCardFactory();
            CancelCardIDAL cancelCardIDAL = cancelCardFactory.cancelCard();
            CancelCardEntity cancelCardEntity = new CancelCardEntity();
            //退卡实体赋值
            cancelCardEntity.billState = Constant.NOACCOUNT;
            cancelCardEntity.cancelCash = cash;
            cancelCardEntity.cancelDateTime = Convert.ToDateTime(cancelCardTime);
            cancelCardEntity.cardID = cardID;
            cancelCardEntity.userID = userID;
            //退卡
            int res = cancelCardIDAL.insertCancelCard(cancelCardEntity);
            if (i <= 0 || res <= 0)
            {
                throw new Exception("退卡失败");
            }
            hashtable.Add("cash", cash);
            return hashtable;
        }
    }

B层调用

  public Hashtable cancelCardID(string cardID,string userID)
        {
            try
            {
                CancelChain cancelChain = new CancelChain();
                Hashtable hashtable= cancelChain.doHandler(cardID, userID);
                return hashtable;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            
        }

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值