package server; import java.util.HashMap; import org.springframework.stereotype.Service; @Service //Spring automatically makes this class a singleton bean public class Dictionary { private final HashMap dictionary = new HashMap();; public int addWord(String w, String d){ synchronized (this) { if (dictionary.containsKey(w)) { return -1; //Key is already presnet } else { dictionary.put(w, d); System.out.println("Added word: " + w + "; Definition: " + dictionary.get(w)); return 0; } } } public int changeWordDefinition(String w, String d){ synchronized (this) { if (!dictionary.containsKey(w)) { return -1; //Key is not present } else { dictionary.replace(w, d); return 0; } } } public String viewDefinition(String w){ if(!dictionary.containsKey(w)){ System.out.println("Definition of " + w + " not found"); return null; }else{ System.out.println("Definition of " + w + ": " + dictionary.get(w)); return dictionary.get(w); } } public void deleteWord(String w){ dictionary.remove(w); } }