Skip to content

Commit

Permalink
小傅哥 | 重学 Java 设计模式:实战策略模式「模拟多种营销类型优惠券,折扣金额计算策略场景」
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhengwei committed Jul 5, 2020
1 parent 95021e3 commit 9acccc2
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
/itstack-demo-design-19-02/itstack-demo-design-19-02.iml
/itstack-demo-design-19-01/itstack-demo-design-19-01.iml
/itstack-demo-design-19-00/itstack-demo-design-19-00.iml
/itstack-demo-design-20-01/itstack-demo-design-20-01.iml
15 changes: 15 additions & 0 deletions itstack-demo-design-20-01/pom.xml
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>
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;
}

}
15 changes: 15 additions & 0 deletions itstack-demo-design-20-02/pom.xml
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>
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);
}

}
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);

}
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;
}
}
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);
}

}
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;
}

}
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;
}

}
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);
}

}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
<module>itstack-demo-design-19-00</module>
<module>itstack-demo-design-19-01</module>
<module>itstack-demo-design-19-02</module>
<module>itstack-demo-design-20-01</module>
<module>itstack-demo-design-20-02</module>
</modules>

<dependencies>
Expand Down

0 comments on commit 9acccc2

Please sign in to comment.