会议室计费实现

需求:在规定时间段内收费(7:00 - 22:00),其余时间不收费

代码实现(BigDecimal 处理精度问题):

  1 package Others;
  2 
  3 import java.math.BigDecimal;
  4 import java.time.Duration;
  5 import java.time.LocalDate;
  6 import java.time.LocalDateTime;
  7 import java.time.temporal.ChronoUnit;
  8 
  9 /**
 10  * @Author MrNiurh
 11  * @Date Creat in 2020/12/22
 12  * @Description 会议室计费
 13  * @See <a href="https://github.com/MrNiurh?tab=repositories">github</a>
 14  */
 15 public class MoneyCount {
 16 
 17     // 会议室计费开启时间
 18     private final static int startHour = 7;
 19     // 会议室计费关闭时间
 20     // 只在此时间内计费
 21     private final static int endHour = 22;
 22 
 23     // 会议室价格/每小时
 24     private final static BigDecimal moneyPerHour = BigDecimal.valueOf(40.0);
 25 
 26     public static void main(String[] args) {
 27         getMoney();
 28     }
 29 
 30 
 31     /**
 32      * 计算会议室收费
 33      * BigDecimal 处理精度问题
 34      *
 35      * @return BigDecimal
 36      */
 37     private static BigDecimal getMoney() {
 38 
 39         // 会议开始日期、时间
 40         LocalDateTime startTime = LocalDateTime.of(2021, 1, 11, 21, 0);
 41         // 会议结束日期、时间
 42         LocalDateTime endTime = LocalDateTime.of(2021, 1, 12, 13, 0);
 43 
 44         LocalDate s = startTime.toLocalDate();
 45         LocalDate e = endTime.toLocalDate();
 46         // 计算日期差(天数,不在同一天即有差值)
 47         long days = s.until(e, ChronoUnit.DAYS);
 48 
 49         Duration duration = Duration.between(startTime, endTime);
 50         //long days = duration.toDays();
 51 
 52 
 53         if (duration.toMinutes() < 0) {
 54             System.err.println("会议结束时间不能早于开始时间");
 55             return null;
 56         }
 57 
 58         // 开始 & 结束 时间的时间点(小时)
 59         int start = startTime.getHour();
 60         int end = endTime.getHour();
 61 
 62         int startMinute = startTime.getMinute();
 63         int endMinute = endTime.getMinute();
 64 
 65         // 时间转换
 66         BigDecimal realStart = timeTransfer(start, startMinute);
 67         BigDecimal realEnd = timeTransfer(end, endMinute);
 68 
 69         /**
 70          * 总时间合计
 71          * (realEnd - realStart) + days * (endHour - startHour)
 72          */
 73         BigDecimal totalHours = realEnd.subtract(realStart)
 74                 .add(BigDecimal.valueOf(days)
 75                         .multiply(BigDecimal.valueOf(endHour).subtract(BigDecimal.valueOf(startHour))));
 76         /**
 77          * 价格计算
 78          * totalHours * moneyPerHour
 79          */
 80         BigDecimal totalMoney = totalHours.multiply(moneyPerHour);
 81 
 82         // 将时间内的 T 去掉
 83         System.out.println("会议开始时间: " + startTime.toString().replace("T", " "));
 84         System.out.println("会议结束时间 " + endTime.toString().replace("T", " "));
 85         System.out.println("会议总耗时:" + totalHours + "h");
 86         System.out.println("会议室价格:" + moneyPerHour + "¥/h");
 87         System.out.println("总计:" + totalMoney + "元");
 88         // 返回价格
 89         return totalMoney;
 90     }
 91 
 92     /**
 93      * 将时间转换为正常范围
 94      * 小于计费开启时间记为开启时间
 95      * 大于计费关闭时间记为关闭时间
 96      *
 97      * @param time
 98      */
 99     private static BigDecimal timeTransfer(int time, int minute) {
100         if (time < startHour) {
101             return BigDecimal.valueOf(startHour);
102         }
103         if (time >= endHour) {
104             return BigDecimal.valueOf(endHour);
105         } else {
106             /**
107              * time + minute/60.0
108              */
109             return BigDecimal.valueOf(time).add(BigDecimal.valueOf(minute).divide(BigDecimal.valueOf(60.0)));
110         }
111     }
112 }

 运行结果:

 

posted @ 2020-12-22 15:00  宁任翃  阅读(123)  评论(0编辑  收藏  举报