@@ -19,11 +19,16 @@ public class ProductInfo {
1919 Product product ;
2020 int quantity ;
2121 ScannedCode scannedCode ;
22+ boolean hasManualDiscount ;
2223
23- public ProductInfo (Product product , int quantity , ScannedCode scannedCode ) {
24+ public ProductInfo (Product product ,
25+ int quantity ,
26+ ScannedCode scannedCode ,
27+ boolean hasManualDiscount ) {
2428 this .product = product ;
2529 this .quantity = quantity ;
2630 this .scannedCode = scannedCode ;
31+ this .hasManualDiscount = hasManualDiscount ;
2732 }
2833 }
2934
@@ -32,6 +37,7 @@ public ProductInfo(Product product, int quantity, ScannedCode scannedCode) {
3237 private ArrayList <String > encodedCodes ;
3338 private int codeCount ;
3439 private boolean hasAgeRestrictedCode ;
40+ private boolean hasAppliedManualDiscount ;
3541
3642 public EncodedCodesGenerator (EncodedCodesOptions encodedCodesOptions ) {
3743 encodedCodes = new ArrayList <>();
@@ -45,9 +51,9 @@ public void add(String code) {
4551 }
4652
4753 if (options .repeatCodes ) {
48- addScannableCode (code , false );
54+ addScannableCode (code , false , false );
4955 } else {
50- addScannableCode ("1" + options .countSeparator + code , false );
56+ addScannableCode ("1" + options .countSeparator + code , false , false );
5157 }
5258 }
5359
@@ -71,7 +77,6 @@ public void add(ShoppingCart shoppingCart) {
7177 }
7278 }
7379 }
74-
7580 } else {
7681 Product product = item .getProduct ();
7782 if (product == null ) {
@@ -80,7 +85,8 @@ public void add(ShoppingCart shoppingCart) {
8085
8186 productInfos .add (new ProductInfo (product ,
8287 item .getQuantity (),
83- item .getScannedCode ()));
88+ item .getScannedCode (),
89+ item .isManualCouponApplied ()));
8490 }
8591 }
8692
@@ -98,8 +104,20 @@ public void clear() {
98104 }
99105
100106 public ArrayList <String > generate () {
101- if (options .finalCode != null && options .finalCode .length () != 0 ) {
102- append (options .finalCode );
107+ if (options .finalCode != null && !options .finalCode .isEmpty ()) {
108+ if (getCountSeparatorLength () > 0 ) {
109+ append ("1" + options .countSeparator + options .finalCode );
110+ } else {
111+ append (options .finalCode );
112+ }
113+ }
114+
115+ if (hasAppliedManualDiscount ) {
116+ if (getCountSeparatorLength () > 0 ) {
117+ append ("1" + options .countSeparator + options .manualDiscountFinalCode );
118+ } else {
119+ append (options .manualDiscountFinalCode );
120+ }
103121 }
104122
105123 finishCode ();
@@ -161,9 +179,13 @@ private void addProducts(final List<ProductInfo> productInfos, boolean ageRestri
161179 .buildCode ();
162180
163181 if (options .repeatCodes ) {
164- addScannableCode (scannedCode .getCode (), ageRestricted );
182+ addScannableCode (scannedCode .getCode (),
183+ ageRestricted ,
184+ productInfo .hasManualDiscount );
165185 } else {
166- addScannableCode ("1" + options .countSeparator + scannedCode .getCode (), ageRestricted );
186+ addScannableCode ("1" + options .countSeparator + scannedCode .getCode (),
187+ ageRestricted ,
188+ productInfo .hasManualDiscount );
167189 }
168190 break ;
169191 }
@@ -180,9 +202,13 @@ private void addProducts(final List<ProductInfo> productInfos, boolean ageRestri
180202 }
181203
182204 if (options .repeatCodes ) {
183- addScannableCode (transmissionCode , ageRestricted );
205+ addScannableCode (transmissionCode ,
206+ ageRestricted ,
207+ productInfo .hasManualDiscount );
184208 } else {
185- addScannableCode ("1" + options .countSeparator + productInfo .scannedCode .getCode (), ageRestricted );
209+ addScannableCode ("1" + options .countSeparator + productInfo .scannedCode .getCode (),
210+ ageRestricted ,
211+ productInfo .hasManualDiscount );
186212 }
187213 } else {
188214 int q = productInfo .quantity ;
@@ -227,10 +253,14 @@ private void addProducts(final List<ProductInfo> productInfos, boolean ageRestri
227253
228254 if (options .repeatCodes ) {
229255 for (int j = 0 ; j < q ; j ++) {
230- addScannableCode (transmissionCode , ageRestricted );
256+ addScannableCode (transmissionCode ,
257+ ageRestricted ,
258+ productInfo .hasManualDiscount );
231259 }
232260 } else {
233- addScannableCode (q + options .countSeparator + transmissionCode , ageRestricted );
261+ addScannableCode (q + options .countSeparator + transmissionCode ,
262+ ageRestricted ,
263+ productInfo .hasManualDiscount );
234264 }
235265 }
236266 }
@@ -243,9 +273,18 @@ private void finishCode() {
243273 encodedCodes .add (code );
244274 stringBuilder = new StringBuilder ();
245275 codeCount = 0 ;
276+ hasAppliedManualDiscount = false ;
277+ }
278+
279+ private int getCountSeparatorLength () {
280+ if (options .repeatCodes ) {
281+ return 0 ;
282+ } else {
283+ return options .countSeparator .length ();
284+ }
246285 }
247286
248- private void addScannableCode (String scannableCode , boolean isAgeRestricted ) {
287+ private void addScannableCode (String scannableCode , boolean isAgeRestricted , boolean hasManualDiscount ) {
249288 String nextCode = hasAgeRestrictedCode ? options .nextCodeWithCheck : options .nextCode ;
250289
251290 if (isAgeRestricted
@@ -257,8 +296,29 @@ private void addScannableCode(String scannableCode, boolean isAgeRestricted) {
257296 }
258297
259298 int charsLeft = options .maxChars - stringBuilder .length ();
260- int suffixCodeLength = Math .max (nextCode .length (), options .finalCode .length ());
261- int codesNeeded = (suffixCodeLength > 0 ? 2 : 1 );
299+ int manualDiscountLength = hasManualDiscount ? options .manualDiscountFinalCode .length () : 0 ;
300+ int suffixCodeLength = Math .max (nextCode .length (), options .finalCode .length () + manualDiscountLength );
301+
302+ if (options .finalCode .length () > 0 && getCountSeparatorLength () > 0 ) {
303+ suffixCodeLength += getCountSeparatorLength () + 1 ;
304+ }
305+
306+ if (options .manualDiscountFinalCode .length () > 0 && getCountSeparatorLength () > 0 ) {
307+ suffixCodeLength += getCountSeparatorLength () + 1 ;
308+ }
309+
310+ int suffixCodes = 0 ;
311+
312+ if (options .finalCode .length () > 0 ) {
313+ suffixCodes ++;
314+ }
315+
316+ if (manualDiscountLength > 0 ){
317+ suffixCodes ++;
318+ hasAppliedManualDiscount = true ;
319+ }
320+
321+ int codesNeeded = 1 + suffixCodes ;
262322 int requiredLength = scannableCode .length ()
263323 + suffixCodeLength
264324 + options .separator .length () * codesNeeded
0 commit comments