package beans; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.util.HashMap; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Dictionary { @XmlElement(name = "dictionary") private HashMap dictionary; private static Dictionary instance; public Dictionary(){ dictionary = new HashMap(); } public Dictionary(HashMap dict){ dictionary = dict; } //See the singleton pattern public synchronized static Dictionary getInstance(){ if(instance==null){ instance = new Dictionary(); } return instance; } 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); } }