解决停车库分时间段收费的问题

最近遇到了一个开发问题,要求跟据车辆停车的时间,
完成分时间的收费的功能,要求大概如下图:收费标准
其实逻辑上并不算复杂,就是采用if循环或者for each进行分支判断就可以了,但是实际操作时发现了一个问题:如何确定车辆进入车库时属于哪一个时间段呢?我搜索了一些文章,却发现他们解决的是同一天时间判断,而跨天的这种情况却没有考虑。我通过github上找到的几个类似的项目,找到了这样的解决方案:

       //自定义时间格式
SimpleDateFormat s1 = new SimpleDateFormat("HH:mm:ss")

        //根据信息查出该车辆的入库时间
        TbOrder order = countTimeDao.findBeginTime(carNumber);
     
        //获取当前时间 
        Date date = new Date();
    
        //根据order的开始时间,进行时长的判断
        //获取
        long beginTime = order.getBeginTime().getTime();
        //获取时间戳
        Timestamp timestamp = new Timestamp(date.getTime());
        
        long endTime = timestamp.getTime();
        //得到时间差
        double minute = (double) (endTime - beginTime) / (60 * 1000);

        int hour = (int) minute / 60;
        //截取当前时间的时分秒并转化为String类型
        String formatLoginTime = s1.format(date.getTime());
        //设置高峰期时间段
        String begin = "09:00:00";
        String last = "21:00:00";
        //将String类型转化为int类型进行比较
        int time = Integer.parseInt(formatLoginTime.replaceAll(":", ""));
        int fast = Integer.parseInt(begin.replaceAll(":", ""));
        int end = Integer.parseInt(last.replaceAll(":", ""));

        if (minute <= 30) {
            //根据order的车牌,查出redis中的key值,判断是否收费
            //进行时间段判断
            if (minute <= 30) {
                return R.ok("10");
            } else {
                return R.ok("0");
            }
        } else if (30 < minute & hour <= 2) {

            return R.ok("10");
        } else if (2 < hour & hour <= 4) {
            if (fast < time & time < end) {
                double sum = ((minute - 120) / 30) * 3 + 10;
                String str = String.valueOf(sum);
                return R.ok(str);
            } else {
                double sum = ((minute - 120) / 30) * 2 + 10;
                String str = String.valueOf(sum);
                return R.ok(str);
            }
        } else if (hour <= 24) {
            if (fast < time & time < end) {
                return R.ok("25");
            } else {
//                double sum = ((minute - 120) / 30) * 2 + 10;
//                String str = String.valueOf(sum);
                return R.ok("20");
            }
        } else {
            double sum = ((hour / 24) + 1) * 25;
            String str = String.valueOf(sum);
            return R.ok(str);
        }

    }
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个 Java 代码示例,实现停车场按不同时间段收取不同费用的功能。假设停车场收费规则如下: - 8:00-18:00:每小时收费 10 元; - 18:00-22:00:每小时收费 15 元; - 22:00-8:00:每小时收费 8 元。 示例代码如下: ```java import java.time.Duration; import java.time.LocalDateTime; import java.time.LocalTime; public class ParkingLot { private LocalDateTime enterTime; private LocalDateTime exitTime; public void enter() { enterTime = LocalDateTime.now(); } public void exit() { exitTime = LocalDateTime.now(); } public int calculateFee() { int fee = 0; LocalDateTime tempEnterTime = enterTime; while (tempEnterTime.isBefore(exitTime)) { LocalDateTime tempExitTime = tempEnterTime.plusHours(1); if (tempExitTime.isAfter(exitTime)) { tempExitTime = exitTime; } int hour = tempEnterTime.getHour(); if (hour >= 8 && hour < 18) { fee += Duration.between(tempEnterTime, tempExitTime).toHours() * 10; } else if (hour >= 18 && hour < 22) { fee += Duration.between(tempEnterTime, tempExitTime).toHours() * 15; } else { fee += Duration.between(tempEnterTime, tempExitTime).toHours() * 8; } tempEnterTime = tempExitTime; } return fee; } } ``` 其中,`enter()` 方法表示车辆进入停车场,`exit()` 方法表示车辆离开停车场,`calculateFee()` 方法计停车费用。在计停车费用时,使用了一个循环,每次计进入时间和下一个整点时间之间的停车费用,直到计出离开时间的停车费用。在计停车费用时,使用了 `LocalTime` 类来获取小时数,根据不同的时间段设置不同的收费标准。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值