增加 ShoppingCart 中的数量而不是添加新行-JAVA
Increase Quantity in ShoppingCart instead of adding a new line-JAVA
我正在尝试创建一个购物车,当您添加商品时,如果它已经存在于购物车中,则只会增加数量而不是添加新行。目前我已经正确地增加了数量,但它是将新数量添加为新行而不是更新上一行。如果能提供有关如何解决此问题的任何提示,我将不胜感激。
这是我的代码。
这是购物车:
public class CartItem implements ICartItem {
private int productID;
private String productName;
private BigDecimal retailPrice;
private int quantity;
private int formatID;
private String description;
public CartItem(int productID, String productName, BigDecimal retailPrice, int quantity, int formatID, String description) {
this.setProductID(productID);
this.setProductName(productName);
this.setRetailPrice(retailPrice);
this.setQuantity(quantity);
this.setFormatID(formatID);
this.setDescription(description);
}
public int getProductID(){
return productID;
}
private void setProductID(int productID) {
this.productID = productID;
}
public String getProductName() {
return productName;
}
private void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getRetailPrice() {
return recommendedRetailPrice;
}
private void setRetailPrice(BigDecimal retailPrice) {
if (retailPrice.compareTo(new BigDecimal(0)) >= 0)
this.retailPrice = retailPrice;
}
public int getQuantity() {
if(this.quantity <1 || this.quantity > 800) {
return -1;}
return quantity;
}
private void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getFormatID() {
return formatID;
}
private void setFormatID(int formatID) {
this.formatID = formatID;
}
public String getDescription() {
return description;
}
private void setDescription(String description) {
this.description = description;
}
public BigDecimal getTotalValueOfBasketItem(){
BigDecimal totalPrice = wholesalePrice.multiply(new BigDecimal(quantity));
return totalPrice;
}
public int increaseQuantity(int quantity) {
if (quantity <0) {
return -1;}
this.setQuantity(this.getQuantity() + quantity);
return this.getQuantity();}
public int decreaseQuantity(int quantity) {
if (quantity <0) {
return -1;}
this.setQuantity(this.getQuantity() - quantity);
return this.getQuantity();
}
这是界面:
public interface ICartItem {
int getProductID();
String getProductName();
BigDecimal getRetailPrice();
int getQuantity();
int increaseQuantity(int quantity);
int decreaseQuantity(int quantity);
int getFormatID();
String getDescription();
BigDecimal getTotalValueOfBasketItem();
}
这是购物篮:
public class CartBasket 实现 ICartBasket {
Customer customer = null;
private List<ICartItem> cartItems = new ArrayList<ICartItem>();
public CartBasket(Customer customer) {
this.customer = customer;
}
public List<ICartItem> getcartItems() {
return cartItems;
}
private void setcartItems(List<ICartItem> cartItems) {
this.cartItems = cartItems;
}
public int getNumberOfProducts(){
return cartItems.size();
}
public BigDecimal getBasketTotal() {
BigDecimal totalPrice = new BigDecimal(0);
for (ICartItem item : cartItems){
totalPrice = totalPrice.add(item.getTotalValueOfcartItem());
}
return totalPrice;
}
public int getNumberOfItems(){
int numberOfcartItems = 0;
for (ICartItem item : cartItems){
numberOfcartItems += item.getQuantity();
}
return numberOfcartItems;
}
public void addItem(ICartItem cartItem){
for (ICartItem item : cartItems){
if (item.getProductID()==cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID())
item.increaseQuantity(cartItem.increaseQuantity(0));
}
cartItems.add(cartItem);
public void removeItem(ICartItem cartItem){
ICartItem matchingItem = findcartItem(cartItem);
if (matchingItem != null) {
cartItems.remove(matchingItem);
}
}
public void clearBasket(){
cartItems.clear();
}
public boolean isProductNameInBasket(String productName)
{ for (ICartItem item : cartItems){
if (item.getProductName().equals(productName)) {
return true;
}
}
return false;
}
private ICartItem findcartItem(ICartItem cartItem){
for (ICartItem item : cartItems){
if (item.getProductID() == cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID() && item.getDegreeOfRoastID() == cartItem.getDegreeOfRoastID() ) {
return item;
}
}
return null;
您只需要在添加之前检查您的项目是否已经存在。您可以使用布尔值或仅使项目成为一组项目并定义 equals 方法。
这是一个使用布尔值的例子:
public void addItem(ICartItem cartItem){
boolean exists = false;
for (ICartItem item : cartItems){
if (item.getProductID()==cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID()) {
item.increaseQuantity(cartItem.increaseQuantity(0));
exists = true;
}
}
if (!exists) {
cartItems.add(cartItem);
}
}
我正在尝试创建一个购物车,当您添加商品时,如果它已经存在于购物车中,则只会增加数量而不是添加新行。目前我已经正确地增加了数量,但它是将新数量添加为新行而不是更新上一行。如果能提供有关如何解决此问题的任何提示,我将不胜感激。
这是我的代码。 这是购物车:
public class CartItem implements ICartItem {
private int productID;
private String productName;
private BigDecimal retailPrice;
private int quantity;
private int formatID;
private String description;
public CartItem(int productID, String productName, BigDecimal retailPrice, int quantity, int formatID, String description) {
this.setProductID(productID);
this.setProductName(productName);
this.setRetailPrice(retailPrice);
this.setQuantity(quantity);
this.setFormatID(formatID);
this.setDescription(description);
}
public int getProductID(){
return productID;
}
private void setProductID(int productID) {
this.productID = productID;
}
public String getProductName() {
return productName;
}
private void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getRetailPrice() {
return recommendedRetailPrice;
}
private void setRetailPrice(BigDecimal retailPrice) {
if (retailPrice.compareTo(new BigDecimal(0)) >= 0)
this.retailPrice = retailPrice;
}
public int getQuantity() {
if(this.quantity <1 || this.quantity > 800) {
return -1;}
return quantity;
}
private void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getFormatID() {
return formatID;
}
private void setFormatID(int formatID) {
this.formatID = formatID;
}
public String getDescription() {
return description;
}
private void setDescription(String description) {
this.description = description;
}
public BigDecimal getTotalValueOfBasketItem(){
BigDecimal totalPrice = wholesalePrice.multiply(new BigDecimal(quantity));
return totalPrice;
}
public int increaseQuantity(int quantity) {
if (quantity <0) {
return -1;}
this.setQuantity(this.getQuantity() + quantity);
return this.getQuantity();}
public int decreaseQuantity(int quantity) {
if (quantity <0) {
return -1;}
this.setQuantity(this.getQuantity() - quantity);
return this.getQuantity();
}
这是界面:
public interface ICartItem {
int getProductID();
String getProductName();
BigDecimal getRetailPrice();
int getQuantity();
int increaseQuantity(int quantity);
int decreaseQuantity(int quantity);
int getFormatID();
String getDescription();
BigDecimal getTotalValueOfBasketItem();
}
这是购物篮:
public class CartBasket 实现 ICartBasket {
Customer customer = null;
private List<ICartItem> cartItems = new ArrayList<ICartItem>();
public CartBasket(Customer customer) {
this.customer = customer;
}
public List<ICartItem> getcartItems() {
return cartItems;
}
private void setcartItems(List<ICartItem> cartItems) {
this.cartItems = cartItems;
}
public int getNumberOfProducts(){
return cartItems.size();
}
public BigDecimal getBasketTotal() {
BigDecimal totalPrice = new BigDecimal(0);
for (ICartItem item : cartItems){
totalPrice = totalPrice.add(item.getTotalValueOfcartItem());
}
return totalPrice;
}
public int getNumberOfItems(){
int numberOfcartItems = 0;
for (ICartItem item : cartItems){
numberOfcartItems += item.getQuantity();
}
return numberOfcartItems;
}
public void addItem(ICartItem cartItem){
for (ICartItem item : cartItems){
if (item.getProductID()==cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID())
item.increaseQuantity(cartItem.increaseQuantity(0));
}
cartItems.add(cartItem);
public void removeItem(ICartItem cartItem){
ICartItem matchingItem = findcartItem(cartItem);
if (matchingItem != null) {
cartItems.remove(matchingItem);
}
}
public void clearBasket(){
cartItems.clear();
}
public boolean isProductNameInBasket(String productName)
{ for (ICartItem item : cartItems){
if (item.getProductName().equals(productName)) {
return true;
}
}
return false;
}
private ICartItem findcartItem(ICartItem cartItem){
for (ICartItem item : cartItems){
if (item.getProductID() == cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID() && item.getDegreeOfRoastID() == cartItem.getDegreeOfRoastID() ) {
return item;
}
}
return null;
您只需要在添加之前检查您的项目是否已经存在。您可以使用布尔值或仅使项目成为一组项目并定义 equals 方法。
这是一个使用布尔值的例子:
public void addItem(ICartItem cartItem){
boolean exists = false;
for (ICartItem item : cartItems){
if (item.getProductID()==cartItem.getProductID() && item.getFormatID() == cartItem.getFormatID()) {
item.increaseQuantity(cartItem.increaseQuantity(0));
exists = true;
}
}
if (!exists) {
cartItems.add(cartItem);
}
}