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做顺序扣减库存 Ⅰ.废话在前面 首先这篇笔记源自于最近的一次需求,这个临时性需求是根据两份数据(库存数据以及出库数据) 算出实际库存给到业务,至于库存为什么不等于剩余库存, ...
随机推荐
- Mac 下的截图技巧
最近想制作GIF图片,截图后,发现没有截出鼠标小效果,自己就查阅了一下资料,总结了不少的截图技巧,这里写下来,权当笔记,方便今后检索,方便别人共享. 方法一: 下载 QQ,在QQ的皮娜好设置里面设置截 ...
- In-App Purchase Programming Guide----(三) ----Retrieving Product Information
Retrieving Product Information In the first part of the purchase process, your app retrieves informa ...
- k8s-高级调度方式-二十一
两类: 节点选择器:nodeSelector(给node打上标签,pod通过标签预选节点),nodeName 节点亲和调度:nodeAffinity 1.节点选择器(nodeSelector,node ...
- django上课笔记2-视图CBV-ORM补充-Django的自带分页-Django的自定义分页
一.视图CBV 1.urls url(r'^login.html$', views.Login.as_view()), 2.views from django.views import View cl ...
- 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时7
课时7 线性分类器损失函数与最优化(下) 我们为什么要最大化对数概率而非直接最大化概率? 你在做逻辑斯蒂回归时,如果你只是想要最大化概率,那你使用log是无意义的.因为log函数是单调函数,最大化概率 ...
- 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时6
课时6 线性分类器损失函数与最优化(上) 多类SVM损失:这是一个两分类支持向量机的泛化 SVM损失计算了所有不正确的例子,将所有不正确的类别的评分,与正确类别的评分之差加1,将得到的数值与0作比较, ...
- 使用PDO操作数据库的好处
PDO一是PHP数据对象(PHP Data Object)的缩写. 并不能使用PDO扩展本身执行任何数据库操作,必须使用一个database-specific PDO driver(针对特定数据库的P ...
- python 字典 dict items values keys
dict.items() 1 >>> d = dict(one=1,two=2) 2 >>> it1 = d.items() 3 >>> it1 ...
- BZOJ 2457 [BeiJing2011] 双端队列
2457: [BeiJing2011]双端队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 340 Solved: 167[Submit][Sta ...
- chrome调试中resource改到application中了
如题,看视频的时候发现在resource下面查看cookie,但是自己试的时候发现没有了这个工具, google之后发现原来该位置了