• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / jpetstore-6 / #1229

pending completion
#1229

push

github

web-flow
Merge pull request #710 from mybatis/renovate/org.apache.maven.plugins-maven-failsafe-plugin-3.x

chore(deps): update dependency org.apache.maven.plugins:maven-failsafe-plugin to v3.1.2

421 of 612 relevant lines covered (68.79%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/main/java/org/mybatis/jpetstore/domain/Cart.java
1
/*
2
 *    Copyright 2010-2022 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.jpetstore.domain;
17

18
import java.io.Serializable;
19
import java.math.BigDecimal;
20
import java.util.ArrayList;
21
import java.util.Collections;
22
import java.util.HashMap;
23
import java.util.Iterator;
24
import java.util.List;
25
import java.util.Map;
26

27
/**
28
 * The Class Cart.
29
 *
30
 * @author Eduardo Macarron
31
 */
32
public class Cart implements Serializable {
1✔
33

34
  private static final long serialVersionUID = 8329559983943337176L;
35

36
  private final Map<String, CartItem> itemMap = Collections.synchronizedMap(new HashMap<>());
1✔
37
  private final List<CartItem> itemList = new ArrayList<>();
1✔
38

39
  public Iterator<CartItem> getCartItems() {
40
    return itemList.iterator();
1✔
41
  }
42

43
  public List<CartItem> getCartItemList() {
44
    return itemList;
1✔
45
  }
46

47
  public int getNumberOfItems() {
48
    return itemList.size();
1✔
49
  }
50

51
  public Iterator<CartItem> getAllCartItems() {
52
    return itemList.iterator();
1✔
53
  }
54

55
  public boolean containsItemId(String itemId) {
56
    return itemMap.containsKey(itemId);
1✔
57
  }
58

59
  /**
60
   * Adds the item.
61
   *
62
   * @param item
63
   *          the item
64
   * @param isInStock
65
   *          the is in stock
66
   */
67
  public void addItem(Item item, boolean isInStock) {
68
    CartItem cartItem = itemMap.get(item.getItemId());
1✔
69
    if (cartItem == null) {
1✔
70
      cartItem = new CartItem();
1✔
71
      cartItem.setItem(item);
1✔
72
      cartItem.setQuantity(0);
1✔
73
      cartItem.setInStock(isInStock);
1✔
74
      itemMap.put(item.getItemId(), cartItem);
1✔
75
      itemList.add(cartItem);
1✔
76
    }
77
    cartItem.incrementQuantity();
1✔
78
  }
1✔
79

80
  /**
81
   * Removes the item by id.
82
   *
83
   * @param itemId
84
   *          the item id
85
   *
86
   * @return the item
87
   */
88
  public Item removeItemById(String itemId) {
89
    CartItem cartItem = itemMap.remove(itemId);
1✔
90
    if (cartItem == null) {
1✔
91
      return null;
1✔
92
    } else {
93
      itemList.remove(cartItem);
1✔
94
      return cartItem.getItem();
1✔
95
    }
96
  }
97

98
  /**
99
   * Increment quantity by item id.
100
   *
101
   * @param itemId
102
   *          the item id
103
   */
104
  public void incrementQuantityByItemId(String itemId) {
105
    CartItem cartItem = itemMap.get(itemId);
1✔
106
    cartItem.incrementQuantity();
1✔
107
  }
1✔
108

109
  public void setQuantityByItemId(String itemId, int quantity) {
110
    CartItem cartItem = itemMap.get(itemId);
1✔
111
    cartItem.setQuantity(quantity);
1✔
112
  }
1✔
113

114
  /**
115
   * Gets the sub total.
116
   *
117
   * @return the sub total
118
   */
119
  public BigDecimal getSubTotal() {
120
    return itemList.stream()
1✔
121
        .map(cartItem -> cartItem.getItem().getListPrice().multiply(new BigDecimal(cartItem.getQuantity())))
1✔
122
        .reduce(BigDecimal.ZERO, BigDecimal::add);
1✔
123
  }
124

125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc