Java 優惠券最優使用實現

2021-08-15 06:58:16 字數 3879 閱讀 9978

業務需求:使用者可以用優惠券在支付相關費用時減免優惠券對應的金額,優惠券分為三種型別:現金券,代金券,折扣券,需要根據使用者的優惠券列表,找出優惠券金額最多的使用方案。

優惠券使用說明:所有優惠券都是分批次的,根據公司活動,按批次進行發放。同批次現金券可以疊加使用,不同批次不可疊加;代金券,折扣券不可疊加,每次只能使用一張。優惠券抵銷金額,可以大於支付金額。

直接上**:

優惠券實體類:

public class coupon implements comparable

public coupon(int id, string name, bigdecimal amount)

public int getid()

public void setid(int id)

public int getbatchid()

public void setbatchid(int batchid)

public string getname()

public void setname(string name)

public bigdecimal getamount()

public void setamount(bigdecimal amount)

public string getcoupontype()

public void setcoupontype(string coupontype)

@override

public int compareto(coupon o)

@override

public string tostring()

}

找出現金券同批次所有組合演算法,以及找出組合中最接近支付金額的組合的演算法

public class combinations 

return result;

}/**

* 由n個元素組成的組合

* * @param arr 陣列

* @param i 組合的元素個數

* @return 組合集合

*/private static > list> combinationselect(listarr, int i)

public static > hashmapgetcombinationmap(listcouponlist)

return map;

}// 篩選最接近支付金額的結果

public static int binarysearchkey(object array, bigdecimal targetnum)

if (targetnum.compareto(new bigdecimal(midvalue)) == 1) else

if (mid <= 1)

}int rightnum = ((integer) array[right]).intvalue();

int leftnum = ((integer) array[left]).intvalue();

// 如果乙個大於支付金額,乙個小於支付金額,優先返回大於支付金額的結果

if (new bigdecimal(rightnum).compareto(targetnum) == 1 && new bigdecimal(leftnum).compareto(targetnum) == -1)

bigdecimal rightiffval = new bigdecimal(rightnum).subtract(targetnum);

bigdecimal leftdiffval = new bigdecimal(leftnum).subtract(targetnum);

int ret = math.abs(leftdiffval.intvalue()) > math.abs(rightiffval.intvalue()) ? rightnum : leftnum;

// system.out.println("要查詢的數:" + targetnum + "最接近的數:" + ret);

return ret;

}/**

* dfs實現組合

*/private static class dfscombination>

private void dfs(int index)

for (int i = 0; i < arr.size(); i++)

}// 第幾個位置放置什麼元素

bucks.put(index, element);

bookset.add(element);

dfs(index + 1);

bookset.remove(element);}}

}public list> select()

private listcomposite()

}}

demo

public class testoptimumcoupon 

private static listgetoptimumlist(listcouponlist, bigdecimal amount) else

}if ("voucher".equals(coupon.getcoupontype()))

if ("discount".equals(coupon.getcoupontype()))

}// endresultmap 同批次現金券最優解,折扣券最優解,代金券最優解

// endresultmap key 優惠券id字串 value 最優解優惠金額

hashmapendresultmap = new hashmap<>();

// 判斷現金券map長度 現金券處理開始

if (!cashmap.isempty())

// 現金券處理結束

}// 代金券處理開始 判斷長度

if (voucherlist.size() > 0)

// 折扣券處理開始 判斷長度

if (discountlist.size() > 0)

// 獲取最終結果

int endresult = getoptimumval4map(endresultmap, amount);

// 獲取優惠券id集合字串

string coupondetailstr = getoptimumstr(endresultmap, endresult);

// 判斷是否為空

listcoupondetailidlist = new arraylist<>();

if (stringutils.isnotempty(coupondetailstr))

} else

}listoptimumlist = new arraylist<>();

// 迴圈使用者優惠券列表,進行比較

for (coupon coupon : couponlist) }}

return optimumlist;

}private static int getoptimumval4map(hashmapmap, bigdecimal amount)

int result = combinations.binarysearchkey(array.toarray(), amount);

return result;

}private static void getoptimummap4samebatch(hashmapmap, int result,

hashmapendresultmap) }}

private static string getoptimumstr(hashmapmap, int result)

}return null;

}}

推薦最優優惠券組合

最近在開發 專案,其中有乙個需求是使用者下單時如何選擇優惠券的問題。現在有時間總結下三種優惠券組合方式。先假設乙個場景 當前有三個商品,分別是商品a b c,三個優惠券 假設優惠力度相同 分別是優惠券1 2 3,其中優惠券1可用於商品a b,優惠券2可用於商品c,優惠券3可用於商品b c,現在購物車...

css繪製卡券優惠券 CSS 實現優惠券樣式

本文將介紹如何使用 css 中的 radial gradient 實現如下圖所示的優惠券樣式效果 繪製基本樣式 首先,我們繪製出優惠券的基本樣式,這很簡單,就不多說了。滿 100 減 30 scss voucher width 600px height 200px display flex left...

優惠券實現 從0到1,優惠券體系的搭建

優惠券體系可以說是任何產品都可能會用到的,近期進行了優惠券體系的設計,為了有更深的理解,筆者總結了自己從0到1的優惠券體系搭建,與大家分享。有自動發券跟人工發券。自動發券一般是在固定的場景下系統自動發放的。人工發券是給某個或者一批使用者進行批量發券。1 技術實現上需要考慮要有場景的編號。方便後續場景...