吃货至高境界,学会这个技巧让你不买过期食品——过期时间算法实战

吃货至高境界,学会这个技巧让你不买过期食品——过期时间算法实战

1、原型设计

嗯,有点简陋,大概意思就是输入生产日期,保质期计算出过期时间。

在这里插入图片描述

2、页面设计

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta name="description" content="Responsive Admin Dashboard Template">
    <meta name="keywords" content="admin,dashboard">
    <title>Neo</title>
    <link href="http://1.12.228.117/neo/assets/css/googleapis.css?family=Poppins:400,500,700,800&amp;display=swap" rel="stylesheet">
    <link href="http://1.12.228.117/neo/assets/css/googlefonts.css?family=Material+Icons" rel="stylesheet">
    <link href="http://1.12.228.117/neo/assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://1.12.228.117/neo/assets/plugins/font-awesome/css/all.min.css" rel="stylesheet">
    <link href="http://1.12.228.117/neo/assets/plugins/perfectscroll/perfect-scrollbar.css" rel="stylesheet">
    <link href="http://1.12.228.117/neo/assets/plugins/pace/pace.css" rel="stylesheet">
    <link href="http://1.12.228.117/neo/assets/css/main.min.css" rel="stylesheet">
    <link href="http://1.12.228.117/neo/assets/css/custom.css" rel="stylesheet">
    <style>
            .xs-label{
				width: 150px;line-height: 40px;
			}
			.w-150{
				width: 150px;
			}
    </style>
</head>
<body class="">
<div class="container" style="padding: 20px;">
<div class="row">
    <div class="col-sm-12">
		<h2>DateCount - 生产日期</h2>
	</div>
</div>
<div class="row">
	<label class="col-xs-6 xs-label">生产日期:</label>
	<label class="col-xs-6 xs-label">保质日期:</label>
</div>
<div class="row">
    <div class="col-xs-6 w-150">
		<input type="text" class="form-control" id="s1">
	</div>
	<div class="col-xs-6 w-150">
		 <select class="form-select" id="s2" onchange="s11();">
		                                <option value="" selected></option>
		                                <option value="10">10天</option>
		                                <option value="15">15天</option>
										<option value="21">21天</option>
		                                <option value="25">25天</option>
										<option value="30">一个月</option>
										<option value="90">3个月</option>
										<option value="180">半年</option>
										<option value="360">12个月</option>
		                            </select>
	</div>
</div>
<div class="row">
	<label class="col-xs-6 xs-label">当前日期:</label>
	<label class="col-xs-6 xs-label">保质天数:</label>
</div>
<div class="row">
    <div class="col-xs-6 w-150">
		<input type="text" class="form-control" id="s3">
	</div>
	<div class="col-xs-6 w-150">
		 <input type="text" class="form-control" id="s4">
	</div>
</div>
<div class="row">
	<label class="col-xs-6 xs-label">过期日期:</label>
</div>
<div class="row">
    <div class="col-xs-6 w-150">
		<input type="text" class="form-control" id="s5">
	</div>
	<div class="col-xs-6 w-150" id="s6" style="display: none;line-height: 40px;">
		 还有<span style="color: red;" id="s61"></span>天过期
	</div>
</div>
<div class="row">
	<label class="col-xs-6 xs-label">结果判断:</label>
	<label class="col-xs-6">
		<input type="text" class="form-control" id="s7" value="">
	</label>
</div>
<div class="row" style="margin-top: 30px;">
	    <div class="col-xs-12">
			<button type="button" class="btn btn-info m-b-xs" onclick="s8()">查询</button>
			<button type="button" class="btn btn-info m-b-xs" onclick="s9()">清空</button>
			<button type="button" class="btn btn-info m-b-xs" onclick="s10()">跳转赠品贴</button>
		</div>
	</div>
	<script>
		function s11(){
			var d1 = $("#s2").find("option:selected").val();
			$("#s4").val(d1);
		}
		function s8(){
			var d1 = $("#s1").val();
			var d2 = $("#s4").val();
			$.ajax({
			            type: 'post',
			            url: 'http://127.0.0.1/api-cors/openCV/test/dateCount',
			            data: {"scDate":d1,"bzDay":d2},
			            dataType: 'json',
			            success: function (data) {
							var data_ = data.data;
			                $("#s3").val(data_.nowDate);
							$("#s5").val(data_.endDate);
							$("#s6").show();
							$("#s61").text(data_.endDay);
							$("#s7").val(data_.result);
			            }
			        });
		}
		function s9(){
			$("#s1").val("");
			$("#s2").val("");
			$("#s3").val("");
			$("#s4").val("");
			$("#s5").val("");
			$("#s6").hide();
			$("#s61").text("");
			$("#s7").val("");
		}
		function s10(){
			location.href = 'http://1.12.228.117/yhawler/zengpin';
		}
	</script>
