@@ -27,6 +27,10 @@ public class ShoppingCart {
2727 private int modCount = 0 ;
2828 private int addCount = 0 ;
2929 private Integer onlineTotalPrice ;
30+
31+ private boolean hasRaisedMaxCheckoutLimit ;
32+ private boolean hasRaisedMaxOnlinePaymentLimit ;
33+
3034 private transient List <ShoppingCartListener > listeners ;
3135 private transient Handler handler ;
3236 private transient Project project ;
@@ -87,6 +91,7 @@ void insert(Item item, int index, boolean update) {
8791 items .remove (existing );
8892 items .add (index , item );
8993 modCount ++;
94+ checkLimits ();
9095 notifyQuantityChanged (this , item );
9196
9297 if (update ) {
@@ -100,6 +105,7 @@ void insert(Item item, int index, boolean update) {
100105 addCount ++;
101106 modCount ++;
102107 items .add (index , item );
108+ checkLimits ();
103109 notifyItemAdded (this , item );
104110
105111 if (update ) {
@@ -146,7 +152,9 @@ public int indexOf(Item item) {
146152
147153 public void remove (int index ) {
148154 modCount ++;
149- notifyItemRemoved (this , items .remove (index ), index );
155+ Item item = items .remove (index );
156+ checkLimits ();
157+ notifyItemRemoved (this , item , index );
150158 invalidateOnlinePrices ();
151159 updatePrices (true );
152160 }
@@ -160,6 +168,7 @@ public void clear() {
160168 modCount = 0 ;
161169 addCount = 0 ;
162170 onlineTotalPrice = null ;
171+ checkLimits ();
163172 notifyCleared (this );
164173 }
165174
@@ -199,6 +208,7 @@ public void invalidateOnlinePrices() {
199208 }
200209 }
201210
211+ checkLimits ();
202212 notifyPriceUpdate (this );
203213 }
204214
@@ -282,6 +292,27 @@ private void updateTimestamp() {
282292 lastModificationTime = System .currentTimeMillis ();
283293 }
284294
295+ void checkLimits () {
296+ int totalPrice = getTotalPrice ();
297+ if (totalPrice < project .getMaxCheckoutLimit ()) {
298+ hasRaisedMaxCheckoutLimit = false ;
299+ }
300+
301+ if (totalPrice < project .getMaxOnlinePaymentLimit ()) {
302+ hasRaisedMaxOnlinePaymentLimit = false ;
303+ }
304+
305+ if (!hasRaisedMaxCheckoutLimit && project .getMaxCheckoutLimit () > 0
306+ && totalPrice >= project .getMaxCheckoutLimit ()) {
307+ hasRaisedMaxCheckoutLimit = true ;
308+ notifyCheckoutLimitReached (this );
309+ } else if (!hasRaisedMaxOnlinePaymentLimit && project .getMaxOnlinePaymentLimit () > 0
310+ && totalPrice >= project .getMaxOnlinePaymentLimit ()) {
311+ hasRaisedMaxOnlinePaymentLimit = true ;
312+ notifyOnlinePaymentLimitReached (this );
313+ }
314+ }
315+
285316 public static class Item {
286317 private Product product ;
287318 private ScannedCode scannedCode ;
@@ -620,6 +651,10 @@ public interface ShoppingCartListener {
620651 void onProductsUpdated (ShoppingCart list );
621652
622653 void onPricesUpdated (ShoppingCart list );
654+
655+ void onCheckoutLimitReached (ShoppingCart list );
656+
657+ void onOnlinePaymentLimitReached (ShoppingCart list );
623658 }
624659
625660 public static abstract class SimpleShoppingCartListener implements ShoppingCartListener {
@@ -654,6 +689,16 @@ public void onItemRemoved(ShoppingCart list, Item item, int pos) {
654689 public void onPricesUpdated (ShoppingCart list ) {
655690 onChanged (list );
656691 }
692+
693+ @ Override
694+ public void onCheckoutLimitReached (ShoppingCart list ) {
695+
696+ }
697+
698+ @ Override
699+ public void onOnlinePaymentLimitReached (ShoppingCart list ) {
700+
701+ }
657702 }
658703
659704 private void notifyItemAdded (final ShoppingCart list , final Item item ) {
@@ -717,6 +762,27 @@ public void run() {
717762 });
718763 }
719764
765+ void notifyCheckoutLimitReached (final ShoppingCart list ) {
766+ handler .post (new Runnable () {
767+ @ Override
768+ public void run () {
769+ for (ShoppingCartListener listener : listeners ) {
770+ listener .onCheckoutLimitReached (list );
771+ }
772+ }
773+ });
774+ }
775+
776+ void notifyOnlinePaymentLimitReached (final ShoppingCart list ) {
777+ handler .post (new Runnable () {
778+ @ Override
779+ public void run () {
780+ for (ShoppingCartListener listener : listeners ) {
781+ listener .onOnlinePaymentLimitReached (list );
782+ }
783+ }
784+ });
785+ }
720786 /**
721787 * Notifies all {@link #listeners} that the shopping list was cleared of all entries.
722788 *
0 commit comments