Ciao DjPg87,
La soluzione è alquanto banale, nella Classe prodotto aggiungi il metodo equals, in modo da poter confrontare due oggetti "prodotto" :
il metodo aggiungi prodotto a questo punto può essere implementato come segue :codice:public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Prodotto other = (Prodotto) obj; if (marca == null) { if (other.marca != null) return false; } else if (!marca.equals(other.marca)) return false; if (nome_prod == null) { if (other.nome_prod != null) return false; } else if (!nome_prod.equals(other.nome_prod)) return false; if (Double.doubleToLongBits(prezzo_unitario) != Double .doubleToLongBits(other.prezzo_unitario)) return false; if (quantità != other.quantità) return false; return true; }
codice:public void aggiungiProdotto(Prodotto p) { //Ritorna l'indice dell'oggetto contenuto, -1 se non presente int indexOf = lista.indexOf(p); if (indexOf != 1) { lista.get(indexOf).incrementaQuantità(); } else { lista.add(p); } }

Rispondi quotando