forked from fuzhengwei/itstack-demo-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
小傅哥 | 重学 Java 设计模式:实战策略模式「模拟多种营销类型优惠券,折扣金额计算策略场景」
- Loading branch information
1 parent
95021e3
commit 9acccc2
Showing
12 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http:https://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>itstack-demo-design</artifactId> | ||
<groupId>org.itstack</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>itstack-demo-design-20-01</artifactId> | ||
|
||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
itstack-demo-design-20-01/src/main/java/org/itstack/demo/design/CouponDiscountService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.itstack.demo.design; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! | ||
* 公众号:bugstack虫洞栈 | ||
* Create by 小傅哥(fustack) @2020 | ||
* 优惠券折扣计算接口 | ||
* <p> | ||
* 优惠券类型; | ||
* 1. 直减券 | ||
* 2. 满减券 | ||
* 3. 折扣券 | ||
* 4. n元购 | ||
*/ | ||
public class CouponDiscountService { | ||
|
||
public double discountAmount(int type, double typeContent, double skuPrice, double typeExt) { | ||
// 1. 直减券 | ||
if (1 == type) { | ||
return skuPrice - typeContent; | ||
} | ||
// 2. 满减券 | ||
if (2 == type) { | ||
if (skuPrice < typeExt) return skuPrice; | ||
return skuPrice - typeContent; | ||
} | ||
// 3. 折扣券 | ||
if (3 == type) { | ||
return skuPrice * typeContent; | ||
} | ||
// 4. n元购 | ||
if (4 == type) { | ||
return typeContent; | ||
} | ||
return 0D; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http:https://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>itstack-demo-design</artifactId> | ||
<groupId>org.itstack</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>itstack-demo-design-20-02</artifactId> | ||
|
||
|
||
</project> |
22 changes: 22 additions & 0 deletions
22
itstack-demo-design-20-02/src/main/java/org/itstack/demo/design/Context.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.itstack.demo.design; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! | ||
* 公众号:bugstack虫洞栈 | ||
* Create by 小傅哥(fustack) @2020 | ||
*/ | ||
public class Context<T> { | ||
|
||
private ICouponDiscount<T> couponDiscount; | ||
|
||
public Context(ICouponDiscount<T> couponDiscount) { | ||
this.couponDiscount = couponDiscount; | ||
} | ||
|
||
public BigDecimal discountAmount(T couponInfo, BigDecimal skuPrice) { | ||
return couponDiscount.discountAmount(couponInfo, skuPrice); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
itstack-demo-design-20-02/src/main/java/org/itstack/demo/design/ICouponDiscount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.itstack.demo.design; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! | ||
* 公众号:bugstack虫洞栈 | ||
* Create by 小傅哥(fustack) @2020 | ||
* <p> | ||
* 优惠券折扣计算接口 | ||
* <p> | ||
* 优惠券类型; | ||
* 1. 直减券 | ||
* 2. 满减券 | ||
* 3. 折扣券 | ||
* 4. n元购 | ||
*/ | ||
public interface ICouponDiscount<T> { | ||
|
||
/** | ||
* 优惠券金额计算 | ||
* @param couponInfo 券折扣信息;直减、满减、折扣、N元购 | ||
* @param skuPrice sku金额 | ||
* @return 优惠后金额 | ||
*/ | ||
BigDecimal discountAmount(T couponInfo, BigDecimal skuPrice); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
itstack-demo-design-20-02/src/main/java/org/itstack/demo/design/impl/MJCouponDiscount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.itstack.demo.design.impl; | ||
|
||
import org.itstack.demo.design.ICouponDiscount; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Map; | ||
|
||
/** | ||
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! | ||
* 公众号:bugstack虫洞栈 | ||
* Create by 小傅哥(fustack) @2020 | ||
* | ||
* 满减 | ||
*/ | ||
public class MJCouponDiscount implements ICouponDiscount<Map<String,String>> { | ||
|
||
/** | ||
* 满减计算 | ||
* 1. 判断满足x元后-n元,否则不减 | ||
* 2. 最低支付金额1元 | ||
*/ | ||
public BigDecimal discountAmount(Map<String,String> couponInfo, BigDecimal skuPrice) { | ||
String x = couponInfo.get("x"); | ||
String o = couponInfo.get("n"); | ||
|
||
// 小于商品金额条件的,直接返回商品原价 | ||
if (skuPrice.compareTo(new BigDecimal(x)) < 0) return skuPrice; | ||
// 减去优惠金额判断 | ||
BigDecimal discountAmount = skuPrice.subtract(new BigDecimal(o)); | ||
if (discountAmount.compareTo(BigDecimal.ZERO) < 1) return BigDecimal.ONE; | ||
|
||
return discountAmount; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
itstack-demo-design-20-02/src/main/java/org/itstack/demo/design/impl/NYGCouponDiscount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.itstack.demo.design.impl; | ||
|
||
import org.itstack.demo.design.ICouponDiscount; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! | ||
* 公众号:bugstack虫洞栈 | ||
* Create by 小傅哥(fustack) @2020 | ||
* | ||
* n元购买 | ||
*/ | ||
public class NYGCouponDiscount implements ICouponDiscount<Double> { | ||
|
||
/** | ||
* n元购购买 | ||
* 1. 无论原价多少钱都固定金额购买 | ||
*/ | ||
public BigDecimal discountAmount(Double couponInfo, BigDecimal skuPrice) { | ||
return new BigDecimal(couponInfo); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
itstack-demo-design-20-02/src/main/java/org/itstack/demo/design/impl/ZJCouponDiscount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.itstack.demo.design.impl; | ||
|
||
import org.itstack.demo.design.ICouponDiscount; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! | ||
* 公众号:bugstack虫洞栈 | ||
* Create by 小傅哥(fustack) @2020 | ||
* | ||
* 直减 | ||
*/ | ||
public class ZJCouponDiscount implements ICouponDiscount<Double> { | ||
|
||
/** | ||
* 直减计算 | ||
* 1. 使用商品价格减去优惠价格 | ||
* 2. 最低支付金额1元 | ||
*/ | ||
public BigDecimal discountAmount(Double couponInfo, BigDecimal skuPrice) { | ||
BigDecimal discountAmount = skuPrice.subtract(new BigDecimal(couponInfo)); | ||
if (discountAmount.compareTo(BigDecimal.ZERO) < 1) return BigDecimal.ONE; | ||
return discountAmount; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
itstack-demo-design-20-02/src/main/java/org/itstack/demo/design/impl/ZKCouponDiscount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.itstack.demo.design.impl; | ||
|
||
import org.itstack.demo.design.ICouponDiscount; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! | ||
* 公众号:bugstack虫洞栈 | ||
* Create by 小傅哥(fustack) @2020 | ||
* | ||
* 折扣 | ||
*/ | ||
public class ZKCouponDiscount implements ICouponDiscount<Double> { | ||
|
||
|
||
/** | ||
* 折扣计算 | ||
* 1. 使用商品价格乘以折扣比例,为最后支付金额 | ||
* 2. 保留两位小数 | ||
* 3. 最低支付金额1元 | ||
*/ | ||
public BigDecimal discountAmount(Double couponInfo, BigDecimal skuPrice) { | ||
BigDecimal discountAmount = skuPrice.multiply(new BigDecimal(couponInfo)).setScale(2, BigDecimal.ROUND_HALF_UP); | ||
if (discountAmount.compareTo(BigDecimal.ZERO) < 1) return BigDecimal.ONE; | ||
return discountAmount; | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
itstack-demo-design-20-02/src/test/java/org/itstack/demo/design/test/ApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.itstack.demo.design.test; | ||
|
||
import org.itstack.demo.design.Context; | ||
import org.itstack.demo.design.impl.MJCouponDiscount; | ||
import org.itstack.demo.design.impl.NYGCouponDiscount; | ||
import org.itstack.demo.design.impl.ZJCouponDiscount; | ||
import org.itstack.demo.design.impl.ZKCouponDiscount; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获! | ||
* 公众号:bugstack虫洞栈 | ||
* Create by 小傅哥(fustack) @2020 | ||
*/ | ||
public class ApiTest { | ||
|
||
private Logger logger = LoggerFactory.getLogger(ApiTest.class); | ||
|
||
@Test | ||
public void test_zj() { | ||
// 直减;100-10,商品100元 | ||
Context<Double> context = new Context<Double>(new ZJCouponDiscount()); | ||
BigDecimal discountAmount = context.discountAmount(10D, new BigDecimal(100)); | ||
logger.info("测试结果:直减优惠后金额 {}", discountAmount); | ||
} | ||
|
||
@Test | ||
public void test_mj() { | ||
// 满100减10,商品100元 | ||
Context<Map<String,String>> context = new Context<Map<String,String>>(new MJCouponDiscount()); | ||
Map<String,String> mapReq = new HashMap<String, String>(); | ||
mapReq.put("x","100"); | ||
mapReq.put("n","10"); | ||
BigDecimal discountAmount = context.discountAmount(mapReq, new BigDecimal(100)); | ||
logger.info("测试结果:满减优惠后金额 {}", discountAmount); | ||
} | ||
|
||
|
||
@Test | ||
public void test_zk() { | ||
// 折扣9折,商品100元 | ||
Context<Double> context = new Context<Double>(new ZKCouponDiscount()); | ||
BigDecimal discountAmount = context.discountAmount(0.9D, new BigDecimal(100)); | ||
logger.info("测试结果:折扣9折后金额 {}", discountAmount); | ||
} | ||
|
||
@Test | ||
public void test_nyg() { | ||
// n元购;100-10,商品100元 | ||
Context<Double> context = new Context<Double>(new NYGCouponDiscount()); | ||
BigDecimal discountAmount = context.discountAmount(90D, new BigDecimal(100)); | ||
logger.info("测试结果:n元购优惠后金额 {}", discountAmount); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters