Advertisement
Guest User

MIndfulDictionary

a guest
Nov 19th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package dictionary;
  7.  
  8. import java.util.List;
  9. import java.util.ArrayList;
  10. import java.io.File;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.util.Scanner;
  14. import java.util.Arrays;
  15. import java.io.FileNotFoundException;
  16. import java.util.Map;
  17. import java.util.HashMap;
  18.  
  19. /**
  20. *
  21. * @author colin
  22. */
  23. public class MindfulDictionary {
  24.  
  25. private String file;
  26. private Map<String,String> dict = new HashMap<String,String>();
  27.  
  28. public MindfulDictionary(){
  29.  
  30. }
  31.  
  32. public MindfulDictionary(String file){
  33. this.file = file;
  34. }
  35.  
  36. //makes the contents of the file accessible
  37. //by putting the word and the translation into the instance hashMap
  38. public boolean load(){
  39. try{
  40. Scanner reader = new Scanner(new File(file));
  41. while (reader.hasNextLine()){
  42. String line = reader.nextLine();
  43. System.out.println("current line: " +line);
  44. String addition[] = line.split(":");
  45. dict.put(addition[0], addition[1]);
  46. }
  47.  
  48. return true;
  49.  
  50. }catch(Exception e){
  51. return false;
  52. }
  53. }
  54.  
  55. public void printDict(){
  56. for (String key : dict.keySet()){
  57. System.out.println("Key: " +key+ " | Value: " +dict.get(key));
  58. }
  59. }
  60.  
  61. public boolean save(){
  62. try{
  63. FileWriter writer = new FileWriter(file);
  64.  
  65. System.out.print("save(): ");
  66. printDict();
  67. for (String key : dict.keySet()){
  68. System.out.println("current key: " +key+ " | current value: " +dict.get(key) );
  69. writer.write(key+ ":" +dict.get(key)+ "\n");
  70. }
  71. writer.close();
  72.  
  73. System.out.println("end of save()");
  74. return true;
  75.  
  76. }catch (Exception e){
  77. return false;
  78. }
  79. }
  80.  
  81.  
  82.  
  83. public void add(String word, String translation){
  84. if (!dict.containsKey(word)){
  85. dict.put(word, translation);
  86. }
  87. }
  88.  
  89.  
  90.  
  91. public String translate(String word){
  92. for (String key : dict.keySet()){
  93. if (key.equals(word)){
  94. return dict.get(key);
  95. }
  96. else if(dict.get(key).equals(word)){
  97. return key;
  98. }
  99.  
  100. }
  101. return null;
  102. }
  103.  
  104. public void remove(String word){
  105. for (String key : dict.keySet()){
  106. if (key.equals(word)){
  107. dict.remove(key);
  108. }
  109. else if (dict.get(key).equals(word)){
  110. dict.remove(key);
  111. }
  112. }
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement