orderCreateQo.setCustomerPrice(orderCreateDto.getTotalPrice().setScale(0,BigDecimal.ROUND_HALF_UP));
时间: 2024-06-07 21:06:05 浏览: 25
这段代码的作用是将订单创建DTO中的总价值设置为订单创建查询对象中的客户价格。在这里,setScale()方法用于将总价值四舍五入到最接近的整数,并将其转换为BigDecimal类型。然后,将其设置为orderCreateQo对象的客户价格属性。这样做的目的是确保客户的价格是整数,并且避免出现小数点后面的数字。
相关问题
List<HospitalizationReceivingMedicineVO> dispensing = this.getDispensing(id); if(dispensing.size() == 0){throw new RuntimeException("打印失败,查无可打印的发药单");} Map<Long, HospitalizationReceivingMedicineVO> collect = dispensing.stream().collect(Collectors.toMap(x -> x.getDispensingNumber(), x -> x, (x1, x2) -> { x1.setTotalPrice(x1.getTotalPrice().add(x2.getTotalPrice())); return x1; })); List<HospitalizationReceivingMedicineVO> hospitalizationReceivingMedicineList = collect.entrySet().stream().map(x -> x.getValue()).collect(Collectors.toList()); BigDecimal totalPrice = hospitalizationReceivingMedicineList.get(0).getTotalPrice(); String diseassAreaName = dispensing.get(0).getDiseassAreaName(); Long dispensingNumber = dispensing.get(0).getDispensingNumber(); Date medicineTime = dispensing.get(0).getMedicineTime(); DispensingPrintVO dispensingPrintVO = new DispensingPrintVO(); dispensingPrintVO.setDispensingTime(medicineTime); dispensingPrintVO.setDispensingNumber(dispensingNumber); dispensingPrintVO.setInpatientArea(diseassAreaName); dispensingPrintVO.setChildren(dispensing); dispensingPrintVO.setAmountMoney(totalPrice); return dispensingPrintVO;优化以上代码
可以尝试对以上代码进行如下优化:
```
List<HospitalizationReceivingMedicineVO> dispensing = this.getDispensing(id);
if(dispensing.isEmpty()){
throw new RuntimeException("打印失败,查无可打印的发药单");
}
Map<Long, HospitalizationReceivingMedicineVO> collect = dispensing.stream()
.collect(Collectors.toMap(HospitalizationReceivingMedicineVO::getDispensingNumber, Function.identity(),
(x1, x2) -> {
x1.setTotalPrice(x1.getTotalPrice().add(x2.getTotalPrice()));
return x1;
}));
BigDecimal totalPrice = collect.values().stream()
.map(HospitalizationReceivingMedicineVO::getTotalPrice)
.reduce(BigDecimal.ZERO, BigDecimal::add);
HospitalizationReceivingMedicineVO first = collect.values().iterator().next();
DispensingPrintVO dispensingPrintVO = new DispensingPrintVO();
dispensingPrintVO.setDispensingTime(first.getMedicineTime());
dispensingPrintVO.setDispensingNumber(first.getDispensingNumber());
dispensingPrintVO.setInpatientArea(first.getDiseassAreaName());
dispensingPrintVO.setChildren(new ArrayList<>(collect.values()));
dispensingPrintVO.setAmountMoney(totalPrice);
return dispensingPrintVO;
```
主要的优化包括:
1. 使用 `isEmpty()` 方法来判断 `List` 是否为空,替代 `size() == 0` 的方式。
2. 使用方法引用和 `Function.identity()` 来简化 `toMap()` 方法中的 lambda 表达式。
3. 将 `collect()` 方法的结果转换为 `Map`,而不是 `List`,这样可以避免后面再次遍历 `List`。
4. 使用 `iterator().next()` 方法来获取第一个元素,替代 `get(0)` 的方式,避免 `List` 为空时出现异常。
vue.js实现购物车
Vue.js可以很方便地实现购物车功能,以下是一个简单的购物车示例:
1. 首先,创建一个Vue实例,并在data中定义购物车的数据:
```
new Vue({
el: '#app',
data: {
cart: [], // 购物车数组
products: [ // 商品列表
{ name: '商品1', price: 10 },
{ name: '商品2', price: 20 },
{ name: '商品3', price: 30 },
{ name: '商品4', price: 40 },
]
},
methods: {
addToCart: function(product) { // 添加商品到购物车
var found = false;
for (var i = 0; i < this.cart.length; i++) {
if (this.cart[i].product.name === product.name) {
this.cart[i].quantity++;
found = true;
}
}
if (!found) {
this.cart.push({ product: product, quantity: 1 });
}
},
removeFromCart: function(item) { // 从购物车中移除商品
var index = this.cart.indexOf(item);
if (index !== -1) {
this.cart.splice(index, 1);
}
},
getTotalPrice: function() { // 计算购物车总价
var total = 0;
for (var i = 0; i < this.cart.length; i++) {
total += this.cart[i].product.price * this.cart[i].quantity;
}
return total;
}
}
});
```
2. 在页面中使用Vue指令绑定数据和方法:
```
<div id="app">
<h1>购物车</h1>
<div>
<h2>商品列表</h2>
<ul>
<li v-for="product in products">
{{ product.name }} - {{ product.price }}元
<button v-on:click="addToCart(product)">添加到购物车</button>
</li>
</ul>
</div>
<div>
<h2>购物车</h2>
<ul>
<li v-for="item in cart">
{{ item.product.name }} - {{ item.product.price }}元 x {{ item.quantity }}
<button v-on:click="removeFromCart(item)">移除</button>
</li>
</ul>
<p>总价:{{ getTotalPrice() }}元</p>
</div>
</div>
```
3. 运行代码,即可看到购物车功能的实现。
以上示例只是一个简单的购物车实现,实际应用中还需要考虑更多的因素,如商品库存、商品属性选择等。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)