Java练习demo 20190402 优惠券扣减
实体类:
package org.jimmy.autosearch2019.pojo;
import java.math.BigDecimal;
public class TestEntity20190402 {
private Integer time;
private BigDecimal discountMoney;
public Integer getTime() {
return time;
}
public void setTime(Integer time) {
this.time = time;
}
public BigDecimal getDiscountMoney() {
return discountMoney;
}
public void setDiscountMoney(BigDecimal discountMoney) {
this.discountMoney = discountMoney;
}
}
业务扣减类:
package org.jimmy.autosearch2019.test; import java.math.BigDecimal;
import java.util.ArrayList; import org.jimmy.autosearch2019.pojo.TestEntity20190402; /**
* @author ラピスラズリ(Dawn)
* @date 2019年4月2日 上午11:15:39
* @detail 循环扣减demo
*/
public class Test20190402 { public static ArrayList<TestEntity20190402> list = new ArrayList<TestEntity20190402>(); static {
TestEntity20190402 testEntity = new TestEntity20190402();
testEntity.setTime(1);
testEntity.setDiscountMoney(new BigDecimal(100));
list.add(testEntity);
testEntity = new TestEntity20190402();
testEntity.setTime(5);
testEntity.setDiscountMoney(new BigDecimal(10));
list.add(testEntity);
testEntity = new TestEntity20190402();
testEntity.setTime(3);
testEntity.setDiscountMoney(new BigDecimal(20));
list.add(testEntity);
testEntity = new TestEntity20190402();
testEntity.setTime(2);
testEntity.setDiscountMoney(new BigDecimal(50));
list.add(testEntity);
} public static void main(String[] args) {
sort();
TestEntity20190402 getMiniTestEntity = getMiniTimeTestEntity();
BigDecimal total = new BigDecimal(155);
ArrayList<TestEntity20190402> tempList = new ArrayList<TestEntity20190402>();
while((total = total.subtract(getMiniTestEntity.getDiscountMoney())).intValue() > 0) {
tempList = new ArrayList<TestEntity20190402>();
for(int i = 1; i < list.size(); i++) {
tempList.add(list.get(i));
}
list = tempList;
getMiniTestEntity = getMiniTimeTestEntity();
}
BigDecimal currentDiscountMoney = null;
if(total.intValue() < 0) {
total = total.add(getMiniTestEntity.getDiscountMoney());
currentDiscountMoney = getMiniTestEntity.getDiscountMoney();
}
System.out.println(total.intValue());
System.out.println(currentDiscountMoney.intValue());
} public static TestEntity20190402 getMiniTimeTestEntity() {
/*for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getTime() + "," + list.get(i).getDiscountMoney());
}*/
//获取第一个就是时间最早的
TestEntity20190402 testEntity = list.get(0);
return testEntity;
} public static void sort() {
for(int i = 0; i < list.size(); i++) {
for(int j = i + 1; j < list.size(); j++) {
TestEntity20190402 outerTestEntity = list.get(i);
TestEntity20190402 innerTestEntity = list.get(j);
if(innerTestEntity.getTime() < outerTestEntity.getTime()) {
list.set(i, innerTestEntity);
list.set(j, outerTestEntity);
}
}
}
} }
Java练习demo 20190402 优惠券扣减的更多相关文章
- 自实现CAS原理JAVA版,模拟下单库存扣减
在做电商系统时,库存是一个非常严格的数据,根据CAS(check and swap)原来下面对库存扣减提供两种方法,一种是redis,一种用java实现CAS. 第一种 redis实现: 以下这个类是 ...
- redis分布式锁扣减库存弊端: 吞吐量低, 解决方法:使用 分段锁 分布式分段锁并发扣减库存--代码实现
package tech.codestory.zookeeper.aalvcai.ConcurrentHashMapLock; import lombok.AllArgsConstructor; im ...
- EF+MySQL乐观锁控制电商并发下单扣减库存,在高并发下的问题
下订单减库存的方式 现在,连农村的大姐都会用手机上淘宝购物了,相信电商对大家已经非常熟悉了,如果熟悉电商开发的同学,就知道在买家下单购买商品的时候,是需要扣减库存的,当然有2种扣减库存的方式, 一种是 ...
- 微信公众号Java接入demo
微信公众号Java接入demo 前不久买了一台服务,本来是用来当梯子用的,后来买了一个域名搭了一个博客网站,后来不怎么在上面写博客一直闲着,最近申请了一个微信公众号就想着弄点什么玩玩.周末没事就鼓捣了 ...
- 【Java】【4】关于Java中的自增自减
摘要:理解j = j++与j = ++j的区别:正确用法:直接用j++,不要用前两种 正文: import java.util.*; public class Test{ public static ...
- 修复回写PR时到料日期重复扣减检验周期的问题:
问题描述: 修复回写PR时到料日期重复扣减检验周期的问题:系统回写的外购半成品PR交货日期未按采购周期回写,从8-10日开始均于10天交期回写,例以下9-5日今天回写的PR,采购周期12天,结果回写到 ...
- 利用redis实现分布式事务锁,解决高并发环境下库存扣减
利用redis实现分布式事务锁,解决高并发环境下库存扣减 问题描述: 某电商平台,首发一款新品手机,每人限购2台,预计会有10W的并发,在该情况下,如果扣减库存,保证不会超卖 解决方案一 利用数据 ...
- Kafka2.4发布——新特性介绍(附Java Api Demo代码)
新功能 允许消费者从最近的副本进行获取 为 Consumer Rebalance Protocol 增加对增量协同重新均衡(incremental cooperative rebalancing)的支 ...
- 笔记:如何使用postgresql做顺序扣减库存
如何使用postgresql做顺序扣减库存 Ⅰ.废话在前面 首先这篇笔记源自于最近的一次需求,这个临时性需求是根据两份数据(库存数据以及出库数据) 算出实际库存给到业务,至于库存为什么不等于剩余库存, ...
随机推荐
- saltstack自动化运维快速入门
saltstack自动化运维快速入门 关于saltstack 这个软件是干啥的 我这里就不介绍了 只是简单的说下是干啥的 网上的说法是 它是func的强化版本+ puppet的精简版 关于puppet ...
- 一个不当使用fclose引发的异常
最近服务器上一个后台传输文件的服务,经常会报出异常来,只能强行终止并重启. 昨天刚好有空,现场抓了一下dump,再把程序扔到IDA里看了一下,很快就找出原因了,原来是调用fclose时出错的. 使用C ...
- angular源码剖析之Provider系列--CacheFactoryProvider
CacheFactoryProvider 简介 源码里是这么描述的: Factory that constructs {@link $cacheFactory.Cache Cache} objects ...
- 875. Koko Eating Bananas
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The g ...
- HDU5589:Tree(莫队+01字典树)
传送门 题意 略 分析 f[u]表示u到根的边的异或 树上两点之间的异或值为f[u]^f[v], 然后将查询用莫队算法分块,每个点插入到字典树中,利用字典树维护两点异或值大于等于M复杂度O(N^(3/ ...
- .bashrc等文件中的rc是什么意思
转自: https://blog.csdn.net/u010167269/article/details/52612260 刚刚配置 .bashrc 文件时,突然想,这里面的 rc 是什么意思? 使 ...
- hdu1811 Rank of Tetris 并查集+拓扑排序
#include <stdio.h> #include <string.h> #include <vector> #include <queue> us ...
- Apache Kylin Cube 的构建过程
不多说,直接上干货! 1. Cube的物理模型 Cube物理模型 如上图所示,一个常用的3维立方体,包含:时间.地点.产品.假如data cell 中存放的是产量,则我们可以根据时间.地点.产品来确定 ...
- 贴图、纹理、材质的区别是什么? 还有shader
贴图.纹理.材质的区别是什么? 还有shader 整个 CG 领域中这三个概念都是差不多的,在一般的实践中,大致上的层级关系是:材质 Material包含贴图 Map,贴图包含纹理 Texture.纹 ...
- Android开发-浅谈架构(一)
写在前面的话 嗯 聊聊架构. 这段时间一直在维护旧项目. 包括自己之前写的新项目 越来越发现 一个架构清晰的项目往往让人赏心悦目.不至于在一个bug丢过来之后手足无措.包括以后别人接收自己的项目 能很 ...