实体类:

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. saltstack自动化运维快速入门

    saltstack自动化运维快速入门 关于saltstack 这个软件是干啥的 我这里就不介绍了 只是简单的说下是干啥的 网上的说法是 它是func的强化版本+ puppet的精简版 关于puppet ...

  2. 18-Angular 自定义模块以及配置路由模块懒加载

    新建项目,新建几个子模块,实现懒加载 用户.商品.文章 新建这三个模块 创建模块的时候后面加 --routing.会自动生成模块的路由文件 先删掉. 重新创建模块带routing 这样就会生成两个文件 ...

  3. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时26&&27

    课时26 图像分割与注意力模型(上) 语义分割:我们有输入图像和固定的几个图像分类,任务是我们想要输入一个图像,然后我们要标记每个像素所属的标签为固定数据类中的一个 使用卷积神经,网络为每个小区块进行 ...

  4. python 字符串操作一

    一.创建字符串 >>> s1 = '12345' >>> s1 '12345' >>> s2 = str(12345) >>> ...

  5. 前端笔记之React(二)组件内部State&React实战&表单元素的受控

    一.组件内部的State 1.1 state state叫状态,是每一个类式组件都有的属性,但函数式组件,没有state. state是一个对象,什么值都可以定义. 在任何类式组件的构造函数中,可以用 ...

  6. php 中的引用(&)与foreach结合后的一个注意点

    关于php中引用的概念及foreach循环的的应用就不多说了,php文档已经说的很明白了.直接上一段代码: <?php $arr = array(1,2, 3); foreach($arr as ...

  7. Django Views: Dynamic Content

    世味年来薄似纱,谁令骑马客京华. 小楼一夜听春雨,深巷明朝卖杏花. 矮纸斜行闲作草,晴窗细乳戏分茶. 素衣莫起风尘叹,犹及清明可到家. Your Second View: Dynamic Conten ...

  8. C# ref和out传递参数总结

    如有雷同,不胜荣幸,若转载,请注明 C#中ref和out传递参数总结,两个都可用来传递参数,ref使用时必须先进行初始化,out则不需要,只要在返回之前赋值即可,文字废话到此,下面直接上例子 ref例 ...

  9. hql语法及自定义函数(含array、map讲解) + hive的java api

    本博文的主要内容如下: .hive的详细官方手册    .hive支持的数据类型   .Hive Shell .Hive工程所需依赖的jar包  .hive自定义函数 .分桶4   .附PPT hiv ...

  10. Jquery | 基础 | 使用 jQuery 表单过滤选择器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...