sir nino
sir nino

Reputation: 1

How to do cart limitations?

How to do cart limitations?

  1. Number of items for exp 5 items and can't add more
  2. Add from only one category

    btnCart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new Database(getBaseContext()).addToCart(new Order(
                    foodId,
                    currentFood.getName(),
                    numberButton.getNumber(),
                    currentFood.getPrice(),
                    currentFood.getDiscount()
            ));
    
        }
    });
    btnCart.setCount(new Database(this).getCountCart())
    

Upvotes: 0

Views: 29

Answers (1)

Capricorn
Capricorn

Reputation: 2089

Inside you onClick method check how many items are in the cart already. Only in case there are less than 5 items, add the new item to the cart.

btnCart.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    Database db = new Database(getBaseContext());

    if (db.getCountCart() < 5) {
          db.addToCart(new Order(
              foodId,
              currentFood.getName(),
              numberButton.getNumber(),
              currentFood.getPrice(),
              currentFood.getDiscount()
          ));
        }
    }
});

Upvotes: 1

Related Questions