/*** encoding: utf-8* 版权所有 2026 ©涂聚文有限公司 ®* 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:Enumeration Algorithms* Author : geovindu,Geovin Du 涂聚文.* IDE : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS : window10* Datetime : 2026 - 2026/7/15 - 22:24* User : geovindu* Product : IntelliJ IDEA* Project : JavaAlgorithms* File : Jewelry.java* explain : 学习 类**/package Enumeration;import java.math.BigDecimal;
import java.util.List;/*** 珠宝实体*/
public class Jewelry {private String jid;private String category;private String material;private BigDecimal price;private int stock;private int sales;private List<String> sceneTags;public Jewelry(String jid, String category, String material, BigDecimal price, int stock, int sales, List<String> sceneTags) {this.jid = jid;this.category = category;this.material = material;this.price = price;this.stock = stock;this.sales = sales;this.sceneTags = sceneTags;}// 计算折后价public BigDecimal getDiscountPrice(BigDecimal discount) {return price.multiply(discount);}// getterpublic String getJid() { return jid; }public String getCategory() { return category; }public String getMaterial() { return material; }public BigDecimal getPrice() { return price; }public int getStock() { return stock; }public int getSales() { return sales; }public List<String> getSceneTags() { return sceneTags; }@Overridepublic String toString() {String scenes = String.join(",", sceneTags);return String.format("【%s】%s | %s | 原价:%.0f元 | 库存:%d件 | 销量:%d | 适用场景:%s",jid, category, material, price, stock, sales, scenes);}
}/*** encoding: utf-8* 版权所有 2026 ©涂聚文有限公司 ®* 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:Enumeration Algorithms* Author : geovindu,Geovin Du 涂聚文.* IDE : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS : window10* Datetime : 2026 - 2026/7/15 - 22:25* User : geovindu* Product : IntelliJ IDEA* Project : JavaAlgorithms* File : RecommendItem.java* explain : 学习 类**/package Enumeration;/*** 带评分推荐单品*/
public class RecommendItem {private int score;private Jewelry goods;public RecommendItem(int score, Jewelry goods) {this.score = score;this.goods = goods;}public int getScore() { return score; }public Jewelry getGoods() { return goods; }
}/*** encoding: utf-8* 版权所有 2026 ©涂聚文有限公司 ®* 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:Enumeration Algorithms* Author : geovindu,Geovin Du 涂聚文.* IDE : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS : window10* Datetime : 2026 - 2026/7/15 - 22:26* User : geovindu* Product : IntelliJ IDEA* Project : JavaAlgorithms* File : ComboTwo.java* explain : 学习 类**/package Enumeration;import java.math.BigDecimal;
/*** 两件套:项链+戒指*/
public class ComboTwo {private Jewelry necklace;private Jewelry ring;private BigDecimal total;public ComboTwo(Jewelry necklace, Jewelry ring, BigDecimal total) {this.necklace = necklace;this.ring = ring;this.total = total;}public Jewelry getNecklace() { return necklace; }public Jewelry getRing() { return ring; }public BigDecimal getTotal() { return total; }
}/*** encoding: utf-8* 版权所有 2026 ©涂聚文有限公司 ®* 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:Enumeration Algorithms* Author : geovindu,Geovin Du 涂聚文.* IDE : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS : window10* Datetime : 2026 - 2026/7/15 - 22:26* User : geovindu* Product : IntelliJ IDEA* Project : JavaAlgorithms* File : ComboThree.java* explain : 学习 类**/package Enumeration;import java.math.BigDecimal;
/*** 三金套装:项链+手镯+戒指(三层循环枚举)*/
public class ComboThree {private Jewelry necklace;private Jewelry bracelet;private Jewelry ring;private BigDecimal total;public ComboThree(Jewelry necklace, Jewelry bracelet, Jewelry ring, BigDecimal total) {this.necklace = necklace;this.bracelet = bracelet;this.ring = ring;this.total = total;}public Jewelry getNecklace() { return necklace; }public Jewelry getBracelet() { return bracelet; }public Jewelry getRing() { return ring; }public BigDecimal getTotal() { return total; }
}/*** encoding: utf-8* 版权所有 2026 ©涂聚文有限公司 ®* 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:Enumeration Algorithms* Author : geovindu,Geovin Du 涂聚文.* IDE : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS : window10* Datetime : 2026 - 2026/7/15 - 22:27* User : geovindu* Product : IntelliJ IDEA* Project : JavaAlgorithms* File : ComboFive.java* explain : 学习 类**/package Enumeration;import java.math.BigDecimal;
/*** 五金套装:项链+手镯+戒指+耳饰+吊坠(五层循环枚举)*/
public class ComboFive {private Jewelry necklace;private Jewelry bracelet;private Jewelry ring;private Jewelry earrings;private Jewelry pendant;private BigDecimal total;public ComboFive(Jewelry necklace, Jewelry bracelet, Jewelry ring, Jewelry earrings, Jewelry pendant, BigDecimal total) {this.necklace = necklace;this.bracelet = bracelet;this.ring = ring;this.earrings = earrings;this.pendant = pendant;this.total = total;}public Jewelry getNecklace() { return necklace; }public Jewelry getBracelet() { return bracelet; }public Jewelry getRing() { return ring; }public Jewelry getEarrings() { return earrings; }public Jewelry getPendant() { return pendant; }public BigDecimal getTotal() { return total; }
}/*** encoding: utf-8* 版权所有 2026 ©涂聚文有限公司 ®* 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:Enumeration Algorithms* Author : geovindu,Geovin Du 涂聚文.* IDE : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS : window10* Datetime : 2026 - 2026/7/15 - 22:27* User : geovindu* Product : IntelliJ IDEA* Project : JavaAlgorithms* File : JewelryService.java* explain : 学习 类**/package Enumeration;import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;/*** 业务服务类:枚举算法核心*/
public class JewelryService {// 单品筛选枚举,返回 主推款 / 升级款public Pair<List<Jewelry>, List<Jewelry>> enumerateFilter(List<Jewelry> goodsList,BigDecimal maxBudget,BigDecimal discount,String targetMaterial,String targetCategory,String targetScene) {List<RecommendItem> candidates = new ArrayList<>();BigDecimal limitRate = new BigDecimal("1.2");for (Jewelry item : goodsList) {if (item.getStock() < 1) continue;BigDecimal realPrice = item.getDiscountPrice(discount);// 超过预算120%直接过滤if (realPrice.compareTo(maxBudget.multiply(limitRate)) > 0) continue;// 材质过滤if (targetMaterial != null && !targetMaterial.isEmpty() && !item.getMaterial().equals(targetMaterial)) continue;// 品类过滤if (targetCategory != null && !targetCategory.isEmpty() && !item.getCategory().equals(targetCategory)) continue;int score = 0;BigDecimal priceRatio = realPrice.divide(maxBudget, 4, BigDecimal.ROUND_HALF_UP);// 价格打分if (priceRatio.compareTo(new BigDecimal("0.7")) >= 0 && priceRatio.compareTo(new BigDecimal("0.9")) <= 0) {score += 50;} else if (priceRatio.compareTo(BigDecimal.ONE) <= 0) {score += 30;} else {score += 10;}// 场景匹配加分if (targetScene != null && !targetScene.isEmpty() && item.getSceneTags().contains(targetScene)) {score += 30;}// 销量加分上限20int saleScore = item.getSales() / 10;if (saleScore > 20) saleScore = 20;score += saleScore;candidates.add(new RecommendItem(score, item));}// 按分数降序排序candidates.sort((a, b) -> Integer.compare(b.getScore(), a.getScore()));List<Jewelry> mainList = new ArrayList<>();List<Jewelry> upgradeList = new ArrayList<>();for (RecommendItem ri : candidates) {BigDecimal realPrice = ri.getGoods().getDiscountPrice(discount);if (realPrice.compareTo(maxBudget) <= 0) {mainList.add(ri.getGoods());} else {upgradeList.add(ri.getGoods());}}return new Pair<>(mainList, upgradeList);}// 两层循环:项链+戒指两件套public List<ComboTwo> enumerateTwoCombo(List<Jewelry> goodsList, BigDecimal totalBudget, BigDecimal discount) {List<ComboTwo> result = new ArrayList<>();List<Jewelry> necklaces = goodsList.stream().filter(g -> g.getStock() > 0 && "项链".equals(g.getCategory())).collect(Collectors.toList());List<Jewelry> rings = goodsList.stream().filter(g -> g.getStock() > 0 && "戒指".equals(g.getCategory())).collect(Collectors.toList());for (Jewelry n : necklaces) {for (Jewelry r : rings) {BigDecimal sum = n.getDiscountPrice(discount).add(r.getDiscountPrice(discount));if (sum.compareTo(totalBudget) <= 0) {result.add(new ComboTwo(n, r, sum));}}}return result;}// 三层循环:三金 项链+手镯+戒指public List<ComboThree> enumerateThreeCombo(List<Jewelry> goodsList, BigDecimal totalBudget, BigDecimal discount) {List<ComboThree> result = new ArrayList<>();List<Jewelry> necklaces = goodsList.stream().filter(g -> g.getStock() > 0 && "项链".equals(g.getCategory())).collect(Collectors.toList());List<Jewelry> bracelets = goodsList.stream().filter(g -> g.getStock() > 0 && "手镯".equals(g.getCategory())).collect(Collectors.toList());List<Jewelry> rings = goodsList.stream().filter(g -> g.getStock() > 0 && "戒指".equals(g.getCategory())).collect(Collectors.toList());for (Jewelry n : necklaces) {for (Jewelry b : bracelets) {for (Jewelry r : rings) {BigDecimal sum = n.getDiscountPrice(discount).add(b.getDiscountPrice(discount)).add(r.getDiscountPrice(discount));if (sum.compareTo(totalBudget) <= 0) {result.add(new ComboThree(n, b, r, sum));}}}}return result;}// 五层循环:五金 项链+手镯+戒指+耳饰+吊坠public List<ComboFive> enumerateFiveCombo(List<Jewelry> goodsList, BigDecimal totalBudget, BigDecimal discount) {List<ComboFive> result = new ArrayList<>();List<Jewelry> necklaces = goodsList.stream().filter(g -> g.getStock() > 0 && "项链".equals(g.getCategory())).collect(Collectors.toList());List<Jewelry> bracelets = goodsList.stream().filter(g -> g.getStock() > 0 && "手镯".equals(g.getCategory())).collect(Collectors.toList());List<Jewelry> rings = goodsList.stream().filter(g -> g.getStock() > 0 && "戒指".equals(g.getCategory())).collect(Collectors.toList());List<Jewelry> earrings = goodsList.stream().filter(g -> g.getStock() > 0 && "耳饰".equals(g.getCategory())).collect(Collectors.toList());List<Jewelry> pendants = goodsList.stream().filter(g -> g.getStock() > 0 && "吊坠".equals(g.getCategory())).collect(Collectors.toList());for (Jewelry n : necklaces) {for (Jewelry b : bracelets) {for (Jewelry r : rings) {for (Jewelry e : earrings) {for (Jewelry p : pendants) {BigDecimal sum = n.getDiscountPrice(discount).add(b.getDiscountPrice(discount)).add(r.getDiscountPrice(discount)).add(e.getDiscountPrice(discount)).add(p.getDiscountPrice(discount));if (sum.compareTo(totalBudget) <= 0) {result.add(new ComboFive(n, b, r, e, p, sum));}}}}}}return result;}// 初始化门店商品数据public List<Jewelry> initStoreData() {List<Jewelry> list = new ArrayList<>();list.add(new Jewelry("N001", "项链", "黄金", new BigDecimal("5280"), 12, 120, Arrays.asList("婚嫁", "送礼")));list.add(new Jewelry("N002", "项链", "铂金", new BigDecimal("7600"), 3, 60, Arrays.asList("求婚", "日常")));list.add(new Jewelry("N003", "项链", "钻石", new BigDecimal("12800"), 5, 45, Arrays.asList("纪念日")));list.add(new Jewelry("N004", "项链", "K金", new BigDecimal("3680"), 8, 150, Arrays.asList("日常")));list.add(new Jewelry("R001", "戒指", "黄金", new BigDecimal("2150"), 15, 200, Arrays.asList("日常", "婚嫁")));list.add(new Jewelry("R002", "戒指", "钻石", new BigDecimal("9999"), 2, 88, Arrays.asList("求婚")));list.add(new Jewelry("R003", "戒指", "银饰", new BigDecimal("599"), 30, 320, Arrays.asList("日常")));list.add(new Jewelry("B001", "手镯", "黄金", new BigDecimal("8600"), 4, 80, Arrays.asList("婚嫁", "送礼")));list.add(new Jewelry("B002", "手镯", "银饰", new BigDecimal("1280"), 22, 260, Arrays.asList("日常")));list.add(new Jewelry("E001", "耳饰", "K金", new BigDecimal("1680"), 18, 190, Arrays.asList("日常", "纪念日")));list.add(new Jewelry("E002", "耳饰", "铂金", new BigDecimal("4200"), 6, 72, Arrays.asList("求婚")));list.add(new Jewelry("E003", "耳饰", "钻石", new BigDecimal("6500"), 0, 30, Arrays.asList("纪念日")));list.add(new Jewelry("P001", "吊坠", "黄金", new BigDecimal("2600"), 9, 110, Arrays.asList("婚嫁", "送礼")));list.add(new Jewelry("P002", "吊坠", "K金", new BigDecimal("1200"), 14, 170, Arrays.asList("日常")));return list;}// 简易二元返回容器public static class Pair<T1, T2> {public T1 first;public T2 second;public Pair(T1 f, T2 s) {first = f;second = s;}}
}/*** encoding: utf-8* 版权所有 2026 ©涂聚文有限公司 ®* 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎* 描述:Enumeration Algorithms* Author : geovindu,Geovin Du 涂聚文.* IDE : IntelliJ IDEA 2024.3.6 Java 17* # database : Oracle21c,MySQL 9.0,SQL Server 2019,PostgreSQL 17.1 Neo4j* # OS : window10* Datetime : 2026 - 2026/7/15 - 22:17* User : geovindu* Product : IntelliJ IDEA* Project : JavaAlgorithms* File : EnumerationBll.java* explain : 学习 类**/package Bll;import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
import Enumeration.*;public class EnumerationBll {public void Demo(){JewelryService service = new JewelryService();List<Jewelry> allGoods = service.initStoreData();BigDecimal discount = new BigDecimal("0.95"); // 95折System.out.println("===== 珠宝门店全部商品(枚举全集)全场95折 =====");for (Jewelry item : allGoods) {System.out.printf("%s 折后价:%.0f元%n", item, item.getDiscountPrice(discount));}System.out.println(new String(new char[110]).replace('\0', '-'));// 场景1:单品推荐 预算6000,不限材质品类场景System.out.println("\n【顾客需求1】单品推荐|预算≤6000元,不限材质品类,全场95折");JewelryService.Pair<List<Jewelry>, List<Jewelry>> filterResult =service.enumerateFilter(allGoods, new BigDecimal("6000"), discount, "", "", "");List<Jewelry> mainList = filterResult.first;List<Jewelry> upgradeList = filterResult.second;if (mainList.isEmpty()) {System.out.println("无符合条件单品");} else {for (Jewelry g : mainList) {System.out.printf("%s 折后价:%.0f元%n", g, g.getDiscountPrice(discount));}}// 场景2:两件套 项链+戒指,组合预算10000System.out.println("\n===== 两件套装(项链+戒指)总价≤10000(95折) =====");List<ComboTwo> twoCombo = service.enumerateTwoCombo(allGoods, new BigDecimal("10000"), discount);if (twoCombo.isEmpty()) {System.out.println("无两件套组合");} else {for (ComboTwo c : twoCombo) {System.out.println("套装组合:");System.out.printf(" %s%n", c.getNecklace());System.out.printf(" %s%n", c.getRing());System.out.printf(" 折后合计:%.0f元%n", c.getTotal());System.out.println("----------------------------------------");}}// 场景3:三层循环 婚嫁三金(项链+手镯+戒指)预算16000System.out.println("\n===== 婚嫁三金套装(三层循环枚举)总价≤16000(95折) =====");List<ComboThree> threeCombo = service.enumerateThreeCombo(allGoods, new BigDecimal("16000"), discount);if (threeCombo.isEmpty()) {System.out.println("无三金组合");} else {for (ComboThree c : threeCombo) {System.out.println("三金套装:");System.out.printf(" %s%n", c.getNecklace());System.out.printf(" %s%n", c.getBracelet());System.out.printf(" %s%n", c.getRing());System.out.printf(" 折后合计:%.0f元%n", c.getTotal());System.out.println("----------------------------------------");}}// 场景4:五层循环 婚嫁五金 预算22000System.out.println("\n===== 婚嫁五金套装(五层循环枚举)总价≤22000(95折) =====");List<ComboFive> fiveCombo = service.enumerateFiveCombo(allGoods, new BigDecimal("22000"), discount);if (fiveCombo.isEmpty()) {System.out.println("无五金组合");} else {for (ComboFive c : fiveCombo) {System.out.println("五金套装:");System.out.printf(" %s%n", c.getNecklace());System.out.printf(" %s%n", c.getBracelet());System.out.printf(" %s%n", c.getRing());System.out.printf(" %s%n", c.getEarrings());System.out.printf(" %s%n", c.getPendant());System.out.printf(" 折后合计:%.0f元%n", c.getTotal());System.out.println("----------------------------------------");}}// 小幅超预算升级单品展示System.out.println("\n【预算6000升级备选单品(小幅超预算)】");for (Jewelry g : upgradeList) {System.out.printf("%s 折后价:%.0f元%n", g, g.getDiscountPrice(discount));}}
}
输出:
