职责链模式—上机

职责链模式的入口

   /// <summary>
    /// 该类是上机职责链的入口,用于b层调用
    /// </summary>
    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);
        }
    }

用于上机的抽象类

    /// <summary>
    /// 该类用于上机的抽象类,定义了骨架
    /// </summary>
    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);
    }

用于正常上机的情况

   /// <summary>
    /// 该类用于最终正常上机情况的处理
    /// </summary>
    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);
            //把数据回显给U层
            hashtable.Add("cType", cType);
            hashtable.Add("cCash", cCash);
            hashtable.Add("cName", cName);
            hashtable.Add("rate", rate);
            hashtable.Add("datetime", datetime);
            return hashtable;
        }
    }

用于卡号不存在的情况

   /// <summary>
    /// 该类用于卡号不存在的情况
    /// </summary>
    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);

            }
        }
    }

处理卡号已经上机的情况

   /// <summary>
    /// 该类用于处理卡号已经上机的情况 
    /// </summary>
    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);
            }
        }
    }

余额不足的情况

   /// <summary>
    /// 该类用于余额不足上机情况的处理
    /// </summary>
    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);
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张_Laura

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值