Reputation: 641
I have a basket where products are added to and for each product there is a delete button:
Whenever I click the bottom button it deletes the bottom product, the problem is it also deletes the last item when clicked in the top button until the amount is 0 and the item is gone it deletes the 2nd one on the bottom (in this case the first item)
Here is my basket class code:
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;
}
}
As seen in the deleteProduct method I did products.remove(product) so I assume it then takes the last Item, if I System out the hash map it shows me: {Cars=2, Dames onderbroek=1} (according to the picture above). How can I make sure it deletes the selected one instead of the last one in the list?
The product class:
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;
}
}
Upvotes: 0
Views: 543
Reputation: 109567
WinkelApplication.getBasket()
maybe delivers another basket?
Not an answer but one could get an error when the product is not in products
.
public int getProductAmount(Product product) {
Integer i = products.get(product);
return i == null ? 0 : i.intValue();
}
Upvotes: 0
Reputation: 115338
Unfortunately you have not sent us the class Product
but I can assume that you have not implemented hashCode()
and equals()
for this class.
The short answer - do this and your logic will work.
The longer answer is that hash mechanism uses these methods to identify your object. By default hashCode()
returns the "address" in java heap, so 2 different instances of Product
are different even if all their fields are equal. Pay attention that if equals()
returns true hashCode()
must return equal value for both objects.
Upvotes: 1