I don't know why my code doesn't run even though I re-corrected everything.
Please see my code for both of these points. I can't compile the code.
package fr.codegym.task.task08.task0819;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/*
Ensemble de chats
*/
public class Solution {
public static void main(String[] args) {
Set<Chat> chats = creerChats();
Chat supprimerChat = new Chat("Chat1");
Iterator<Chat> iter = chats.iterator();
while (iter.hasNext()) {
Chat chat = iter.next();
if (chat.equals(supprimerChat)) {
iter.remove();
// System.out.println("Chat supprimé : " + chat);
break;
}
}
imprimerChats(chats);
}
public static Set<Chat> creerChats() {
Set<Chat> set = new HashSet<>();
Chat chat1 = new Chat("Chat1");
Chat chat2 = new Chat("Chat2");
Chat chat3 = new Chat("Chat3");
set.add(chat1);
set.add(chat2);
set.add(chat3);
return set;
}
public static void imprimerChats(Set<Chat> chats) {
for (Chat chat : chats) {
System.out.println(chat);
}
}
// Classe Chat
public static class Chat {
private String nom;
public Chat() {
this.nom = "Inconnu";
}
public Chat(String nom) {
this.nom = nom;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
@Override
public String toString() {
return "Nom Chat : " + nom;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Chat chat = (Chat) obj;
return nom.equals(chat.nom);
}
@Override
public int hashCode() {
return nom.hashCode();
}
}
}