实体类:

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 优惠券扣减的更多相关文章

  1. 自实现CAS原理JAVA版,模拟下单库存扣减

    在做电商系统时,库存是一个非常严格的数据,根据CAS(check and swap)原来下面对库存扣减提供两种方法,一种是redis,一种用java实现CAS. 第一种 redis实现: 以下这个类是 ...

  2. redis分布式锁扣减库存弊端: 吞吐量低, 解决方法:使用 分段锁 分布式分段锁并发扣减库存--代码实现

    package tech.codestory.zookeeper.aalvcai.ConcurrentHashMapLock; import lombok.AllArgsConstructor; im ...

  3. EF+MySQL乐观锁控制电商并发下单扣减库存,在高并发下的问题

    下订单减库存的方式 现在,连农村的大姐都会用手机上淘宝购物了,相信电商对大家已经非常熟悉了,如果熟悉电商开发的同学,就知道在买家下单购买商品的时候,是需要扣减库存的,当然有2种扣减库存的方式, 一种是 ...

  4. 微信公众号Java接入demo

    微信公众号Java接入demo 前不久买了一台服务,本来是用来当梯子用的,后来买了一个域名搭了一个博客网站,后来不怎么在上面写博客一直闲着,最近申请了一个微信公众号就想着弄点什么玩玩.周末没事就鼓捣了 ...

  5. 【Java】【4】关于Java中的自增自减

    摘要:理解j = j++与j = ++j的区别:正确用法:直接用j++,不要用前两种 正文: import java.util.*; public class Test{ public static ...

  6. 修复回写PR时到料日期重复扣减检验周期的问题:

    问题描述: 修复回写PR时到料日期重复扣减检验周期的问题:系统回写的外购半成品PR交货日期未按采购周期回写,从8-10日开始均于10天交期回写,例以下9-5日今天回写的PR,采购周期12天,结果回写到 ...

  7. 利用redis实现分布式事务锁,解决高并发环境下库存扣减

    利用redis实现分布式事务锁,解决高并发环境下库存扣减   问题描述: 某电商平台,首发一款新品手机,每人限购2台,预计会有10W的并发,在该情况下,如果扣减库存,保证不会超卖 解决方案一 利用数据 ...

  8. Kafka2.4发布——新特性介绍(附Java Api Demo代码)

    新功能 允许消费者从最近的副本进行获取 为 Consumer Rebalance Protocol 增加对增量协同重新均衡(incremental cooperative rebalancing)的支 ...

  9. 笔记:如何使用postgresql做顺序扣减库存

    如何使用postgresql做顺序扣减库存 Ⅰ.废话在前面 首先这篇笔记源自于最近的一次需求,这个临时性需求是根据两份数据(库存数据以及出库数据) 算出实际库存给到业务,至于库存为什么不等于剩余库存, ...

随机推荐

  1. CS231n 2016 通关 第一章-内容介绍

    第一节视频的主要内容: Fei-Fei Li 女神对Computer Vision的整体介绍.包括了发展历史中的重要事件,其中最为重要的是1959年测试猫视觉神经的实验. In 1959 Harvar ...

  2. CentOS下安装SecureCRT的sz/rz工具包

    对于使用SecureCRT的玩家,rzsz是一个灰常灰常实用的功能.但是定制安装的linux可能没有把rzsz包安装到系统,这对用securecrt这样的windows工具传输文件特别不方便.为了使用 ...

  3. Java中对类的主动引用和被动引用

    1.遇到new,getstatic,putstatic,invokestatic这4条字节码指令时,类如果没初始化就会被初始化,创建对象,读取或设置静态字段,调用静态方法. 2.反射 3.子类初始化前 ...

  4. 015--python集合和字符串

    一.集合 关系测试 交集: 两个都有   .intersettion() 或用  &  符号 差集: 列表a有,列表b没有  .difference() 或用  -  符号 并集: 两列表合并 ...

  5. bzoj 1814: Ural 1519 Formula 1【插头dp】

    设f[i][j][s]为轮廓线推到格子(i,j),状态为s的方案数 括号表示一段线的左端和右端,表示成左括号和右括号,状压的时候用1和2表示,0表示已经闭合 下面的蓝线是黄色格子的轮廓线,dp转移要把 ...

  6. bzoj 2440: [中山市选2011]完全平方数【莫比乌斯函数+二分】

    二分答案,然后用莫比乌斯函数作为容斥系数,计算当前枚举的mid内有几个满足要求的数 #include<iostream> #include<cstdio> #include&l ...

  7. GraphicsLab Project学习项目

    作者:i_dovelemon 日期:2016 / 05 / 30 主题:3D,Graphics 引言 进公司以来,主要在学习的就是如何保证代码的质量,以前热爱的图形学也放置了.但是,作为游戏程序员,特 ...

  8. Python标准库 datetime

    >>> import datetime >>> now = datetime.datetime.now() >>> now datetime.da ...

  9. Reduce实现

    Reduce实现 参考 第一版 Array.prototype.fakeReduce = function (fn, base) { // this 指向原数组 // 拷贝数据, 更改指针方向 var ...

  10. android动画(3)layout动画,layoutChanged动画及算定义它,ListViewActivity的Layout动画(代码和xm配置两种实现l)

    1.layout切换动画 代码: 本示例是fragment切换.在它的oncreateView中 public class LayoutAnimationFrgmt extends Fragment ...