Reputation: 191
I have a shopping cart that looks like this:
Title Price Amount Total
Item1 20 3 60
Item2 15 2 30
TotalAll:90
Amount
column is input tag where user can put how many items he wants to buy, and everything works as long as the user is adding items since in backend I'm doing sum total(TotalAll+=Total)
, the problem is when user put a smaller amount
than currently entered for example for Item1 from 3 to 2 items, TotalAll will still add those 2 and value will be TotalAll:130
but it should be 70 because it's now 2 items. So how can I subtract from TotalAll
when amount
is reduced? In what case?
Upvotes: 0
Views: 193
Reputation: 191
This my solution:
totalAll = 0.0;
List<Article> articleList = ArticleSelectDAO.selectArticles();
for (Article a : articleList) {
totalAll+=a.getTotal();
}
Before i reset totalAll
i have to update database with current total value per item. Then i add those articles in List, iterate over List and add value to totalAll
.
Upvotes: 0
Reputation: 339
Whenever user changes the input, as long as Total
value is refreshed for that item, You can reset the TotalAll
value to 0 in your backend wherever you're summing all the Totals
and recalculate the TotalAll
value. This may sound inefficient but should work fine if you're dealing with minimal number of items.
Upvotes: 1