我有一个购物篮,其中添加了产品,每个产品都有一个删除按钮:
每当我单击底部按钮时,它会删除底部产品,问题是它还会在单击顶部按钮时删除最后一个项目,直到数量为 0 并且该项目消失时,它会删除底部的第二个项目(在这种情况下第一项)
这是我的篮子类代码:
package model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Observable;
import main.WinkelApplication;
import view.Payment;
public class Basket extends Observable {
private final Map<Product, Integer> products;
public Basket() {
super();
products = new LinkedHashMap<Product, Integer>(); //Map zorgt ervoor dat keys??? aan values word gebonden (dit is een soort van variabele[i], i staat voor de key
}
public void addProduct(Product product) {
// check if product is allready added to the basket
if (products.containsKey(product)) {
products.put(product, products.get(product) + 1);
} else {
products.put(product, 1);
}
setChanged();
notifyObservers();
}
public void deleteProduct(Product product) {
// check if product is allready added to the basket
int i = WinkelApplication.getBasket().getProductAmount(product);
int id = product.getProductId();
if (WinkelApplication.getBasket().getProductAmount(product) == 1) {
products.remove(product);
WinkelApplication.getInstance().showPanel(new view.Payment());
}else{
i--;
products.put(product, i);
WinkelApplication.getInstance().showPanel(new view.Payment());
}
if (products.size() == 0) {
WinkelApplication.getInstance().showPanel(new view.CategoryList());
}
}
public void empty() {
products.clear();
setChanged();
notifyObservers();
}
public List<Product> getProducts() {
List<Product> list = new LinkedList<Product>(products.keySet());
return list;
}
public int getProductAmount(Product product) {
return products.get(product);
}
public int size() {
return products.size();
}
public double getTotalCosts() {
double total = 0.0;
for (Entry<Product, Integer> entry : products.entrySet()) { //gaat de lijst van producten af in basket, en doet de prijs bij totaal * het hoeveelheid van zo'n product
total += entry.getKey().getPrice() * entry.getValue();
}
return total;
}
}
正如在 deleteProduct 方法中看到的那样,我做了 products.remove(product) 所以我假设它然后取最后一个项目,如果我系统出它显示我的哈希映射: {Cars=2, Dames onderbroek=1} (根据图片以上)。如何确保它删除选定的一个而不是列表中的最后一个?
产品类别:
package model;
public class Product {
private int productId;
private int categorieId;
private String name;
private String description;
private double price;
public Product() {
this(-1, -1, "", "", 0.0);
}
public Product(int product_id, int categorie_id, String name, String description, double price) {
this.productId = product_id;
this.categorieId = categorie_id;
this.name = name;
this.description = description;
this.price = price;
}
/**
* @return the productId
*/
public int getProductId() {
return productId;
}
/**
* @param productId the productId to set
*/
public void setProductId(int productId) {
this.productId = productId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the categorieId
*/
public int getCategorieId() {
return categorieId;
}
/**
* @param categorieId the categorieId to set
*/
public void setCategorieId(int categorieId) {
this.categorieId = categorieId;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
boolean value;
if (obj instanceof Product) {
value = this.productId == ((Product)obj).productId; //VB: product1 (met ID 1) past deze methode toe, en geeft product2 als parameter: deze method geeft dan false terug.
} else {
value = super.equals(obj); //van wat is Product een subclass van? en wat heeft dit voor nut?
}
return value;
}
@Override
public int hashCode() { //wth is dit?
return 13 * 3 + this.productId;
}
}