</div> 
<script src="http://1.12.228.117/neo/assets/plugins/jquery/jquery-3.4.1.min.js"></script>
<script src="http://1.12.228.117/neo/assets/js/popper.min.js"></script>
<script src="http://1.12.228.117/neo/assets/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="http://1.12.228.117/neo/assets/js/feather.min.js"></script>
<script src="http://1.12.228.117/neo/assets/plugins/perfectscroll/perfect-scrollbar.min.js"></script>
<script src="http://1.12.228.117/neo/assets/plugins/pace/pace.min.js"></script>
<script src="http://1.12.228.117/neo/assets/js/main.min.js"></script>
<script src="http://1.12.228.117/code/flicker/js/modernizr-custom-v2.7.1.min.js" type="text/javascript"></script>
<script src="http://1.12.228.117/code/flicker/js/jquery-finger-v0.1.0.min.js" type="text/javascript"></script>
<link href="http://1.12.228.117/code/flicker/css/flickerplate.css"  type="text/css" rel="stylesheet">
<script src="http://1.12.228.117/code/flicker/js/flickerplate.min.js" type="text/javascript"></script>
<link href="http://1.12.228.117/neo/assets/plugins/treejs/themes/default/style.css" rel="stylesheet">
<script src="http://1.12.228.117/neo/assets/plugins/treejs/jstree.min.js"></script>
<script src="http://1.12.228.117/neo/assets/js/pages/tree.js"></script>
</body>
</html>

3、后端实体设计

    private String scDate; //生产日期
    private int bzDay; //保质天数
    private String nowDate; //当前日期
    private String endDate; //到期日期
    private int endDay; //距离到期日期天数
    private String result; //判断结果

4、后端逻辑设计

从这个逻辑看,d3负数怎么办,是不是过期,提示不友好,命名规则不符合阿里巴巴等等。

逻辑这个东西,能跑就行。一个是程序能跑,一个则是你能跑。

我们需求是线上用的,但在我们实际工作中计算出来不可能负数,因为我们的卖家不会卖过期商品,下面程序是根据我们现在实际的需求设计的,暂时不考虑负数情况,如果真是负数,这就是实际工作中重大失误了。

		int p = dateCountVo.getBzDay();
        String d = dateUtil.getDate(dateCountVo.getScDate(), p);
        Date d1 = new Date();
        String d2 = dateUtil.dateToStr(d1);
        int d3 = dateUtil.getDayCount(d1, dateUtil.strToDate(d));
        dateCountVo.setEndDate(d);
        dateCountVo.setEndDay(d3);
        dateCountVo.setNowDate(d2);


        dateCountVo.setResult("");
        int p2 = p / 3;
        String d12 = dateUtil.getDate(dateCountVo.getScDate(), p2);
        int d34 = dateUtil.getDayCount(d1, dateUtil.strToDate(d12));
        if(d34>=0){

        }else{
            dateCountVo.setResult("保质期过三分之一");
        }

        int p1 = p / 2;
        String d11 = dateUtil.getDate(dateCountVo.getScDate(), p1);
        int d33 = dateUtil.getDayCount(d1, dateUtil.strToDate(d11));
        if(d33>=0){

        }else{
            dateCountVo.setResult("保质期过半");
        }

        return ApiResult.ok("查询成功", dateCountVo);

时间工具类

	public static Date strToDate(String d)  throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(df.parse(d));
        Date d1 = calendar.getTime();
        return d1;
    }

    public static String dateToStr(Date d)  throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        d = calendar.getTime();
        String date = df.format(d);
        return date;
    }

    public static String getDate(String releaseDate, int day) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
        Calendar calendar = Calendar.getInstance();
        Date d = new Date();
        calendar.setTime(df.parse(releaseDate));
        calendar.add(Calendar.DATE, day);
        d = calendar.getTime();
        String date = df.format(d);
        return date;
    }

    public static int getDayCount(Date date1, Date date2) {
        date1 = calToDate(date1);
        date2 = calToDate(date2);
        int days = (int)((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }

    public static Date calToDate(Date date1){
        Calendar date = Calendar.getInstance();
        date.setTime(date1);
        int year = date.get(Calendar.YEAR);
        int month = date.get(Calendar.MONTH);
        int day = date.get(Calendar.DATE);
        Calendar date2 = Calendar.getInstance();
        date2.set(year,month,day,23,59,59);
        return date2.getTime();
    }

大家好,我是yanhui。好久不见,甚是想念。有一段时间没写博客了,并不是放弃Java,在这段时间也并没有放弃自己,在这段时间也一直坚持学习,一直使用jdk1.8,尽管官方出到了jdk16,尽管我和马云人均资产500亿,还是一直学习Java。在这里学习到的Java乐趣,分享给大家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值