• Java秒杀简单设计三:数据封装类


    上一篇https://www.cnblogs.com/taiguyiba/p/9828984.html 整合了数据库表和Dao层代码

    这一篇继续设计数据封装类:

    涉及到获取秒杀地址,查询,返回秒杀结果等内容

    1.数据封装:

    1.1 秒杀地址封装类Exposer

    创建Exposer类,用于封装秒杀地址:如果开启秒杀,则返回状态、秒杀地址md5加密值和ID,如果秒杀未开启,则返回开始时间、结束时间等。

    Exposer.java

    package com.seckill.dto;
    
    public class Exposer {
        //是否开启秒杀
        private boolean exposed;
        //md5加密
        private String md5;
        //id
        private long seckillId;
        //now time
        private long now;
        //start time
        private long start;
        //end time
        private long end;
        public Exposer(boolean exposed, String md5,long seckillId) {
            super();
            this.exposed = exposed;
            this.md5 = md5;
            this.seckillId = seckillId;
        }
        public Exposer(boolean exposed, long now, long start, long end,long seckillId) {
            super();
            this.exposed = exposed;
            this.now = now;
            this.start = start;
            this.end = end;
            this.seckillId = seckillId;
        }
        public Exposer(boolean exposed, long seckillId) {
            super();
            this.exposed = exposed;
            this.seckillId = seckillId;
        }
        public boolean isExposed() {
            return exposed;
        }
        public void setExposed(boolean exposed) {
            this.exposed = exposed;
        }
        public String getMd5() {
            return md5;
        }
        public void setMd5(String md5) {
            this.md5 = md5;
        }
        public long getSeckillId() {
            return seckillId;
        }
        public void setSeckillId(long seckillId) {
            this.seckillId = seckillId;
        }
        public long getNow() {
            return now;
        }
        public void setNow(long now) {
            this.now = now;
        }
        public long getStart() {
            return start;
        }
        public void setStart(long start) {
            this.start = start;
        }
        public long getEnd() {
            return end;
        }
        public void setEnd(long end) {
            this.end = end;
        }
        @Override
        public String toString() {
            return "Exposer [exposed=" + exposed + ", md5=" + md5 + ", seckillId=" + seckillId + ", now=" + now + ", start="
                    + start + ", end=" + end + "]";
        }
    }

     1.2 秒杀结果封装类 SeckillExecution

       SeckillExecution主要用于封装秒杀结果

       

    package com.seckill.dto;
    
    import com.seckill.entity.SuccessKilled;
    import com.seckill.enums.SeckillStateEnum;
    /**
     * 数据传递
     * 其中并没有添加枚举类型,转换成json有问题
     * @author lll
     *
     */
    public class SeckillExecution {
    
        private long seckillId;
    
        private int state;
        
        private String stateInfo;
        
        private SuccessKilled successKilled;
    
        public SeckillExecution(long seckillId,SeckillStateEnum seckillStateEnum, SuccessKilled successKilled) {
            super();
            this.seckillId = seckillId;
            this.state = seckillStateEnum.getState();
            this.stateInfo = seckillStateEnum.getStateInfo();
            this.successKilled = successKilled;
        }
        public SeckillExecution(long seckillId, SeckillStateEnum seckillStateEnum) {
            super();
            this.seckillId = seckillId;
            this.state = seckillStateEnum.getState();
            this.stateInfo = seckillStateEnum.getStateInfo();
        }
        public SeckillExecution(SeckillStateEnum seckillStateEnum) {
            super();
            this.state = seckillStateEnum.getState();
            this.stateInfo = seckillStateEnum.getStateInfo();
        }
        public long getSeckillId() {
            return seckillId;
        }
        public void setSeckillId(long seckillId) {
            this.seckillId = seckillId;
        }
        public int getState() {
            return state;
        }
    
        public void setState(int state) {
            this.state = state;
        }
    
        public String getStateInfo() {
            return stateInfo;
        }
    
        public void setStateInfo(String stateInfo) {
            this.stateInfo = stateInfo;
        }
    
        public SuccessKilled getSuccessKilled() {
            return successKilled;
        }
    
        public void setSuccessKilled(SuccessKilled successKilled) {
            this.successKilled = successKilled;
        }
    
        @Override
        public String toString() {
            return "SeckillExecution [seckillId=" + seckillId + ", state=" + state + ", stateInfo=" + stateInfo
                    + ", successKilled=" + successKilled + "]";
        }
    
    }

    1.3 前端请求结果封装类 SeckillResult

          SeckillResult<T> 用于封装前端请求的结果:

    package com.seckill.dto;
    /**
     * 所有ajax请求封装
     * @author mayn
     *
     */
    public class SeckillResult<T> {
        
        private boolean success;
        
        private T data;
        
        private String error;
    
        public SeckillResult(boolean success, T data) {
            super();
            this.success = success;
            this.data = data;
        }
    
        public SeckillResult(boolean success, String error) {
            super();
            this.success = success;
            this.error = error;
        }
    
        public boolean isSuccess() {
            return success;
        }
    
        public void setSuccess(boolean success) {
            this.success = success;
        }
    
        public T getData() {
            return data;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        public String getError() {
            return error;
        }
    
        public void setError(String error) {
            this.error = error;
        }
    }

    1.4 常量数据字典 SeckillStateEnum

     SeckillStateEnum用于封装数据字典:

    package com.seckill.enums;
    /**
     * 使用枚举标示常量数据字典
     * @author mayn
     *
     */
    public enum SeckillStateEnum {
        SUCCESSD(1,"秒杀成功"),
        END(0,"秒杀结束"),
        REPEAT_KILL(-1,"重复秒杀"),
        INNER_ERROR(-2,"内部错误"),
        DATA_REWRITE(-3,"数据篡改")
        ;
        private int state;
        
        private String stateInfo;
    
        private SeckillStateEnum(int state, String stateInfo) {
            this.state = state;
            this.stateInfo = stateInfo;
        }
    
        public int getState() {
            return state;
        }
    
        public String getStateInfo() {
            return stateInfo;
        }
        public static SeckillStateEnum stateOf(int index) {
            for(SeckillStateEnum state : values()) {
                if(state.getState() == index) {
                    return state;
                }
            }
            return null;
        }
    }

     1.5 封装异常类

     RepeatKillException 重复秒杀异常类

    package com.seckill.exception;
    
    public class RepeatKillException extends RuntimeException {
    
        public RepeatKillException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
        public RepeatKillException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    }

     SeckillCloseException 秒杀关闭异常类

    package com.seckill.exception;
    
    public class SeckillCloseException extends RuntimeException {
    
        public SeckillCloseException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
        public SeckillCloseException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    
    }

    SeckillException 秒杀异常类

    package com.seckill.exception;
    
    public class SeckillException extends RuntimeException {
    
        public SeckillException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
        public SeckillException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    
    }
  • 相关阅读:
    函数的返回值以及局部,全局变量
    函数的参数
    文件操作 函数
    数据类型一些基本操作方法
    设置精度的方法
    字符串和编码问题
    python中一些函数应用
    初步认识类
    P1613 跑路
    P2383 狗哥玩木棒
  • 原文地址:https://www.cnblogs.com/taiguyiba/p/9829191.html
Copyright © 2020-2023  润新知